Skip to content

Instantly share code, notes, and snippets.

@theworkflow
Last active June 20, 2017 21:23
Show Gist options
  • Save theworkflow/012d11fbc6ef167bbdc2df00675e4900 to your computer and use it in GitHub Desktop.
Save theworkflow/012d11fbc6ef167bbdc2df00675e4900 to your computer and use it in GitHub Desktop.
Mongo database host AMI setup
#!/bin/bash
set -e
DOCKER_VERSION=1.13.1-0~ubuntu-xenial
COMPOSE_VERSION=1.13.0
NVM_VERSION=0.33.2
NODE_LTS=boron
MONGO_RS_VERSION=0.2.0
# Install utilities and dependencies
echo "Installing utilities and dependencies"
apt-get update
apt-get install -y --no-install-recommends ntp iotop htop vim \
apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://apt.dockerproject.org/gpg | apt-key add -
add-apt-repository "deb https://apt.dockerproject.org/repo/ ubuntu-$(lsb_release -cs) main"
# Install docker and docker-compose
echo "Installing docker and docker-compose"
apt-get update
apt-get -y install docker-engine=${DOCKER_VERSION}
curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# Install NVM, Node.js, and Mongo-RS
echo "Installing NVM, Node.js, and Mongo-RS"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v${NVM_VERSION}/install.sh | bash
source ${HOME}/.nvm/nvm.sh
nvm install --lts=${NODE_LTS}
npm install -g npm@latest
npm install -g mongo-rs@$MONGO_RS_VERION
# Mount EBS volumes (this assumes ebs block volume names)
echo "Mounting EBS volumes"
mkfs -t ext4 /dev/xvdb
mount /dev/xvdb /mnt
cp /etc/fstab /etc/fstab.orig
echo '/dev/xvdb /mnt ext4 defaults,auto,noatime,noexec 0 0' | tee -a /etc/fstab
mount -a
# Create directories
echo "Creating directories"
mkdir -p /mnt/data /mnt/backups /opt/services /opt/conf
# Set read/write permissions on /mnt
echo "Setting read/write permissions on /mnt directory"
chmod -R 777 /mnt
# ulimits and read ahead settings
echo "Setting ulimits and read ahead settings"
echo '* soft nofile 64000
* hard nofile 64000
* soft nproc 64000
* hard nproc 64000' | tee /etc/security/limits.d/90-mongodb.conf && blockdev --setra 32 /dev/xvdb
echo 'ACTION=="add", KERNEL=="xvdb", ATTR{bdi/read_ahead_kb}="16"' | tee -a /etc/udev/rules.d/85-ebs.rules
# Swap
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment