forked from bn222/cluster-deployment-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reglocal.py
134 lines (109 loc) · 4.13 KB
/
reglocal.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
import json
import shlex
import os
import host
import k8sClient
CONTAINER_NAME = "local-container-registry"
def _dir_name(rsh: host.Host) -> str:
return rsh.home_dir(".local-container-registry")
def _hostname(rsh: host.Host) -> str:
ret = rsh.run("hostname -f")
h = ret.out.strip()
if not ret.success() or not h:
raise RuntimeError("Failure to get hostname")
return h
def ensure_running(rsh: host.Host, *, delete_all: bool = False, listen_port: int = 5000) -> tuple[str, str, int, str]:
dir_name = _dir_name(rsh)
hostname = _hostname(rsh)
ret = rsh.run(shlex.join(["podman", "inspect", CONTAINER_NAME, "--format", "{{.Id}}"]))
if ret.success() and rsh.run(shlex.join(['test', '-d', dir_name])).success():
if not delete_all:
return dir_name, hostname, listen_port, ret.out.strip()
_delete_all(rsh, dir_name)
dir_name_certs = os.path.join(dir_name, "certs")
dir_name_data = os.path.join(dir_name, "data")
dir_name_auth = os.path.join(dir_name, "auth")
rsh.run(shlex.join(["mkdir", "-p", dir_name]))
rsh.run(shlex.join(["mkdir", "-p", dir_name_certs]))
rsh.run(shlex.join(["mkdir", "-p", dir_name_data]))
rsh.run(shlex.join(["mkdir", "-p", dir_name_auth]))
rsh.run_or_die(
shlex.join(
[
"openssl",
"req",
"-newkey",
"rsa:4096",
"-nodes",
"-sha256",
"-keyout",
os.path.join(dir_name_certs, "domain.key"),
"-x509",
"-days",
"365",
"-out",
os.path.join(dir_name_certs, "domain.crt"),
"-subj",
f"/CN={hostname}",
"-addext",
f"subjectAltName = DNS:{hostname}",
]
)
)
# We need both domain.crt and domain.cert for `podman push --cert-dir` to work.
rsh.run(shlex.join(["ln", "-snf", "domain.crt", os.path.join(dir_name_certs, "domain.cert")]))
ret = rsh.run_or_die(
shlex.join(
[
"podman",
"run",
"--name",
CONTAINER_NAME,
"-p",
f"{listen_port}:5000",
"-v",
f"{dir_name_data}:/var/lib/registry:z",
"-v",
f"{dir_name_auth}:/auth:z",
"-v",
f"{dir_name_certs}:/certs:z",
"-e",
"REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt",
"-e",
"REGISTRY_HTTP_TLS_KEY=/certs/domain.key",
"-e",
"REGISTRY_COMPATIBILITY_SCHEMA1_ENABLED=true",
f"--annotation=LOCAL_CONTAINER_REGISTRY_HOSTNAME={hostname}",
"-d",
"docker.io/library/registry:latest",
]
)
)
return dir_name, hostname, listen_port, ret.out.strip()
def _delete_all(rsh: host.Host, dir_name: str) -> None:
rsh.run(shlex.join(["podman", "rm", "-f", CONTAINER_NAME]))
rsh.run(shlex.join(["rm", "-rf", dir_name]))
def delete_all(rsh: host.Host) -> None:
_delete_all(rsh, _dir_name(rsh))
def ocp_trust(client: k8sClient.K8sClient, dir_name: str, hostname: str, listen_port: int) -> None:
cm_name = f"local-container-registry-{hostname}"
crt_file = os.path.join(dir_name, 'certs/domain.crt')
if not os.path.isfile(crt_file):
# This function can only operate locally, like K8sClient. The file must
# exist.
raise RuntimeError(f"Certificate file {crt_file} does not exist!")
client.oc(f"delete configmap -n openshift-config {shlex.quote(cm_name)}")
client.oc(
shlex.join(
[
"create",
"configmap",
"-n",
"openshift-config",
cm_name,
f"--from-file={hostname}..{listen_port}={crt_file}",
]
),
)
data = {"spec": {"additionalTrustedCA": {"name": cm_name}}}
client.oc(f"patch image.config.openshift.io/cluster --patch {shlex.quote(json.dumps(data))} --type=merge")