-
Notifications
You must be signed in to change notification settings - Fork 1
/
autopkg_tools.py
executable file
·311 lines (268 loc) · 11 KB
/
autopkg_tools.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
#!/usr/bin/env python3
# BSD-3-Clause
# Copyright (c) Facebook, Inc. and its affiliates.
# Copyright (c) tig <https://6fx.eu/>.
# Copyright (c) Gusto, Inc.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import json
import logging
import os
import plistlib
import requests
import shutil
import subprocess
import sys
import threading
from datetime import datetime
from optparse import OptionParser
from pathlib import Path
import git
SLACK_WEBHOOK = os.environ.get("SLACK_WEBHOOK_TOKEN", None)
AUTOPKG_REPO_DIR = os.getenv("GITHUB_WORKSPACE", "./")
MUNKI_REPO_DIR = os.path.join(AUTOPKG_REPO_DIR, "munki_repo")
RECIPE_TO_RUN = os.environ.get("RECIPE", None)
AUTOPKG_REPOSITORY = os.getenv("GITHUB_REPOSITORY", "None")
MUNKI_REPOSITORY = os.getenv("MUNKI_REPOSITORY", "None")
# setup gitpython repos
MUNKI_REPO = git.Repo(MUNKI_REPO_DIR)
AUTOPKG_REPO = git.Repo(AUTOPKG_REPO_DIR)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
class Recipe(object):
def __init__(self, path):
self.path = os.path.join(AUTOPKG_REPO_DIR, "overrides", path)
self.error = False
self.results = {}
self.updated = False
self.verified = None
self._keys = None
self._has_run = False
@property
def plist(self):
if self._keys is None:
with open(self.path, "rb") as f:
self._keys = plistlib.load(f)
return self._keys
@property
def branch(self):
return (
"{}_{}".format(self.name, self.updated_version)
.strip()
.replace(" ", "")
.replace(")", "-")
.replace("(", "-")
)
@property
def updated_version(self):
if not self.results or not self.results["imported"]:
return None
return self.results["imported"][0]["version"].strip().replace(" ", "")
@property
def name(self):
return self.plist["Input"]["NAME"]
def verify_trust_info(self):
cmd = ["/usr/local/bin/autopkg", "verify-trust-info", self.path, "-vvv"]
output, err, exit_code = run_cmd(cmd)
if exit_code == 0:
self.verified = True
else:
err = err.decode()
self.results["message"] = err
self.verified = False
return self.verified
def update_trust_info(self):
cmd = ["/usr/local/bin/autopkg", "update-trust-info", self.path]
output, err, exit_code = run_cmd(cmd)
return output
def _parse_report(self, report):
with open(report, "rb") as f:
report_data = plistlib.load(f)
failed_items = report_data.get("failures", [])
imported_items = []
if report_data["summary_results"]:
# This means something happened
munki_results = report_data["summary_results"].get(
"munki_importer_summary_result", {}
)
imported_items.extend(munki_results.get("data_rows", []))
return {"imported": imported_items, "failed": failed_items}
def run(self):
if self.verified == False:
self.error = True
self.results["failed"] = True
self.results["imported"] = ""
else:
report = "/tmp/autopkg.plist"
if not os.path.isfile(report):
# Letting autopkg create them has led to errors on github runners
Path(report).touch()
cmd = [
"/usr/local/bin/autopkg",
"run",
self.path,
"-v",
"--post",
"io.github.hjuutilainen.VirusTotalAnalyzer/VirusTotalAnalyzer",
"--report-plist",
report,
]
output, err, exit_code = run_cmd(cmd)
if err:
self.error = True
self.results["failed"] = True
self.results["imported"] = ""
self._has_run = True
self.results = self._parse_report(report)
if not self.results["failed"] and not self.error and self.updated_version:
self.updated = True
return self.results
def run_cmd(cmd):
logging.debug(f"Running { ' '.join(cmd)}")
run = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = run.communicate()
exit_code = run.wait()
return output, err, exit_code
def create_pull_request(repo, payload):
url = f"https://api.github.com/repos/{repo}/pulls"
headers = {
"Authorization": f"token {os.environ['GH_TOKEN']}",
"Accept": "application/vnd.github.v3+json",
}
json_payload = json.dumps(payload)
response = requests.post(url, headers=headers, data=json_payload)
return response.json()
# Would need repo, repo dir, branch name, file to commit, commit message, and pr title
# The munki repo could have multiple files?
def worktree_commit(recipe):
MUNKI_REPO.git.worktree("add", recipe.branch, "-b", recipe.branch)
worktree_repo_path = os.path.join(MUNKI_REPO_DIR, recipe.branch)
worktree_repo = git.Repo(worktree_repo_path)
worktree_repo.git.fetch()
if recipe.branch in MUNKI_REPO.git.branch("--list", "-r"):
worktree_repo.git.pull("origin", recipe.branch)
for imported in recipe.results["imported"]:
shutil.move(
f"{MUNKI_REPO_DIR}/pkgsinfo/{ imported['pkginfo_path'] }",
f"{worktree_repo_path}/pkgsinfo/{ imported['pkginfo_path'] }",
)
recipe_path = f"{worktree_repo_path}/pkgsinfo/{ imported['pkginfo_path'] }"
worktree_repo.index.add([recipe_path])
worktree_repo.index.commit(
f"'Updated { recipe.name } to { recipe.updated_version }'"
)
worktree_repo.git.push("--set-upstream", "origin", recipe.branch)
MUNKI_REPO.git.worktree("remove", recipe.branch, "-f")
pr_payload = {
"title": f"feat: { recipe.name } update",
"body": f"Updated { recipe.name } to { recipe.updated_version }",
"head": recipe.branch,
"base": "main",
}
create_pull_request(MUNKI_REPOSITORY, pr_payload)
return
def handle_recipe(recipe, opts):
logging.debug(f"Handling {recipe.name}")
recipe.verify_trust_info()
if recipe.verified is False:
recipe.update_trust_info()
branch_name = (
f"update_trust-{recipe.name}-{datetime.now().strftime('%Y-%m-%d')}"
)
AUTOPKG_REPO.git.worktree("add", branch_name, "-b", branch_name)
autopkg_worktree_path = os.path.join(AUTOPKG_REPO_DIR, branch_name)
autopkg_worktree_repo = git.Repo(autopkg_worktree_path)
logging.debug(f"Creating {autopkg_worktree_path}")
overrides_path = os.path.join(autopkg_worktree_path, "overrides")
logging.debug(f"Creating {overrides_path}")
shutil.copy(recipe.path, overrides_path)
autopkg_worktree_repo.git.add(overrides_path)
autopkg_worktree_repo.git.commit(m=f"Update trust for {recipe.name}")
autopkg_worktree_repo.git.push("--set-upstream", "origin", branch_name)
payload = {
"title": f"feat: Update trust for { recipe.name }",
"body": recipe.results["message"],
"head": branch_name,
"base": "main",
}
create_pull_request(AUTOPKG_REPOSITORY, payload)
AUTOPKG_REPO.git.worktree("remove", branch_name, "-f")
if recipe.verified in (True, None):
recipe.run()
if recipe.results["imported"]:
print("Imported")
worktree_commit(recipe)
# slack_alert(recipe, opts)
return
def parse_recipes(recipes, opts):
recipe_list = []
if RECIPE_TO_RUN:
for recipe in recipes:
ext = os.path.splitext(recipe)[1]
if ext != ".recipe":
recipe_list.append(recipe + ".recipe")
else:
recipe_list.append(recipe)
else:
ext = os.path.splitext(recipes)[1]
if ext == ".json":
parser = json.load
elif ext == ".plist":
parser = plistlib.load
else:
print(f'Invalid run list extension "{ ext }" (expected plist or json)')
sys.exit(1)
with open(recipes, "rb") as f:
recipe_list = parser(f)
return map(Recipe, recipe_list)
## Icon handling
def import_icons():
branch_name = "icon_import_{}".format(datetime.now().strftime("%Y-%m-%d"))
MUNKI_REPO.git.worktree("add", branch_name, "-b", branch_name)
result = subprocess.check_call(
"/usr/local/munki/iconimporter munki_repo", shell=True
)
MUNKI_REPO.index.add(["icons/"])
MUNKI_REPO.index.commit("Added new icons")
MUNKI_REPO.git.push("--set-upstream", "origin", branch_name)
MUNKI_REPO.git.worktree("remove", branch_name)
return result
def main():
parser = OptionParser(description="Wrap AutoPkg with git support.")
parser.add_option(
"-l", "--list", help="Path to a plist or JSON list of recipe names."
)
parser.add_option(
"-i",
"--icons",
action="store_true",
help="Run iconimporter against git munki repo.",
)
(opts, _) = parser.parse_args()
recipes = (
RECIPE_TO_RUN.split(", ") if RECIPE_TO_RUN else opts.list if opts.list else None
)
if recipes is None:
print("Recipe --list or RECIPE_TO_RUN not provided!")
sys.exit(1)
recipes = parse_recipes(recipes, opts)
threads = []
for recipe in recipes:
# handle_recipe(recipe, opts)
thread = threading.Thread(target=handle_recipe(recipe, opts))
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if opts.icons:
import_icons()
if __name__ == "__main__":
main()