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
|
#!/usr/bin/python3
# SPDX-License-Identifier: MIT
"""mdbp backend wrapper via ssh"""
import argparse
import contextlib
import io
import json
import pathlib
import random
import re
import subprocess
import sys
import tarfile
import typing
import urllib.parse
from .common import JsonObject, buildjson, get_dsc_files, tar_add
class RepoForward:
def __init__(self):
self.forwards = {}
def get_forward(self, destination: str) -> str:
try:
return self.forwards[destination]
except KeyError:
forward = "localhost:%d" % random.randrange(1024, 65536)
self.forwards[destination] = forward
return forward
def proxy(self, repoline: str) -> str:
"""Transform an extra repository line and to pass it through a ssh
forward."""
match = re.match(
r"^(deb(?:-src)?(?:\s+\[[^]]*\])?)\s+(\S+)\s+(.*)$", repoline
)
if not match:
raise ValueError(
"failed to understand repository specification %r" % repoline
)
head, url, tail = match.groups()
spliturl = urllib.parse.urlsplit(url)
if spliturl.scheme != "http":
raise ValueError("cannot proxy url of scheme %r" % spliturl.scheme)
netloc = self.get_forward(
"%s:%d" % (spliturl.hostname, spliturl.port or 80)
)
if spliturl.username:
netloc = "@" + netloc
if spliturl.password:
netloc = ":" + spliturl.password + netloc
netloc = spliturl.username + netloc
url = urllib.parse.urlunsplit((spliturl.scheme, netloc) + spliturl[2:])
return " ".join((head, url, tail))
def ssh_options(self) -> typing.Iterable[str]:
for dest, proxy in self.forwards.items():
yield "-R"
yield "%s:%s" % (proxy, dest)
def produce_request_tar(buildjsonobj: JsonObject,
fileobj: typing.IO[bytes]) -> None:
"""Write a tar file suitable for mdbp-streamapi into the given `fileobj`
based on the given `buildjsonobj`.
* An .output.directory is discarded.
* A referenced .dsc file and its components is included.
"""
sendjsonobj = buildjsonobj.copy()
sendjsonobj["output"] = sendjsonobj["output"].copy()
del sendjsonobj["output"]["directory"]
dscpath: typing.Optional[pathlib.Path]
try:
dscpath = pathlib.Path(buildjsonobj["input"]["source_package_path"])
except KeyError:
dscpath = None
else:
sendjsonobj["input"] = sendjsonobj["input"].copy()
sendjsonobj["input"]["source_package_path"] = dscpath.name
with tarfile.open(mode="w|", fileobj=fileobj) as tar:
info = tarfile.TarInfo("build.json")
sendjsonfile = io.BytesIO()
for chunk in json.JSONEncoder().iterencode(sendjsonobj):
sendjsonfile.write(chunk.encode("utf8"))
info.size = sendjsonfile.tell()
sendjsonfile.seek(0)
tar.addfile(info, sendjsonfile)
if dscpath:
for path in [dscpath] + get_dsc_files(dscpath):
tar_add(tar, path)
def main() -> None:
"""Entry point for mdbp-ssh backend"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--proxyrepos",
default=False,
action="store_true",
help="proxy http repositories over the ssh connection",
)
parser.add_argument("host", type=str)
parser.add_argument("command", nargs=argparse.REMAINDER)
args = parser.parse_args()
if len(args.command) < 2:
parser.error("missing command or json file")
build = buildjson(args.command.pop())
cmd = ["ssh"]
if args.proxyrepos and "extrarepositories" in build:
repoforward = RepoForward()
build["extrarepositories"] = list(
map(repoforward.proxy, build["extrarepositories"])
)
cmd.extend(repoforward.ssh_options())
cmd.extend([args.host, "mdbp-streamapi", *args.command])
with contextlib.ExitStack() as stack:
proc = stack.enter_context(
subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=sys.stdout if build["output"].get("log", True)
else subprocess.DEVNULL
),
)
assert proc.stdin is not None
produce_request_tar(build, proc.stdin)
proc.stdin.close()
exitcode = 0
try:
outtar = stack.enter_context(tarfile.open(fileobj=proc.stdout,
mode="r|"))
except tarfile.ReadError as err:
if str(err) != "empty file":
raise
exitcode = 1
else:
for member in outtar:
if "/" in member.name or not member.isfile():
raise ValueError("expected flat tar as output")
outtar.extract(member, build["output"]["directory"],
set_attrs=False)
sys.exit(proc.wait() or exitcode)
if __name__ == "__main__":
main()
|