Skip to content

Instantly share code, notes, and snippets.

@ggtools
Created February 13, 2017 20:53
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 ggtools/467f98fa65ea588d568eea195b6c17ef to your computer and use it in GitHub Desktop.
Save ggtools/467f98fa65ea588d568eea195b6c17ef to your computer and use it in GitHub Desktop.
Update services secrets
#!/usr/bin/env bash
for service in $(docker service ls -f "label=le_auto" -q)
do
echo "Checking service $service"
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Secrets}}' $service \
| sed -r 's/("File":\{)/\n/g' \
| sed -n -r '/Name/ s/"[^"]+":"([^"]+)"[^"]+/\1 /g;s/"Mode":([[:digit:]]+)[^[:alnum:]]*/\1 / p' \
| ( while read target uid gid mode secret_id secret
do
base_secret=$(echo $secret | sed -r 's/\.[0-9]+$//')
last_secret=$(docker secret ls | tail -n +2 | awk '/'$base_secret'/ {print $2}' | sort | tail -1)
if [ $secret != $last_secret ]
then
echo "Updating $secret to $last_secret"
CHANGES="--secret-rm $secret --secret-add source=$last_secret,target=$target,uid=$uid,gid=$gid,mode=$mode $CHANGES"
fi
done
if [ -n "$CHANGES" ]
then
docker service update $CHANGES $service
fi)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment