Skip to content

Instantly share code, notes, and snippets.

@minrwhite
Forked from dnephin/compose-hooks.sh
Last active March 12, 2018 16:30
Show Gist options
  • Save minrwhite/cd33f4755b51a2f4c2f6275e0a299ed4 to your computer and use it in GitHub Desktop.
Save minrwhite/cd33f4755b51a2f4c2f6275e0a299ed4 to your computer and use it in GitHub Desktop.
Execute a hook in './hooks/<service>/<action>' when that event is received - with hook for first start up
#!/usr/bin/env bash
set -e
declare -A first_start
function handle_event() {
local entry="$1"
local action=$(echo $entry | jq -r '.action')
local service=$(echo $entry | jq -r '.service')
local hook="./hooks/$service/$action"
if [ -x "$hook" ]; then
"$hook" "$entry"
fi
if [ "$action" == "create" ]; then
first_start["$service"]=1
fi
if [ "$action" == "start" ] && [ "${first_start[$service]}" -eq "1" ]; then
unset first_start["$service"]
local subhook="./hooks/$service/first_start"
if [ -x "$subhook" ]; then
"$subhook" "$entry"
fi
fi
}
docker-compose events --json | (
while read line; do
handle_event "$line"
done
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment