-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinit_node.py
316 lines (255 loc) · 9.68 KB
/
init_node.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
from botocore.exceptions import NoCredentialsError
from botocore.exceptions import BotoCoreError
import boto3
import json
import logging
import os
import os.path
import random
import stat
import subprocess
import time
import urllib.request
import zipfile
DAEMON_JSON = "/etc/docker/daemon.json"
logger = logging.getLogger(__name__)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "WARNING"))
# Set values loaded by the template
instance_index = int('${instance_index}')
vpc_name = "${vpc_name}"
group = "${group}"
cloudwatch_log_group = "${cloudwatch_log_group}"
ssh_authorization_method = "${ssh_authorization_method}"
# Global cached results
_current_instance = None
class TokenRequest(urllib.request.Request, object):
"""
A urllib request specifically used to obtain the token to access the metadata.
"""
def __init__(self):
super(TokenRequest, self).__init__(
"http://169.254.169.254/latest/api/token",
headers={"X-aws-ec2-metadata-token-ttl-seconds": "21600"},
)
def get_method(self, *args, **kwargs):
return "PUT"
metadata_token = urllib.request.urlopen(TokenRequest()).read()
instance_identity_request = urllib.request.Request(
"http://169.254.169.254/latest/dynamic/instance-identity/document",
headers={"X-aws-ec2-metadata-token": metadata_token},
)
# Extract metadata
instance_identity = json.load(urllib.request.urlopen(instance_identity_request))
instance_id = instance_identity["instanceId"]
region_name = instance_identity["region"]
# AWS resources
ec2 = boto3.resource("ec2", region_name=region_name)
def configure_logging():
"""
Updates daemon.json to enable AWS Cloudwatch logging
"""
if cloudwatch_log_group == "":
return
if os.path.exists(DAEMON_JSON):
with open(DAEMON_JSON) as json_file:
daemon_json = json.load(json_file)
else:
daemon_json = json.loads("{}")
daemon_json["log-driver"] = "awslogs"
daemon_json["log-opts"] = {
"awslogs-group": cloudwatch_log_group,
"tag": "{{.Name}}",
}
f = open(DAEMON_JSON, "w")
f.write(json.dumps(daemon_json))
f.close()
def initialize_system_daemons_and_hostname():
"""
Load system daemons and host name
"""
subprocess.check_call(["systemctl", "daemon-reload"])
subprocess.check_call(["systemctl", "enable", "docker.service"])
subprocess.check_call(["systemctl", "start", "docker.service"])
list_unit_files = subprocess.check_output(["systemctl", "list-unit-files"]).decode("utf-8")
if "dnf-automatic.service" in list_unit_files:
subprocess.check_call(["systemctl", "enable", "dnf-automatic"])
subprocess.check_call(["systemctl", "start", "dnf-automatic"])
subprocess.check_call(
["hostnamectl", "set-hostname", "%s%d-%s" % (group, instance_index, vpc_name)]
)
def create_swap():
"""
Initializes and registers the swap volume
"""
subprocess.check_call(["mkswap", "/dev/xvdf"])
f = open("/etc/fstab", "a")
f.write("/dev/xvdf none swap defaults 0 0\n")
f.close()
subprocess.check_call(["swapon", "-a"])
def initialize_swarm():
"""
Initializes an empty swarm and returns the tokens as a tuple.
"""
subprocess.check_call(["docker", "swarm", "init"])
manager_token = subprocess.check_output(
["docker", "swarm", "join-token", "-q", "manager"]
).decode("utf-8").strip()
worker_token = subprocess.check_output(
["docker", "swarm", "join-token", "-q", "worker"]
).decode("utf-8").strip()
return manager_token, worker_token
def instance_tags(instance):
"""
Converts boto3 tags to a dict()
"""
return {tag["Key"]: tag["Value"] for tag in instance.tags}
def get_running_instances():
"""
Gets the running instances in a VPC as a set.
"""
return {
vpc_instance
for vpc_instance in get_vpc().instances.all()
if vpc_instance.state["Name"] == "running"
}
class ManagerInstance:
def __init__(self, instance, manager_token, worker_token):
self.ip = instance.private_ip_address
self.manager_token = manager_token
self.worker_token = worker_token
def join_swarm_with_token(swarm_manager_ip, token):
"""
Joins the swarm
"""
logging.debug("join %s %s", swarm_manager_ip, token)
subprocess.check_call(
["docker", "swarm", "join", "--token", token, swarm_manager_ip]
)
def get_manager_instance_vpc_tags(exclude_self=False):
instances_considered = get_running_instances()
if exclude_self:
instances_considered = filter(
lambda vpc: vpc != get_current_instance(), instances_considered
)
for vpc_instance in instances_considered:
vpc_instance_tags = instance_tags(instance=vpc_instance)
if (
vpc_instance_tags["Role"] == "manager"
and vpc_instance_tags["ManagerJoinToken"]
and vpc_instance_tags["WorkerJoinToken"]
):
return ManagerInstance(
vpc_instance,
vpc_instance_tags["ManagerJoinToken"],
vpc_instance_tags["WorkerJoinToken"],
)
return None
def update_tokens_vpc_tags(instance, manager_token, worker_token):
instance.create_tags(
Tags=[
{"Key": "ManagerJoinToken", "Value": manager_token},
{"Key": "WorkerJoinToken", "Value": worker_token},
]
)
logger.debug(
"update %s %s %s", instance.private_ip_address, manager_token, worker_token
)
def join_as_manager(get_manager_instance, update_tokens):
def initialize_swarm_and_update_tokens():
(manager_token, worker_token) = initialize_swarm()
update_tokens(get_current_instance(), manager_token, worker_token)
another_manager_instance = get_manager_instance(exclude_self=True)
if another_manager_instance is None:
for attempt in range(10 * instance_index):
another_manager_instance = get_manager_instance(exclude_self=True)
if another_manager_instance is None:
logger.warning("Attempt #%d failed, retrying after sleep...", attempt)
time.sleep(random.randint(5, 15))
else:
break
if another_manager_instance is None:
initialize_swarm_and_update_tokens()
else:
try:
join_swarm_with_token(
another_manager_instance.ip, another_manager_instance.manager_token
)
update_tokens(
get_current_instance(),
another_manager_instance.manager_token,
another_manager_instance.worker_token,
)
except:
# Unable to join the swarm, it may no longer be valid. Create a new one.
initialize_swarm_and_update_tokens()
def join_as_worker(get_manager_instance):
manager_instance = None
for attempt in range(100):
manager_instance = get_manager_instance()
if manager_instance is None:
logger.warning("Attempt #%d failed, retrying after sleep...", attempt)
time.sleep(random.randint(5, 15))
else:
break
if manager_instance is None:
raise Exception("Unable to join swarm, no manager found")
join_swarm_with_token(manager_instance.ip, manager_instance.worker_token)
def is_manager_role():
return instance_tags(get_current_instance())["Role"] == "manager"
def get_vpc():
mac_request = urllib.request.Request(
"http://169.254.169.254/latest/meta-data/mac",
headers={"X-aws-ec2-metadata-token": metadata_token},
)
mac = urllib.request.urlopen(mac_request).read().decode()
vpc_id_request = urllib.request.Request(
"http://169.254.169.254/latest/meta-data/network/interfaces/macs/%s/vpc-id"
% mac,
headers={"X-aws-ec2-metadata-token": metadata_token},
)
vpc_id = urllib.request.urlopen(vpc_id_request).read().decode()
return ec2.Vpc(vpc_id)
def get_current_instance():
global _current_instance
if _current_instance:
return _current_instance
_current_instance = ec2.Instance(instance_id)
return _current_instance
def join_swarm():
get_manager_instance = get_manager_instance_vpc_tags
update_tokens = update_tokens_vpc_tags
if is_manager_role():
join_as_manager(get_manager_instance, update_tokens)
else:
join_as_worker(get_manager_instance)
def set_ssh_authorization_mode():
if ssh_authorization_method == "ec2-instance-connect":
subprocess.check_call(["yum", "install", "ec2-instance-connect"])
elif ssh_authorization_method == "iam":
f = open("/etc/ssh/sshd_config", mode="r")
sshd_config = []
for line in f.readlines():
if not line.startswith("AuthorizedKeysCommand ") and not line.startswith(
"AuthorizedKeysCommandUser "
):
sshd_config.append(line)
f.close()
sshd_config.append(
"AuthorizedKeysCommand /opt/iam-authorized-keys-command %u %f\n"
)
sshd_config.append("AuthorizedKeysCommandUser nobody\n")
f = open("/etc/ssh/sshd_config", mode="w")
f.writelines(sshd_config)
f.close()
subprocess.check_call(["systemctl", "restart", "sshd"])
def install_docker():
subprocess.check_call(["yum-config-manager", "--add-repo", "https://download.docker.com/linux/centos/docker-ce.repo"])
subprocess.check_call(["yum", "install", "docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", "docker-compose-plugin"])
subprocess.check_call(["systemctl", "start", "docker"])
# install_docker()
configure_logging()
initialize_system_daemons_and_hostname()
join_swarm()
create_swap()
set_ssh_authorization_mode()