Docker-development/windows/install-docker-in-wsl.sh
2018-08-22 08:41:59 +02:00

247 lines
6.3 KiB
Bash
Executable File

#!/usr/bin/env bash
installDocker() {
local continue
local distribution
local codename
echo "Installing prerequisites for Docker"
sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
echo "Fetching GPG key"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
echo "Verify the fingerprint is correct:"
sudo apt-key fingerprint 0EBFCD88
echo "Does the fingerprint match [9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88] ? [y]/n"
read continue
[[ "${continue}" != "" ]] && [[ "${continue}" = "y" ]] && [[ "${continue}" = "Y" ]] && return 137
distribution=$(lsb_release -is | tr '[:upper:]' '[:lower:]')
codename=$(lsb_release -cs)
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/${distribution} ${codename} stable"
sudo apt update && sudo apt install -y docker-ce
[[ $? -ne 0 ]] && return 137
echo "Do you want to run docker commands without sudo? This will make your system less secure, effectively granting your user root privileges through the use of docker"
echo "y/[n]"
read continue
[[ "${continue}" != "" ]] && [[ "${continue}" = "y" ]] && [[ "${continue}" = "Y" ]] && return 0
sudo usermod -aG docker ${USER}
return 0
}
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"
sudo apt update
sudo apt install -y socat
[[ $? -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() {
installDocker
[[ $? -ne 0 ]] && echo "Could not install Docker" && return 137
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 $?