OCI list instances requiring maintenance from Cloud Shell

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(
   delegation_token=delegation_token
)

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'",
                                                                           type='Structured',
                                                                           matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE)
    instances = search_client.search_resources(structured_search)
    for instance in instances.data.items:
        print("{}, {}, {}".format(instance.display_name, instance.identifier, instance.availability_domain))

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

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.

Simo

Recent Posts

Helping to troubleshoot with OCI VCN Flow Logs

I'm a huge fan of using tools available to help troubleshoot any issues there are.…

12 hours ago

OCI Routing checklist when using 3rd party firewall

This post will be checklist for items you'll need when you have Firewall (or Hub)…

1 year ago

OCI ExaCS Database Upgrade Rollback

Recently I was testing OCI database upgrade from 12c to 19c and ran into an…

1 year ago

Issues with OCI ExaCS PDB cloning

This is mostly just to document if you hit similar issues and how to get…

1 year ago

OCI Tips and Tricks – Managed MySQL Database in OCI (and trying out Heatwave)

Here I'm looking on how to provision MySQL DB on OCI, see how read replicas…

1 year ago

OCI Tips and Tricks: Create 19c Oracle Database (and manage it)

This time I go over on how to create 19c Oracle Database on OCI (hint:…

1 year ago