forked from bn222/cluster-deployment-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microshift.py
239 lines (182 loc) · 7.61 KB
/
microshift.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
import time
import host
import sys
from host import Host
from nfs import NFS
from logger import logger
import os
import shutil
from typing import Optional
from jinja2 import Template
from clustersConfig import NodeConfig
import common
from host import BMC
# cleans microshift artifacts from previous installation
def cleanup_microshift(h: host.Host, version: str) -> None:
files = ["rhocp.toml", "fast-datapath.toml", "kickstart.ks", "*.tar", "*.iso"]
for file in files:
if os.path.exists(f"./{file}"):
os.remove(f"./{file}")
cmd = "podman rm -f minimal-microshift-server"
output = h.run(cmd)
logger.info(output.out)
cleanup_compose_cli(h)
cleanup_blueprints(h)
cleanup_sources(h, version)
cleanup_cache_path = "/var/cache/osbuild-worker/osbuild-store/stage/"
if os.path.exists(cleanup_cache_path):
shutil.rmtree(cleanup_cache_path)
# cleanup blueprints
def cleanup_blueprints(h: host.Host) -> None:
composer_cli_cmd("blueprints delete microshift-installer", h)
composer_cli_cmd("blueprints delete minimal-microshift", h)
def cleanup_sources(h: host.Host, version: str) -> None:
composer_cli_cmd("sources delete fast-datapath", h)
composer_cli_cmd(f"sources delete rhocp-{version}", h)
# cleans composes generated by composer cli
def cleanup_compose_cli(h: host.Host) -> None:
old_compose = h.run("composer-cli compose status").out
old_compose_splitlines = old_compose.splitlines()
for line in old_compose_splitlines[1:]:
compose_id = line.split(" ")[0]
if 'FAILED' or 'FINISHED' not in line:
cmd = f" composer-cli compose cancel {compose_id}"
output = h.run(cmd)
logger.info(output.out)
cmd = f"composer-cli compose delete {compose_id}"
output = h.run(cmd)
logger.info(output.out)
# uses jinja to generate a kickstart file
def generate_kickstart(rhel_number: str, uname_m: str) -> None:
with open('pull_secret.json', 'r') as f_in:
file_contents = f_in.read()
ssh_pub, _, _ = next(common.iterate_ssh_keys(), (None, None, None))
if ssh_pub is not None:
with open(ssh_pub, 'r') as ssh_in:
ssh_contents = ssh_in.read()
else:
logger.error("No ssh keys found in generate_kickstart")
sys.exit(-1)
with open('kickstart.ks.j2', 'r') as f:
lines = f.read()
with open('kickstart.ks', 'w') as f_out:
template = Template(lines)
f_out.write(template.render(rhel_number=rhel_number, uname_m=uname_m, pull_secret=file_contents, ssh_key=ssh_contents))
# generates non static toml files
def generate_toml_file(content: str, file_name: str, h: host.Host) -> None:
with open(file_name, 'w') as file:
file.write(content)
# generates final iso
def generate_final_iso(file: str, name_of_final_iso: str, h: host.Host) -> None:
cmd = f"sudo mkksiso kickstart.ks {file} {name_of_final_iso}"
output = h.run(cmd)
logger.info(output.out)
logger.info("Microshift iso is ready in final.iso")
def change_permissions_to_read_for_all(file: str) -> None:
uid = os.getuid()
gid = os.getgid()
os.chown(file, uid, gid)
os.chmod(file, 444)
def vgrename(h: host.Host) -> None:
vgname_command = "vgs --noheadings -o vg_name"
vgname_output = h.run(vgname_command)
vgname = vgname_output.out.split(" ")[0]
cmd = f"sudo vgrename {vgname} rhel"
output = h.run(cmd)
logger.info(output.out)
# runs various composer-cli commands
def composer_cli_cmd(subcommand: str, h: host.Host, file_name: Optional[str] = None) -> str:
if file_name is None:
cmd = f"sudo composer-cli {subcommand}"
else:
cmd = f"sudo composer-cli {subcommand} {file_name}"
output = h.run(cmd)
logger.info(output.out)
return output.out
def wait_for_build_to_finish(h: host.Host, build_id_two: Optional[str] = None) -> None:
while True:
status_output = composer_cli_cmd("compose status", h)
if build_id_two:
build_status_line = next((line for line in status_output.split('\n') if build_id_two in line), None)
if build_status_line and 'FINISHED' in build_status_line:
break
else:
if 'FINISHED' in status_output:
break
logger.info("waiting on build to finish")
time.sleep(5)
# generates an iso file that builds microshift
def iso_builder(h: host.Host, name_of_final_iso: str, version: str) -> None:
rhel_number = '9'
cmd = "uname -m"
uname_m = h.run(cmd).out.strip()
cleanup_microshift(h, version)
generate_kickstart(rhel_number, uname_m)
rhel_version = 'rhel9'
rhel_name = 'RHEL 9'
content_rhocp = f'''
id = "rhocp-{version}"
name = "Red Hat OpenShift Container Platform {version} for {rhel_name}"
type = "yum-baseurl"
url = "https://cdn.redhat.com/content/dist/layered/{rhel_version}/{uname_m}/rhocp/{version}/os"
check_gpg = true
check_ssl = true
system = false
rhsm = true'''.strip()
rhocp_file = "rhocp.toml"
generate_toml_file(content_rhocp, rhocp_file, h)
content_fdp = f'''
id = "fast-datapath"
name = "Fast Datapath for {rhel_name}"
type = "yum-baseurl"
url = "https://cdn.redhat.com/content/dist/layered/{rhel_version}/{uname_m}/fast-datapath/os"
check_gpg = true
check_ssl = true
system = false
rhsm = true'''.strip()
fdp_file = "fast-datapath.toml"
generate_toml_file(content_fdp, fdp_file, h)
composer_cli_cmd("sources add", h, rhocp_file)
composer_cli_cmd("sources add", h, fdp_file)
composer_cli_cmd("blueprints push", h, "minimal-microshift.toml")
build_id_result = composer_cli_cmd(f"compose start-ostree --ref 'rhel/9/{uname_m}/edge' minimal-microshift edge-container", h)
build_id = build_id_result.split(" ")[1]
logger.info(build_id)
wait_for_build_to_finish(h)
composer_cli_cmd(f"compose image {build_id}", h)
change_permissions_to_read_for_all(f"{build_id}-container.tar")
image_id_command = f"sudo podman load -i ./{build_id}-container.tar"
result = h.run(image_id_command)
image_id = result.out.split(':')[-1].strip()
cmd = f"sudo podman run -d --name=minimal-microshift-server -p 8085:8080 {image_id}"
result = h.run(cmd)
composer_cli_cmd("blueprints push", h, "microshift-installer.toml")
url = "http://127.0.0.1:8085/repo/"
ref = f"rhel/{rhel_number}/{uname_m}/edge"
build_command_two = composer_cli_cmd(f"compose start-ostree --url {url} --ref \"{ref}\" microshift-installer edge-installer", h)
build_id_two = build_command_two.split(" ")[1]
time.sleep(10)
wait_for_build_to_finish(h, build_id_two)
composer_cli_cmd(f"compose image {build_id_two}", h)
change_permissions_to_read_for_all(f"{build_id_two}-installer.iso")
vgrename(h)
generate_final_iso(f"{build_id_two}-installer.iso", name_of_final_iso, h)
shutil.rmtree("/var/cache/osbuild-worker/osbuild-store/stage/")
def deploy(cluster_name: str, node: NodeConfig, external_port: str, version: str) -> None:
lh = host.LocalHost()
bmc = BMC.from_bmc(node.bmc, node.bmc_user, node.bmc_password)
h = Host(node.node, bmc)
name_of_final_iso = os.path.join(os.getcwd(), 'final.iso')
login_uname = "redhat"
iso_builder(lh, name_of_final_iso, version)
if os.path.exists(f"{name_of_final_iso}"):
logger.info(f"The file {name_of_final_iso} exists.")
else:
logger.error(f"The file {name_of_final_iso} does not exist.")
sys.exit(-1)
logger.info("Microshift iso inserted")
nfs = NFS(lh, external_port)
iso = nfs.host_file(name_of_final_iso)
h.boot_iso_redfish(iso)
h.ssh_connect(login_uname)
logger.info("Microshift finished booting")