-
-
Save JeanMertz/455b8d4a33e0c9099d22 to your computer and use it in GitHub Desktop.
/opt/bin/d - docker management script for CoreOS/systemd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
command -v systemd-cat &>/dev/null && exec 2> >(systemd-cat -t d) | |
source /etc/environment | |
run() { | |
shift | |
$docker run --cidfile "$pid_file" $@ | |
} | |
create() { | |
shift | |
$docker create --cidfile "$pid_file" $@ | |
} | |
stop() { | |
__execute_if_running 'stop' | |
} | |
kill() { | |
__execute_if_running 'kill' | |
} | |
rm() { | |
__exists || return 0 | |
__is_running && args='--force' | |
$docker rm $args $(name) && command rm -f "$pid_file" && curl -X DELETE $kv_url/$kv_path/cid | |
} | |
enrol() { | |
__wait_for_active_registry | |
curl -X PUT -d "$(__container_pidfile_contents)" $kv_url/$kv_path/cid | |
command rm -f "$pid_file" | |
} | |
name() { | |
__enroled || return 1 | |
[ -z "$name" ] && name=$(__container_id | xargs docker inspect --format '{{.Name}}') | |
# See: https://github.com/docker/docker/issues/6705 | |
[ "${name::1}" = "/" ] && name=${name:1} | |
echo $name | |
} | |
other() { | |
$docker $@ | |
} | |
main() { | |
local readonly subcommand=$1 | |
[[ -z "$subcommand" ]] && help | |
shift | |
case "$subcommand" in | |
run) run "$@";; | |
create) create "$@";; | |
stop) stop "$@";; | |
kill) kill "$@";; | |
rm) rm "$@";; | |
enrol) enrol "$@";; | |
name) name "$@";; | |
*) other "$subcommand $@";; | |
esac | |
} | |
__container_id() { | |
__wait_for_active_registry | |
[ -z "$cid" ] && cid=$(/usr/bin/curl -s $kv_url/$kv_path/cid | \ | |
/opt/bin/jq '.[] | .Value' | xargs echo | base64 --decode 2>/dev/null) | |
echo $cid | |
} | |
__wait_for_started_registry() { | |
until echo 'dummy-value' | ncat $COREOS_PRIVATE_IPV4 8400 >/dev/null 2>&1; do | |
sleep 3 | |
done | |
} | |
__wait_for_active_registry() { | |
__wait_for_started_registry | |
if [ -f /opt/bin/consul ]; then | |
until command /opt/bin/consul info -rpc-addr $COREOS_PRIVATE_IPV4:8400 | grep 'last_log_index' | grep -qv '= 0'; do | |
sleep 3 | |
done | |
else | |
until curl "$kv_url/?recurse" 2>&1 | grep -qv "No cluster leader"; do | |
sleep 3 | |
done | |
fi | |
} | |
__container_pidfile_contents() { | |
local contents="" | |
until [ -f "$pid_file" ]; do | |
sleep 1 | |
done | |
while [ -z "$contents" ]; do | |
contents=$(cat $pid_file) | |
sleep 1 | |
done | |
echo $contents | |
} | |
__inspect() { | |
[ -z "$(name)" ] && return 1 | |
[ -n "$1" ] && local readonly args="--format {{$1}}" | |
$docker inspect $args $(name) 2>/dev/null | |
} | |
__enroled() { | |
[ -n "$(__container_id)" ] | |
} | |
__exists() { | |
[ -z "$(name)" ] && return 1 | |
[ "$(__inspect)" != "[]" ] | |
} | |
__is_running() { | |
[ "$(__inspect '.State.Running')" = "true" ] | |
} | |
__execute_if_running() { | |
__is_running || return 0 | |
$docker $1 $(name) | |
} | |
readonly ARGS="$@" | |
readonly cmd="$1" | |
readonly kv_name="$2" | |
readonly pid_file="/tmp/${kv_name/\//_}.cid" | |
readonly kv_url="http://${COREOS_PRIVATE_IPV4}:8500/v1/kv" | |
readonly kv_path="$(hostname)/${kv_name}" | |
readonly docker=$(which docker) | |
name="" | |
cid="" | |
main $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment