diff --git a/scope b/scope index 4fdb07786..36b11a213 100755 --- a/scope +++ b/scope @@ -2,27 +2,16 @@ set -eu -ARGS="$@" - -usage() { - echo "Usage:" - echo "scope launch [ ...]" - echo "scope stop" - echo "scope command" - echo - echo "scope is of the form [:]" - exit 1 -} - +ARGS="$*" SCRIPT_VERSION="(unreleased version)" if [ "$SCRIPT_VERSION" = "(unreleased version)" ] ; then IMAGE_VERSION=latest else - IMAGE_VERSION=$SCRIPT_VERSION + IMAGE_VERSION="$SCRIPT_VERSION" fi IMAGE_VERSION=${VERSION:-$IMAGE_VERSION} SCOPE_IMAGE_NAME=weaveworks/scope -SCOPE_IMAGE=$SCOPE_IMAGE_NAME:$IMAGE_VERSION +SCOPE_IMAGE="$SCOPE_IMAGE_NAME:$IMAGE_VERSION" SCOPE_CONTAINER_NAME=weavescope SCOPE_APP_CONTAINER_NAME=weavescope-app IP_REGEXP="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" @@ -30,7 +19,32 @@ IP_ADDR_CMD="find /sys/class/net -type l | xargs -n1 basename | grep -vE 'docker xargs -n1 ip addr show | grep inet | awk '{ print \$2 }' | grep -oE '$IP_REGEXP'" WEAVESCOPE_DOCKER_ARGS=${WEAVESCOPE_DOCKER_ARGS:-} -[ $# -gt 0 ] || usage +usage() { + name=$(basename "$0") + cat >&2 <<-EOF + Usage: + $name launch {OPTIONS} {PEERS} - Launch Scope + $name stop - Stop Scope + $name command - Print the docker command used to start Scope + $name help - Print usage info + $name version - Print version info + + PEERS are of the form HOST[:PORT] + HOST may be an ip or hostname. + PORT defaults to 4040. + + Launch options: + EOF + docker run --rm -e CHECKPOINT_DISABLE --entrypoint=/home/weave/scope \ + $WEAVESCOPE_DOCKER_ARGS "$SCOPE_IMAGE" -h >&2 +} + +usage_and_die() { + usage + exit 1 +} + +[ $# -gt 0 ] || usage_and_die COMMAND=$1 shift 1 @@ -98,7 +112,7 @@ check_docker_for_mac() { # Check that a container named $1 with image $2 is not running check_not_running() { - case $(docker inspect --format='{{.State.Running}} {{.Config.Image}}' $1 2>/dev/null) in + case $(docker inspect --format='{{.State.Running}} {{.Config.Image}}' "$1" 2>/dev/null) in "true $2") echo "$1 is already running." >&2 exit 1 @@ -108,10 +122,10 @@ check_not_running() { exit 1 ;; "false $2") - docker rm $1 >/dev/null + docker rm "$1" >/dev/null ;; "false $2:"*) - docker rm $1 >/dev/null + docker rm "$1" >/dev/null ;; true*) echo "Found another running container named '$1'. Aborting." >&2 @@ -135,29 +149,29 @@ create_plugins_dir() { # sockets do not cross VM boundaries. We need this directory to exits on the VM. docker run --rm --entrypoint=/bin/sh \ -v /var/run:/var/run \ - $SCOPE_IMAGE -c "mkdir -p /var/run/scope/plugins" + "$SCOPE_IMAGE" -c "mkdir -p /var/run/scope/plugins" } launch_command() { - echo docker run --privileged -d --name=$SCOPE_CONTAINER_NAME --net=host --pid=host \ + echo docker run --privileged -d --name="$SCOPE_CONTAINER_NAME" --net=host --pid=host \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/scope/plugins:/var/run/scope/plugins \ -e CHECKPOINT_DISABLE \ - $WEAVESCOPE_DOCKER_ARGS $SCOPE_IMAGE --probe.docker=true + $WEAVESCOPE_DOCKER_ARGS "$SCOPE_IMAGE" --probe.docker=true } launch_docker4mac_app_command() { - echo docker run -d --name=$SCOPE_APP_CONTAINER_NAME \ + echo docker run -d --name="$SCOPE_APP_CONTAINER_NAME" \ -e CHECKPOINT_DISABLE \ -p 0.0.0.0:4040:4040 \ - $WEAVESCOPE_DOCKER_ARGS $SCOPE_IMAGE --no-probe + $WEAVESCOPE_DOCKER_ARGS "$SCOPE_IMAGE" --no-probe } launch() { - check_not_running $SCOPE_CONTAINER_NAME $SCOPE_IMAGE_NAME - docker rm -f $SCOPE_CONTAINER_NAME >/dev/null 2>&1 || true - CONTAINER=$($(launch_command) "$@") - echo $CONTAINER + check_not_running "$SCOPE_CONTAINER_NAME" "$SCOPE_IMAGE_NAME" + docker rm -f "$SCOPE_CONTAINER_NAME" >/dev/null 2>&1 || true + $(launch_command) "$@" + echo "Scope probe started" } print_app_endpoints() { @@ -172,40 +186,32 @@ check_docker_version case "$COMMAND" in command) - # TODO: properly escape/quote the output of "$@" - echo $(launch_command) "$@" + # Most systems should have printf, but the %q specifier isn't mandated by posix + # and can't be guaranteed. Since this is mainly a cosmetic output and the alternative + # is not making any attempt to do escaping at all, we might as well try. + quoted=$(printf '%q ' "$@" 2>/dev/null || true) + # printf %q behaves oddly with zero args (it acts as though it recieved one empty arg) + # so we ignore that case. + if [ -z "$quoted" ] || [ $# -eq 0 ]; then + quoted="$*" + fi + echo "$(launch_command) $quoted" ;; version) docker run --rm -e CHECKPOINT_DISABLE --entrypoint=/home/weave/scope \ - $WEAVESCOPE_DOCKER_ARGS $SCOPE_IMAGE --mode=version + $WEAVESCOPE_DOCKER_ARGS "$SCOPE_IMAGE" --mode=version ;; - help) - cat >&2 <&2 </dev/null 2>&1 || true - CONTAINER=$($(launch_docker4mac_app_command) "$@") - echo $CONTAINER + check_not_running "$SCOPE_APP_CONTAINER_NAME" "$SCOPE_IMAGE_NAME" + check_not_running "$SCOPE_CONTAINER_NAME" "$SCOPE_IMAGE_NAME" + docker rm -f "$SCOPE_APP_CONTAINER_NAME" >/dev/null 2>&1 || true + $(launch_docker4mac_app_command) "$@" + echo "Scope probe started" app_ip=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' "${CONTAINER}") - docker rm -f $SCOPE_CONTAINER_NAME >/dev/null 2>&1 || true + docker rm -f "$SCOPE_CONTAINER_NAME" >/dev/null 2>&1 || true CONTAINER=$($(launch_command --no-app "$@" "${app_ip}:4040")) print_app_endpoints "localhost" exit @@ -232,27 +238,27 @@ EOF launch "$@" if ! check_probe_only ; then - IP_ADDRS=$(docker run --rm --net=host --entrypoint /bin/sh $SCOPE_IMAGE -c "$IP_ADDR_CMD") + IP_ADDRS=$(docker run --rm --net=host --entrypoint /bin/sh "$SCOPE_IMAGE" -c "$IP_ADDR_CMD") print_app_endpoints $IP_ADDRS fi ;; stop) - [ $# -eq 0 ] || usage - if docker inspect $SCOPE_CONTAINER_NAME >/dev/null 2>&1 ; then - docker stop $SCOPE_CONTAINER_NAME >/dev/null + [ $# -eq 0 ] || usage_and_die + if docker inspect "$SCOPE_CONTAINER_NAME" >/dev/null 2>&1 ; then + docker stop "$SCOPE_CONTAINER_NAME" >/dev/null fi if check_docker_for_mac ; then - if docker inspect $SCOPE_APP_CONTAINER_NAME >/dev/null 2>&1 ; then - docker stop $SCOPE_APP_CONTAINER_NAME >/dev/null + if docker inspect "$SCOPE_APP_CONTAINER_NAME" >/dev/null 2>&1 ; then + docker stop "$SCOPE_APP_CONTAINER_NAME" >/dev/null fi fi ;; *) echo "Unknown scope command '$COMMAND'" >&2 - usage + usage_and_die ;; esac