Skip to content

Commit

Permalink
Merge pull request #9 from oracle-quickstart/boot_order_script
Browse files Browse the repository at this point in the history
Adding boot_order.py script
  • Loading branch information
lgabriel-oracle authored Mar 28, 2024
2 parents 982bc06 + 90fe9da commit c10957d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Scripts/Troubleshooting/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Overview
Scripts related to troublshooting tasks or workarounds.
Scripts related to troublshooting tasks or workarounds.

| Script | Description|
| -------|------------|
| boot_order/boot_order.py | Displays content of the `compute.disks` object of a source asset and adjusts boot order to point onto a specific disk for the target asset to boot from |
50 changes: 50 additions & 0 deletions Scripts/Troubleshooting/boot_order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# boot_order.py

A simple script, which displays content of the `compute.disks` object of a source asset and adjusts boot order to point onto a specific disk for the target asset to boot from. Takes optional parameter -b/--boot-order with the index of the index of the disk (starting from 0) in `compute.disks` object to boot from. Readied for execution from the Cloud Shell.

Usage:
```
ansokolo@cloudshell:~ (us-ashburn-1)$ python ./boot_order.py
usage: edit_boot_order.py [-h] [-b BOOT_DISK_INDEX] ocid
edit_boot_order.py: error: the following arguments are required: ocid
ansokolo@cloudshell:~ (us-ashburn-1)$ python ./boot_order.py ocid1.ocbinventoryasset.oc1.iad.amaaaaaabkvmyqqa5u2ypgvw5k67be656j45oceyjbd4u4srkax435locshq
[{
"boot_order": null,
"location": "[vsanDatastore] 5b69e665-ea2a-6de1-0dfe-b02628368f10/egolenko-new-windows-workload-bootorder_4.vmdk",
"name": "Hard disk 1",
"persistent_mode": "persistent",
"size_in_mbs": 61440,
"uuid": "6000C29a-03a9-9a90-f23d-89cf4c893316",
"uuid_lun": null
}, {
"boot_order": 0,
"location": "[vsanDatastore] 5b69e665-ea2a-6de1-0dfe-b02628368f10/egolenko-new-windows-workload-bootorder_6.vmdk",
"name": "Hard disk 2",
"persistent_mode": "persistent",
"size_in_mbs": 92160,
"uuid": "6000C29f-3ef9-9d9b-1264-25375c439e2e",
"uuid_lun": null
}]
ansokolo@cloudshell:~ (us-ashburn-1)$ python ./boot_order.py ocid1.ocbinventoryasset.oc1.iad.amaaaaaabkvmyqqa5u2ypgvw5k67be656j45oceyjbd4u4srkax435locshq -b 0
[{
"boot_order": 0,
"location": "[vsanDatastore] 5b69e665-ea2a-6de1-0dfe-b02628368f10/egolenko-new-windows-workload-bootorder_4.vmdk",
"name": "Hard disk 1",
"persistent_mode": "persistent",
"size_in_mbs": 61440,
"uuid": "6000C29a-03a9-9a90-f23d-89cf4c893316",
"uuid_lun": null
}, {
"boot_order": null,
"location": "[vsanDatastore] 5b69e665-ea2a-6de1-0dfe-b02628368f10/egolenko-new-windows-workload-bootorder_6.vmdk",
"name": "Hard disk 2",
"persistent_mode": "persistent",
"size_in_mbs": 92160,
"uuid": "6000C29f-3ef9-9d9b-1264-25375c439e2e",
"uuid_lun": null
}]
```
64 changes: 64 additions & 0 deletions Scripts/Troubleshooting/boot_order/boot_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import argparse
import oci, oci.exceptions
import json
import re
import sys
import os

def parse_ocid(ocid):
pattern = '^ocid1\.([a-z0-9]+)\.([a-z0-9]+)*\.([a-z0-9\-]+)\.[a-z0-9]{60}'
match = re.match(pattern, ocid)
if not match:
return False
return {
'type': match.group(1),
'realm': match.group(2),
'region': match.group(3)
}

def eprint(*args):
print(*args, file=sys.stderr)

if (__name__ == '__main__'):
parser = argparse.ArgumentParser(description='Edits boot order of disks for an source asset.')
parser.add_argument('ocid', type=str, help='OCID of a target asset')
parser.add_argument('-b', '--boot-disk', dest='boot_disk_index', help='Index of a boot disk starting from 0')

args = parser.parse_args()

source_asset_id = args.ocid
parsed_asset_id = parse_ocid(source_asset_id)
if not parsed_asset_id or parsed_asset_id['type'] != 'ocbinventoryasset':
eprint(f'Invalid source asset id was passed: {source_asset_id}')
exit(1)

tenancy_id = os.environ.get('OCI_TENANCY')
config = oci.config.from_file()
cloud_bridge_client = oci.cloud_bridge.InventoryClient(config)

asset = cloud_bridge_client.get_asset(asset_id=source_asset_id).data

if not args.boot_disk_index:
print(str(asset.compute.disks))
else:
if not args.boot_disk_index.isdigit:
eprint(f'Invalid boot disk index: {args.boot_disk_index}')
exit(1)

if len(asset.compute.disks) <= int(args.boot_disk_index):
eprint(f'Too large boot disk index: {args.boot_disk_index}, compute.disks array: {asset.compute.disks}')
exit(1)

for disk in asset.compute.disks:
disk.boot_order = None

asset.compute.disks[int(args.boot_disk_index)].boot_order = 0

update_asset_response = cloud_bridge_client.update_asset(asset_id=source_asset_id, update_asset_details=asset)
if update_asset_response.status != 200:
eprint(f'Failed to update source asset resource. Status: {update_asset_response.status}, Error: {update_asset_response.data}')
else:
print(update_asset_response.data.compute.disks)



0 comments on commit c10957d

Please sign in to comment.