Compare commits

...

2 Commits

Author SHA1 Message Date
d9eff7bece Add WSL startup scripts 2018-08-21 18:10:08 +02:00
ba8fc9be8d Add docker relay installation script 2018-08-21 17:53:23 +02:00

205
windows/install-docker-in-wsl.sh Executable file
View File

@ -0,0 +1,205 @@
#!/usr/bin/env bash
installGo() {
echo "Installing golang"
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 "Installing npiperelay"
echo "Fetching the 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 binary 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
}
installDockerCompose() {
echo "Installing docker-compose"
echo "Fetching latest tags from github..."
local tags=$(curl -s https://github.com/docker/compose/tags | awk '/docker\/compose\/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
local compose_path=$(which docker-compose)
which docker-compose > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
compose_path=/usr/local/bin/docker-compose
__warn "You do not seem to have docker compose installed, installing to ${compose_path}"
fi
__msg "Installing docker-compose ${latest}"
sudo curl -L https://github.com/docker/compose/releases/download/${latest}/docker-compose-`uname -s`-`uname -m` --output "${compose_path}" &&
sudo chmod +x "${compose_path}"
[[ $? -ne 0 ]] && echo "Docker compose might be running, please close all processes before installing this version" && return 2
return 0
}
initialize() {
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
installDockerCompose
[[ $? -ne 0 ]] && echo "Could not install docker compose" && return 137
return 0
}
[[ ${1} = "" ]] && initialize || "$@"
exit $?