Skip to content

Instantly share code, notes, and snippets.

@nrvale0
Last active November 17, 2016 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrvale0/da54db84dcdd5f5f67a5ceefd9fb1c45 to your computer and use it in GitHub Desktop.
Save nrvale0/da54db84dcdd5f5f67a5ceefd9fb1c45 to your computer and use it in GitHub Desktop.
Rancher CI Bootstrap scripts
#!/bin/sh
# send all stdout & stderr to rancherci-bootstrap.log
exec > /tmp/rancherci-bootstrap.log
exec 2>&1
set -uxe
###############################################################################
# figure out the OS family for our context
###############################################################################
get_osfamily() {
local osfamily='unknown'
# ugly way to figure out what OS family we are running.
set +e
if apt-get --version > /dev/null 2>&1; then
osfamily='debian'
elif yum --version > /dev/null 2>&1; then
osfamily='redhat'
fi
set -e
echo "${osfamily}"
}
###############################################################################
# populate system with Rancher Labs SSH keys
###############################################################################
fetch_rancherlabs_ssh_keys() {
wget -c -O - \
https://raw.githubusercontent.com/rancherlabs/ssh-pub-keys/master/ssh-pub-keys/ci >> ~/.ssh/authorized_keys
}
###############################################################################
# install some things required to query Docker version from tag
###############################################################################
system_prep() {
local osfamily
osfamily="$(get_osfamily)" || exit $?
case $osfamily in
'redhat')
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y wget jq python-pip htop
sudo pip install awscli
sudo wget -O /usr/local/bin/ec2metadata http://s3.amazonaws.com/ec2metadata/ec2-metadata
sudo chmod +x /usr/local/bin/ec2metadata
;;
'debian')
sudo apt-get update && apt-get install -y jq awscli wget
;;
esac
}
###############################################################################
# get the AWS region
###############################################################################
aws_region() {
local region
region="$(ec2metadata -z | cut -f2 -d' ' | sed -e 's/.$//g')" || exit $?
if [ -z "${region}" ]; then
echo 'Falied to query AWS region!'
exit -1
fi
echo "${region}"
}
###############################################################################
# get the volid for extra volumes (redhat osfamily)
###############################################################################
aws_instance_id() {
local instance_id
instance_id="$(ec2metadata --instance-id | cut -f2 -d' ')" || exit $?
if [ -z "${instance_id}" ]; then
echo 'Failed to query AWS instance-id!'
exit -1
fi
echo "${instance_id}"
}
###############################################################################
# get the volid for extra volumes (redhat osfamily)
###############################################################################
aws_addtl_volid() {
local instance_id
local vol_id
instance_id="$(aws_instance_id)" || exit $?
vol_id="$(aws ec2 --region us-west-2 describe-tags --filter Name=resource-id,Values="${instance_id}" --out=json | \
jq '.Tags[]| select(.Key == "rancherlabs.ci.addtl_volume")|.Value' | \
sed -e 's/\"//g')" || exit $?
if [ -z "${vol_id}" ]; then
echo 'Failed to query secondary volid from AWS.'
exit 1
fi
echo "${vol_id}"
}
###############################################################################
# get the Docker version specified in AWS tag rancher.docker.version
###############################################################################
get_specified_docker_version() {
local instance_id
local region
local docker_version
instance_id="$(aws_instance_id)" || exit $?
region="$(aws_region)" || exit $?
docker_version="$(aws ec2 --region "${region}" describe-tags --filter Name=resource-id,Values="${instance_id}" --out=json | \
jq '.Tags[]| select(.Key == "rancher.docker.version")|.Value' | \
sed -e 's/\"//g')" || exit $?
if [ -z "${docker_version}" ]; then
echo 'Failed to query rancher.docker.version from instance tags.'
exit 1
fi
echo "${docker_version}"
}
################################################################################
# install specified Docker version
################################################################################
docker_install() {
local docker_version="${1}"
wget -O - "https://releases.rancher.com/install-docker/${docker_version}.sh" | sudo bash -
sudo systemctl restart docker
}
###############################################################################
# make adjustments to LVM etc for RedHat OS family
###############################################################################
prep_for_redhat() {
local instance_id
local docker_vol_volid
local region
instance_id="$(aws_instance_id)" || exit $?
docker_vol_volid="$(aws_addtl_volid)" || exit $?
region="$(aws_region)" || exit $?
sudo aws ec2 attach-volume --region "${region}" --device /dev/xvdb --volume-id "${docker_vol_volid}" --instance-id "${instance_id}"
sudo yum install -y lvm2
echo "Waiting for storage device mappings to settle..."; sleep 5
sudo pvcreate -ff -y /dev/xvdb
sudo vgcreate docker /dev/xvdb
sudo lvcreate --wipesignatures y -n thinpool docker -l 95%VG
sudo lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
sudo lvconvert -y --zero n -c 512K --thinpool docker/thinpool --poolmetadata docker/thinpoolmeta
sudo tee /etc/lvm/profile/docker-thinpool.profile <<-EOF
activation {
thin_pool_autoextend_threshold=80
thin_pool_autoextend_percent=20
}
EOF
sudo lvchange --metadataprofile docker-thinpool docker/thinpool
sudo lvs -o+seg_monitor
}
###############################################################################
# make adjustments to LVM etc for RedHat OS family
###############################################################################
config_for_redhat() {
sudo tee /usr/lib/systemd/system/docker.service <<-EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket
Requires=docker.socket
[Service]
Type=notify
ExecStart=/usr/bin/docker daemon -H fd:// --storage-driver=devicemapper --storage-opt=dm.thinpooldev=/dev/mapper/docker-thinpool --storage-opt=dm.use_deferred_removal=true
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl stop docker
sudo rm -rf /var/lib/docker/network
sudo ip link del docker0
sleep 3
sudo systemctl daemon-reload
sudo systemctl restart docker
}
###############################################################################
# the main() function
###############################################################################
main() {
system_prep
fetch_rancherlabs_ssh_keys
local osfamily
osfamily="$(get_osfamily)" || exit $?
echo "Detected osfamily \'${osfamily}\'..."
if [ 'redhat' == "${osfamily}" ]; then
prep_for_redhat
fi
local docker_version
docker_version="$(get_specified_docker_version)" || exit $?
echo "Docker version \'${docker_version}\' specified..."
docker_install "${docker_version}"
if [ 'redhat' == "${osfamily}" ]; then
config_for_redhat
fi
}
# the fun starts here
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment