Git over SSH for Forgejo and Gitea (tcp_ports) (#27)
This commit is contained in:
1 parent
7b6851c023
commit
f3157b71f3
9 files changed
+72
-15
No files matched your search
@@ -38,6 +38,17 @@ env = {
|
||||
"HARBOR_HOSTNAME": "localhost",
|
||||
"HARBOR_PUBLIC_URL": f"http://127.0.0.1:{port}",
|
||||
}
|
||||
# public TCP ports (tcp_ports): servers assign one from a range and give the host that
|
||||
# reaches it; here each gets a free local port, published on 127.0.0.1
|
||||
tcp = []
|
||||
for t in x.get("tcp_ports") or []:
|
||||
with socket.socket() as s:
|
||||
s.bind(("127.0.0.1", 0))
|
||||
tcp.append((t["name"], t["service"], t["port"], s.getsockname()[1]))
|
||||
if tcp:
|
||||
env["HARBOR_TCP_HOST"] = "localhost"
|
||||
for name, _, _, pub in tcp:
|
||||
env[f"HARBOR_PORT_{name.upper()}"] = str(pub)
|
||||
for p in x.get("parameters") or []:
|
||||
if p.get("generate"):
|
||||
env[p["key"]] = GEN[p["generate"]]()
|
||||
@@ -56,12 +67,15 @@ with open(os.path.join(work, "env"), "w") as f:
|
||||
override = {"services": {}}
|
||||
for name, svc in cfg["services"].items():
|
||||
o = {"security_opt": ["no-new-privileges:true"]}
|
||||
ports = [f"127.0.0.1:{pub}:{cport}" for _, svc_name, cport, pub in tcp if svc_name == name]
|
||||
if name == primary["service"]:
|
||||
o["ports"] = [f"127.0.0.1:{port}:{primary['port']}"]
|
||||
ports.insert(0, f"127.0.0.1:{port}:{primary['port']}")
|
||||
if res.get("memory_mb"):
|
||||
o["mem_limit"] = f"{res['memory_mb']}m"
|
||||
if res.get("cpu_millis"):
|
||||
o["cpus"] = res["cpu_millis"] / 1000
|
||||
if ports:
|
||||
o["ports"] = ports
|
||||
mounts = []
|
||||
for m in svc.get("volumes") or []:
|
||||
if m.get("type") == "volume" and m.get("source") in owned:
|
||||
@@ -78,4 +92,5 @@ parts = []
|
||||
for name, o in owned.items():
|
||||
parts.append(f"{name}:{o.get('uid', 0)}:{o.get('gid', 0)}:{o.get('mode', '0755')}")
|
||||
with open(os.path.join(work, "plan.sh"), "w") as f:
|
||||
f.write(f"port={port}\nhealth={shlex.quote(health)}\nowned={shlex.quote(' '.join(parts))}\n")
|
||||
tcp_list = " ".join(f"{n}:{pub}" for n, _, _, pub in tcp)
|
||||
f.write(f"port={port}\nhealth={shlex.quote(health)}\nowned={shlex.quote(' '.join(parts))}\ntcp={shlex.quote(tcp_list)}\n")
|
||||
+15
-1
@@ -31,7 +31,8 @@ trap cleanup EXIT INT TERM
|
||||
|
||||
docker compose -f "$dir/template.yml" config --format json --no-interpolate > "$work/config.json"
|
||||
python3 -I "$here/local-test-plan.py" "$slug" "$work"
|
||||
# plan.sh sets: port, health, owned (space-separated "volume:uid:gid:mode")
|
||||
# plan.sh sets: port, health, owned (space-separated "volume:uid:gid:mode"),
|
||||
# tcp (space-separated "name:port" for tcp_ports)
|
||||
. "$work/plan.sh"
|
||||
|
||||
echo "$slug: starting on http://127.0.0.1:$port"
|
||||
@@ -55,5 +56,18 @@ if [ "$code" != 200 ]; then
|
||||
compose logs --tail 40 || true
|
||||
exit 1
|
||||
fi
|
||||
# each public TCP port must accept a connection; an "ssh" port must greet like an SSH server
|
||||
for t in $tcp; do
|
||||
name=${t%%:*}; tport=${t#*:}
|
||||
banner=$(python3 -I -c 'import socket,sys
|
||||
s=socket.create_connection(("127.0.0.1",int(sys.argv[1])),timeout=10); s.settimeout(10)
|
||||
print(s.recv(64).decode("ascii","replace").strip())' "$tport" 2>/dev/null || true)
|
||||
if [ "$name" = ssh ] && [ "${banner#SSH-}" = "$banner" ]; then
|
||||
echo "$slug: tcp port $name (127.0.0.1:$tport) didn't answer like an SSH server: '$banner'"
|
||||
compose logs --tail 40 || true
|
||||
exit 1
|
||||
fi
|
||||
echo "$slug: tcp port $name answered on 127.0.0.1:$tport ($banner)"
|
||||
done
|
||||
passed=1
|
||||
echo "$slug: ok ($url answered 200)"
|
||||
Reference in new issue
Block a user