-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser-data-server.sh
148 lines (124 loc) · 3.59 KB
/
user-data-server.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# This script is meant to be run in the User Data of each EC2 Instance while it's booting.
set -e
set -x
export TERM=xterm-256color
export DEBIAN_FRONTEND=noninteractive
# Send the log output from this script to user-data.log, syslog, and the console
# From: https://alestic.com/2010/12/ec2-user-data-output/
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
apt-get update
apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common \
jq \
unzip
#####
# Configure resolving
#####
echo "Determining local IP address"
LOCAL_IPV4=$(ec2metadata --local-ipv4)
mkdir -p /etc/systemd/resolved.conf.d
cat << EOSDRCF >/etc/systemd/resolved.conf.d/consul.conf
# Enable forward lookup of the 'consul' domain:
[Resolve]
Cache=no
DNS=127.0.0.1:8600
Domains=~.consul
EOSDRCF
cat << EOSDRLF >/etc/systemd/resolved.conf.d/listen.conf
# Enable listener on private ip:
[Resolve]
DNSStubListenerExtra=$${LOCAL_IPV4}
EOSDRLF
systemctl restart systemd-resolved.service
echo "Checking latest Consul and Nomad versions..."
CHECKPOINT_URL="https://checkpoint-api.hashicorp.com/v1/check"
CONSUL_VERSION=$(curl -s "$${CHECKPOINT_URL}"/consul | jq -r .current_version)
NOMAD_VERSION=$(curl -s "$${CHECKPOINT_URL}"/nomad | jq -r .current_version)
cd /tmp/
echo "Fetching Consul version $${CONSUL_VERSION} ..."
curl -s https://releases.hashicorp.com/consul/$${CONSUL_VERSION}/consul_$${CONSUL_VERSION}_linux_amd64.zip -o consul.zip
echo "Installing Consul version $${CONSUL_VERSION} ..."
unzip consul.zip
chmod +x consul
mv consul /usr/local/bin/consul
mkdir -p /etc/consul.d
cat << EOCCF >/etc/consul.d/server.hcl
advertise_addr = "{{ GetPrivateIP }}"
bootstrap_expect = 3
client_addr = "0.0.0.0"
data_dir = "/var/lib/consul"
datacenter = "${cluster_tag_value}"
enable_syslog = true
log_level = "DEBUG"
retry_join = ["provider=aws tag_key=${cluster_tag_key} tag_value=${cluster_tag_value}"]
server = true
ui = true
ports {
"grpc" = 8502
}
connect {
enabled = true
}
EOCCF
cat << EOCSU >/etc/systemd/system/consul.service
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
[Service]
Type=notify
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOCSU
##########
# Nomad config
##########
echo "Fetching Nomad version $${NOMAD_VERSION} ..."
curl -s https://releases.hashicorp.com/nomad/$${NOMAD_VERSION}/nomad_$${NOMAD_VERSION}_linux_amd64.zip -o nomad.zip
echo "Installing Nomad version $${NOMAD_VERSION} ..."
unzip nomad.zip
chmod +x nomad
mv nomad /usr/local/bin/nomad
mkdir -p /etc/nomad.d/
cat << EONCF >/etc/nomad.d/server.hcl
bind_addr = "0.0.0.0"
region = "${nomad_region}"
datacenter = "${nomad_datacenter}"
data_dir = "/var/lib/nomad/"
log_level = "DEBUG"
leave_on_interrupt = true
leave_on_terminate = true
server {
enabled = true
bootstrap_expect = 3
}
EONCF
cat << EONSU >/etc/systemd/system/nomad.service
[Unit]
Description=nomad agent
Requires=network-online.target consul.service
After=network-online.target consul.service
[Service]
LimitNOFILE=65536
Restart=on-failure
EnvironmentFile=-/etc/default/nomad
ExecStart=/usr/local/bin/nomad agent -config /etc/nomad.d
KillSignal=SIGINT
RestartSec=5s
[Install]
WantedBy=multi-user.target
EONSU
systemctl daemon-reload
systemctl start consul
systemctl start nomad