2022-04-12 14:08:21 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-04-10 15:36:59 +00:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2017-06-25 18:54:56 +00:00
|
|
|
set -e
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
CONFIGDIR=/ops/shared/config
|
2017-06-25 18:54:56 +00:00
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
CONSULCONFIGDIR=/etc/consul.d
|
|
|
|
VAULTCONFIGDIR=/etc/vault.d
|
|
|
|
NOMADCONFIGDIR=/etc/nomad.d
|
2019-03-15 19:55:34 +00:00
|
|
|
CONSULTEMPLATECONFIGDIR=/etc/consul-template.d
|
2017-05-15 18:56:41 +00:00
|
|
|
HOME_DIR=ubuntu
|
|
|
|
|
2017-08-04 18:00:05 +00:00
|
|
|
# Wait for network
|
2017-06-25 17:09:54 +00:00
|
|
|
sleep 15
|
|
|
|
|
|
|
|
DOCKER_BRIDGE_IP_ADDRESS=(`ifconfig docker0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`)
|
2017-11-01 21:15:58 +00:00
|
|
|
CLOUD=$1
|
|
|
|
SERVER_COUNT=$2
|
|
|
|
RETRY_JOIN=$3
|
2018-03-20 17:46:32 +00:00
|
|
|
NOMAD_BINARY=$4
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2020-07-16 19:21:57 +00:00
|
|
|
# Get IP from metadata service
|
|
|
|
if [ "$CLOUD" = "gce" ]; then
|
|
|
|
IP_ADDRESS=$(curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip)
|
|
|
|
else
|
|
|
|
IP_ADDRESS=$(curl http://instance-data/latest/meta-data/local-ipv4)
|
|
|
|
fi
|
|
|
|
# IP_ADDRESS="$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')"
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
# Consul
|
|
|
|
sed -i "s/IP_ADDRESS/$IP_ADDRESS/g" $CONFIGDIR/consul.json
|
|
|
|
sed -i "s/SERVER_COUNT/$SERVER_COUNT/g" $CONFIGDIR/consul.json
|
2017-11-01 21:15:58 +00:00
|
|
|
sed -i "s/RETRY_JOIN/$RETRY_JOIN/g" $CONFIGDIR/consul.json
|
2017-05-15 18:56:41 +00:00
|
|
|
sudo cp $CONFIGDIR/consul.json $CONSULCONFIGDIR
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo cp $CONFIGDIR/consul_$CLOUD.service /etc/systemd/system/consul.service
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2019-07-11 20:36:58 +00:00
|
|
|
sudo systemctl enable consul.service
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo systemctl start consul.service
|
2017-08-04 18:00:05 +00:00
|
|
|
sleep 10
|
2017-05-15 18:56:41 +00:00
|
|
|
export CONSUL_HTTP_ADDR=$IP_ADDRESS:8500
|
|
|
|
export CONSUL_RPC_ADDR=$IP_ADDRESS:8400
|
|
|
|
|
|
|
|
# Vault
|
|
|
|
sed -i "s/IP_ADDRESS/$IP_ADDRESS/g" $CONFIGDIR/vault.hcl
|
|
|
|
sudo cp $CONFIGDIR/vault.hcl $VAULTCONFIGDIR
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo cp $CONFIGDIR/vault.service /etc/systemd/system/vault.service
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2019-07-11 20:36:58 +00:00
|
|
|
sudo systemctl enable vault.service
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo systemctl start vault.service
|
2017-05-15 18:56:41 +00:00
|
|
|
|
|
|
|
# Nomad
|
2018-03-20 17:46:32 +00:00
|
|
|
|
|
|
|
## Replace existing Nomad binary if remote file exists
|
|
|
|
if [[ `wget -S --spider $NOMAD_BINARY 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
|
|
|
|
curl -L $NOMAD_BINARY > nomad.zip
|
|
|
|
sudo unzip -o nomad.zip -d /usr/local/bin
|
|
|
|
sudo chmod 0755 /usr/local/bin/nomad
|
|
|
|
sudo chown root:root /usr/local/bin/nomad
|
|
|
|
fi
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
sed -i "s/SERVER_COUNT/$SERVER_COUNT/g" $CONFIGDIR/nomad.hcl
|
|
|
|
sudo cp $CONFIGDIR/nomad.hcl $NOMADCONFIGDIR
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo cp $CONFIGDIR/nomad.service /etc/systemd/system/nomad.service
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2019-07-11 20:36:58 +00:00
|
|
|
sudo systemctl enable nomad.service
|
2017-11-15 19:39:36 +00:00
|
|
|
sudo systemctl start nomad.service
|
2017-05-15 18:56:41 +00:00
|
|
|
sleep 10
|
|
|
|
export NOMAD_ADDR=http://$IP_ADDRESS:4646
|
|
|
|
|
2019-03-15 19:55:34 +00:00
|
|
|
# Consul Template
|
|
|
|
sudo cp $CONFIGDIR/consul-template.hcl $CONSULTEMPLATECONFIGDIR/consul-template.hcl
|
|
|
|
sudo cp $CONFIGDIR/consul-template.service /etc/systemd/system/consul-template.service
|
|
|
|
|
2017-06-25 17:09:54 +00:00
|
|
|
# Add hostname to /etc/hosts
|
|
|
|
|
|
|
|
echo "127.0.0.1 $(hostname)" | sudo tee --append /etc/hosts
|
|
|
|
|
|
|
|
# Add Docker bridge network IP to /etc/resolv.conf (at the top)
|
|
|
|
|
|
|
|
echo "nameserver $DOCKER_BRIDGE_IP_ADDRESS" | sudo tee /etc/resolv.conf.new
|
|
|
|
cat /etc/resolv.conf | sudo tee --append /etc/resolv.conf.new
|
|
|
|
sudo mv /etc/resolv.conf.new /etc/resolv.conf
|
|
|
|
|
2017-06-25 18:54:56 +00:00
|
|
|
# Move examples directory to $HOME
|
|
|
|
sudo mv /ops/examples /home/$HOME_DIR
|
|
|
|
sudo chown -R $HOME_DIR:$HOME_DIR /home/$HOME_DIR/examples
|
|
|
|
sudo chmod -R 775 /home/$HOME_DIR/examples
|
|
|
|
|
2017-06-24 23:25:46 +00:00
|
|
|
# Set env vars for tool CLIs
|
2017-05-15 18:56:41 +00:00
|
|
|
echo "export CONSUL_RPC_ADDR=$IP_ADDRESS:8400" | sudo tee --append /home/$HOME_DIR/.bashrc
|
|
|
|
echo "export CONSUL_HTTP_ADDR=$IP_ADDRESS:8500" | sudo tee --append /home/$HOME_DIR/.bashrc
|
|
|
|
echo "export VAULT_ADDR=http://$IP_ADDRESS:8200" | sudo tee --append /home/$HOME_DIR/.bashrc
|
|
|
|
echo "export NOMAD_ADDR=http://$IP_ADDRESS:4646" | sudo tee --append /home/$HOME_DIR/.bashrc
|
|
|
|
echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre" | sudo tee --append /home/$HOME_DIR/.bashrc
|