Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Created August 22, 2014 18:47
Show Gist options
  • Save kballenegger/68059d02d209049e2116 to your computer and use it in GitHub Desktop.
Save kballenegger/68059d02d209049e2116 to your computer and use it in GitHub Desktop.
Auto-deploy system through a system of etcd locks.
git fetch
# check if a deploy is necessary
checked_out=$(git rev-parse HEAD)
production=$(git rev-parse origin/production)
if [ $checked_out = $production ]; then
echo 'Nothing to deploy. Moving on...'
exit 0
fi
# check lock states
val=$(etcdctl get --consistent locks/api-internal-deploy)
if [[ $? = 4 ]]; then # when lock is brand new
echo 'Setting up lock for the first time.'
etcdctl mk locks/api-internal-deploy '0'
elif [[ "$val" != 0 ]]; then # when the lock is not brand new
echo 'Waiting for lock...'
etcdctl watch locks/api-internal-deploy # wait for change
fi
# acquire the actual lock
etcdctl set --swap-with-value '0' locks/api-internal-deploy '1'
if [[ $? != 0 ]]; then
echo 'Cannot acquire lock... EXIT.'; exit 1
fi
# finally, we can do the deploy
echo "Updating api-internal.""$1"".service..."
systemctl try-restart api-internal."$1".service
restart_code=$?
# clean up
etcdctl set locks/api-internal-deploy '0'
if [[ $restart_code != 0 ]]; then
echo 'Failed to restart service.'; exit 1
fi
echo "Updated api-internal.""$1"".service..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment