forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelease.py
343 lines (307 loc) · 12.3 KB
/
release.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
These commands are used to release Salt.
"""
# pylint: disable=resource-leakage,broad-except,3rd-party-module-not-gated
from __future__ import annotations
import json
import logging
import os
import pathlib
import tempfile
import time
import boto3
import virustotal3.core
from botocore.exceptions import ClientError
from ptscripts import Context, command_group
import tools.utils
import tools.utils.repo
log = logging.getLogger(__name__)
# Define the command group
release = command_group(
name="release",
help="Release Related Commands",
description=__doc__,
)
@release.command(
name="upload-artifacts",
arguments={
"salt_version": {
"help": "The salt version to release.",
},
"artifacts_path": {
"help": "Local path to directory containing release artifacts",
},
},
)
def upload_artifacts(ctx: Context, salt_version: str, artifacts_path: pathlib.Path):
"""
Upload release artifacts to staging bucket folder `release-artifacts/<salt-version>`.
These will be used when we later actually publish the release.
"""
ctx.info("Preparing upload ...")
s3 = boto3.client("s3")
to_delete_paths: list[dict[str, str]] = []
remote_path = f"release-artifacts/{salt_version}"
try:
ret = s3.list_objects(
Bucket=tools.utils.STAGING_BUCKET_NAME,
Prefix=remote_path,
)
if "Contents" in ret:
objects = []
for entry in ret["Contents"]:
if entry["Key"].endswith(".release-backup-done"):
continue
objects.append({"Key": entry["Key"]})
to_delete_paths.extend(objects)
except ClientError as exc:
if "Error" not in exc.response:
raise
if exc.response["Error"]["Code"] != "404":
raise
if to_delete_paths:
with tools.utils.create_progress_bar() as progress:
bucket_uri = f"s3://{tools.utils.STAGING_BUCKET_NAME}/{remote_path}"
task = progress.add_task(f"Deleting '{bucket_uri}'", total=1)
try:
ret = s3.delete_objects(
Bucket=tools.utils.STAGING_BUCKET_NAME,
Delete={"Objects": objects},
)
except ClientError:
log.exception("Failed to delete '%s'", bucket_uri)
finally:
progress.update(task, advance=1)
ctx.info("Uploading release artifacts ...")
to_upload_paths: list[pathlib.Path] = []
copy_exclusions = [
".json",
]
for fpath in artifacts_path.iterdir():
if fpath.suffix in copy_exclusions:
continue
to_upload_paths.append(fpath)
try:
for fpath in to_upload_paths:
upload_path = f"{remote_path}/{fpath.name}"
size = fpath.stat().st_size
ctx.info(f" {upload_path}")
with tools.utils.create_progress_bar(file_progress=True) as progress:
task = progress.add_task(description="Uploading...", total=size)
s3.upload_file(
str(fpath),
tools.utils.STAGING_BUCKET_NAME,
upload_path,
Callback=tools.utils.repo.UpdateProgress(progress, task),
)
except KeyboardInterrupt:
pass
@release.command(
name="download-onedir-artifact",
arguments={
"salt_version": {
"help": "The salt version to release.",
},
"platform": {
"help": "The onedir platform archive to download.",
"required": True,
"choices": ("linux", "windows", "darwin", "macos"),
},
"arch": {
"help": "The onedir arch archive to download.",
"required": True,
},
},
)
def download_onedir_artifact(
ctx: Context, salt_version: str, platform: str = "linux", arch: str = "x86_64"
):
"""
Download onedir artifact from staging bucket.
"""
s3 = boto3.client("s3")
if platform == "darwin":
platform = "macos"
if arch == "aarch64":
arch = "arm64"
arch = arch.lower()
platform = platform.lower()
if platform in ("linux", "macos") and arch not in ("x86_64", "arm64"):
ctx.error(
f"The 'arch' value for {platform} must be one of: 'x86_64', 'aarch64', 'aarch64'"
)
ctx.exit(1)
if platform == "windows" and arch not in ("x86", "amd64"):
ctx.error(f"The 'arch' value for {platform} must be one of: 'x86', 'amd64'")
ctx.exit(1)
archive_name = f"salt-{salt_version}-onedir-{platform}-{arch}.tar.xz"
archive_path = tools.utils.REPO_ROOT / "artifacts" / archive_name
if "rc" in salt_version:
prefix = "salt_rc/salt"
else:
prefix = "salt"
remote_path = f"{prefix}/py3/onedir/minor/{salt_version}/{archive_name}"
archive_path.parent.mkdir()
try:
ret = s3.head_object(Bucket=tools.utils.STAGING_BUCKET_NAME, Key=remote_path)
size = ret["ContentLength"]
with archive_path.open("wb") as wfh:
ctx.info(
f"Downloading s3://{tools.utils.STAGING_BUCKET_NAME}/{remote_path} to {archive_path} ..."
)
with tools.utils.create_progress_bar(file_progress=True) as progress:
task = progress.add_task(
description="Downloading ...",
total=size,
)
s3.download_fileobj(
Bucket=tools.utils.STAGING_BUCKET_NAME,
Key=remote_path,
Fileobj=wfh,
Callback=tools.utils.repo.UpdateProgress(progress, task),
)
except ClientError as exc:
if "Error" not in exc.response:
log.exception("Error downloading %s: %s", remote_path, exc)
ctx.exit(1)
if exc.response["Error"]["Code"] == "404":
ctx.error(f"Could not find {remote_path} in bucket.")
ctx.exit(1)
elif exc.response["Error"]["Code"].startswith("4"):
ctx.error(f"Could not download {remote_path} from bucket: {exc}")
ctx.exit(1)
else:
log.exception("Failed to download %s: %s", remote_path, exc)
ctx.exit(1)
if not archive_path.exists():
ctx.error(f"The {archive_path} does not exist")
ctx.exit(1)
if not archive_path.stat().st_size:
ctx.error(f"The {archive_path} size is zero!")
ctx.exit(1)
@release.command(
name="upload-virustotal",
arguments={
"salt_version": {
"help": "The salt version to release.",
},
},
)
def upload_virustotal(ctx: Context, salt_version: str):
# Get a list of files to upload
files_to_copy: list[str]
if salt_version.startswith("v"):
salt_version = salt_version[1:]
ctx.info("Grabbing remote file listing of files in staging ...")
s3 = boto3.client("s3")
repo_release_files_path = pathlib.Path(
f"release-artifacts/{salt_version}/.release-files.json"
)
with tempfile.TemporaryDirectory(prefix=f"{salt_version}_release_") as tsd:
local_release_files_path = pathlib.Path(tsd) / repo_release_files_path.name
try:
with local_release_files_path.open("wb") as wfh:
ctx.info(f"Downloading file: {repo_release_files_path}")
s3.download_fileobj(
Bucket=tools.utils.STAGING_BUCKET_NAME,
Key=str(repo_release_files_path.as_posix()),
Fileobj=wfh,
)
files_to_copy = json.loads(local_release_files_path.read_text())
except ClientError as exc:
if "Error" not in exc.response:
log.exception("Error downloading %s: %s", repo_release_files_path, exc)
ctx.exit(1)
if exc.response["Error"]["Code"] == "404":
ctx.error(f"Could not find {repo_release_files_path} in bucket.")
ctx.exit(1)
if exc.response["Error"]["Code"] == "400":
ctx.error(
f"Could not download {repo_release_files_path} from bucket: {exc}"
)
ctx.exit(1)
log.exception("Error downloading %s: %s", repo_release_files_path, exc)
ctx.exit(1)
# If we get approval, we can add RPM and DEB
file_types = [".msi", ".exe", ".pkg"]
files_to_upload = []
for file in sorted(files_to_copy):
if f"minor/{salt_version}" in file:
if os.path.splitext(file)[1] in file_types:
files_to_upload.append(file)
# These are showing errors for Windows and macOS
# if f"onedir/minor/{salt_version}" in file:
# if file.endswith("tar.xz"):
# files_to_upload.append(file)
ctx.info("Found the following files to upload:")
for file in files_to_upload:
ctx.info(f"- {os.path.basename(file)}")
# download each file, then upload to VirusTotal
# This takes around 4 minutes per file
# Maybe we could do this asynchronously
failed_files = {}
for file in files_to_upload:
ctx.info("-" * 80)
download_file = pathlib.Path(file)
with tempfile.TemporaryDirectory(prefix=f"{salt_version}_release_") as tsd:
local_download_file = pathlib.Path(tsd) / download_file.name
try:
with local_download_file.open("wb") as wfh:
ctx.info(f"Downloading from repo: {download_file}")
s3.download_fileobj(
Bucket=tools.utils.STAGING_BUCKET_NAME,
Key=str(download_file.as_posix()),
Fileobj=wfh,
)
except ClientError as exc:
if "Error" not in exc.response:
log.exception("Error downloading %s: %s", download_file, exc)
ctx.exit(1)
if exc.response["Error"]["Code"] == "404":
ctx.error(f"Could not find {download_file} in bucket.")
ctx.exit(1)
if exc.response["Error"]["Code"] == "400":
ctx.error(f"Could not download {download_file} from bucket: {exc}")
ctx.exit(1)
log.exception("Error downloading %s: %s", download_file, exc)
ctx.exit(1)
# API key should be an environment variable
api_key = os.environ.get("VIRUSTOTAL_API_KEY")
ctx.info(
f"Uploading to VirusTotal: {os.path.basename(local_download_file)}"
)
vt = virustotal3.core.Files(api_key)
response = vt.upload(local_download_file)
# We want the id
analysis_id = response["data"]["id"]
# Lets check the results
results = virustotal3.core.get_analysis(api_key, analysis_id)
status = results["data"]["attributes"]["status"]
ctx.info("Waiting for results from VirusTotal (takes a few minutes)")
while "completed" not in status:
time.sleep(10)
results = virustotal3.core.get_analysis(api_key, analysis_id)
status = results["data"]["attributes"]["status"]
ctx.info("Results summary:")
stats = results["data"]["attributes"]["stats"]
failures = False
for field in stats:
ctx.info(f"- {field}: {stats[field]}")
if field in ["malicious", "suspicious"]:
if stats[field] > 0:
failures = True
sha256 = results["meta"]["file_info"]["sha256"]
if failures:
ctx.info("ERROR: VirusTotal scan encountered failures")
failed_files[os.path.basename(local_download_file)] = sha256
ctx.info("See details here:")
ctx.info(f"- File: {os.path.basename(local_download_file)}")
ctx.info(f"- URL: https://www.virustotal.com/gui/file/{sha256}")
if failed_files:
# We want to exit with errors if there are failures
ctx.info("-" * 80)
ctx.info("VirusTotal flagged the following files:")
for file in failed_files:
ctx.info(f"- {file}")
ctx.info(f" https://www.virustotal.com/gui/file/{failed_files[file]}")
ctx.exit(1)