spearhead-docker-cli/triton-docker

153 lines
5.5 KiB
Plaintext
Raw Normal View History

2017-05-19 20:17:38 +03:00
#!/bin/bash
set -e -o pipefail
2017-05-19 20:17:38 +03:00
2017-05-19 23:06:38 +03:00
triton_docker_version=1.12.6
triton_compose_version=1.9.0
2017-05-19 20:17:38 +03:00
# Check if triton is installed and executable
2017-05-19 21:45:36 +03:00
function checkTritonInstalled () {
2018-02-12 17:44:56 +02:00
command -v spearhead >/dev/null 2>&1 || { echo >&2 "Spearhead CLI tools do not appear to be installed. Please install before continuing."; exit 1; }
2017-05-19 21:45:36 +03:00
}
2017-05-19 20:17:38 +03:00
2017-05-19 21:45:36 +03:00
# Check if curl is installed and executable
function checkCurlInstalled () {
command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required to complete this installation. Please install before continuing."; exit 1; }
}
2018-02-12 17:44:56 +02:00
# Check if the Docker binary (named spearhead-docker-helper)
# and Docker Compose (named spearhead-compose-helper) are installed and executable
2017-05-19 21:45:36 +03:00
function checkDockerInstalled () {
2018-02-12 17:44:56 +02:00
if
[ ! -f /usr/local/bin/spearhead-docker-helper ] ||
[ ! -x /usr/local/bin/spearhead-docker-helper ] ||
[ "${triton_docker_version}," != "$(/usr/local/bin/spearhead-docker-helper -v | awk '{print $3}')" ] ||
[ ! -f /usr/local/bin/spearhead-compose-helper ] ||
[ ! -x /usr/local/bin/spearhead-compose-helper ] ||
[ "${triton_compose_version}," != "$(/usr/local/bin/spearhead-compose-helper -v | awk '{print $3}')" ]
2017-05-19 21:45:36 +03:00
then
echo 'Additional or updated components are required.'
2018-02-12 17:44:56 +02:00
echo 'Please run `sudo spearhead-docker-install` to continue.'
2017-05-19 21:45:36 +03:00
echo
exit 1
2017-05-19 21:45:36 +03:00
fi
}
# Install architecture-specific Docker and Docker Compose binaries
function install () {
2018-02-12 17:44:56 +02:00
echo "Spearhead Cloud needs to install additional components for Docker and Docker Compose interactions."
echo "This installation will only happen once."
# Check if curl is installed and executable
checkCurlInstalled
2017-05-19 20:17:38 +03:00
# Install the specific version of Docker for Triton
2018-02-12 17:44:56 +02:00
rm -Rf /tmp/spearhead-docker /usr/local/bin/spearhead-docker-helper
mkdir -p /tmp/spearhead-docker
curl https://get.docker.com/builds/"$(uname -a | awk '{ print $1 }')"/x86_64/docker-${triton_docker_version}.tgz | tar zxvf - -C /tmp/spearhead-docker
mv /tmp/spearhead-docker/docker/docker /usr/local/bin/spearhead-docker-helper
rm -Rf /tmp/spearhead-docker
chmod +x /usr/local/bin/spearhead-docker-helper
echo "The spearhead-docker-helper is now installed."
echo
# Install the specific version of Docker Compose for Triton
2018-02-12 17:44:56 +02:00
curl -Lo /usr/local/bin/spearhead-compose-helper https://github.com/docker/compose/releases/download/${triton_compose_version}/docker-compose-"$(uname -a | awk '{ print $1 }')"-x86_64
chmod +x /usr/local/bin/spearhead-compose-helper
2018-02-12 17:44:56 +02:00
echo "The spearhead-compose-helper is now installed."
echo
}
2017-06-02 19:46:34 +03:00
# Emit the current Triton profile and time
function currentProfile () {
2018-02-12 17:44:56 +02:00
if [ -n "$SC_PROFILE" ]
2017-06-02 19:46:34 +03:00
then
2018-02-12 17:44:56 +02:00
local profile_source='via env var; use `eval "$(spearhead env <profile name>)"` to change'
2017-06-02 19:46:34 +03:00
else
2018-02-12 17:44:56 +02:00
local profile_source='default; use `spearhead profile set <profile name>` to change'
2017-06-02 19:46:34 +03:00
fi
2017-06-03 00:42:16 +03:00
# Get and the user's account information and CloudAPI URL for parsing later
2018-02-12 17:44:56 +02:00
local triton_account="$(spearhead account get)"
2017-06-03 00:42:16 +03:00
2018-02-12 17:44:56 +02:00
echo "Executing in '$(spearhead profile get | awk 'NR == 1 {print $2}')' (${profile_source})"
2017-06-03 00:42:16 +03:00
# Check if CNS is enabled, require it
2017-06-03 18:58:09 +03:00
if [ ! "true" == "$(echo "${triton_account}" | awk -F": " '/^triton_cns_enabled:/{print $2}')" ]
2017-06-03 00:42:16 +03:00
then
echo
tput rev # reverse
tput bold # bold
2017-06-03 18:58:09 +03:00
echo 'Warning! Triton CNS is not enabled.'
2017-06-03 00:42:16 +03:00
tput sgr0 # clear
echo
echo 'Please run the following to activate:'
2018-02-12 17:44:56 +02:00
echo ' spearhead account update triton_cns_enabled=true'
2017-06-03 00:42:16 +03:00
echo
fi
2017-06-02 19:46:34 +03:00
}
# Set env vars to make using CNS easier
2017-06-03 00:20:37 +03:00
function cnsEnvVars () {
2017-06-02 19:46:34 +03:00
# If the CNS env vars are already set, don't bother continuing
2018-02-12 17:44:56 +02:00
if [ -n "$SC_CNS_SEARCH_DOMAIN_PUBLIC" ] && [ -n "$SC_CNS_SEARCH_DOMAIN_PRIVATE" ]
2017-06-02 19:46:34 +03:00
then
return
2017-06-02 19:46:34 +03:00
fi
# Get and the user's account information and CloudAPI URL for parsing later
2018-02-12 17:44:56 +02:00
local triton_account="$(spearhead account get)"
local triton_url="$(spearhead profile get | awk -F"/" '/url:/{print $3}')"
2018-02-12 17:44:56 +02:00
Do not continue if the target is not a Spearhead Cloud data center
if [ ! ".api.joyent.com" == "${triton_url: -15}" ] || [ ! ".api.spearhead.cloud" == "${triton_url: -15}" ]
then
return
fi
# Get the user's UUID
2017-06-03 18:58:09 +03:00
local triton_account_uuid="$(echo "${triton_account}" | awk -F": " '/^id:/{print $2}')"
# Get current data center name
# Note: this makes assumptions that work in our public cloud, but might not work elsewhere
# Further note: set TRITON_CNS_SUFFIX_PUBLIC and TRITON_CNS_SUFFIX_PRIVATE to work around this
local triton_dc="$(echo "${triton_url}" | awk -F'.' '{print $1}')"
# Set the CNS base for public and private names
2017-06-03 00:42:16 +03:00
# Note, this also makes assumptions that only work in our public cloud
2018-02-12 17:44:56 +02:00
export SC_CNS_SEARCH_DOMAIN_PUBLIC="${triton_account_uuid}.${triton_dc}.spearhead.cloud"
export SC_CNS_SEARCH_DOMAIN_PRIVATE="${triton_account_uuid}.${triton_dc}.cns.spearhead.cloud"
2017-06-02 19:46:34 +03:00
}
function checkAll () {
checkTritonInstalled &&
checkDockerInstalled
2017-05-19 21:45:36 +03:00
}
2017-05-19 20:17:38 +03:00
if [ "profile" == "$1" ]
then
currentProfile
exit 1
fi
2017-06-02 19:12:45 +03:00
case "$(basename $0)" in
2018-02-12 17:44:56 +02:00
spearhead-docker)
checkAll &&
2018-02-12 17:44:56 +02:00
eval "$(spearhead env)" &&
2017-06-03 00:20:37 +03:00
cnsEnvVars &&
2018-02-12 17:44:56 +02:00
exec /usr/local/bin/spearhead-docker-helper $@
2017-05-19 21:45:36 +03:00
;;
2018-02-12 17:44:56 +02:00
spearhead-compose)
checkAll &&
2017-05-19 21:45:36 +03:00
eval "$(triton env)" &&
export COMPOSE_HTTP_TIMEOUT=750 &&
2017-06-03 00:20:37 +03:00
cnsEnvVars &&
2018-02-12 17:44:56 +02:00
exec /usr/local/bin/spearhead-compose-helper $@
2017-05-19 21:45:36 +03:00
;;
2018-02-12 17:44:56 +02:00
spearhead-docker-install)
install
;;
2017-05-19 21:45:36 +03:00
esac