OCI #5 – CLI Setup

This recipe shows how to install and configure Oracle Cloud Infrastructure CLI on your client machine.

Before you start, it is recommended that you read API Signing Key recipe to understand the concept of request signing.

Oracle Cloud Infrastructure REST API

Oracle Cloud Infrastructure exposes a comprehensive REST API to manage OCI resources and configurations. Every API request must be signed with Oracle Cloud Infrastructure API Request Signature and sent using secure HTTPS protocol with TLS 1.2. Signing a request is a multi-step process that can be seen as non-trivial. This is why you usually use tools like CLI, Terraform or custom SDK-based programs that encapsulate API calls and sign the requests on your behalf. All these tools eventually make calls to OCI REST API, therefore OCI REST API is the ultimate gateway to the cloud management plane.

Oracle Cloud Infrastructure CLI

Oracle Cloud Infrastructure CLI is a python-based command line utility that encapsulates API calls to OCI REST API. This simplifies the way you consume the API, because OCI CLI takes the burden of request signing. Furthermore, you can script API consumption using mature ecosystem of Python libraries.

Installing OCI CLI

Oracle has prepared two installation scripts. One for Linux/macOs with bash and one for Windows with Powershell. The two scripts perform similar steps. They install Python and virtualenv, create an isolated Python environment, install the latest version of CLI and alter the PATH variable. Alternatively, you can perform all these steps manually.

Installing OCI CLI on Linux or macOS

  1. Execute the following commands and follow the console-based installation wizard:
    bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

Installing OCI CLI on Windows

  1. Launch Powershell console with Run as Administrator option
  2. Execute the following commands and follow the console-based installation wizard:
    Set-ExecutionPolicy RemoteSigned
    powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1'))"

Configuring OCI CLI

CLI features with an embedded configuration wizard that generates API Signing key pair and creates CLI configuration file, based on the parameters given during configuration wizard run. You should identify a few Oracle Cloud Infrastructure Identifiers (OCIDs) before you launch the wizard.

  1. Sign in to OCI Console
  2. Go to Identity ➟ Users and copy the OCID of the user on whose behalf OCI CLI prepares, signs and sends OCI REST API requests.
  3. Go to Administration Tenancy Details and copy the OCID of the tenancy.
  4. Open a new command line window on your client machine and execute:
    oci setup config
  5. Provide the user OCID, the tenancy OCID and the region you are working with.
  6. Say Y(es) when asked if you want to generate a new RSA key pair, unless you prefer to use your own API Signing Key. To learn more on that topic, have a look at API Signing Key recipe.
  7. Say N(o) when asked if you want to write your private key passphrase to the config file, unless you do not mind storing in an open text.
  8. If you use default options for the remaining parameters, your config file will be generated as ~/.oci/config
  9. Finally, you should upload the generated public key to OCI and associate it with the user you’ve chosen in the second step. You can learn how to do it in API Signing Key recipe.

Majority, if not all, OCI REST API resource operations require Compartment OCID. You can define the default values for input parameters to OCI CLI commands, to avoid unnecessary typing, every time you invoke a CLI command. To add a default value for Compartment OCID, perform these steps:

  1. Sign in to OCI Console
  2. Go to Identity ➟ Compartments and copy the OCID of the compartment you would like to work with. If needed, create a new Compartment.
  3. Create a new file: ~/.oci/oci_cli_rc and place there the following lines:
    compartment-id = placeHereTheCompartmentOCID

Now, you should be ready to test OCI CLI. Let’s list the available CentOS Images:

oci compute image list --operating-system CentOS --output table --query "data [*].{Image:\"display-name\"}"
| Image                    |
| CentOS-7-2018.09.19-0    |
| CentOS-7-2018.08.15-0    |
| CentOS-7-2018.06.22-0    |
| CentOS-6.9-2018.06.22-0  |
| CentOS-6.10-2018.09.19-0 |
| CentOS-6.10-2018.08.15-0 |

You can find the complete reference of CLI commands here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s