Skip to content

Commit 772cd4e

Browse files
Updating cloud-init config files, base-image and examples
1 parent 011a2f5 commit 772cd4e

20 files changed

+161
-150
lines changed

base-image/Containerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ RUN dnf -y install cloud-init && \
55
rm -rf /var/{cache,log} /var/lib/{dnf,rhsm}
66
COPY usr/ /usr/
77

8-
COPY base_config.sh /usr/bin/
9-
COPY base_config.service /etc/systemd/system
8+
COPY pim_init.service /etc/systemd/system
109

11-
RUN systemctl unmask base_config.service
12-
RUN systemctl enable base_config.service
10+
RUN systemctl unmask pim_init.service
11+
RUN systemctl enable pim_init.service

base-image/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ There are three distros of bootc image available to use. Fedora, CentOS and RHEL
1515

1616
**Note:**
1717
- You need Red Hat account to get the credentials to pull the image.
18-
- Need to build the image on RHEL machine where subsciption activated.
18+
- Need to build the image on RHEL machine where subscription activated.
1919

2020

2121

@@ -28,11 +28,10 @@ COPY usr/ /usr/
2828
Install cloud-init to configure AI image and PIM partition's network and user
2929

3030
```Dockerfile
31-
COPY base_config.sh /usr/bin/
32-
COPY base_config.service /etc/systemd/system
31+
COPY pim_init.service /etc/systemd/system
3332

34-
RUN systemctl unmask base_config.service
35-
RUN systemctl enable base_config.service
33+
RUN systemctl unmask pim_init.service
34+
RUN systemctl enable pim_init.service
3635
```
3736
systemd service to setup pimconfig like copying cloud init config and pim config files to respective directory
3837

base-image/base_config.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

base-image/base_config.service renamed to base-image/pim_init.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Wants=network-online.target cloud-config.target
55

66
[Service]
77
Type=oneshot
8-
ExecStart=/usr/bin/env /bin/bash /usr/bin/base_config.sh
8+
ExecStart=/bin/true
99
RemainAfterExit=yes
1010
TimeoutSec=0
1111

cli/cloud-init-iso/templates/99_custom_network.cfg

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
ethernets:
3+
env{{ config["partition"]["network"]["slot_num"] }}:
4+
dhcp4: false
5+
addresses:
6+
- {{ config["partition"]["network"]["ip"]["address"] }}/{{ config["partition"]["network"]["ip"]["prefix-length"] }}
7+
gateway4: {{ config["partition"]["network"]["ip"]["gateway"] }}
8+
nameservers:
9+
addresses:
10+
- {{ config["partition"]["network"]["ip"]["nameserver"] }}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#cloud-config
2+
users:
3+
- name: {{ config["ssh"]["user-name"] }}
4+
sudo: ['ALL=(ALL) NOPASSWD:ALL']
5+
lock_passwd: true
6+
groups: sudo
7+
shell: /bin/bash
8+
ssh_authorized_keys:
9+
- {{ config["ssh"]["pub-key"] }}
10+
11+
write_files:
12+
- path: /etc/pim/pim_config.json
13+
content: |
14+
{{ config["ai"]["config-json"] }}
15+
owner: root:root
16+
permissions: '0644'
17+
append: false
18+
- path: /etc/pim/auth.json
19+
content: |
20+
{{ config["ai"]["auth-json"] }}
21+
owner: root:root
22+
permissions: '0644'
23+
append: false
24+
- path: /etc/pim/env.conf
25+
content: |
26+
REGISTRY_AUTH_FILE=/etc/pim/auth.json
27+
owner: root:root
28+
permissions: '0644'
29+
append: false

cli/cmd/update_config.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import shutil
2+
from scp import SCPClient
3+
import os
24

35
import cli.network.virtual_network as virtual_network
4-
import cli.partition.activation as activation
56
import cli.partition.partition as partition
67
import cli.utils.monitor_util as monitor_util
7-
import cli.storage.vopt_storage as vopt
88
import cli.utils.command_util as command_util
99
import cli.utils.common as common
1010
import cli.utils.iso_util as iso_util
1111
import cli.utils.string_util as util
12-
import cli.vios.vios as vios
1312

1413

1514
logger = common.get_logger("pim-update-config")
@@ -20,18 +19,18 @@ def update_config(config_file_path):
2019
logger.info("Updating PIM partition's config")
2120
config = common.initialize_config(config_file_path)
2221
# Invoking initialize_command to perform common actions like validation, authentication etc.
23-
is_config_valid, cookies, sys_uuid, vios_uuid_list = command_util.initialize_command(
22+
is_config_valid, cookies, sys_uuid, _ = command_util.initialize_command(
2423
config)
2524
if is_config_valid:
26-
_update_config(config, cookies, sys_uuid, vios_uuid_list)
25+
_update_config(config, cookies, sys_uuid)
2726
logger.info("PIM partition's config successfully updated")
2827
except Exception as e:
2928
logger.error(f"encountered an error: {e}")
3029
finally:
3130
if cookies:
3231
command_util.cleanup(config, cookies)
3332

34-
def _update_config(config, cookies, sys_uuid, vios_uuid_list):
33+
def _update_config(config, cookies, sys_uuid):
3534
try:
3635
logger.debug("Checking partition exists")
3736
exists, _, partition_uuid = partition.check_partition_exists(config, cookies, sys_uuid)
@@ -60,34 +59,45 @@ def _update_config(config, cookies, sys_uuid, vios_uuid_list):
6059
shutil.rmtree(common.cloud_init_update_config_dir)
6160
return
6261
logger.info("Detected config change, updating")
63-
iso_util.generate_cloud_init_iso_file(common.update_iso_dir, config, common.cloud_init_update_config_dir)
6462

65-
logger.info("Shutting down the partition")
66-
activation.shutdown_partition(config, cookies, partition_uuid)
67-
logger.info("Partition shut down to attach the new config")
63+
# Create pim_config.json file
64+
pim_config = util.get_pim_config_json(config)
65+
with open(f"{common.cloud_init_update_config_dir}/pim_config.json", "w") as config_file:
66+
config_file.write(pim_config)
6867

69-
cloud_init_iso = util.get_cloud_init_iso(config)
70-
logger.info("Uploading the new cloud init with the config changes")
71-
vios_cloudinit_media_uuid = iso_util.upload_iso_to_media_repository(config, cookies, common.update_iso_dir, cloud_init_iso, sys_uuid, vios_uuid_list)
72-
logger.debug("Cloud init uploaded")
73-
74-
logger.info("Attaching the cloud init to the partition")
75-
vios_payload = vios.get_vios_details(config, cookies, sys_uuid, vios_cloudinit_media_uuid)
76-
vopt.attach_vopt(vios_payload, config, cookies, partition_uuid, sys_uuid, vios_cloudinit_media_uuid, cloud_init_iso)
77-
logger.info("New cloud init config attached to the partition.")
68+
ssh_client = common.ssh_to_partition(config)
7869

79-
logger.info("Activating the partition")
80-
activation.activate_partition(config, cookies, partition_uuid)
81-
logger.info("Partition activated")
70+
with SCPClient(ssh_client.get_transport()) as scp:
71+
scp.put(f'{common.cloud_init_update_config_dir}/pim_config.json', '/tmp')
72+
73+
move_cmd = "sudo mv /tmp/pim_config.json /etc/pim/"
74+
_, stdout, stderr = ssh_client.exec_command(move_cmd)
75+
exit_status = stdout.channel.recv_exit_status()
76+
if exit_status == 0:
77+
logger.info("Successfully updated the config of the partition.")
78+
else:
79+
errorMsg = stderr.read().decode('utf-8')
80+
logger.error(f"failed to update config of the partition. error: {errorMsg}")
81+
raise Exception(errorMsg)
8282

83-
logger.info("Monitoring boot process, this will take a while")
84-
monitor_util.monitor_pim(config)
83+
84+
# Restart pim_init.service
85+
restart_command = "sudo systemctl restart pim_init.service"
86+
_, stdout, stderr = ssh_client.exec_command(restart_command)
87+
exit_status = stdout.channel.recv_exit_status()
8588

86-
# Move used cloud init iso to iso dir
87-
shutil.move(f"{common.update_iso_dir}/{cloud_init_iso}", f"{common.iso_dir}/{cloud_init_iso}")
89+
if exit_status == 0:
90+
logger.info("Successfully restarted pim_init.service")
91+
else:
92+
errorMsg = stderr.read().decode('utf-8')
93+
logger.error(f"failed to restart pim_init.service. error: {errorMsg}")
94+
raise Exception(errorMsg)
8895

96+
os.remove(f"{common.cloud_init_update_config_dir}/pim_config.json")
8997
# Cleanup existing config and move updated config
9098
shutil.rmtree(common.cloud_init_config_dir)
9199
shutil.move(common.cloud_init_update_config_dir, common.cloud_init_config_dir)
100+
logger.info("Monitoring AI application, this will take a while")
101+
monitor_util.monitor_pim(config)
92102
except Exception as e:
93103
raise e

cli/utils/iso_util.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,37 @@ def generate_cloud_init_iso_config(config, slot_num, config_dir):
2828
file_loader = FileSystemLoader(f'{common.getclidir()}/cloud-init-iso/templates')
2929
env = Environment(loader=file_loader)
3030

31-
network_config_template = env.get_template('99_custom_network.cfg')
31+
network_config_template = env.get_template('network-config')
3232
network_config_output = network_config_template.render(config=config)
3333

34-
common.create_dir(config_dir)
35-
3634
pim_config_json = config["ai"]["config-json"] if config["ai"]["config-json"] != "" else "{}"
3735
pim_config_json = json.loads(pim_config_json)
3836

3937
# 'workloadImage' is being used inside the bootstrap iso to write the bootc image into disk, in case of modification of this field name, needs same modification in bootstrap.iso too.
4038
pim_config_json["workloadImage"] = get_workload_image(config)
39+
config["ai"]["config-json"] = json.dumps(pim_config_json, separators=(',', ':'))
40+
41+
auth_json = config["ai"]["auth-json"]
42+
if auth_json == "":
43+
auth_json = "{}"
44+
else:
45+
auth_data = json.loads(auth_json)
46+
auth_json = json.dumps(auth_data, separators=(',', ':'))
47+
config["ai"]["auth-json"] = auth_json
4148

42-
pim_config_file = open(config_dir + "/pim_config.json", "w")
43-
pim_config_file.write(json.dumps(pim_config_json))
49+
user_data_template = env.get_template('user-data')
50+
user_data_output = user_data_template.render(config=config)
51+
52+
common.create_dir(config_dir)
4453

45-
network_config_file = open(
46-
config_dir + "/99_custom_network.cfg", "w")
54+
network_config_file = open(config_dir + "/network-config", "w")
4755
network_config_file.write(network_config_output)
56+
57+
user_data_file = open(config_dir + "/user-data", "w")
58+
user_data_file.write(user_data_output)
4859

49-
auth_json = "{}" if config["ai"]["auth-json"] == "" else config["ai"]["auth-json"]
50-
auth_config_file = open(config_dir + "/auth.json", "w")
51-
auth_config_file.write(auth_json)
60+
open(config_dir+"/meta-data", "w")
61+
5262
logger.debug("Generated config files for the cloud-init ISO")
5363

5464

@@ -57,7 +67,7 @@ def generate_cloud_init_iso_file(iso_dir, config, config_dir):
5767
common.create_dir(iso_dir)
5868

5969
cloud_init_image_name = get_cloud_init_iso(config)
60-
generate_cmd = f"mkisofs -l -o {iso_dir}/{cloud_init_image_name} {config_dir}"
70+
generate_cmd = f"mkisofs -l -volid cidata -joliet -o {iso_dir}/{cloud_init_image_name} -rock {config_dir}"
6171

6272
try:
6373
subprocess.run(generate_cmd.split(), check=True, capture_output=True)

cli/utils/monitor_util.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,7 @@
99
logger = common.get_logger("monitor")
1010

1111

12-
def monitor_pim_boot(config):
13-
# Re-run scenario: If lpar is in 2nd boot stage, check base_config service logs
14-
logger.debug("PIM boot: Checking base_config.service")
15-
try:
16-
ssh_client = common.ssh_to_partition(config)
17-
18-
base_config_svc_exists = "ls /etc/systemd/system/base_config.service"
19-
_, stdout, _ = ssh_client.exec_command(
20-
base_config_svc_exists, get_pty=True)
21-
if stdout.channel.recv_exit_status() == 0:
22-
logger.debug("base_config.service exists")
23-
24-
logger.debug("Checking base_config.service logs")
25-
base_cfg_svc_cmd = "sudo journalctl -u base_config.service -f 2>&1 | awk '{print} /base_config.sh run successfully/ {print \"Match found: \" $0; exit 0}'"
26-
_, stdout, _ = ssh_client.exec_command(
27-
base_cfg_svc_cmd, get_pty=True)
28-
while True:
29-
out = stdout.readline()
30-
logger.debug(out)
31-
if stdout.channel.exit_status_ready():
32-
if stdout.channel.recv_exit_status() == 0:
33-
logger.debug(
34-
"Found 'base_config.sh run successfully' message")
35-
ssh_client.close()
36-
return
37-
if "base_config.service: Failed with result 'exit-code'" in out:
38-
ssh_client.close()
39-
logger.error(f"failed to start AI application. error: {out}")
40-
raise Exception(f"failed to start AI application. error: {out}")
41-
else:
42-
ssh_client.close()
43-
logger.error(
44-
"failed to find '/etc/systemd/system/base_config.service', please check console for more possible errors")
45-
raise Exception(
46-
"failed to find '/etc/systemd/system/base_config.service', please check console for more possible errors")
47-
except Exception as e:
48-
logger.error(f"failed to monitor PIM boot, error: {e}")
49-
raise Exception(f"failed to monitor PIM boot, error: {e}")
50-
51-
5212
def monitor_pim(config):
53-
monitor_pim_boot(config)
5413
logger.info("Partition booted with PIM image")
5514

5615
# No need to validate the AI application deployed via PIM flow if 'ai.validation.request' set to no, can complete the workflow
@@ -113,7 +72,7 @@ def monitor_bootstrap_boot(config):
11372
raise Exception(f"failed to detect bootc based PIM AI image install completion signature. error: {out}")
11473
else:
11574
logger.debug(
116-
"Could not find 'getcontainer.service', will look for 'base_config.service' in PIM boot since it could be a re-run and bootstrap might have already finished")
75+
"Could not find 'getcontainer.service' in PIM boot since it could be a re-run and bootstrap might have already finished")
11776
ssh_client.close()
11877
except Exception as e:
11978
logger.error(f"failed to monitor bootstrap boot, error: {e}")

0 commit comments

Comments
 (0)