Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Last active April 15, 2019 04:00
Show Gist options
  • Save copiousfreetime/f325343bcfb6978bd14426a6850f4ca1 to your computer and use it in GitHub Desktop.
Save copiousfreetime/f325343bcfb6978bd14426a6850f4ca1 to your computer and use it in GitHub Desktop.
setup-aws-machine

Bootstrap up a new aws machine

#!/bin/bash
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get install -y autoconf bison ntp build-essential libgdbm-compat-dev nvme-cli pciutils libpq-dev postgresql-client-common postgresql-common postgresql-client-10 libgeos-dev libproj-dev libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev nfs-common tree
sudo snap install aws-cli --classic
# Setup rbenv
sudo git clone https://github.com/rbenv/rbenv.git /opt/rbenv
sudo sh -c 'cd /opt/rbenv && src/configure && make -C src'
echo '#!/bin/bash' > /tmp/rbenv.sh
echo >> /tmp/rbenv.sh
echo 'export RBENV_ROOT=/opt/rbenv' >> /tmp/rbenv.sh
echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> /tmp/rbenv.sh
echo 'eval "$(rbenv init -)"' >> /tmp/rbenv.sh
sudo mv /tmp/rbenv.sh /etc/profile.d/
sudo chmod +x /etc/profile.d/rbenv.sh
sudo mkdir -p /opt/rbenv/plugins
sudo git clone https://github.com/rbenv/ruby-build.git /opt/rbenv/plugins/ruby-build
sudo sh -c 'RBENV_ROOT=/opt/rbenv /opt/rbenv/bin/rbenv install 2.6.2'
sudo sh -c 'RBENV_ROOT=/opt/rbenv /opt/rbenv/bin/rbenv global 2.6.2'
# Setup nodejs
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
# Setup the ebs app volume
sudo ./setup-ebs.sh
#!/bin/bash
function log {
echo "$(date --iso-8601=seconds) : $*"
}
# Make sure that the current user is root
if [[ ${UID} -ne "0" ]]; then
echo "ERROR: Must be the root user."
exit 1
fi
################################################################################
# Install tools needed to setup the rest of the machine
################################################################################
apt-get install -q -y nvme-cli pciutils
################################################################################
# Setup the ebs drive
#
# / - default OS
# /ebs/apps - EBS volume for the app
################################################################################
root_dev=$(findmnt --noheadings --first-only / | tr -s [:blank:] | cut -f 2 -d' ')
root_sn=$(sudo nvme id-ctrl ${root_dev} | grep ^sn | cut -f2 -d: | tr -d [:blank:])
log "${root_dev} is mounted on / with serial number ${root_sn}"
app_mnt="/ebs/apps"
app_dev=$(nvme list | grep Elastic | grep -v ${root_sn} | cut -f1 -d ' ')
log "device ${app_dev} is to be mounted at ${app_mnt}"
# Format and mount the EBS volume if it is not mounted
if ! findmnt ${app_mnt}> /dev/null 2>&1; then
log "${app_mnt} is not mounted"
mkdir -p ${app_mnt}
mkfs -t xfs ${app_dev}
mount -t xfs ${app_dev} ${app_mnt}
else
app_sn=$(lsblk --noheadings -o serial ${app_dev} | head -1)
log "${app_dev} is mounted as ${app_mnt} with serial number ${app_sn}"
fi
# Make sure the app drive is in /etc/fstab
if ! grep -q ${app_mnt} /etc/fstab; then
app_uuid=$(findmnt --noheadings --first-only --output uuid ${app_mnt})
log "${app_mnt} has UUID ${app_uuid}"
echo -e "UUID=${app_uuid}\t${app_mnt}\txfs\tdefaults,nofail\t0\t2" >> /etc/fstab
else
log "${app_mnt} is in /etc/fstab"
fi
#!/bin/bash
function log {
echo "$(date --iso-8601=seconds) : $*"
}
function join_by { local IFS="$1"; shift; echo "$*"; }
# Make sure that the current user is root
if [[ ${UID} -ne "0" ]]; then
echo "ERROR: Must be the root user."
exit 1
fi
################################################################################
# Install tools needed to setup the rest of the machine
################################################################################
apt-get install -q -y nvme-cli pciutils
################################################################################
# Setup the local SSD drive
# /mnt/instance-store
# We currently assume that there is only 1 local drive
################################################################################
mnt_instance="/mnt/instance-store"
dev_md0="/dev/md0"
raid_label="instance-store"
mapfile -t instance_devs < <(nvme list | grep Instance | cut -f 1 -d ' ')
drive_count=${#instance_devs[@]}
drive_list=$(IFS=" "; echo "${instance_devs[*]}")
#
# Make sure the raid device exists
#
if ! mdadm --misc -Q ${dev_md0}; then
mdadm --create --verbose ${dev_md0} --auto=yes --level=0 --name=${raid_label} --raid-devices=${drive_count} ${drive_list}
mdadm --misc --wait ${dev_md0}
else
log "${dev_md0} exists"
mdadm --detail ${dev_md0}
fi
#
# Format and mount the Instance Store SSD if it is not mounted
#
if ! findmnt ${mnt_instance}> /dev/null 2>&1; then
log "${mnt_instance} is not mounted"
mkdir -p ${mnt_instance}
mkfs -t xfs ${dev_md0}
mount -t xfs ${dev_md0} ${mnt_instance}
else
mounted_source=$(findmnt ${mnt_instance} -o source --noheading)
log "${mnt_instance} is mounted from ${mounted_source}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment