forked from fepitre/qubes-builderv2-github
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-command.py
executable file
·285 lines (257 loc) · 9.12 KB
/
github-command.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
#!/usr/bin/python3
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2022 Frédéric Pierret (fepitre) <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import sys
import os
import subprocess
import datetime
import logging
from pathlib import Path
log = logging.getLogger("github-command")
class GithubCommandError(Exception):
pass
def run_command(cmd, env=None, wait=False, ignore_exit_codes=(0,)):
if wait:
try:
subprocess.run(cmd, env=env, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
if e.returncode in ignore_exit_codes:
return
raise GithubCommandError(f"Failed to run command: {e.stderr}")
else:
subprocess.Popen(cmd, env=env)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--log-basename")
parser.add_argument(
"--no-builders-update",
action="store_true",
default=False,
help="Don't update builders.",
)
parser.add_argument(
"--wait",
action="store_true",
default=False,
help="Don't put processes into background.",
)
parser.add_argument(
"--config-file",
default=Path.home() / ".config/qubes-builder-github/builders.list",
)
parser.add_argument(
"--scripts-dir", default=Path("/usr/local/lib/qubes-builder-github")
)
parser.add_argument(
"--local-log-file",
help="Use local log file instead of qubesbuilder.BuildLog RPC.",
)
signer = parser.add_mutually_exclusive_group()
signer.add_argument(
"--no-signer-github-command-check",
action="store_true",
default=False,
help="Don't check signer fingerprint.",
)
signer.add_argument(
"--signer-fpr",
help="Signer GitHub command fingerprint.",
)
parser.add_argument("command")
parser.add_argument("command_file")
args = parser.parse_args()
scripts_dir = Path(args.scripts_dir).resolve()
if not scripts_dir.exists():
raise GithubCommandError("Cannot find GitHub scripts directory.")
if args.command not in (
"Build-component",
"Upload-component",
"Build-template",
"Upload-template",
"Build-iso",
):
raise GithubCommandError("Invalid command.")
command_file = Path(args.command_file).resolve()
if not command_file.exists():
raise GithubCommandError("Cannot find command file.")
command = command_file.read_text().rstrip("\n").split()
if command[0] != args.command:
raise GithubCommandError("Wrong command file for requested command.")
timestamp = None
component_name = None
commit_sha = None
repository_publish = None
distribution_name = None
template_name = None
template_timestamp = None
template_sha = None
iso_version = None
iso_timestamp = None
try:
if args.command == "Build-component":
release_name, component_name = None, command[1]
elif args.command == "Upload-component":
(
release_name,
component_name,
commit_sha,
repository_publish,
distribution_name,
) = command[1:]
elif args.command == "Build-template":
release_name, template_name, template_timestamp = command[1:]
timestamp = datetime.datetime.strptime(
template_timestamp + "Z", "%Y%m%d%H%M%z"
)
elif args.command == "Build-iso":
release_name, iso_version, iso_timestamp = command[1:]
timestamp = datetime.datetime.strptime(
iso_timestamp + "Z", "%Y%m%d%H%M%z"
)
elif args.command == "Upload-template":
(
release_name,
template_name,
template_sha,
repository_publish,
) = command[1:]
else:
raise GithubCommandError(f"Unsupported command: {args.command}")
except IndexError as e:
raise GithubCommandError(f"Wrong number of args provided: {str(e)}")
if timestamp:
# we are not seeking nanosecond precision
utcnow = datetime.datetime.now(datetime.UTC)
timestamp_max = utcnow + datetime.timedelta(minutes=5)
timestamp_min = utcnow - datetime.timedelta(hours=1)
if (
timestamp.timestamp() < timestamp_min.timestamp()
or timestamp_max.timestamp() < timestamp.timestamp()
):
raise GithubCommandError(
f"Timestamp outside of allowed range (min: {timestamp_min}, max: {timestamp_max}, current={timestamp}"
)
# Update GitHub Builder
cmd = [
"flock",
"-x",
"-n",
"-E",
"11",
str(scripts_dir / "builder.lock"),
"bash",
"-c",
f"trap 'rm -f /tmp/update-qubes-builder' EXIT && cp {str(scripts_dir / 'utils/update-qubes-builder')} /tmp && /tmp/update-qubes-builder {str(scripts_dir)}",
]
if not args.no_builders_update:
run_command(cmd, wait=args.wait, ignore_exit_codes=(0, 11))
with open(args.config_file, "r") as f:
content = f.read().splitlines()
for line in content:
builder_release_name, builder_dir_str, builder_conf = line.split("=")
if not Path(builder_dir_str).resolve().exists():
log.error(f"Cannot find {builder_dir_str}")
continue
# Check if requested release name is supported by this builder instance
if release_name is not None and release_name != builder_release_name:
log.info(f"Requested release does not match builder release.")
continue
builder_dir = Path(builder_dir_str).resolve()
# Update Qubes Builder
cmd = [
"flock",
"-x",
"-n",
"-E",
"11",
str(builder_dir / "builder.lock"),
str(scripts_dir / "utils/update-qubes-builder"),
str(builder_dir),
]
if not args.no_builders_update:
run_command(cmd, wait=args.wait, ignore_exit_codes=(0, 11))
# Prepare github-action
github_action_cmd = [str(scripts_dir / "github-action.py")]
if args.signer_fpr:
github_action_cmd += ["--signer-fpr", args.signer_fpr]
else:
github_action_cmd += ["--no-signer-github-command-check"]
if args.local_log_file:
github_action_cmd += ["--local-log-file", args.local_log_file]
github_action_cmd += [
str(args.command).lower(),
str(builder_dir),
builder_conf,
]
if args.command == "Build-component":
assert component_name
github_action_cmd += [component_name]
elif args.command == "Upload-component":
assert (
component_name
and commit_sha
and repository_publish
and distribution_name
)
github_action_cmd += [
component_name,
commit_sha,
repository_publish,
]
if distribution_name == "all":
github_action_cmd += ["--distribution", "all"]
else:
for d in distribution_name.split(","):
github_action_cmd += ["--distribution", d]
elif args.command == "Build-template":
assert template_name and template_timestamp
github_action_cmd += [template_name, template_timestamp]
elif args.command == "Upload-template":
assert template_name and template_sha and repository_publish
github_action_cmd += [
template_name,
template_sha,
repository_publish,
]
elif args.command == "Build-iso":
assert iso_version and iso_timestamp
github_action_cmd += [iso_version, iso_timestamp]
cmd = [
"flock",
"-x",
str(builder_dir / "builder.lock"),
"bash",
"-c",
" ".join(github_action_cmd),
]
run_command(
cmd,
wait=args.wait,
env={
"PYTHONPATH": f"{builder_dir!s}:{os.environ.get('PYTHONPATH','')}",
**os.environ,
},
)
if __name__ == "__main__":
try:
main()
except Exception as e:
log.error(str(e))
sys.exit(1)