Skip to content

Instantly share code, notes, and snippets.

@timrchavez
Created August 29, 2017 17:42
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 timrchavez/c8808e1f296a0ffdf636fe33deb28101 to your computer and use it in GitHub Desktop.
Save timrchavez/c8808e1f296a0ffdf636fe33deb28101 to your computer and use it in GitHub Desktop.
concourse upgrade script
!/bin/bash
CLUSTERS_PATH=${CLUSTERS_PATH:-"$(dirname \"$0\")/../clusters"}
CLUSTER_NAME=${CLUSTER_NAME:-"na"}
get_concourse_version() {
node=$1
ssh -M $node "sudo /opt/concourse/bin/concourse --version"
}
land_worker() {
node=$1
if ! ssh $node "sudo /opt/concourse/bin/concourse land-worker --name=$node --tsa-public-key=/opt/concourse/keys/host_key.pub --tsa-worker-private-key=/opt/concourse/keys/worker_key --tsa-host=$tsa_node"
then
echo "Upgrade failed!"
exit 1
fi
while [ "$worker_state" != "landed" ]
do
echo "Waiting for worker to land..."
sleep 1
worker_state=`fly -t main-$CLUSTER_NAME workers | awk -v node="$node" '$1 ~ node {print $6}'`
done
}
prompt_user() {
answer=$1
if [[ "$answer" != "yes" ]]
then
read -r -p "Are you sure? [y/N] " response
response=${response,,}
if [[ -z "$response" || "$response" =~ ^(no|n)$ ]]
then
echo "Upgrade aborted!"
exit 0
fi
fi
}
run_chef_client() {
node=$1
if ! ssh $node "sudo chef-client"
then
echo "Upgrade failed!"
exit 1
fi
}
strip_str() {
str=$1
echo `echo $str | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | xargs`
}
sync_fly() {
fly -t main-$CLUSTER_NAME sync
}
upgrade_web_nodes() {
IFS=',' read -r -a web_nodes <<< "`terraform-do show | grep web_names | awk -F= '{print $2}'`"
echo "Upgrading web nodes..."
for node in ${web_nodes[@]}
do
node=$(strip_str $node)
echo Upgrading web node: $node...""
old_version=$(get_concourse_version $node)
echo "Running chef-client..."
run_chef_client $node
new_version=$(get_concourse_version $node)
echo "Upgraded from $old_version to $new_version"
done
}
upgrade_workers() {
IFS=',' read -r -a web_nodes <<< "`terraform-do show | grep web_names | awk -F= '{print $2}'`"
IFS=',' read -r -a worker_nodes <<< "`terraform-do show | grep worker_names | awk -F= '{print $2}'`"
tsa_node=$(strip_str ${web_nodes[0]})
echo "Upgrading worker nodes..."
for node in ${worker_nodes[@]}
do
node=$(strip_str $node)
echo "Upgrading worker: $node..."
old_version=$(get_concourse_version $node)
echo "Landing worker..."
land_worker $node
echo "Worker landed!"
echo "Running chef-client..."
run_chef_client $node
new_version=$(get_concourse_version $node)
echo "Upgraded from $old_version to $new_version"
done
}
usage() { echo "Usage: $0 [-y]" 1>&2; exit 1; }
while getopts ":y" o
do
case "${o}" in
y)
answer="yes"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ ! -d "${CLUSTERS_PATH}/${CLUSTER_NAME}" ]
then
echo "Could not find '${CLUSTER_NAME}' at '${CLUSTERS_PATH}'!"
exit 1
fi
cd $CLUSTERS_PATH/$CLUSTER_NAME
echo "Upgrading Concourse in the '$CLUSTER_NAME' cluster..."
prompt_user $answer
# Use correct version of fly tool
sync_fly
# Upgrade workers
upgrade_workers
# Upgrade web nodes
upgrade_web_nodes
cd -
echo "Upgrade complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment