Skip to content

Instantly share code, notes, and snippets.

@darkmuggle
Created April 28, 2016 18:57
Show Gist options
  • Save darkmuggle/a6ce0445ce39b4164e6ced28c56cae78 to your computer and use it in GitHub Desktop.
Save darkmuggle/a6ce0445ce39b4164e6ced28c56cae78 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright 2016, DigitalOcean
# Author: Ben Howard <bh@do.co>
#
# Fetch the latest version of doctl
#
work_d=$(mktemp --directory)
trap "rm -rf ${work_d}" EXIT
export PATH="/usr/bin:/usr/sbin:/sbin:/bin:/usr/local/bin:/usr/local/sbin"
api_base_url="https://api.github.com/repos/digitalocean/doctl"
tar_base_url="https://github.com/digitalocean/doctl/releases/download"
# Locate utilities
tar_bin="$(which tar)"
wget_bin="$(which wget)"
logger_bin="$(which logger) -t DigitalOcean"
fetch_cmd=""
log_write() {
echo "${@}"
${logger_bin} "${@}"
}
if [ -n "${wget_bin}" ]; then
fetch_cmd="${wget_bin} --prefer-family=IPv4 --quiet -O -"
else
log_write "Distro does not have a tool for fetching doctl, bailing."
exit 0
fi
# Locate the stuff
log_write "locating latest release of doctl"
latest_release=$(${fetch_cmd} ${api_base_url}/tags | awk '-F"' '/name/ {print$4; exit}')
# Bail if there is a problem
if [ -z "${latest_release}" ]; then
log_write "Unable to find the latest release of 'doctl'"
exit 0
fi
# Make sure we get the right build
build_suffix="amd64"
bin_ver=$(getconf LONG_BIT)
if [ "${bin_ver}" -eq 32 ]; then
build_suffix="386"
fi
# Now fetch it
tarball_url="${tar_base_url}/${latest_release}/doctl-${latest_release//v/}-linux-${build_suffix}.tar.gz"
log_write "Fetching ${tarball_url}"
count=0
until $(${work_d}/doctl &> /dev/null >&1);
do
echo "Trying to fetch doctl [$count/4]"
${fetch_cmd} ${tarball_url} | ${tar_bin} -C ${work_d} -xz
count=$(($count + 1))
if [ ${count} -ge 4 ]; then
log_write "failed to fetch a runnable version of doctl"
fi
done
# Now install it
local_f="/usr/local/bin/doctl"
mkdir -p /usr/local/bin
if [ -e ${local_f} ]; then
mv ${local_f} ${local_f}.old
fi
mv ${work_d}/doctl ${local_f}
log_write "Installed 'doctl' to ${local_f}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment