X

News, tips, partners, and perspectives for the Oracle Linux operating system and upstream Linux kernel work

Installing Python SDK and CLI for Oracle Cloud Infrastructure on Oracle Linux: a Tutorial

There are several ways to install the Python SDK and CLI for Oracle Cloud Infrastructure (OCI). In this blog post I describe two ways to do so on Oracle Linux 7:

 

  • using Oracle Linux system Python 2.7 —included in the OS— and the python-oci-sdk and python-oci-cli RPMs
  • Using Python 3.6 from the Oracle Linux EPEL repository and pip

Configuring Required Keys and OCIDs

For the OCI SDK and CLI to work, basic configuration and authentication information is required. For example, Oracle Cloud IDs (OCID) for user and tenancy. This is covered in the OCI Documentation: SDK and Tool Configuration and Required Keys and OCIDs. To set up public and private API keys for the OCI SDK and CLI:

 

mkdir ~/.oci
openssl genrsa -out ~/.oci/oci_api_key.pem 2048
chmod go-rwx ~/.oci/oci_api_key.pem
openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem
# add this public key to your profile under User Settings > API Keys
cat ~/.oci/oci_api_key_public.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo/7hKYCiExQJjo3C4M+V
QVgBowAUYRRRCEIkkfolpYSlHuyfl0Y34OuYlat6mqnZReuSl6MKa2e8PKIHfa4T
0s/01cWvgETYnFty6lskNQO3eGO1KIQ5rZCSuCgcrnWNUzYKQ+0FopSDHj1BPzbv
9Kr13cE9BRU7nBYyG4hGbjUmY9qt6LUKR/MzZZjI5+IAlwpywlYN7X2uwUf30K/c
M/zUpgSZNNiI9lwnJB679pAOHBGdK0JfXEDS6VUAITgzHbNQcFcl6567LCd4U15j
1DjWAEWIObtYvzpX0nqUsn8Is8GoS6eTQBes8Nhf+rUgLSodsP8rGBboWtkDfRBk
9wIDAQAB
-----END PUBLIC KEY-----

Copy the Public Key output and upload it by Signing in to the Console and pasting it under User Settings > API Keys. See Figure 1.

Figure 1. Adding Public Key to User Settings in OCI Console
 

Create a file ~/.oci/config that contains your user OCID, your tenancy OCID, the fingerprint for your Public Key and your region. The tenancy OCID can be found bottom left in the footer of the Console (see Documentation: Where to Find Your Tenancy's OCID) For example:

$ cat ~/.oci/config
[DEFAULT]
user=ocid1.user.oc1..aaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlhxw4mqc2r2reqxx
fingerprint=ba:xx:45:xx:ab:61:c7:xx:0b:46:58:xx:80:89:xx:b7
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaywf4txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
region=us-ashburn-1

Set the permissions for the ~/.oci/config file to limit access:

chmod go-rwx ~/.oci/config

Oracle Linux System Python 2.7: Install Using RPM from Oracle Linux Yum Server

The Oracle Linux team packages the OCI CLI and SDK in RPM form and publishes them in the Developer repository on Oracle Linux yum server. To install these for (system) Python 2.7:

sudo yum -y install yum-utils
sudo yum-config-manager --enable ol7_developer ol7_developer_epel
sudo yum -y install python-oci-sdk python-oci-cli

Python 3.6 from EPEL: Install Using pip

Oracle Linux yum server has an EPEL repository that includes Python 3.6. You can read more about Python for Oracle Linux here.

	sudo yum -y install yum-utils
	sudo yum-config-manager --enable ol7_developer_epel
	sudo yum install -y python36
	python3.6 -m venv py36env
	source py36env/bin/activate
	python -m pip install oci oci-cli

Testing the OCI CLI

To test the CLI, you'll need the Compartment OCID. You can find this via the Console main "hamburger" menu Identity > Compartments. See figure 2.

Figure 2. Finding your Compartment OCID
 

A quick test of the CLI by obtaining a list of available images in the Compute service:

oci compute image list --compartment-id=ocid1.compartment.oc1..aaaa..xxx
...
{
      "base-image-id": null, 
      "compartment-id": null, 
      "create-image-allowed": true, 
      "defined-tags": {}, 
      "display-name": "Oracle-Linux-7.5-Gen2-GPU-2018.05.09-1", 
      "freeform-tags": {}, 
      "id": "ocid1.image.oc1.iad.aaaaaaaaafiwqi57icjjdkwvlkf7li5lfnk4ad3jw4bvt73uvfdzrgvz7iqq", 
      "launch-mode": "NATIVE", 
      "launch-options": {
        "boot-volume-type": "ISCSI", 
        "firmware": "UEFI_64", 
        "network-type": "VFIO", 
        "remote-data-volume-type": "PARAVIRTUALIZED"
      }, 
      "lifecycle-state": "AVAILABLE", 
      "operating-system": "Oracle Linux", 
      "operating-system-version": "7.5", 
      "size-in-mbs": 47694, 
      "time-created": "2018-05-15T03:22:42.463000+00:00"
    }, 
    {
      "base-image-id": null, 
      "compartment-id": null, 
      "create-image-allowed": true, 
      "defined-tags": {}, 
      "display-name": "Oracle-Linux-7.5-Gen2-GPU-2018.05.09-0", 
      "freeform-tags": {}, 
      "id": "ocid1.image.oc1.iad.aaaaaaaafde2zxmzk6abzji6hndayiqzb3jglubvvxhugfjqvvsywag355va", 
      "launch-mode": "NATIVE", 
      "launch-options": {
        "boot-volume-type": "ISCSI", 
        "firmware": "UEFI_64", 
        "network-type": "VFIO", 
        "remote-data-volume-type": "PARAVIRTUALIZED"
      }, 
      "lifecycle-state": "AVAILABLE", 
      "operating-system": "Oracle Linux", 
      "operating-system-version": "7.5", 
      "size-in-mbs": 47694, 
      "time-created": "2018-05-10T15:41:47.117000+00:00"
    }, 
...

Testing the OCI SDK

Here's a quick test of the Python SDK, using sample code from oci-python-sdk on GitHub, reading from the ~/.oci/config file:

(py36env) $ python
Python 3.6.3 (default, Feb  8 2018, 05:35:00) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import oci
>>> config = oci.config.from_file("~/.oci/config","DEFAULT")
>>> identity = oci.identity.IdentityClient(config)
>>> user = identity.get_user(config["user"]).data
>>> print(user)
{
  "compartment_id": "ocid1.tenancy.oc1..aaaaaaadkjfhksdjfhkjhsdfasieoirusnksndflknasdf7zxoxrw5jq",
  "defined_tags": {},
  "description": "sergio",
  "freeform_tags": {},
  "id": "ocid1.user.oc1..aaaadskjfhaksdjfhkljasdhfkjahsdfkjhasdflkjhsdf2r2req",
  "inactive_status": null,
  "lifecycle_state": "ACTIVE",
  "name": "sergio@domain.com",
  "time_created": "2017-04-08T22:54:30.717000+00:00"
}
>>>

References

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services