Take Care

I haven’t posted this blog for a while because of coronavirus crisis. I do study programming and technology on a daily basis but I spend other time to follow and be alert about the current situation. Protecting my family is my first priority.

Engineers out there, please take care of yourself. Even if you think you are going to be OK because you are not in the risky age bracket, you might affect others. Please stay home as much as you can.

I just heard of a news that Shimura Ken (the most famous Japanese comedian) passed away today. This is the saddest news I have heard in a while.

Stay safe, but keep coding for our better days…

How to Filter and Format OCI CLI Data

All commands of OCI CLI return JSON data. It’s nice but it’s kind of hard to read. Let’s see how we can format it just for readability.

We are going to choose oci compute image list command in this example.

$ oci compute image list --compartment-id $T

Make sure you have $T set to your tenancy OCID. As a result of the command, you will see data like the following.

{
"data": [
{
"agent-features": null,
"base-image-id": null,
"compartment-id": null,
"create-image-allowed": true,
"defined-tags": {},
"display-name": "Windows-Server-2016-Standard-Edition-VM-Gen2-2020.01.14-0",
"freeform-tags": {},
"id": "ocid1.image.oc1.iad.aaaaaaaauwpipy7yex62fvqix7a7ipdzdhc6pdz57vkowvc4jhkfrazm6bwa",
"launch-mode": "NATIVE",
"launch-options": {
"boot-volume-type": "PARAVIRTUALIZED",
"firmware": "UEFI_64",
"is-consistent-volume-naming-enabled": false,
"is-pv-encryption-in-transit-enabled": true,
"network-type": "VFIO",
"remote-data-volume-type": "PARAVIRTUALIZED"
},
"lifecycle-state": "AVAILABLE",
"operating-system": "Windows",
"operating-system-version": "Server 2016 Standard",
"size-in-mbs": 262144,
"time-created": "2020-01-15T00:08:30.361000+00:00"
},
SNIP

I’m going to just extract display-name and operationg-system as a table formatted data.

oci compute image list --compartment-id $T --query "data[*]".{"name:\"display-name\",os:\"operating-system\""} --output table

Here is the output.

Let’s do a basic filtering of the data. We will list only the images where the operating-system is Oracle Linux.

oci compute image list --compartment-id $T --query "data[?\"operating-system\"=='Oracle Linux']".{"name:\"display-name\",os:\"operating-system\""} --output table

The filtering looks kind of cryptic because of the dash in the JSON data key. Output looks like the image below.

It’s quite useful to be able to filter the data and also format it in a better looking table format.

I hope this helps someone out there because I struggled with the –query part of this effort because of the dashed JSON data key.

How to Install Oracle Cloud CLI Tool

I’m getting really into OCI (Oracle Cloud Infrastructure) CLI (Command Line Interface). It’s really cool to be able query and manipulate the Oracle Cloud Infrastructure resources. First, let’s see how we can install it.

Installation

Copy the following command and paste it in your terminal.

$ bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

Pretty much all default values will do when prompted to enter information and the install is complete when you execute the next command.

$ source ~/.bashrc # ~/.bash_profile if macOS.

Depending on your environment, have your shell read the correct source.

Configuration

Enter the following command to start to configure.

$ oci setup config

You will be prompted to choose where you want to store the configuration file for OCI CLI. I usually leave it at the default location, so simply hit enter (return) here.

Enter a location for your config [/home/amaterasu48/.oci/config]:

Now you will be prompted to enter user OCID. First login to OCI UI and click the Profile menu icon. And then select User Settings. By the way, OCID is resource identifier throughout Oracle Cloud Infrastructure. For more details, please refer to this documentation.

Then copy your OCID to your clipboard by clicking Copy link.

Back in your terminal, paste the copied user OCID and then hit enter.

Enter a user OCID: [Paste your user OCID here]

Next, you will be prompted to enter tenancy ID. Tenancy is equivalent to account. Click Profile -> Tenancy: [your tenancy name]

Now click Copy link to copy the Tenancy OCID.

Back in your terminal, paste the copied Tenancy OCID and then hit enter.

Enter a tenancy OCID: [Paste your tenancy OCID here]

Now you will be asked to enter the default region. All the available regions are listed in the prompt. I am entering us-ashburn-1 as my default region.

Enter a region (e.g. ap-melbourne-1, ap-mumbai-1, ap-osaka-1, ap-seoul-1, ap-sydney-1, ap-tokyo-1, ca-toronto-1, eu-amsterdam-1, eu-frankfurt-1, eu-zurich-1, me-jeddah-1, sa-saopaulo-1, uk-gov-london-1, uk-london-1, us-ashburn-1, us-gov-ashburn-1, us-gov-chicago-1, us-gov-phoenix-1, us-langley-1, us-luke-1, us-phoenix-1): us-ashburn-1

Now you will be prompted to generate private and public key pair. Enter Y to proceed.

Do you want to generate a new RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: Y

The key files will be generated to ~/.oci directory. You have a chance to change the directory where you generate them but I would leave it to the default location. Simply hit enter here.

Enter a directory for your keys to be created [/home/amaterasu48/.oci]:

You are not prompted to enter a name for the key. I would just leave it as default. Hit enter.

Enter a name for your key [oci_api_key]:

Next, you will be asked to enter a passphrase for the private key. This is optional, so it’s up to you whether you enter a passphrase or not. I’m leaving it blank in this example.

Enter a passphrase for your private key (empty for no passphrase):

Copy the public key to your clipboard.

$ cat ~/.oci/oci_api_key_public.pem

Back in Oracle Cloud Infrastructure UI, navigate to User Settings.

Click Add Public Key button.

Click PASTE PUBLIC KEYS, paste the copied public key from ~/.oci/oci_api_key_public.pem in the PUBLIC KEY text area and finally click Add button.

Back in your terminal, execute the following lines to check if your OCI CLI can actually talk to Oracle Cloud Infrastructure.

$ T=[your Tenancy OCID here]
$ oci compute image list --compartment-id $T

If it’s successful, you would get a list of available images for you to spin up instances (VM hosts) in JSON data.

It’s actually pretty fun to explore Oracle Cloud Infrastructure using CLI tool. You can find references here on Oracle’s official documentation site.