Skip to content

Instantly share code, notes, and snippets.

@MichalCz
Created October 16, 2019 11:30
Show Gist options
  • Save MichalCz/74a0b2e6a91ace8ed0d549e62258c609 to your computer and use it in GitHub Desktop.
Save MichalCz/74a0b2e6a91ace8ed0d549e62258c609 to your computer and use it in GitHub Desktop.
#!/bin/bash
mnt=$1
host=$2
user=$3
red=$'\e[31m'
yellow=$'\e[33m'
green=$'\e[32m'
cyan=$'\e[36m'
reset=$'\e[0m'
function op() {
arg=""
if [[ "$1" == "-n" ]]; then
arg="-n"; shift
currentop=""
elif [[ "$1" == "-q" ]]; then
shift
currentop=""
else
currentop=$1" "
fi
echo $arg $cyan" >-- "$reset "$@"
}
function status() {
retval=$?
if [[ $retval -eq 0 ]]; then
echo $cyan" --> "${green}OK${reset} ${currentop}${yellow}"$@"${reset}
else
echo $cyan" --X "${red}FAIL${reset} ${currentop}${yellow}"$@"${reset}
[[ -n "$OUTPUT" ]] && echo && echo $OUTPUT
exit 100
fi
OUTPUT=''
}
echo -n Trying to find $host ip...
configfile=$HOME/.docker/machine/machines/${host}/config.json
hostip=$(nslookup virage 2> /dev/null | awk '/Address/{a=$2}END{print a}' )
if [[ -z "$hostip" ]]; then
hostip=$(avahi-resolve-address -4n ${host}.local | cut -d$'\t' -f2)
fi
status $hostip
hostexists=`docker-machine ls -q | egrep ^${host}'$' | wc -l`
if (( hostexists )); then
currenthost=$(jq -M '.Driver.IPAddress' $configfile)
if [[ ! '"'$hostip'"' == "$currenthost" ]]; then
op "removing instance" "$yellow IP changed $reset"
docker-machine rm -y ${host}
status
hostexists=0
else
op -z Using existing docker-machine ${host}
fi
fi
if ! (( hostexists )); then
op "Creating docker-machine" for $host
docker-machine create --driver generic \
--generic-ip-address ${hostip} \
--generic-ssh-user ${michal} \
--generic-ssh-key=$HOME/.ssh/id_rsa ${host}
status
fi
op -n Running vs code with docker on ${host}
eval $(docker-machine env $host)
code --user-data-dir ${HOME}/.vscode-${host}
status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment