Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active July 13, 2021 11:48
Show Gist options
  • Save mjf/b9e94f02c017f5df0d8c87c6977621c2 to your computer and use it in GitHub Desktop.
Save mjf/b9e94f02c017f5df0d8c87c6977621c2 to your computer and use it in GitHub Desktop.
Docker Object Tools
#! /bin/sh
# dockercat - Print Docker config or secret object
# Copyright (C) 2021 Matous Jan Fialka, <https://mjf.cz/>
# Released under the terms of the "MIT License"
PROGRAM="${0##*/}"
if [ $# -eq 0 -o $# -ne 2 ]; then
echo "Usage: $PROGRAM { config | secret } OBJECT" >&2
exit 1
fi
case "$1" in
'config' | 'secret')
;;
*)
echo "$PROGRAM: Unsupported object '$1'" >&2
exit 2
esac
docker $1 inspect --format='{{.Spec.Data}}' --pretty "$2" |sed '1,/^Data:/d;${/^$/d}'
# vi:ft=sh
#! /bin/sh
# dockeredit - Edit Docker service config or secret object
# Copyright (C) 2021 Matous Jan Fialka, <https://mjf.cz/>
# Released under the terms of the "MIT License"
# WARNING: The service gets updated twice to keep the original object name!
PROGRAM="${0##*/}"
if [ $# -eq 0 -o $# -ne 4 ]; then
echo "Usage: $PROGRAM { config | secret } SERVICE OBJECT PATH" >&2
exit 1
fi
case $1 in
'config' | 'secret')
;;
*)
echo "$PROGRAM: Unsupported object '$1'" >&2
exit 1
;;
esac
tid="$(mktemp -q -u 'XXXXX')"
tmp="/tmp/$PROGRAM-$1-$2-$3-$tid.tmp"
cat <<- EOT
$PROGRAM: Using temporary identifier '$tid'
$PROGRAM: Using temporary file '$tmp'
EOT
if ! dockercat $1 "$3" >"$tmp"; then
exit $?
fi
if ! ${EDITOR:-vi} "$tmp"; then
exit $?
fi
if docker $1 create "$3.$tid" "$tmp" >/dev/null; then
if docker service update -qd --update-failure-action rollback --$1-rm "$3" --$1-add source="$3.$tid",target="$4" "$2" >/dev/null; then
if docker $1 rm "$3" >/dev/null; then
if docker $1 create "$3" "$tmp" >/dev/null; then
if docker service update --update-failure-action rollback --$1-rm "$3.$tid" --$1-add source="$3",target="$4" "$2"; then
if docker $1 rm "$3.$tid" >/dev/null; then
rm -f "$tmp"
fi
fi
fi
fi
fi
fi
# vi:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment