Skip to content

Instantly share code, notes, and snippets.

@ChaosJohn
Last active April 12, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaosJohn/d804b936d8de691a749c650a9aaf2e1d to your computer and use it in GitHub Desktop.
Save ChaosJohn/d804b936d8de691a749c650a9aaf2e1d to your computer and use it in GitHub Desktop.
Install docker-machine executable from official (Automatically check the running system)
#!/bin/sh
# Extract the value for the key
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
# Get the latest version name from github
VERSION=`curl -s -X GET https://api.github.com/repos/docker/machine/releases/latest | jsonValue name 1`
VERSION=`echo $VERSION` # trim the leading whitespaces
if [[ $VERSION =~ ^v* ]]; then
echo "Latest version is $VERSION"
else
VERSION="v0.14.0"
echo "Failed to get latest version name, remain $VERSION"
fi
# Directory to store the docker-machine executable
DIR="/usr/local/bin"
if [[ -d $1 ]]; then
echo "Path changed from $DIR to $1"
DIR=$1
fi
URL_PREFIX="https://github.com/docker/machine/releases/download"
URL_FILE="docker-machine-`uname -s`-`uname -m`"
FILE_NAME="docker-machine"
# Fix the docker-machine url_file and executable filename by running environment, such as Linux, Mac, Windows(Cygwin or Ming[git-bash])
UNAME=`uname -s`
case "${UNAME}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*)
machine=Cygwin
URL_FILE="docker-machine-Windows-x86_64.exe"
DIR="$HOME/bin"
FILE_NAME="docker-machine.exe"
;;
MINGW*)
machine=MinGw
URL_FILE="docker-machine-Windows-x86_64.exe"
DIR="$HOME/bin"
FILE_NAME="docker-machine.exe"
;;
*) machine="UNKNOWN:${UNAME}"
esac
echo ${machine}
# The docker-machine executable filename
BIN="$DIR/$FILE_NAME"
echo "Execuable path is ${BIN}"
# Url for downloading
URL="$URL_PREFIX/$VERSION/$URL_FILE"
echo ${URL}
if [[ ! -d $DIR ]]; then mkdir -p $DIR; fi && \
curl -L $URL >$BIN && \
chmod +x $BIN
@ChaosJohn
Copy link
Author

ChaosJohn commented Apr 12, 2018

sh installDockerMachine.sh: install docker-machine to default path(/usr/local/bin for *nix and $HOME/bin for win)
sh installDockerMachine.sh [DIR]: install docker-machine to [DIR]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment