Skip to content

Instantly share code, notes, and snippets.

@strund3r
Last active September 28, 2020 14:51
Show Gist options
  • Save strund3r/ad6d9ae30b89f3586a35e9e35ff13982 to your computer and use it in GitHub Desktop.
Save strund3r/ad6d9ae30b89f3586a35e9e35ff13982 to your computer and use it in GitHub Desktop.
Deploy script for CI/CD (Docker 4 AWS)
#!/bin/bash
set -eo pipefail
############################ VARIABLES ############################
# ssh key's location #
ssh_key="/home/circleci/example/example.pem" #
# docker-compose for metrics #
metrics="/home/circleci/example/<metrics-docker-compose>.yml" #
# sleep time #
secs=$((20)) #
############################ VARIABLES ############################
echo -e "
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ INSTALLING DEPENDENCIES @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
# INSTALL PYTHON, PIP, AWS_CLI
sudo apt-get -y install jq python python-pip python-virtualenv -qq && pip install awscli
pip install --upgrade awscli
aws --version
echo -e "
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ CONFIGURING AWS @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
# CONFIGURE AWS_CLI
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set default.region us-east-1
aws configure set default.output json
echo -e "
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ DEPLOYING SERVICES TO AWS @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
export _sshconfig=$(mktemp -u)
export _ssh_ctrl_socket=$(mktemp -u)
cfn_stack_name= <stack-name>
jqScript=".AutoScalingGroups[] | select(.Tags[].Value == \"${cfn_stack_name}-Manager\").Instances[] | select(.HealthStatus == \"Healthy\").InstanceId"
manager_id=$(aws autoscaling describe-auto-scaling-groups | jq -r "${jqScript}" | head -n1)
manager=$(aws ec2 describe-instances --instance-ids ${manager_id} | jq -r '.Reservations[].Instances[].PublicDnsName')
cat <<EOF > ${_sshconfig}
User docker
LogLevel error
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
IdentityFile ${ssh_key}
ControlPath ${_ssh_ctrl_socket}
EOF
chmod 400 ${ssh_key}
# Set up an SSH control socket for tunneling, so that we can cleanly close it when we're done
ssh -M -F ${_sshconfig} \
-fnNT -L localhost:2374:/var/run/docker.sock ${manager}
# configure all `docker` commands to communicate through the SSH tunnel instead of any local docker engine
export DOCKER_HOST=localhost:2374
# now run `docker` commands as normal:
docker stack deploy --with-registry-auth -c /home/circleci/example/<docker-compose-file>.yml <stack-name>
if [ -e "$metrics" ]
then
docker stack deploy --with-registry-auth -c ~/seucondominio/docker-compose_metrics.yml metrics
echo -e "\n"
while [ $secs -gt 0 ]; do
echo -ne "Waiting for InfluxDB to get up... $secs seconds remaining\033[0K\r"
sleep 1
: $((secs--))
done
echo -e "\n"
influx_id=$(docker ps -q -l -f name=influx)
docker exec -it ${influx_id} influx -execute 'CREATE DATABASE cadvisor'
else
echo "No Metrics!"
fi
# Close the tunnel
ssh -F ${_sshconfig} -O exit -
# remove the temporary SSH-related files
rm -f ${_ssh_ctrl_socket}
unset DOCKER_HOST
influx_id=$(docker ps -q -f name=influx)
docker exec -it ${influx_id} influx -execute 'CREATE DATABASE cadvisor'
else
echo "No Metrics!"
fi
# Close the tunnel
ssh -F ${_sshconfig} -O exit -
# remove the temporary SSH-related files
rm -f ${_ssh_ctrl_socket}
unset DOCKER_HOST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment