-
-
Save arikw/7d4229b25195abbe0cd13020469eb11c to your computer and use it in GitHub Desktop.
docker-machine command with default machine name
This file contains hidden or 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
| #!/bin/bash | |
| set -euo pipefail | |
| MACHINE_NAME=$(printf "%q" ${DOCKER_MACHINE_NAME:-null}) | |
| if [ "$MACHINE_NAME" == "null" ]; then | |
| echo "Error: No docker machine env variables are set" | |
| exit -1; | |
| fi | |
| DM_SPECIAL_COMMANDS=( | |
| 'config' 'env' 'inspect' 'ip' 'kill' 'provision' | |
| 'regenerate-certs' 'restart' 'scp' 'ssh' 'start' | |
| 'status' 'stop' 'upgrade' | |
| ) | |
| PRINT_EXPLANATION=0 | |
| SCRIPT_NAME=`basename "$0"` | |
| ARGS="$@" | |
| FIRST_ARG="${1:-}" | |
| SECOND_ARG="${2:-}" | |
| LAST_ARG="${@: -1}" | |
| if [ "$LAST_ARG" == "--explain" ]; then | |
| # remove last arg | |
| ARGS="${@:1:${#}-1}" | |
| PRINT_EXPLANATION=1 | |
| fi | |
| parse () { | |
| if [ -z "$ARGS" ]; then | |
| return | |
| fi | |
| # if using a docker-machine flag or an option, it's too complicated to parse | |
| if [ ${FIRST_ARG:0:1} == '-' ]; then | |
| # echo "skipped - option usage detected" | |
| return | |
| fi | |
| USING_SPECIAL_COMMAND=0 | |
| for COMMAND in "${DM_SPECIAL_COMMANDS[@]}"; do | |
| if [ "$COMMAND" == "$FIRST_ARG" ]; then | |
| USING_SPECIAL_COMMAND=1 | |
| SPECIAL_COMMAND="$COMMAND" | |
| break | |
| fi | |
| done | |
| if [ $USING_SPECIAL_COMMAND == 0 ]; then | |
| # echo "skipped - not using a special command" | |
| return | |
| fi | |
| # Skip injection if machine name is already used in the original command | |
| ALL_MACHINE_NAMES=$(docker-machine ls -t 0 --quiet) | |
| GREP_ARGS=$(echo ${ALL_MACHINE_NAMES} | sed 's/ /\\|/g') | |
| if [ ! -z "$(echo $ARGS | grep -w $GREP_ARGS)" ]; then | |
| # echo "skipped - found a machine name in the command" | |
| return | |
| fi | |
| ARGS=$(echo $ARGS | sed -e "s/^${SPECIAL_COMMAND}\b/${SPECIAL_COMMAND} $MACHINE_NAME/") | |
| } | |
| parse | |
| if [ "$PRINT_EXPLANATION" == 1 ]; then | |
| set -x | |
| fi | |
| docker-machine $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This bash script injects the default docker machine name into commands that are supported by the script.
The default machine name is taken from
DOCKER_MACHINE_NAMEenv variable.Usage example:
This will run
docker-machine ssh $DOCKER_MACHINE_NAMEThe supported commands are
config,env,inspect,ip,kill,provision,regenerate-certs,restart,scp,ssh,start,status,stopandupgradeThe script will not inject the default machine name if a supported command is not used as the first arguments or if there's a use of an existing machine name in the command.
This means that running
dm ssh dev, will rundocker-machine ssh devas usual without the usage ofDOCKER_MACHINE_NAMEenv variable.You can add
--explainas last parameter to print the actual command that the script runs.To use this script, download the
dmfile and make it executable by runningchmod +x dm