Searching, Looping and Recursion using Bash

Before I get into describing all the fun things in OCI, it’s probably worth getting the basics sorted. In my first blog I wrote about setting up your OCI CLI environment, and now I’d like to go over some basics of programming and using that environment to do neat things.

I know there are a million different ways to do call OCI API’s, and loads of different languages. I’m getting to grips with Python, as there is a great module for Python which integrates to OCI, and Python is a massively powerful manipulation language. However, my first love was always Bash, and so I tend to revert to that for my scripting. Also, Bash is pretty universal and most people who use Linux (sorry Windows folks, Powershell is great as well, but I wont be covering that much in my blogs) know the basics of Bash and so can read a bash script, and I wanted to make the scripts I wrote as universally usable as possible for both our support teams today, and in the future.

The basics of any program language (whether Bash or Python) are the same:

  • Get some info
  • do something with it based on a set of conditions
  • end

Then it gets more complicated because you want to get loads of somethings, do something with each of them, and then repeat over and over. I’m obviously talking about conditional if-then-else statements and loops.

I’ll cover most of this through very simple examples, which hopefully even the OCI (and Bash) newbies should be able to grasp, but feel free to reach out if you need further clarification. This is an important step, as I will be using these basics in various other blogs I have planned. This wont teach you the basics of programming (unfortunately you’re on your own with that), but will show you how to apply those standard programming techniques to an OCI environment using conditional searches etc

Searching

The first things we need to do is find the data we to manipulate. There are 2 real ways to do this for OCI, either a direct invocation of an API, or an unstructured query.

All the syntax you need is here, but I’ll give you a few examples to bring it to life: https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.30.2/oci_cli_docs/oci.html

Most of the invocations in OCI uses the OCID. This is a long string of characters which uniquely identified an object, backup, instance, compartment etc. You can tell a lot from the OCID, because they all follow exactly the same format.

ocid1.instance.oc1.uk-london-1.anwgiljtj5n3sfycfmh6ihkq4reypufubuuwkyxxfxqakv7wvqosvgrpnh4q

They all start with “oci1”, then it tells you what it is (a compute instance in this case), which area (most are oc1, but specific government environment have other numbers here), the region and finally the unique identifier. Whenever you’re asked for this, you’re looking for something in this format.

Often you don’t know the OCID, and so it becomes a process of searching based on one thing to find another. For example, to find the status of a Block Volume backup for a Block Volume, you need the OCID, and the easiest way is to search for all backups, filter on a specific value, and you have your OCID which you can then use for getting the status.

As an example, the command to list all block volumes is:

oci bv volume list -c <COMPARTMENT OCID>

So you first need the compartment OCID. In this case, you could just go through the console and get it from there:

But that’s too easy, so let’s do this programmatically:

All the output of the ocicli is in JSON format by default, and in this case you would get 1 “block” per compartment. This specific query includes a –name flag which is nice, as you can get it to return just the data for the compartment you want. You can then get it to just return the oci you want by using something as simple as grep and awk. Again, not the most elegant but everyone understand it, and it gets the job done:

Now you have the OCID for the compartment, you can get the block volumes. There is only 1 block volume in this compartment, but if there was multiple you could capture the output and loop through it to get what you need:

Like building with Lego, this starts of small and then spirals out from there. Start with a single command at a time, and make sure you can execute it using hand-generated values. Once you’re sure of all the syntax, then you can start automating the fetch routines to get data from one place into the next. The “–output table” syntax added to the end of the line reformats the output from JSON so delimed text, which you can then use in standard Linux loops. Here’s a complete example (I now have 2 block volumes in my compartment):

This text, in this format could easily be used in a standard linux loop, and by adding more values to the awk statement you can extract any of the information about the BV. If you then wanted to, you could then use the OCID from here to then loop around all BV Backups, to find all backups related to a specific BV etc..

Happy coding !

Tags:

Leave a comment