github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/terraform/packer/ubuntu-bionic-amd64/provision.sh (about) 1 #!/bin/bash 2 3 set -o errexit 4 set -o nounset 5 set +x 6 7 usage() { 8 cat <<EOF 9 Usage: provision.sh [options...] 10 Options (use one of the following): 11 --nomad_sha SHA full git sha to install from S3 12 --nomad_version VERSION release version number (ex. 0.12.4+ent) 13 --nomad_binary FILEPATH path to file on host 14 15 Options for configuration: 16 --config_profile FILEPATH path to config profile directory 17 --role ROLE role within config profile directory 18 --index INDEX count of instance, for profiles with per-instance config 19 --nostart do not start or restart Nomad 20 --enterprise if nomad_sha is passed, use the ENT version 21 --nomad_acls write Nomad ACL configuration 22 --autojoin the AWS ConsulAutoJoin tag value 23 24 EOF 25 26 exit 2 27 } 28 29 30 INSTALL_DIR=/usr/local/bin 31 INSTALL_PATH="${INSTALL_DIR}/nomad" 32 PLATFORM=linux_amd64 33 START=1 34 install_fn= 35 36 NOMAD_PROFILE= 37 NOMAD_ROLE= 38 NOMAD_INDEX= 39 BUILD_FOLDER="builds-oss" 40 CONSUL_AUTOJOIN= 41 ACLS=0 42 43 install_from_s3() { 44 # check that we don't already have this version 45 if [ "$(command -v nomad)" ]; then 46 nomad -version | grep -q "${NOMAD_SHA}" \ 47 && echo "$NOMAD_SHA already installed" && return 48 fi 49 50 S3_URL="s3://nomad-team-dev-test-binaries/${BUILD_FOLDER}/nomad_${PLATFORM}_${NOMAD_SHA}.tar.gz" 51 aws s3 cp --quiet "$S3_URL" nomad.tar.gz 52 sudo tar -zxvf nomad.tar.gz -C "$INSTALL_DIR" 53 set_ownership 54 } 55 56 install_from_uploaded_binary() { 57 # we don't need to check for reinstallation here because we do it at the 58 # user's end so that we're not copying it up if we don't have to 59 sudo cp "$NOMAD_UPLOADED_BINARY" "$INSTALL_PATH" 60 set_ownership 61 } 62 63 install_from_release() { 64 # check that we don't already have this version 65 if [ "$(command -v nomad)" ]; then 66 nomad -version | grep -v 'dev' | grep -q "${NOMAD_VERSION}" \ 67 && echo "$NOMAD_VERSION already installed" && return 68 fi 69 70 RELEASE_URL="https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_${PLATFORM}.zip" 71 curl -sL --fail -o /tmp/nomad.zip "$RELEASE_URL" 72 sudo unzip -o /tmp/nomad.zip -d "$INSTALL_DIR" 73 set_ownership 74 } 75 76 set_ownership() { 77 sudo chmod 0755 "$INSTALL_PATH" 78 sudo chown root:root "$INSTALL_PATH" 79 } 80 81 sym() { 82 find "$1" -maxdepth 1 -type f -name "$2" 2>/dev/null \ 83 | sudo xargs -I % ln -fs % "$3" 84 } 85 86 install_config_profile() { 87 88 if [ -d /tmp/custom ]; then 89 rm -rf /opt/config/custom 90 sudo mv /tmp/custom /opt/config/ 91 fi 92 93 # we're removing the whole directory and recreating to avoid 94 # any quirks around dotfiles that might show up here. 95 sudo rm -rf /etc/nomad.d 96 sudo rm -rf /etc/consul.d 97 sudo rm -rf /etc/vault.d 98 99 sudo mkdir -p /etc/nomad.d 100 sudo mkdir -p /etc/consul.d 101 sudo mkdir -p /etc/vault.d 102 103 sym "${NOMAD_PROFILE}/nomad/" '*' /etc/nomad.d 104 sym "${NOMAD_PROFILE}/consul/" '*' /etc/consul.d 105 sym "${NOMAD_PROFILE}/vault/" '*' /etc/vault.d 106 107 if [ -n "$NOMAD_ROLE" ]; then 108 sym "${NOMAD_PROFILE}/nomad/${NOMAD_ROLE}/" '*' /etc/nomad.d 109 sym "${NOMAD_PROFILE}/consul/${NOMAD_ROLE}/" '*' /etc/consul.d 110 sym "${NOMAD_PROFILE}/vault/${NOMAD_ROLE}/" '*' /etc/vault.d 111 fi 112 if [ -n "$NOMAD_INDEX" ]; then 113 sym "${NOMAD_PROFILE}/nomad/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/nomad.d 114 sym "${NOMAD_PROFILE}/consul/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/consul.d 115 sym "${NOMAD_PROFILE}/vault/${NOMAD_ROLE}/indexed/" "*${NOMAD_INDEX}*" /etc/vault.d 116 fi 117 118 if [ $ACLS == "1" ]; then 119 sudo ln -fs /opt/config/shared/nomad-acl.hcl /etc/nomad.d/acl.hcl 120 fi 121 } 122 123 update_consul_autojoin() { 124 sudo sed -i'' -e "s|tag_key=ConsulAutoJoin tag_value=auto-join|tag_key=ConsulAutoJoin tag_value=${CONSUL_AUTOJOIN}|g" /etc/consul.d/*.json 125 } 126 127 while [[ $# -gt 0 ]] 128 do 129 opt="$1" 130 case $opt in 131 --nomad_sha) 132 if [ -z "$2" ]; then echo "Missing sha parameter"; usage; fi 133 NOMAD_SHA="$2" 134 install_fn=install_from_s3 135 shift 2 136 ;; 137 --nomad_release | --nomad_version) 138 if [ -z "$2" ]; then echo "Missing version parameter"; usage; fi 139 NOMAD_VERSION="$2" 140 install_fn=install_from_release 141 shift 2 142 ;; 143 --nomad_binary) 144 if [ -z "$2" ]; then echo "Missing file parameter"; usage; fi 145 NOMAD_UPLOADED_BINARY="$2" 146 install_fn=install_from_uploaded_binary 147 shift 2 148 ;; 149 --config_profile) 150 if [ -z "$2" ]; then echo "Missing profile parameter"; usage; fi 151 NOMAD_PROFILE="/opt/config/${2}" 152 shift 2 153 ;; 154 --role) 155 if [ -z "$2" ]; then echo "Missing role parameter"; usage; fi 156 NOMAD_ROLE="$2" 157 shift 2 158 ;; 159 --index) 160 if [ -z "$2" ]; then echo "Missing index parameter"; usage; fi 161 NOMAD_INDEX="$2" 162 shift 2 163 ;; 164 --autojoin) 165 if [ -z "$2" ]; then ehco "Missing autojoin parameter"; usage; fi 166 CONSUL_AUTOJOIN="$2" 167 shift 2 168 ;; 169 --nostart) 170 # for initial packer builds, we don't want to start Nomad 171 START=0 172 shift 173 ;; 174 --enterprise) 175 BUILD_FOLDER="builds-ent" 176 shift 177 ;; 178 --nomad_acls) 179 ACLS=1 180 shift 181 ;; 182 *) usage ;; 183 esac 184 done 185 186 # call the appropriate installation function 187 if [ -n "$install_fn" ]; then 188 $install_fn 189 fi 190 if [ -n "$NOMAD_PROFILE" ]; then 191 install_config_profile 192 fi 193 194 if [ -n "$CONSUL_AUTOJOIN" ]; then 195 update_consul_autojoin 196 fi 197 198 if [ $START == "1" ]; then 199 if [ "$NOMAD_ROLE" == "server" ]; then 200 sudo systemctl restart vault 201 fi 202 sudo systemctl restart consul 203 sudo systemctl restart nomad 204 fi