2020-08-20 20:10:00 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o nounset
|
|
|
|
set +x
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2020-09-04 12:51:22 +00:00
|
|
|
Usage: provision.sh [options...]
|
2020-08-20 20:10:00 +00:00
|
|
|
Options (use one of the following):
|
|
|
|
--nomad_sha SHA full git sha to install from S3
|
2020-09-09 22:22:37 +00:00
|
|
|
--nomad_version VERSION release version number (ex. 0.12.4+ent)
|
2020-08-20 20:10:00 +00:00
|
|
|
--nomad_binary FILEPATH path to file on host
|
2020-09-18 15:27:24 +00:00
|
|
|
|
|
|
|
Options for configuration:
|
|
|
|
--config_profile FILEPATH path to config profile directory
|
|
|
|
--role ROLE role within config profile directory
|
|
|
|
--index INDEX count of instance, for profiles with per-instance config
|
|
|
|
--nostart do not start or restart Nomad
|
2020-09-23 14:39:04 +00:00
|
|
|
--enterprise if nomad_sha is passed, use the ENT version
|
2020-09-28 13:22:36 +00:00
|
|
|
--nomad_acls write Nomad ACL configuration
|
2020-08-20 20:10:00 +00:00
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
INSTALL_DIR=/usr/local/bin
|
|
|
|
INSTALL_PATH="${INSTALL_DIR}/nomad"
|
|
|
|
PLATFORM=linux_amd64
|
|
|
|
START=1
|
|
|
|
install_fn=
|
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
NOMAD_PROFILE=
|
|
|
|
NOMAD_ROLE=
|
|
|
|
NOMAD_INDEX=
|
2020-09-23 14:39:04 +00:00
|
|
|
BUILD_FOLDER="builds-oss"
|
2020-09-28 13:22:36 +00:00
|
|
|
ACLS=0
|
2020-09-18 15:27:24 +00:00
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
install_from_s3() {
|
|
|
|
# check that we don't already have this version
|
|
|
|
if [ "$(command -v nomad)" ]; then
|
|
|
|
nomad -version | grep -q "${NOMAD_SHA}" \
|
2020-09-17 13:20:18 +00:00
|
|
|
&& echo "$NOMAD_SHA already installed" && return
|
2020-08-20 20:10:00 +00:00
|
|
|
fi
|
|
|
|
|
2020-09-23 14:39:04 +00:00
|
|
|
S3_URL="s3://nomad-team-dev-test-binaries/${BUILD_FOLDER}/nomad_${PLATFORM}_${NOMAD_SHA}.tar.gz"
|
2020-08-20 20:10:00 +00:00
|
|
|
aws s3 cp --quiet "$S3_URL" nomad.tar.gz
|
|
|
|
sudo tar -zxvf nomad.tar.gz -C "$INSTALL_DIR"
|
|
|
|
set_ownership
|
|
|
|
}
|
|
|
|
|
|
|
|
install_from_uploaded_binary() {
|
2020-09-18 15:27:24 +00:00
|
|
|
# we don't need to check for reinstallation here because we do it at the
|
|
|
|
# user's end so that we're not copying it up if we don't have to
|
2020-08-20 20:10:00 +00:00
|
|
|
sudo cp "$NOMAD_UPLOADED_BINARY" "$INSTALL_PATH"
|
|
|
|
set_ownership
|
|
|
|
}
|
|
|
|
|
|
|
|
install_from_release() {
|
|
|
|
# check that we don't already have this version
|
|
|
|
if [ "$(command -v nomad)" ]; then
|
|
|
|
nomad -version | grep -v 'dev' | grep -q "${NOMAD_VERSION}" \
|
2020-09-17 13:20:18 +00:00
|
|
|
&& echo "$NOMAD_VERSION already installed" && return
|
2020-08-20 20:10:00 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
RELEASE_URL="https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_${PLATFORM}.zip"
|
|
|
|
curl -sL --fail -o /tmp/nomad.zip "$RELEASE_URL"
|
|
|
|
sudo unzip -o /tmp/nomad.zip -d "$INSTALL_DIR"
|
|
|
|
set_ownership
|
|
|
|
}
|
|
|
|
|
|
|
|
set_ownership() {
|
|
|
|
sudo chmod 0755 "$INSTALL_PATH"
|
|
|
|
sudo chown root:root "$INSTALL_PATH"
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
sym() {
|
|
|
|
find "$1" -maxdepth 1 -type f -name "$2" 2>/dev/null \
|
|
|
|
| sudo xargs -I % ln -fs % "$3"
|
|
|
|
}
|
|
|
|
|
|
|
|
install_config_profile() {
|
|
|
|
|
|
|
|
if [ -d /tmp/custom ]; then
|
|
|
|
rm -rf /opt/config/custom
|
|
|
|
sudo mv /tmp/custom /opt/config/
|
|
|
|
fi
|
|
|
|
|
|
|
|
# we're removing the whole directory and recreating to avoid
|
|
|
|
# any quirks around dotfiles that might show up here.
|
|
|
|
sudo rm -rf /etc/nomad.d
|
|
|
|
sudo rm -rf /etc/consul.d
|
|
|
|
sudo rm -rf /etc/vault.d
|
|
|
|
|
|
|
|
sudo mkdir -p /etc/nomad.d
|
|
|
|
sudo mkdir -p /etc/consul.d
|
|
|
|
sudo mkdir -p /etc/vault.d
|
|
|
|
|
|
|
|
sym "${NOMAD_PROFILE}/nomad/" '*' /etc/nomad.d
|
|
|
|
sym "${NOMAD_PROFILE}/consul/" '*' /etc/consul.d
|
|
|
|
sym "${NOMAD_PROFILE}/vault/" '*' /etc/vault.d
|
|
|
|
|
|
|
|
if [ -n "$NOMAD_ROLE" ]; then
|
|
|
|
sym "${NOMAD_PROFILE}/nomad/${NOMAD_ROLE}/" '*' /etc/nomad.d
|
|
|
|
sym "${NOMAD_PROFILE}/consul/${NOMAD_ROLE}/" '*' /etc/consul.d
|
|
|
|
sym "${NOMAD_PROFILE}/vault/${NOMAD_ROLE}/" '*' /etc/vault.d
|
|
|
|
fi
|
|
|
|
if [ -n "$NOMAD_INDEX" ]; then
|
|
|
|
sym "${NOMAD_PROFILE}/nomad/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/nomad.d
|
|
|
|
sym "${NOMAD_PROFILE}/consul/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/consul.d
|
|
|
|
sym "${NOMAD_PROFILE}/vault/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/vault.d
|
|
|
|
fi
|
2020-09-28 13:22:36 +00:00
|
|
|
|
|
|
|
if [ $ACLS == "1" ]; then
|
|
|
|
sudo ln -fs /opt/config/shared/nomad-acl.hcl /etc/nomad.d/acl.hcl
|
|
|
|
fi
|
2020-09-18 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-20 20:10:00 +00:00
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
|
|
|
--nomad_sha)
|
|
|
|
if [ -z "$2" ]; then echo "Missing sha parameter"; usage; fi
|
|
|
|
NOMAD_SHA="$2"
|
|
|
|
install_fn=install_from_s3
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--nomad_release | --nomad_version)
|
|
|
|
if [ -z "$2" ]; then echo "Missing version parameter"; usage; fi
|
|
|
|
NOMAD_VERSION="$2"
|
|
|
|
install_fn=install_from_release
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--nomad_binary)
|
|
|
|
if [ -z "$2" ]; then echo "Missing file parameter"; usage; fi
|
|
|
|
NOMAD_UPLOADED_BINARY="$2"
|
|
|
|
install_fn=install_from_uploaded_binary
|
|
|
|
shift 2
|
|
|
|
;;
|
2020-09-18 15:27:24 +00:00
|
|
|
--config_profile)
|
|
|
|
if [ -z "$2" ]; then echo "Missing profile parameter"; usage; fi
|
|
|
|
NOMAD_PROFILE="/opt/config/${2}"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--role)
|
|
|
|
if [ -z "$2" ]; then echo "Missing role parameter"; usage; fi
|
|
|
|
NOMAD_ROLE="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--index)
|
|
|
|
if [ -z "$2" ]; then echo "Missing index parameter"; usage; fi
|
|
|
|
NOMAD_INDEX="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
2020-08-20 20:10:00 +00:00
|
|
|
--nostart)
|
|
|
|
# for initial packer builds, we don't want to start Nomad
|
|
|
|
START=0
|
|
|
|
shift
|
|
|
|
;;
|
2020-09-23 14:39:04 +00:00
|
|
|
--enterprise)
|
|
|
|
BUILD_FOLDER="builds-ent"
|
|
|
|
shift
|
|
|
|
;;
|
2020-09-28 13:22:36 +00:00
|
|
|
--nomad_acls)
|
|
|
|
ACLS=1
|
|
|
|
shift
|
|
|
|
;;
|
2020-08-20 20:10:00 +00:00
|
|
|
*) usage ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-09-18 15:27:24 +00:00
|
|
|
# call the appropriate installation function
|
|
|
|
if [ -n "$install_fn" ]; then
|
|
|
|
$install_fn
|
|
|
|
fi
|
|
|
|
if [ -n "$NOMAD_PROFILE" ]; then
|
|
|
|
install_config_profile
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $START == "1" ]; then
|
2020-10-05 13:28:37 +00:00
|
|
|
if [ "$NOMAD_ROLE" == "server" ]; then
|
|
|
|
sudo systemctl restart vault
|
|
|
|
fi
|
2020-09-18 15:27:24 +00:00
|
|
|
sudo systemctl restart consul
|
|
|
|
sudo systemctl restart nomad
|
|
|
|
fi
|