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

Commit

Permalink
Merge pull request #30 from lawliet89/env-vars
Browse files Browse the repository at this point in the history
Add environment variables to supervisor configuration
  • Loading branch information
brikis98 authored May 30, 2018
2 parents 528b8e5 + 0e07b46 commit 4b12b17
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions modules/run-nomad/run-nomad
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function print_usage {
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."
echo -e " --skip-nomad-config\tIf this flag is set, don't generate a Nomad configuration file. Optional. Default is false."
echo
echo "Example:"
Expand Down Expand Up @@ -76,6 +77,13 @@ function assert_not_empty {
fi
}

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

function lookup_path_in_instance_metadata {
local readonly path="$1"
curl --silent --location "$EC2_INSTANCE_METADATA_URL/$path/"
Expand Down Expand Up @@ -182,6 +190,8 @@ function generate_supervisor_config {
local readonly nomad_log_dir="$5"
local readonly nomad_user="$6"
local readonly use_sudo="$7"
shift 7
local readonly environment=("$@")

if [[ "$use_sudo" == "true" ]]; then
log_info "The --use-sudo flag is set, so running Nomad as the root user"
Expand All @@ -199,6 +209,7 @@ autostart=true
autorestart=true
stopsignal=INT
user=$nomad_user
environment=$(join_by "," "${environment[@]}")
EOF
}

Expand All @@ -225,6 +236,7 @@ function run {
local user=""
local skip_nomad_config="false"
local use_sudo=""
local environment=()
local all_args=()

while [[ $# > 0 ]]; do
Expand Down Expand Up @@ -282,6 +294,11 @@ function run {
--use-sudo)
use_sudo="true"
;;
--environment)
assert_not_empty "$key" "$2"
environment+=("$2")
shift
;;
--help)
print_usage
exit
Expand Down Expand Up @@ -344,8 +361,8 @@ 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"
generate_supervisor_config "$SUPERVISOR_CONFIG_PATH" "$config_dir" "$data_dir" "$bin_dir" "$log_dir" "$user" "$use_sudo" "${environment[@]}"
start_nomad
}

run "$@"
run "$@"

0 comments on commit 4b12b17

Please sign in to comment.