-
Notifications
You must be signed in to change notification settings - Fork 157
/
utils.py
299 lines (258 loc) · 8.51 KB
/
utils.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
# Copyright 2015 Canonical, Ltd.
#
# 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, version 3.
#
# 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 <http://www.gnu.org/licenses/>.
import asyncio
import crypt
import logging
import os
import random
import subprocess
import tempfile
from typing import Any, Dict, List, Sequence
log = logging.getLogger("subiquitycore.utils")
def _clean_env(env, *, locale=True):
if env is None:
env = os.environ.copy()
else:
env = env.copy()
if locale:
env["LC_ALL"] = "C"
# Maaaybe want to remove SNAP here too.
return env
def system_scripts_env() -> dict[str, str]:
"""Generate an environment for running all programs outside of the snap,
but also include those vendored in $SNAP/system_scripts.
"""
env: dict[str, str] = orig_environ(os.environ)
snap_path: str | None = env.get("SNAP")
assert snap_path is not None, "Could not find path to SNAP"
server_bin = f"{snap_path}/system_scripts"
desktop_bin = f"{snap_path}/bin/subiquity/system_scripts"
env["PATH"] = os.pathsep.join(
[
server_bin,
desktop_bin,
env["PATH"],
]
)
return env
def orig_environ(env):
"""Generate an environment dict that is suitable for use for running
programs that live outside the snap."""
if env is None:
env = os.environ
ret = env.copy()
for key, val in env.items():
if key.endswith("_ORIG"):
key_to_restore = key[: -len("_ORIG")]
if val:
ret[key_to_restore] = val
else:
del ret[key_to_restore]
del ret[key]
ret.pop("LD_LIBRARY_PATH", None)
return ret
def run_command(
cmd: Sequence[str],
*,
input=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
errors="replace",
env=None,
clean_locale=True,
**kw,
) -> subprocess.CompletedProcess:
"""A wrapper around subprocess.run with logging and different defaults.
We never ever want a subprocess to inherit our file descriptors!
"""
if input is None:
kw["stdin"] = subprocess.DEVNULL
else:
input = input.encode(encoding)
log.debug("run_command called: %s", cmd)
try:
cp = subprocess.run(
cmd,
input=input,
stdout=stdout,
stderr=stderr,
env=_clean_env(env, locale=clean_locale),
**kw,
)
if encoding:
if isinstance(cp.stdout, bytes):
cp.stdout = cp.stdout.decode(encoding)
if isinstance(cp.stderr, bytes):
cp.stderr = cp.stderr.decode(encoding)
except subprocess.CalledProcessError as e:
log.debug("run_command %s", str(e))
raise
else:
log.debug("run_command %s exited with code %s", cp.args, cp.returncode)
return cp
async def arun_command(
cmd: Sequence[str],
*,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
input=None,
errors="replace",
env=None,
clean_locale=True,
check=False,
**kw,
) -> subprocess.CompletedProcess:
if input is None:
if "stdin" not in kw:
kw["stdin"] = subprocess.DEVNULL
else:
kw["stdin"] = subprocess.PIPE
input = input.encode(encoding)
log.debug("arun_command called: %s", cmd)
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=stdout,
stderr=stderr,
env=_clean_env(env, locale=clean_locale),
**kw,
)
stdout, stderr = await proc.communicate(input=input)
if encoding:
if stdout is not None:
stdout = stdout.decode(encoding)
if stderr is not None:
stderr = stderr.decode(encoding)
log.debug("arun_command %s exited with code %s", cmd, proc.returncode)
# .communicate() forces returncode to be set to a value
assert proc.returncode is not None
if check and proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, cmd, stdout, stderr)
else:
return subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
async def astart_command(
cmd: Sequence[str],
*,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
env=None,
clean_locale=True,
**kw,
) -> asyncio.subprocess.Process:
log.debug("astart_command called: %s", cmd)
return await asyncio.create_subprocess_exec(
*cmd,
stdout=stdout,
stderr=stderr,
stdin=stdin,
env=_clean_env(env, locale=clean_locale),
**kw,
)
async def split_cmd_output(cmd: Sequence[str], split_on: str) -> List[str]:
cp = await arun_command(cmd, check=True)
return cp.stdout.split(split_on)
def start_command(
cmd: Sequence[str],
*,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
errors="replace",
env=None,
clean_locale=True,
**kw,
) -> subprocess.Popen:
"""A wrapper around subprocess.Popen with logging and different defaults.
We never ever want a subprocess to inherit our file descriptors!
"""
log.debug("start_command called: %s", cmd)
return subprocess.Popen(
cmd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
env=_clean_env(env, locale=clean_locale),
**kw,
)
def _log_stream(level: int, stream, name: str):
if stream:
log.log(level, f"{name}: ------------------------------------------")
for line in stream.splitlines():
log.log(level, line)
elif stream is None:
log.log(level, f"<{name} is None>")
else:
log.log(level, f"<{name} is empty>")
def log_process_streams(
level: int, cpe: subprocess.CalledProcessError, command_msg: str
):
log.log(level, f"{command_msg} exited with result: {cpe.returncode}")
_log_stream(level, cpe.stdout, "stdout")
_log_stream(level, cpe.stderr, "stderr")
log.log(level, "--------------------------------------------------")
# FIXME: replace with passlib and update package deps
def crypt_password(passwd, algo="SHA-512"):
# encryption algo - id pairs for crypt()
algos = {"SHA-512": "$6$", "SHA-256": "$5$", "MD5": "$1$", "DES": ""}
if algo not in algos:
raise Exception(
"Invalid algo({}), must be one of: {}. ".format(
algo, ",".join(algos.keys())
)
)
salt_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
salt = 16 * " "
salt = "".join([random.choice(salt_set) for c in salt])
return crypt.crypt(passwd, algos[algo] + salt)
def disable_subiquity():
"""Stop subiquity service; which also restores getty service"""
log.info("disabling subiquity service")
run_command(["mkdir", "-p", "/run/subiquity"])
run_command(["touch", "/run/subiquity/complete"])
run_command(["systemctl", "start", "--no-block", "[email protected]"])
run_command(
[
"systemctl",
"stop",
"--no-block",
"snap.subiquity.subiquity-service.service",
"serial-subiquity@*.service",
]
)
return
def matching_dicts(items: Sequence[Dict[Any, Any]], **criteria):
"""Given an input sequence of dictionaries, return a list of dicts where
the supplied keyword arguments all match those items."""
return [
item
for item in items
if all(k in item and item[k] == v for k, v in criteria.items())
]
def _zsys_uuid_charset() -> list:
charset = [chr(c) for c in range(ord("0"), ord("9") + 1)]
charset += [chr(c) for c in range(ord("a"), ord("z") + 1)]
# random.choice wants a list
return charset
def gen_zsys_uuid():
"""Create a 6 character identifier. Functionally equivalent to
`head -100 /dev/urandom | tr -dc 'a-z0-9' | head -c6`"""
return "".join([random.choice(_zsys_uuid_charset()) for i in range(6)])
def write_named_tempfile(prefix, contents):
f = tempfile.NamedTemporaryFile(prefix=prefix, mode="w", delete=False)
with f:
f.write(contents)
return f.name