We had question to come up how to list OCI Compute instances which are requiring maintenance quickly and I found an excellent script from Radu Dobrinescu written in Python.

But if I would like to run it quickly in cloud shell without any API keys or another instance which is using instance principles how would I do it?

Stephen Cross explains how to use Python SDK with Cloud Shell in his Medium post.

In short I just took best of the two, combined them and pasted the Python script in my Cloud Shell.

simo@cloudshell:~ (us-ashburn-1)$ python m.py 

Instances flagged for maintenance reboot: Instance Name, OCID, Availability Domain
ocitest1, ocid1.instance.oc1.iad.anuwcldsesfso5ici4xtuvlidfddkfofdsre6bbhypkl4ffgijey33a4ltpvq, eORU:US-ASHBURN-AD-1
ocitest2, ocid1.instance.oc1.iad.anuwcljsessko0ccueiieda4idwqb6u3337ovm5ge4jhwxm4kubtaa242bm5a, eORU:US-ASHBURN-AD-1

My Python skills are very limited but thought it’s useful hack to see maintenance status quickly in different tenancies!

Here is full code edited. Big thanks for Stephen and Radu for all the groundwork!

# Oracle OCI - Instances flagged for reboot migration
# Version 1.0 03-December 2018
# Written by: radu.dobrinescu@oracle.com
# Instructions:
# - you need the OCI python API, this can be installed by running: pip install oci
# - you need the OCI CLI, this can be installed by running: pip install oci-cli
# - Make sure you have a user with an API key setup in the OCI Identity
# - Create a config file using the oci cli tool by running: oci setup config
# - In the script specify the config file to be used for running the report
# - You can specify any region in the config file, the script will query all enabled regions

import oci

# get the cloud shell delegated authentication token
delegation_token = open('/etc/oci/delegation_token', 'r').read()
# create the api request signer
signer = oci.auth.signers.InstancePrincipalsDelegationTokenSigner(

compartment_id = signer.tenancy_id

identity_client = oci.identity.IdentityClient(config = {},signer=signer)
response = identity_client.list_region_subscriptions(signer.tenancy_id)
regions = response.data

def reboot_migration_instances():
    # Get all instances which have the reboot migration flag set.
    structured_search = oci.resource_search.models.StructuredSearchDetails(query="query instance resources where timeMaintenanceRebootDue > 'Now'",
    instances = search_client.search_resources(structured_search)
    for instance in instances.data.items:
        print("{}, {}, {}".format(instance.display_name, instance.identifier, instance.availability_domain))

print("Instances flagged for maintenance reboot: Instance Name, OCID, Availability Domain")
for region in regions:
        signer.region = region.region_name
        search_client = oci.resource_search.ResourceSearchClient(config = {},signer=signer)

Another option is just to use Advanced Search from the top. But who would want to do that? Probably if you need to monitor this somehow then using script could be more useful than using the search.

Leave a Reply

Your email address will not be published.