-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy-configurations.py
More file actions
265 lines (227 loc) · 12 KB
/
deploy-configurations.py
File metadata and controls
265 lines (227 loc) · 12 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
import io
import os
import gzip
import tarfile
import hashlib
import argparse
from configparser import ConfigParser
import boto3
import tabulate
config = ConfigParser()
c = config.read('config.cfg')
if not c:
config.read('../config.cfg')
S3_STAGING_BUCKET = config['S3']['staging_bucket']
S3_STAGING_PREFIX = config['S3']['staging_prefix']
AWS_SECRET_KEY_ID = config['AWS']['aws_access_key_id']
AWS_SECRET_ACCESS_KEY = config['AWS']['aws_secret_access_key']
S3_BASE_URL = f'https://{S3_STAGING_BUCKET}.s3.amazonaws.com/{S3_STAGING_PREFIX}'
# ============================================== FILE-IO FUNCTIONS ====================================================
def get_file_hash(path):
"""
Get the MD5 hash given a file path
:param path: The path to the file to hash
:return: MD5 hash 32byte string
"""
md5_hash = hashlib.md5()
with open(path, "rb") as f:
# Read and update hash in chunks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
md5_hash.update(byte_block)
return md5_hash.hexdigest()
def get_deltas(base_root_dir, overwrite_root_dir):
"""
Given two directories recursively merge overwrite_root into base_root, return deltas as list of changes.
:param base_root_dir: The top level directory containing all required files
:param overwrite_root_dir: The top level directory containing additions/modifications relative to base_root
:return: A list of deltas in the format: (type, action, relative_path)
"""
deltas = []
base_root_dir = os.path.join(base_root_dir, '')
overwrite_root_dir = os.path.join(overwrite_root_dir, '')
# Iterate through our overwrite_directory; this directory will be merged into base_directory
for overwrite_root, overwrite_dirs, overwrite_files in os.walk(overwrite_root_dir, topdown=True):
relative_directory = overwrite_root.replace(overwrite_root_dir, '')
base_directory = os.path.join(base_root_dir, relative_directory)
if relative_directory.startswith('.git'):
continue
# Check if the equivalent overwrite path exists in the base directory
if not os.path.exists(base_directory):
# If it doesn't mark the directory for creation
deltas.append(('directory', 'create', relative_directory))
for overwrite_file in overwrite_files:
relative_file = os.path.join(relative_directory, overwrite_file)
base_file_path = os.path.join(base_root_dir, relative_file)
overwrite_file_path = os.path.join(overwrite_root_dir, relative_file)
if not os.path.exists(base_file_path):
deltas.append(('file', 'write', relative_file))
else:
if get_file_hash(overwrite_file_path) != get_file_hash(base_file_path):
deltas.append(('file', 'overwrite', relative_file))
return deltas
def create_tar(version, base_root_dir, overwrite_root_dir=None, separate_mirrors_and_configs=True):
"""
Merge overwrite_root_dir into base_root_dir bundle the results into a tarball;
if overwrite_root_dir is not specified base_root_dir will be wrapped into a tarball without performing a merge.
:param version: The current config version number (must be valid float)
:param base_root_dir: The top level directory containing all required files
:param overwrite_root_dir: The top level directory containing additions/modifications relative to base_root
:param separate_mirrors_and_configs: If True, two archives will be created, default_configs and mirrors; otherwise
a single combined archive will be created.
"""
base_root_dir = os.path.join(base_root_dir, '')
io_combined_bytes = io.BytesIO()
io_config_bytes = io.BytesIO()
io_mirror_bytes = io.BytesIO()
combined_archive = tarfile.open(fileobj=io_combined_bytes, mode='w')
default_configs_archive = tarfile.open(fileobj=io_config_bytes, mode='w')
mirrors_archive = tarfile.open(fileobj=io_mirror_bytes, mode='w')
if overwrite_root_dir:
overwrite_root_dir = os.path.join(overwrite_root_dir, '')
for _type, action, overwrite_relative_path in get_deltas(base_root_dir, overwrite_root_dir):
if _type == 'file':
overwrite_base_file_path = os.path.join(overwrite_root_dir, overwrite_relative_path)
if separate_mirrors_and_configs:
if overwrite_relative_path.startswith('default_configs/'):
default_configs_archive.add(overwrite_base_file_path, overwrite_relative_path)
elif overwrite_relative_path.startswith('mirrors/'):
mirrors_archive.add(overwrite_base_file_path, overwrite_relative_path)
else:
combined_archive.add(overwrite_base_file_path, overwrite_relative_path)
for base_root, base_dirs, base_files in os.walk(base_root_dir):
relative_directory = base_root.replace(base_root_dir, '')
for base_file in base_files:
relative_file = os.path.join(relative_directory, base_file)
base_file_path = os.path.join(base_root_dir, relative_file)
try:
if not separate_mirrors_and_configs:
combined_archive.getmember(relative_file)
else:
if relative_file.startswith('default_configs/'):
default_configs_archive.getmember(relative_file)
elif relative_file.startswith('mirrors/'):
mirrors_archive.getmember(relative_file)
except KeyError:
if separate_mirrors_and_configs:
if relative_file.startswith('default_configs/'):
default_configs_archive.add(base_file_path, relative_file)
elif relative_file.startswith('mirrors/'):
mirrors_archive.add(base_file_path, relative_file)
else:
combined_archive.add(base_file_path, relative_file)
if separate_mirrors_and_configs:
with gzip.open('default_configs.{}.tar.gz'.format(version), 'wb') as config_archive_out:
io_config_bytes.seek(0)
config_archive_out.write(io_config_bytes.read())
with gzip.open('mirrors.{}.tar.gz'.format(version), 'wb') as mirrors_archive_out:
io_mirror_bytes.seek(0)
mirrors_archive_out.write(io_mirror_bytes.read())
else:
with gzip.open('combined.{}.tar.gz'.format(version), 'wb') as combined_archive_out:
io_combined_bytes.seek(0)
combined_archive_out.write(io_combined_bytes.read())
# ================================================ S3 Functions ======================================================
def list_dynamite_config_versions_s3():
"""
Dynamite Configurations must each be versioned (E.G 0.72, 1.0, 1.01); lists the versions currently in this staging
bucket
:return: A list of versions
"""
versions = set()
s3 = boto3.client('s3',
aws_access_key_id=AWS_SECRET_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
for key in s3.list_objects(
Bucket=S3_STAGING_BUCKET,
Prefix=S3_STAGING_PREFIX)['Contents']:
key = key['Key'].split('/')
if len(key) != 3:
continue
try:
versions.add(float(key[1]))
except ValueError:
continue
return list(versions)
def copy_to_latest_prefix_s3(filename, version):
"""
Copy the contents of an uploaded version to the "latest" prefix
:param filename: Name of the file (E.G default_configs.tar.gz OR mirrors.tar.gz)
:param version: The version number for the configuration set you wish to copy.
"""
s3 = boto3.resource('s3',
aws_access_key_id=AWS_SECRET_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
prefix = os.path.join(S3_STAGING_PREFIX, version)
basepath = prefix.split('/')[-2]
s3.meta.client.copy(
{
'Bucket': S3_STAGING_BUCKET,
'Key': os.path.join(prefix, filename)
},
S3_STAGING_BUCKET,
os.path.join(basepath, 'latest', filename),
ExtraArgs={'ACL': 'public-read'}
)
def upload_file_to_s3(f_obj, filename, version):
"""
Uploads a file like object to S3
:param f_obj: A file like object (rb mode)
:param filename: The name of the file in S3
:param version: The version number prefix that the file will be written into.
"""
s3 = boto3.client('s3', aws_access_key_id=AWS_SECRET_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
if filename is None:
filename = f_obj.name.split("/")[-1]
else:
filename = filename
file_key = os.path.join(S3_STAGING_PREFIX, str(version), filename)
s3.upload_fileobj(f_obj, S3_STAGING_BUCKET, file_key, ExtraArgs={'ACL': 'public-read'})
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Stage Dynamite Configurations to public S3 bucket.'
)
parser.add_argument('base_directory', metavar='base_directory', type=str,
help='The path to the directory containing the base configurations.')
parser.add_argument('version', metavar='version', type=float,
help='The version number for the current configuration set.')
parser.add_argument("--merge-directory", dest="merge_directory", type=str, default=None,
help="A directory containing additional/modified configurations "
"you want to merge with the base directory and incorporate into the deployment"
)
parser.add_argument('--overwrite', default=False, dest='overwrite', action='store_true',
help='If true overwrites an old version if one is specified.')
args = parser.parse_args()
if args.merge_directory:
headers = ["File Type", "Merge Action", "Path"]
rows = [delta for delta in get_deltas(args.base_directory, args.merge_directory)]
print(tabulate.tabulate(rows, headers=headers, tablefmt='fancy_grid'))
print('\nDetected {} changes when building merge strategy for {} <- {}'.format(len(rows), args.base_directory,
args.merge_directory))
res = input("OK with the above merge? [Y|n]: ")
while True:
if str(res).strip() == "n":
exit(0)
elif str(res).strip() == "":
break
elif str(res).strip() == "y":
break
existing_versions_in_s3 = list_dynamite_config_versions_s3()
if args.version in existing_versions_in_s3 and not args.overwrite:
print('Version {} already exists in {}. Use the --overwrite flag to overwrite {}'.format(args.version,
S3_BASE_URL,
args.version))
exit(1)
print("Creating tarballs for default_configs and mirrors...")
create_tar(version=args.version, base_root_dir=args.base_directory, overwrite_root_dir=args.merge_directory,
separate_mirrors_and_configs=True)
default_config_f_name = "default_configs.{}.tar.gz".format(args.version)
mirrors_f_name = "mirrors.{}.tar.gz".format(args.version)
print("Uploading {} to {}".format(mirrors_f_name, os.path.join(S3_BASE_URL, str(args.version), 'mirrors.tar.gz')))
with open(mirrors_f_name, 'rb') as mirrors_fobj:
upload_file_to_s3(mirrors_fobj, 'mirrors.tar.gz', args.version)
print(
"Uploading {} to {}".format(default_config_f_name,
os.path.join(S3_BASE_URL, str(args.version), 'default_configs.tar.gz')))
with open(default_config_f_name, 'rb') as default_configs_fobj:
upload_file_to_s3(default_configs_fobj, 'default_configs.tar.gz', args.version)