This repository was archived by the owner on Jun 15, 2023. It is now read-only.
forked from colstrom/forge
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbootstrap.py
More file actions
executable file
·331 lines (256 loc) · 11.3 KB
/
Copy pathbootstrap.py
File metadata and controls
executable file
·331 lines (256 loc) · 11.3 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# Copyright (c) 2015 Chris Olstrom <chris@olstrom.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import re
import argparse
import httplib2
import subprocess
import json
def install_with_pip(packages):
""" Installs packages with pip """
for package in packages:
subprocess.call('sudo -H python3 -m pip install -U ' + package, shell=True)
def detect(setting):
""" Detects a setting in tags, falls back to environment variables """
if setting in resource_tags():
return resource_tags()[setting]
else:
return os.getenv(shell_style(setting))
def shell_style(name):
""" Translates reasonable names into names you would expect for environment
variables. Example: 'ForgeRegion' becomes 'FORGE_REGION' """
return re.sub('(?!^)([A-Z]+)', r'_\1', name).upper()
def download_from_s3(source, destination):
""" Downloads a file from an S3 bucket """
subprocess.call("aws s3 cp --region {region} s3://{bucket}/{file} {save_to}".format(
region=detect('ForgeRegion'),
bucket=detect('ForgeBucket'),
file=source,
save_to=destination
), shell=True)
def download_directory_from_s3(source, destination):
""" Downloads a directory from an S3 bucket """
source = 's3://' + detect('ForgeBucket') + '/' + source
subprocess.call(['aws', 's3', 'cp', '--recursive', '--region', detect('ForgeRegion'), source, destination])
def instance_metadata(item):
""" Returns information about the current instance from EC2 Instace API """
h = httplib2.Http(".cache")
resp, content = h.request("http://169.254.169.254/latest/meta-data/{}".format(item), "GET")
return content
def instance_id():
""" Returns the ID of the current instance """
return instance_metadata('instance-id')
def region():
""" Returns the region the current instance is located in """
return instance_metadata('placement/availability-zone')[:-1].decode('utf-8')
def resource_tags():
""" Returns a dictionary of all resource tags for the current instance """
result_bytes = subprocess.check_output(
"aws ec2 describe-tags --region {region} --filters \"Name=resource-id,Values={instance_id}\"".format(
region=region(),
instance_id=(str(instance_id(), "UTF-8"))
), shell=True)
aws_tags = json.loads(result_bytes.decode("UTF-8")).get("Tags")
resource_tags_dict = {}
for entry in aws_tags:
resource_tags_dict[entry.get("Key")] = entry.get("Value")
return resource_tags_dict
def security_groups():
""" Returns a list of sercurity groups for the current instance """
return instance_metadata('security-groups').split('\n')
def infer_tags(security_group):
""" Attempts to infer tags from a security group name """
matches = re.search(r'(?P<Project>[\w-]+)-(?P<Role>\w+)$', security_group)
return matches.groupdict()
def implicit_tags():
""" Returns a list of tags inferred from security groups """
return [infer_tags(name) for name in security_groups()]
def discover(trait):
""" Tries to find a trait in tags, makes a reasonable guess if it fails """
if trait in resource_tags():
return [resource_tags()[trait]]
else:
return [implicit_tags()[trait]]
def project_path():
""" Returns the forge path for the discovered project """
return discover('Project')[0] + '/'
def role_paths():
""" Returns a list of forge paths for all discovered roles """
return [project_path() + role + '/' for role in discover('Role')]
def unique(enumerable):
""" Returns a list without duplicate items """
return list(set(enumerable))
def applicable_playbooks():
""" Returns a list of playbooks that should be applied to this system """
playbooks = []
# Base Playbook
if not args.skip_base_playbook:
playbooks = ['']
# Project Playbook
if not args.skip_project_playbook:
playbooks.append(project_path())
# System Roles Playbook
if not args.skip_role_playbook:
playbooks.extend(role_paths())
return sorted(unique(playbooks), key=len)
def flat_path(path):
""" Flattens a path by substituting dashes for slashes """
import re
return re.sub('/', '-', path)
def playbook_directory(playbook):
""" construct a directory from playbook """
import os
if len(playbook) == 0:
directory = 'base'
else:
directory = flat_path(playbook.strip('/'))
directory = os.path.join(os.sep, 'tmp', directory)
if not os.path.isdir(directory):
os.makedirs(directory)
return os.path.join(directory, '') # returns with tailing slash
def get_dependencies(playbook):
""" Downloads and installs all roles required for a playbook to run """
path = playbook_directory(playbook)
if not args.skip_download:
download_from_s3(playbook + 'dependencies.yml', path + 'dependencies.yml')
subprocess.call('ansible-galaxy install -ifr' + path + 'dependencies.yml', shell=True)
def get_vault(playbook):
""" Downloads a vault file, and puts it where Ansible can find it. """
vault_name = flat_path(playbook)[:-1]
if len(vault_name) == 0:
vault_name = 'all'
vault_file = '/etc/ansible/group_vars/' + vault_name + '.yml'
if not args.skip_download:
download_from_s3(playbook + 'vault.yml', vault_file)
with open('/etc/ansible/hosts', 'a') as stream:
stream.writelines(["\n[" + vault_name + "]\n", 'localhost\n'])
def get_templates(playbook):
""" Downloads playbook templates """
import os
import shutil
path = playbook_directory(playbook) + 'templates'
if not args.skip_download:
if os.path.isdir(path):
shutil.rmtree(path)
download_directory_from_s3(playbook + 'templates', path)
def configure_environment():
""" Exposes information from Resource Tags in Ansible vars """
get_vault('')
with open('/etc/ansible/group_vars/local.yml', 'w+') as stream:
stream.write("\nproject: " + resource_tags()['Project'])
stream.write("\nenvironment_tier: " + resource_tags()['Environment'])
stream.write("\nsystem_role: " + resource_tags()['Role'])
def record_exit(playbook, exit_status):
""" Saves exit status of playbook for notfication purposes"""
playbook_name = playbook_directory(playbook) + 'playbook' + '.status'
with open(playbook_name, 'w+') as stream:
stream.write(str(exit_status))
def execute(playbook):
""" Downloads and executes a playbook. """
path = playbook_directory(playbook)
for hook in ['pre-', '', 'post-']:
filename = hook + 'playbook.yml'
if not args.skip_download:
download_from_s3(playbook + filename, path + filename)
# Avoid 'file not found' messages from ansible-playbook
import os.path
if os.path.isfile(path + filename):
exit_status = subprocess.call('ansible-playbook ' + path + filename, shell=True)
record_exit(playbook, exit_status)
else:
print('%s file not found, so not executed with ansible-playbook' % (path + filename))
def ssh_keyscan(host):
""" Get the SSH host key from a remote server by connecting to it """
import paramiko
with paramiko.transport.Transport(host) as ssh:
ssh.start_client()
return ssh.get_remote_server_key()
def ssh_host_key(host, port=22):
""" Get SSH host key, return string formatted for known_hosts """
if port != 22:
host = "{host}:{port}".format(host=host, port=port)
key = ssh_keyscan(host)
return "{host} {key_name} {key}".format(
host=host,
key_name=key.get_name(),
key=key.get_base64())
def in_known_hosts(host_key):
""" Checks if a key is in known_hosts """
if not os.path.isfile('/etc/ssh/ssh_known_hosts'):
return False
with open('/etc/ssh/ssh_known_hosts', 'r') as known_hosts:
for entry in known_hosts:
if host_key in entry:
return True
return False
def add_to_known_hosts(host_key):
""" Appends line to a file """
if in_known_hosts(host_key):
return
with open('/etc/ssh/ssh_known_hosts', 'a') as known_hosts:
known_hosts.write(host_key + "\n")
def configure_ansible():
""" Fetches ansible configurations from ForgeBucket """
download_from_s3('ansible.hosts', '/etc/ansible/hosts')
download_from_s3('ansible.cfg', '/etc/ansible/ansible.cfg')
download_from_s3('vault.key', '/etc/ansible/vault.key')
files = ['/etc/ansible/ansible.cfg', '/etc/ansible/vault.key']
set_permissions(files, 0o400)
add_to_known_hosts(ssh_host_key('github.com'))
add_to_known_hosts(ssh_host_key('bitbucket.org'))
def set_permissions(files, mode):
""" Sets permissions on a list of files """
for filename in files:
try:
os.chmod(filename, mode)
except OSError:
pass
def get_credentials():
""" Fetches credentials needed for private repositories """
download_from_s3('ssh.ed25519', '/root/.ssh/id_ed25519')
download_from_s3('ssh.rsa', '/root/.ssh/id_rsa')
set_permissions(['/root/.ssh/id_ed25519', '/root/.ssh/id_rsa'], 0o400)
def preconfigure():
""" Configure everything needed to configure everything else. """
if args.skip_preconfigure:
return
install_with_pip(['ansible==2.2.0.0'])
configure_ansible()
configure_environment()
get_credentials()
download_from_s3('bin/reforge', '/usr/local/sbin/reforge')
set_permissions(['/usr/local/sbin/reforge'], 0o500)
def self_provision():
""" Bring it all together and follow your dreams, little server! """
preconfigure()
for playbook in applicable_playbooks():
get_dependencies(playbook)
get_vault(playbook)
get_templates(playbook)
execute(playbook)
parser = argparse.ArgumentParser()
parser.add_argument('--skip-preconfigure', action='store_true', help='Skip pre-configuration')
parser.add_argument('--skip-base-playbook', action='store_true', help='Skip base playbook')
parser.add_argument('--skip-project-playbook', action='store_true', help='Skip project playbook')
parser.add_argument('--skip-role-playbook', action='store_true', help='Skip role playbook')
parser.add_argument('--skip-download', action='store_true', help='Skip download, so you can test the playbooks in /tmp')
args = parser.parse_args()
self_provision()