Skip to content

Commit

Permalink
refactor: extract scripts from install-docker (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRibeiro authored May 3, 2022
1 parent da6c3a0 commit 2e1e4e4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 74 deletions.
78 changes: 4 additions & 74 deletions src/commands/install-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,7 @@ parameters:
steps:
- run:
name: Install Docker CLI
command: |
if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi
# grab Docker version
if [[ <<parameters.version>> == "latest" ]]; then
# extract latest version from GitHub releases API
declare -i INDEX=0
while :
do
INDEX_VERSION=$(curl --silent --show-error --location --fail --retry 3 \
https://api.github.com/repos/docker/cli/tags | \
jq --argjson index "$INDEX" '.[$index].name')
# filter out betas & release candidates
if [[ $(echo "$INDEX_VERSION" | grep -v beta | grep -v rc) ]]; then
# can't use substring expression < 0 on macOS
DOCKER_VERSION="${INDEX_VERSION:1:$((${#INDEX_VERSION} - 1 - 1))}"
echo "Latest stable version of Docker is $DOCKER_VERSION"
break
else
INDEX=INDEX+1
fi
done
else
DOCKER_VERSION=<<parameters.version>>
echo "Selected version of Docker is $DOCKER_VERSION"
fi
# check if Docker needs to be installed
DOCKER_VERSION_NUMBER="${DOCKER_VERSION:1}"
if command -v docker >> /dev/null 2>&1; then
if docker --version | grep "$DOCKER_VERSION_NUMBER" >> /dev/null 2>&1; then
echo "Docker $DOCKER_VERSION is already installed"
exit 0
else
echo "A different version of Docker is installed ($(docker --version)); removing it"
$SUDO rm -f $(command -v docker)
fi
fi
# get binary download URL for specified version
if uname -a | grep Darwin >> /dev/null 2>&1; then
PLATFORM=mac
else
PLATFORM=linux
fi
DOCKER_BINARY_URL="https://download.docker.com/$PLATFORM/static/stable/x86_64/docker-$DOCKER_VERSION_NUMBER.tgz"
# download binary tarball
DOWNLOAD_DIR="$(mktemp -d)"
DOWNLOAD_FILE="${DOWNLOAD_DIR}/docker.tgz"
curl --output "$DOWNLOAD_FILE" \
--silent --show-error --location --fail --retry 3 \
"$DOCKER_BINARY_URL"
tar xf "$DOWNLOAD_FILE" -C "$DOWNLOAD_DIR" && rm -f "$DOWNLOAD_FILE"
# install Docker binaries
BINARIES=$(ls "${DOWNLOAD_DIR}/docker")
$SUDO mv "$DOWNLOAD_DIR"/docker/* <<parameters.install-dir>>
$SUDO rm -rf "$DOWNLOAD_DIR"
for binary in $BINARIES
do
$SUDO chmod +x "<<parameters.install-dir>>/$binary"
done
# verify version
echo "$(docker --version) has been installed to $(which docker)"
environment:
PARAM_VERSION: << parameters.version >>
PARAM_INSTALL_DIR: << parameters.install-dir >>
command: << include(scripts/install-docker.sh) >>
76 changes: 76 additions & 0 deletions src/scripts/install-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash

if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi

# grab Docker version
if [[ "$PARAM_VERSION" == "latest" ]]; then
# extract latest version from GitHub releases API
declare -i INDEX=0

while :
do
INDEX_VERSION=$(curl --silent --show-error --location --fail --retry 3 \
https://api.github.com/repos/docker/cli/tags | \
jq --argjson index "$INDEX" '.[$index].name')

# filter out betas & release candidates
# shellcheck disable=SC2143 # Doesn't apply to this case.
if [[ $(echo "$INDEX_VERSION" | grep -v beta | grep -v rc) ]]; then

# can't use substring expression < 0 on macOS
DOCKER_VERSION="${INDEX_VERSION:1:$((${#INDEX_VERSION} - 1 - 1))}"

echo "Latest stable version of Docker is $DOCKER_VERSION"
break
else
INDEX=$((INDEX+1))
fi
done
else
DOCKER_VERSION="$PARAM_VERSION"
echo "Selected version of Docker is $DOCKER_VERSION"
fi

# check if Docker needs to be installed
DOCKER_VERSION_NUMBER="${DOCKER_VERSION:1}"

if command -v docker >> /dev/null 2>&1; then
if docker --version | grep "$DOCKER_VERSION_NUMBER" >> /dev/null 2>&1; then
echo "Docker $DOCKER_VERSION is already installed"
exit 0
else
echo "A different version of Docker is installed ($(docker --version)); removing it"
$SUDO rm -f "$(command -v docker)"
fi
fi

# get binary download URL for specified version
if uname -a | grep Darwin >> /dev/null 2>&1; then
PLATFORM=mac
else
PLATFORM=linux
fi

DOCKER_BINARY_URL="https://download.docker.com/$PLATFORM/static/stable/x86_64/docker-$DOCKER_VERSION_NUMBER.tgz"

# download binary tarball
DOWNLOAD_DIR="$(mktemp -d)"
DOWNLOAD_FILE="${DOWNLOAD_DIR}/docker.tgz"
curl --output "$DOWNLOAD_FILE" \
--silent --show-error --location --fail --retry 3 \
"$DOCKER_BINARY_URL"

tar xf "$DOWNLOAD_FILE" -C "$DOWNLOAD_DIR" && rm -f "$DOWNLOAD_FILE"

# install Docker binaries
BINARIES=$(ls "${DOWNLOAD_DIR}/docker")
$SUDO mv "$DOWNLOAD_DIR"/docker/* "$PARAM_INSTALL_DIR"
$SUDO rm -rf "$DOWNLOAD_DIR"

for binary in $BINARIES
do
$SUDO chmod +x "$PARAM_INSTALL_DIR/$binary"
done

# verify version
echo "$(docker --version) has been installed to $(command -v docker)"

0 comments on commit 2e1e4e4

Please sign in to comment.