#!/usr/bin/env bash installGo() { echo "Fetching latest tags from github" local tags=$(curl -s https://github.com/golang/go/tags | awk '/golang\/go\/releases\/tag/{print $7}' FS='["/]') local latest="" if [[ ${tags} = "" ]]; then echo "Could not fetch the tags" return 2 fi echo "Fetched the latest tags, deciding which one to use..." for tag in ${tags}[@]; do tag=${tag//go} if [[ ${tag} = *"rc"* ]]; then continue elif [[ ${tag} = *"beta"* ]]; then continue elif [[ ${tag} = *"alpha"* ]]; then continue fi echo "Found the latest tag: ${tag}" latest=${tag} break done if [[ "${latest}" = "" ]]; then __err "Could not fetch the latest release" return 2 fi echo "Downloading golang ${latest}" sudo curl -L --output "go.tar.gz" "https://storage.googleapis.com/golang/go${latest}.linux-amd64.tar.gz" echo "Installing golang in /usr/local/go" sudo tar -C /usr/local -xzf "go.tar.gz" [[ ! -e /usr/local/go/bin ]] && echo "Did not install go properly" && return 137 grep -q /usr/local/go/bin ~/.bashrc if [[ $? -ne 0 ]]; then echo "Adding PATH export to .bashrc to include golang binary location" echo "export PATH=\$PATH:/usr/local/go/bin" | tee -a ~/.bashrc fi echo "Removing the downloaded file" sudo rm -f "go.tar.gz" return 0 } installRelay() { echo "Fetching the docker relay from github" /usr/local/go/bin/go get -d github.com/jstarks/npiperelay [[ $? -ne 0 ]] && echo "Could not fetch the relay" && return 137 echo "Building the relay for windows" export GOOS=windows /usr/local/go/bin/go build -o /mnt/c/Users/${USER}/go/bin/npiperelay.exe github.com/jstarks/npiperelay [[ $? -ne 0 ]] && echo "Could not build the binary" && return 137 echo "Linking the relay binary" sudo ln -s /mnt/c/Users/${USER}/go/bin/npiperelay.exe /usr/local/bin/npiperelay.exe [[ $? -ne 0 ]] && echo "Could not link the binary" && return 137 echo "Installing socat and docker.io" sudo apt update sudo apt install -y socat docker.io [[ $? -ne 0 ]] && echo "Could not install the requirements" && return 137 return 0 } addStartupScript() { echo "Creating the docker relay startup script" local relayLocation=~/.docker-relay touch "${relayLocation}" chmod +x "${relayLocation}" cat << "EOF" | tee -a "${relayLocation}" > /dev/null #!/usr/bin/env bash isRelayRunning() { [[ $(ps aux | grep -v grep | grep "socat UNIX-LISTEN:/var/run/docker.sock,fork,group=docker,umask=007 EXEC" | wc -l) -eq 0 ]] && return 0 return 1 } isRelayRunning RUNNING=$? if [[ ${RUNNING} -eq 0 ]]; then echo "Starting docker relay" sudo ls > /dev/null exec sudo socat UNIX-LISTEN:/var/run/docker.sock,fork,group=docker,umask=007 EXEC:"npiperelay.exe -ep -s //./pipe/docker_engine",nofork & fi RETRIES=20 until [[ ${RUNNING} -eq 1 ]] || [[ ${RETRIES} -eq 0 ]]; do isRelayRunning RUNNING=$? sleep 1 RETRIES=$((RETRIES-1)) done if [[ ${RUNNING} -ne 1 ]] && [[ ${RETRIES} -eq 0 ]]; then echo "Could not initialize the docker relay!" echo "Exiting..." exit 137 fi exit 0 EOF echo "Aliasing docker to run the relay script first in ~/.bash_aliases" cat << EOF | tee -a ~/.bash_aliases > /dev/null alias docker="${relayLocation} && docker" EOF echo "Done, try running 'docker ps'" return 0 } createRelay() { installGo [[ $? -ne 0 ]] && echo "Could not install Go" && return 137 installRelay [[ $? -ne 0 ]] && echo "Could not install relay" && return 137 addStartupScript [[ $? -ne 0 ]] && echo "Could not add the startup script" && return 137 return 0 } [[ ${1} = "" ]] && createRelay || "$@" exit $?