Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Switch from supervisord to systemd #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 2 additions & 42 deletions modules/install-nomad/install-nomad
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# operating systems:
#
# 1. Ubuntu 16.04
# 1. Amazon Linux
# 1. Amazon Linux 2

set -e

Expand All @@ -13,16 +13,13 @@ readonly DEFAULT_NOMAD_USER="nomad"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SYSTEM_BIN_DIR="/usr/local/bin"

readonly SUPERVISOR_DIR="/etc/supervisor"
readonly SUPERVISOR_CONF_DIR="$SUPERVISOR_DIR/conf.d"

readonly SCRIPT_NAME="$(basename "$0")"

function print_usage {
echo
echo "Usage: install-nomad [OPTIONS]"
echo
echo "This script can be used to install Nomad and its dependencies. This script has been tested with Ubuntu 16.04 and Amazon Linux."
echo "This script can be used to install Nomad and its dependencies. This script has been tested with Ubuntu 16.04 and Amazon Linux 2."
echo
echo "Options:"
echo
Expand Down Expand Up @@ -68,41 +65,6 @@ function assert_not_empty {
fi
}

# Install steps are based on: http://unix.stackexchange.com/a/291098/215969
function install_supervisord_debian {
sudo apt-get install -y supervisor
sudo update-rc.d supervisor defaults

create_supervisor_config
sudo systemctl enable supervisor
}

# Install steps are based on: http://stackoverflow.com/a/31576473/483528
function install_supervisord_amazon_linux {
sudo pip install supervisor

# On Amazon Linux, /usr/local/bin is not in PATH for the root user, so we add symlinks to /usr/bin, which is in PATH
if [[ ! -f "/usr/bin/supervisorctl" ]]; then
sudo ln -s /usr/local/bin/supervisorctl /usr/bin/supervisorctl
fi
if [[ ! -f "/usr/bin/supervisord" ]]; then
sudo ln -s /usr/local/bin/supervisord /usr/bin/supervisord
fi

sudo cp "$SCRIPT_DIR/supervisor-initd-script.sh" "/etc/init.d/supervisor"
sudo chmod a+x /etc/init.d/supervisor
sudo mkdir -p /var/log/supervisor

create_supervisor_config
sudo chkconfig --add supervisor
sudo chkconfig supervisor on
}

function create_supervisor_config {
sudo mkdir -p "$SUPERVISOR_CONF_DIR"
sudo cp "$SCRIPT_DIR/supervisord.conf" "$SUPERVISOR_DIR/supervisord.conf"
}

function has_yum {
[ -n "$(command -v yum)" ]
}
Expand All @@ -117,11 +79,9 @@ function install_dependencies {
if $(has_apt_get); then
sudo apt-get update -y
sudo apt-get install -y awscli curl unzip jq
install_supervisord_debian
elif $(has_yum); then
sudo yum update -y
sudo yum install -y aws curl unzip jq
install_supervisord_amazon_linux
else
log_error "Could not find apt-get or yum. Cannot install dependencies on this OS."
exit 1
Expand Down
116 changes: 0 additions & 116 deletions modules/install-nomad/supervisor-initd-script.sh

This file was deleted.

39 changes: 0 additions & 39 deletions modules/install-nomad/supervisord.conf

This file was deleted.

77 changes: 38 additions & 39 deletions modules/run-nomad/run-nomad
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
set -e

readonly NOMAD_CONFIG_FILE="default.hcl"
readonly SUPERVISOR_CONFIG_PATH="/etc/supervisor/conf.d/run-nomad.conf"
readonly SYSTEMD_CONFIG_PATH="/etc/systemd/system/nomad.service"

readonly EC2_INSTANCE_METADATA_URL="http://169.254.169.254/latest/meta-data"
readonly EC2_INSTANCE_DYNAMIC_DATA_URL="http://169.254.169.254/latest/dynamic"
Expand All @@ -26,7 +26,6 @@ function print_usage {
echo -e " --config-dir\t\tThe path to the Nomad config folder. Optional. Default is the absolute path of '../config', relative to this script."
echo -e " --data-dir\t\tThe path to the Nomad data folder. Optional. Default is the absolute path of '../data', relative to this script."
echo -e " --bin-dir\t\tThe path to the folder with Nomad binary. Optional. Default is the absolute path of the parent folder of this script."
echo -e " --log-dir\t\tThe path to the Nomad log folder. Optional. Default is the absolute path of '../log', relative to this script."
echo -e " --user\t\tThe user to run Nomad as. Optional. Default is to use the owner of --config-dir."
echo -e " --use-sudo\t\tIf set, run the Nomad agent with sudo. By default, sudo is only used if --client is set."
echo -e " --environment\t\A single environment variable in the key/value pair form 'KEY=\"val\"' to pass to Nomad as environment variable when starting it up. Repeat this option for additional variables. Optional."
Expand Down Expand Up @@ -77,11 +76,13 @@ function assert_not_empty {
fi
}

# Based on https://stackoverflow.com/a/17841619
function join_by {
local IFS="$1"
function split_by_lines {
local prefix="$1"
shift
echo "$*"

for var in "$@"; do
echo "${prefix}${var}"
done
}

function lookup_path_in_instance_metadata {
Expand Down Expand Up @@ -182,41 +183,49 @@ EOF
chown "$user:$user" "$config_path"
}

function generate_supervisor_config {
local readonly supervisor_config_path="$1"
function generate_systemd_config {
local readonly systemd_config_path="$1"
local readonly nomad_config_dir="$2"
local readonly nomad_data_dir="$3"
local readonly nomad_bin_dir="$4"
local readonly nomad_log_dir="$5"
local readonly nomad_user="$6"
local readonly use_sudo="$7"
shift 7
local readonly nomad_user="$5"
local readonly use_sudo="$6"
shift 6
local readonly environment=("$@")

if [[ "$use_sudo" == "true" ]]; then
log_info "The --use-sudo flag is set, so running Nomad as the root user"
nomad_user="root"
fi

log_info "Creating Supervisor config file to run Nomad in $supervisor_config_path"
cat > "$supervisor_config_path" <<EOF
[program:nomad]
command=$nomad_bin_dir/nomad agent -config $nomad_config_dir -data-dir $nomad_data_dir
stdout_logfile=$nomad_log_dir/nomad-stdout.log
stderr_logfile=$nomad_log_dir/nomad-error.log
numprocs=1
autostart=true
autorestart=true
stopsignal=INT
user=$nomad_user
environment=$(join_by "," "${environment[@]}")
log_info "Creating systemd config file to run Nomad in $systemd_config_path"
cat > "$systemd_config_path" <<EOF
[Unit]
Description="HashiCorp Nomad"
Documentation=https://www.nomadproject.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=$config_path

[Service]
User=$nomad_user
Group=$nomad_user
ExecStart=$nomad_bin_dir/nomad agent -config $nomad_config_dir -data-dir $nomad_data_dir
KillMode=process
Restart=on-failure
LimitNOFILE=65536
$(split_by_lines "Environment=" "${environment[@]}")

[Install]
WantedBy=multi-user.target
EOF
}

function start_nomad {
log_info "Reloading Supervisor config and starting Nomad"
supervisorctl reread
supervisorctl update
log_info "Reloading systemd config and starting Nomad"

sudo systemctl daemon-reload
sudo systemctl restart nomad.service
}

# Based on: http://unix.stackexchange.com/a/7732/215969
Expand All @@ -232,7 +241,6 @@ function run {
local config_dir=""
local data_dir=""
local bin_dir=""
local log_dir=""
local user=""
local skip_nomad_config="false"
local use_sudo=""
Expand Down Expand Up @@ -268,11 +276,6 @@ function run {
bin_dir="$2"
shift
;;
--log-dir)
assert_not_empty "$key" "$2"
log_dir="$2"
shift
;;
--user)
assert_not_empty "$key" "$2"
user="$2"
Expand Down Expand Up @@ -330,7 +333,7 @@ function run {
fi
fi

assert_is_installed "supervisorctl"
assert_is_installed "systemctl"
assert_is_installed "aws"
assert_is_installed "curl"
assert_is_installed "jq"
Expand All @@ -347,10 +350,6 @@ function run {
bin_dir=$(cd "$SCRIPT_DIR/../bin" && pwd)
fi

if [[ -z "$log_dir" ]]; then
log_dir=$(cd "$SCRIPT_DIR/../log" && pwd)
fi

if [[ -z "$user" ]]; then
user=$(get_owner_of_path "$config_dir")
fi
Expand All @@ -361,7 +360,7 @@ function run {
generate_nomad_config "$server" "$client" "$num_servers" "$config_dir" "$user"
fi

generate_supervisor_config "$SUPERVISOR_CONFIG_PATH" "$config_dir" "$data_dir" "$bin_dir" "$log_dir" "$user" "$use_sudo" "${environment[@]}"
generate_systemd_config "$SYSTEMD_CONFIG_PATH" "$config_dir" "$data_dir" "$bin_dir" "$user" "$use_sudo" "${environment[@]}"
start_nomad
}

Expand Down