Created
October 11, 2018 11:16
-
-
Save kke/40c53b3472cb7193e7ec7de60e5d8718 to your computer and use it in GitHub Desktop.
an installer script for a kontena classic node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# # Kontena "Bring your own node" install script | |
# | |
# This script will configure and run Kontena Agent on a variety of systems, including MacOS and most Linux distributions and join it to a grid | |
# | |
# ## Requirements | |
# | |
# * A working Docker installation (`docker run` should work from the command line) | |
# * A Kontena Master running somewhere | |
# * A grid token obtained by `kontena grid show --token $(kontena grid current --name)` | |
# | |
# ## Usage | |
# | |
# ./install [KONTENA_MASTER_URI] [GRID_TOKEN] <NETWORK_INTERFACE> | |
# | |
# ## Remote usage from a URL | |
# | |
# $ curl -Ls https://URL_TO_THIS_FILE | sh -s [KONTENA_MASTER_URI] [GRID_TOKEN] <NETWORK_INTERFACE> | |
# | |
# ## License | |
# | |
# Kontena BYON Bash Installer is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for full license text. | |
command_exists() { | |
command -v "$1" >/dev/null 2>&1 | |
} | |
error_out() { | |
echo "$@" >&2 | |
exit 1 | |
} | |
validate_requirements() { | |
command_exists "docker" || error_out "[\033[31mError\033[0m] Your system does not have Docker installed" | |
} | |
print_usage() { | |
error_out "Usage: install.sh KONTENA_PLATFORM_URL KONTENA_GRID_TOKEN [NETWORK_INTERFACE]" | |
} | |
validate_options() { | |
[ "$1" = "" ] || [ "$2" = "" ] && print_usage | |
MASTER_URI=$1 | |
GRID_TOKEN=$2 | |
DEFAULT_INTERFACE=$3 | |
} | |
validate_docker_arch() { | |
[ "$(docker version --format '{{.Server.Arch}}')" = "amd64" ] || | |
error_out "[\033[31mError\033[0m] Docker server architecture has to be amd64. Kontena Agent does not currently run on non-x86_64 hosts." | |
} | |
version_gt() { | |
local IFS=. | |
local v1=($1) v2=($2) | |
[ "${v1[0]}" -ge "${v2[0]}" ] || return | |
[ "${v1[1]}" -ge "${v2[1]}" ] | |
} | |
validate_docker_version() { | |
local min_version=1.12 | |
local docker_api_version=$(docker version --format "{{ .Server.APIVersion }}") | |
version_gt $docker_api_version $min_version || | |
error_out "[\033[31mError\033[0m] Docker server API version has to be >= ${min_version}, your version is ${docker_api_version}" | |
} | |
validate_not_running() { | |
docker ps --format '{{.Image}}' | grep "kontena/agent" > /dev/null && error_out "[\033[31mError\033[0m] Docker agent is already running on the host" | |
} | |
query_master_version() { | |
MASTER_VERSION=$(curl -is $MASTER_URI|grep X-Kontena-Version|cut -d" " -f2|tr -d '\n\r') | |
[ "$MASTER_VERSION" = "" ] && error_out "[\033[31mError\033[0m] Could not detect Kontena Master version" | |
} | |
create_env_file() { | |
NODE_NAME=$(random_name) | |
if [ "$(uname)" = "Darwin" ]; then | |
AGENT_ENV_FILE=$HOME/.kontena/kontena-agent.env | |
mkdir $HOME/.kontena 2> /dev/null | |
else | |
AGENT_ENV_FILE=/etc/kontena-agent.env | |
fi | |
[ -e $AGENT_ENV_FILE ] && \ | |
read -p "Configuration file ${AGENT_ENV_FILE} exists and will be overwritten. Press enter to continue or ctrl-c to cancel.. " | |
cat > $AGENT_ENV_FILE <<EOF | |
KONTENA_NODE_NAME=$NODE_NAME | |
KONTENA_URI=$MASTER_URI | |
KONTENA_TOKEN=$GRID_TOKEN | |
KONTENA_PEER_INTERFACE=$DEFAULT_INTERFACE | |
KONTENA_VERSION=$MASTER_VERSION | |
EOF | |
} | |
get_default_interface() { | |
if [ "$DEFAULT_INTERFACE" = "" ]; then | |
DEFAULT_INTERFACE=`docker run kontena/agent:${MASTER_VERSION} route 2>/dev/null | grep '^default' | grep -o '[^ ]*$'` || \ | |
error_out "[\033[31mError\033[0m] Can not detect the default interface" | |
fi | |
} | |
docker_run() { | |
CONTAINER_ID=$( | |
docker run \ | |
--env-file ${AGENT_ENV_FILE} \ | |
-v /var/run/docker.sock:/var/run/docker.sock \ | |
-v ${AGENT_ENV_FILE}:/etc/kontena-agent.env \ | |
--network host \ | |
--restart unless-stopped \ | |
--name kontena-agent \ | |
--detach \ | |
kontena/agent:${MASTER_VERSION} | |
) | |
} | |
report_success() { | |
echo "[\033[32mDone\033[0m] Kontena Agent has been installed, CONTAINER_ID : $CONTAINER_ID NAME: $NODE_NAME" | |
} | |
random_name() { | |
local adjectives=( \ | |
autumn hidden bitter misty silent empty dry dark summer icy delicate quiet white cool spring winter \ | |
patient twilight dawn crimson wispy weathered blue billowing broken cold damp falling frosty green \ | |
long late bold little morning muddy red rough still small sparkling shy \ | |
wandering withered wild black young holy solitary fragrant aged snowy proud floral restless \ | |
polished purple lively nameless \ | |
) | |
local nouns=( \ | |
waterfall river breeze moon rain wind sea morning snow lake sunset pine shadow leaf dawn glitter \ | |
forest hill cloud meadow sun glade bird brook butterfly bush dew dust field fire flower firefly \ | |
feather grass haze mountain night pond darkness snowflake silence sound sky shape surf thunder \ | |
violet water wildflower wave water resonance sun wood dream cherry tree fog frost voice paper \ | |
frog smoke star \ | |
) | |
echo "${adjectives[$RANDOM % ${#adjectives[@]}]}-${nouns[$RANDOM % ${#nouns[@]}]}-$((($RANDOM % 99)+1))" | |
} | |
install() { | |
validate_options $@ | |
validate_requirements | |
validate_docker_arch | |
validate_docker_version | |
validate_not_running | |
query_master_version | |
get_default_interface | |
create_env_file | |
docker_run | |
report_success | |
} | |
test_setup() { | |
return 0 | |
} | |
if [ "$BATS_TEST_FILENAME" = "" ]; then | |
# not running tests, perform installation | |
install $@ | |
else | |
# running tests, so just load functions | |
test_setup | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment