Skip to content

Commit 58d93bd

Browse files
committed
Bug fix on destroy flow and logging improvments
1 parent 011a2f5 commit 58d93bd

File tree

20 files changed

+210
-272
lines changed

20 files changed

+210
-272
lines changed

cli/auth/auth.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,32 @@ def populate_payload(config):
2525
'''
2626

2727
def authenticate_hmc(config):
28-
# Populate Authentication payload
29-
payload = populate_payload(config)
30-
url = "https://" + util.get_host_address(config) + URI
31-
headers = {"Content-Type": CONTENT_TYPE, "Accept": ACCEPT}
32-
response = requests.put(url, headers=headers, data=payload, verify=False)
33-
if response.status_code != 200:
34-
logger.error(f"failed to authenticate HMC, error: {response.text}")
35-
raise AuthError(f"failed to authenticate HMC, error: {response.text}")
36-
37-
soup = BeautifulSoup(response.text, 'xml')
38-
session_key = soup.find("X-API-Session")
28+
try:
29+
# Populate Authentication payload
30+
payload = populate_payload(config)
31+
url = "https://" + util.get_host_address(config) + URI
32+
headers = {"Content-Type": CONTENT_TYPE, "Accept": ACCEPT}
33+
response = requests.put(url, headers=headers, data=payload, verify=False)
34+
response.raise_for_status()
35+
36+
soup = BeautifulSoup(response.text, 'xml')
37+
session_key = soup.find("X-API-Session")
38+
except requests.exceptions.RequestException as e:
39+
raise AuthError(f"failed to authenticate HMC while making http request, error: {e}, response: {e.response.text}")
40+
except Exception as e:
41+
raise AuthError(f"failed to authenticate HMC, error: {e}")
3942
return session_key.text, response.cookies
4043

4144
def delete_session(config, cookies):
42-
url = "https://" + util.get_host_address(config) + URI
43-
headers = {"x-api-key": util.get_session_key(config)}
44-
response = requests.delete(url, cookies=cookies, headers=headers, verify=False)
45-
if response.status_code != 204:
46-
logger.error(f"failed to delete session on HMC, error: {response.text}")
47-
raise AuthError(f"failed to delete session on HMC, error: {response.text}")
48-
logger.debug("Logged off HMC session successfully")
45+
try:
46+
url = "https://" + util.get_host_address(config) + URI
47+
headers = {"x-api-key": util.get_session_key(config)}
48+
response = requests.delete(url, cookies=cookies, headers=headers, verify=False)
49+
response.raise_for_status()
50+
51+
logger.debug("Logged off HMC session successfully")
52+
except requests.exceptions.RequestException as e:
53+
raise AuthError(f"failed to delete session on HMC while making http request, error: {e}, response: {e.response.text}")
54+
except Exception as e:
55+
raise AuthError(f"failed to delete session on HMC, error: {e}")
4956
return

cli/cmd/destroy.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@ def destroy(config_file_path):
1616
# Invoking initialize_command to perform common actions like validation, authentication etc.
1717
is_config_valid, cookies, sys_uuid, vios_uuid_list = command_util.initialize_command(config)
1818
if is_config_valid:
19-
_destroy(config, cookies, sys_uuid, vios_uuid_list)
20-
logger.info("PIM partition successfully destroyed")
19+
destroyed = _destroy(config, cookies, sys_uuid, vios_uuid_list)
20+
if destroyed:
21+
logger.info("PIM partition successfully destroyed")
2122
except (Exception) as e:
2223
logger.error(f"encountered an error: {e}")
2324
finally:
2425
if cookies:
2526
command_util.cleanup(config, cookies)
2627

2728
def _destroy(config, cookies, sys_uuid, vios_uuid_list):
29+
destroyed = False
2830
try:
2931
exists, created_by_pim, partition_uuid = partition.check_partition_exists(
3032
config, cookies, sys_uuid)
3133
if exists:
3234
logger.info("Shutting down the partition")
33-
activation.shutdown_partition(config, cookies, partition_uuid)
35+
destroyed = activation.shutdown_partition(config, cookies, partition_uuid)
3436
logger.info("Partition shut down")
3537

3638
logger.info(
3739
"Detaching installation medias and physical disk from the partition")
38-
vios_operation.cleanup_vios(
40+
destroyed = vios_operation.cleanup_vios(
3941
config, cookies, sys_uuid, partition_uuid, vios_uuid_list)
4042
logger.info(
4143
"Detached installation medias and physical disk from the partition")
4244

4345
if created_by_pim and exists:
4446
logger.info("Destroying the partition")
45-
partition.remove_partition(config, cookies, partition_uuid)
47+
destroyed = partition.remove_partition(config, cookies, partition_uuid)
4648
logger.info("Partition destroyed")
4749
except Exception as e:
4850
raise e
49-
return
51+
return destroyed

cli/cmd/launch.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ def _launch(config, cookies, sys_uuid, vios_uuids):
5151
active_vios_servers = vios_operation.get_active_vios(
5252
config, cookies, sys_uuid, vios_uuids)
5353
if len(active_vios_servers) == 0:
54-
logger.error("failed to find active VIOS server")
55-
raise VIOSError("failed to find active VIOS server")
54+
raise VIOSError(f"no active VIOS server attached to the system '{util.get_host_address(config)}'")
5655
logger.debug(
5756
f"List of active VIOS '{list(active_vios_servers.keys())}'")
5857

5958
vios_media_uuid_list = vios_operation.get_vios_with_mediarepo_tag(active_vios_servers)
6059
if len(vios_media_uuid_list) == 0:
61-
logger.error("failed to find VIOS server for the partition")
62-
raise StorageError("failed to find VIOS server for the partition")
60+
raise StorageError(f"no VIOS server attached with media repository on the system '{util.get_host_address(config)}'")
6361

6462
logger.info("Setting up partition")
6563
exists, created_by_pim, partition_uuid = partition.check_partition_exists(config, cookies, sys_uuid)
@@ -186,10 +184,7 @@ def setup_storage(config, cookies, active_vios, sys_uuid, lpar_id):
186184
vios_storage_list = vios_operation.get_vios_with_physical_storage(
187185
config, active_vios)
188186
if len(vios_storage_list) == 0:
189-
logger.error(
190-
"failed to find physical volume for the partition")
191-
raise StorageError(
192-
"failed to find physical volume for the partition")
187+
raise StorageError(f"no VIOS server attached with available physical disk on the system '{util.get_host_address(config)}' to attach to the PIM partition")
193188
storage.attach_physical_storage(
194189
config, cookies, sys_uuid, lpar_id, vios_storage_list)
195190
except (StorageError, VIOSError, Exception) as e:
@@ -200,8 +195,7 @@ def handle_virtual_disk(config, cookies, active_vios, sys_uuid, lpar_id):
200195
vios_storage_list = vios_operation.get_vios_with_physical_storage(
201196
config, active_vios)
202197
if len(vios_storage_list) == 0:
203-
logger.error("failed to find physical volume for the partition")
204-
raise StorageError("failed to find physical volume for the partition")
198+
raise StorageError(f"no VIOS server attached with available physical disk on the system '{util.get_host_address(config)}' to attach to the PIM partition")
205199
vios_storage_uuid = vios_storage_list[0][0]
206200
updated_vios_payload = vios_operation.get_vios_details(config, cookies, sys_uuid, vios_storage_uuid)
207201

cli/cmd/rollback.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ def _rollback(config):
5757
return False
5858
return True
5959
except Exception as e:
60-
logger.error(f"failed to rollback PIM partition, error: {e}")
6160
raise Exception(f"failed to rollback PIM partition, error: {e}")

cli/cmd/status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ def _status(config, cookies, sys_uuid):
3030
logger.debug("Checking partition exists")
3131
exists, _, partition_uuid = partition.check_partition_exists(config, cookies, sys_uuid)
3232
if not exists:
33-
logger.error(f"Partition named '{util.get_partition_name(config)}' not found")
33+
logger.error(f"partition named '{util.get_partition_name(config)}' not found")
3434
return
3535

3636
lpar_state = activation.check_lpar_status(config, cookies, partition_uuid)
3737
if lpar_state != "running":
38-
logger.error(f"Partition '{util.get_partition_name(config)}' not in running state")
38+
logger.error(f"partition '{util.get_partition_name(config)}' not in running state")
3939
return
4040
logger.info(f"PIM partition '{util.get_partition_name(config)}' is in running state")
4141

cli/cmd/upgrade.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def _upgrade(config):
8080
logger.info("Monitoring boot process, this will take a while")
8181
monitor_util.monitor_pim(config)
8282
except Exception as e:
83-
logger.error(f"failed to upgrade PIM partition, error: {e}")
8483
raise Exception(f"failed to upgrade PIM partition, error: {e}")
8584

8685
return upgraded

cli/network/virtual_network.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def get_network_uuid(config, cookies, system_uuid):
3333
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
3434
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
3535
if response.status_code != 200:
36-
logger.error(f"failed to list VLAN, error: {response.text}")
3736
raise NetworkError(f"failed to list VLAN, error: {response.text}")
3837
uuid = ""
3938
network_name = util.get_vnetwork_name(config)
@@ -43,8 +42,7 @@ def get_network_uuid(config, cookies, system_uuid):
4342
break
4443

4544
if "" == uuid:
46-
logger.error(f"failed to find virtual network with name '{network_name}'")
47-
raise NetworkError(f"failed to find virtual network with name '{network_name}'")
45+
raise NetworkError(f"no virtual network available with name '{network_name}'")
4846
else:
4947
logger.debug(f"Network UUID for the virtual network {network_name}: {uuid}")
5048
return uuid
@@ -56,7 +54,6 @@ def get_vlan_details(config, cookies, system_uuid):
5654
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
5755
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
5856
if response.status_code != 200:
59-
logger.error(f"failed to get VLAN details, error: {response.text}")
6057
raise NetworkError(f"failed to get VLAN details, error: {response.text}")
6158

6259
soup = BeautifulSoup(response.text, 'xml')
@@ -81,11 +78,11 @@ def check_network_adapter(config, cookies, partition_uuid, vlan_id, vswitch_id):
8178
logger.debug(f"Found network with VLAN '{vlan_id}' and Switch '{vswitch_id}' attached to lpar.")
8279
slot_num = soup.find("VirtualSlotNumber").text
8380
return True, slot_num
84-
else:
85-
raise NetworkError(f"failed to check if virtual network is attached to the partition, error: {response.text}")
81+
response.raise_for_status()
82+
except requests.exceptions.RequestException as e:
83+
raise NetworkError(f"failed to check if virtual network is attached to the partition while making http request, error: {e}, response: {e.response.text}")
8684
except Exception as e:
87-
logger.error(f"failed to check if virtual network is attached to the partition, error: {e}")
88-
raise e
85+
raise NetworkError(f"failed to check if virtual network is attached to the partition, error: {e}")
8986
return False, slot_num
9087

9188
def attach_network(config, cookies, system_uuid, partition_uuid):
@@ -105,10 +102,10 @@ def attach_network(config, cookies, system_uuid, partition_uuid):
105102
url = "https://" + util.get_host_address(config) + uri
106103
headers = {"x-api-key": util.get_session_key(config), "Content-Type": CONTENT_TYPE}
107104
response = requests.put(url, headers=headers, cookies=cookies, data=payload, verify=False)
108-
if response.status_code != 200:
109-
logger.error(f"failed to attach virtual network to the partition, error: {response.text}")
110-
raise NetworkError(f"failed to attach virtual network to the partition, error: {response.text}")
105+
response.raise_for_status()
111106
logger.debug(f"Network '{util.get_vnetwork_name(config)}' attached to lpar")
107+
except requests.exceptions.RequestException as e:
108+
raise NetworkError(f"failed to attach virtual network to the partition while making http request, error: {e}, response: {e.response.text}")
112109
except Exception as e:
113-
raise e
110+
raise NetworkError(f"failed to attach virtual network to the partition, error: {e}")
114111
return DEFAULT_NW_SLOT

cli/partition/activation.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def get_lpar_profile_id(config, cookies, partition_uuid):
116116
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartitionProfile"}
117117
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
118118
if response.status_code != 200:
119-
logger.error(f"failed to get LPAR profile ID, error: {response.text}")
120119
raise PartitionError(f"failed to get LPAR profile ID, error: {response.text}")
121120
soup = BeautifulSoup(response.text, 'xml')
122121
entry_node = soup.find('entry')
@@ -129,7 +128,6 @@ def poll_job_status(config, cookies, job_id):
129128
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.web+xml; type=JobRequest"}
130129
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
131130
if response.status_code != 200:
132-
logger.error(f"failed to get job completion, error: {response.text}")
133131
raise PartitionError(f"failed to get job completion, error: {response.text}")
134132
soup = BeautifulSoup(response.text, 'xml')
135133
status = soup.find("Status").text
@@ -172,12 +170,10 @@ def activate_partition(config, cookies, partition_uuid):
172170
headers = {"x-api-key": util.get_session_key(config), "Content-Type": CONTENT_TYPE}
173171
response = requests.put(url, headers=headers, cookies=cookies, data=payload, verify=False)
174172
if response.status_code != 200:
175-
logger.error(f"failed to activate partition, error: {response.text}")
176173
raise PartitionError(f"failed to activate partition, error: {response.text}")
177174
# check job status for COMPLETED_OK
178175
status = check_job_status(config, cookies, response.text)
179176
if not status:
180-
logger.error(f"failed to activate partition, activate job returned false")
181177
raise PartitionError(f"failed to activate partition, activate job returned false")
182178
logger.debug("Partition activated successfully.")
183179
return
@@ -188,20 +184,18 @@ def check_lpar_status(config, cookies, partition_uuid):
188184
headers = {"x-api-key": util.get_session_key(config)}
189185
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
190186
if response.status_code != 200:
191-
logger.error(f"failed to get LPAR details for '{partition_uuid}', error: {response.text}")
192-
raise PartitionError(f'Failed to get LPAR details for {partition_uuid}')
187+
raise PartitionError(f'Failed to get LPAR details for {partition_uuid}, error: {response.text}')
193188
soup = BeautifulSoup(response.text, 'xml')
194189
state = soup.find("PartitionState")
195190
if state == None:
196-
logger.error(f"partition state of LPAR '{partition_uuid}' found to be None")
197-
raise PartitionError(f'Failed to get LPAR status for {partition_uuid}')
191+
raise PartitionError(f"partition state of LPAR '{partition_uuid}' found to be None")
198192
return state.text
199193

200194
def shutdown_partition(config, cookies, partition_uuid):
201195
lpar_state = check_lpar_status(config, cookies, partition_uuid)
202196
if lpar_state == "not activated":
203197
logger.debug("Partition already in 'not activated' state, skipping shutdown")
204-
return
198+
return True
205199

206200
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}/do/PowerOff"
207201
url = "https://" + util.get_host_address(config) + uri
@@ -210,10 +204,10 @@ def shutdown_partition(config, cookies, partition_uuid):
210204
response = requests.put(url, headers=headers, cookies=cookies, data=payload, verify=False)
211205
if response.status_code != 200:
212206
logger.error(f"failed to shutdown partition, error: {response.text}")
213-
return
207+
return False
214208
# check job status for COMPLETED_OK
215209
status = check_job_status(config, cookies, response.text)
216210
if not status:
217211
logger.error(f"failed to shutdown partition, shutdown job returned false")
218-
return
219-
return
212+
return False
213+
return True

0 commit comments

Comments
 (0)