OCI : Script to Scale Up/Down OCPUs in ExaCC

Share on:

I will start this post that the script I am talking about might already be somewhere out there and even in a better version, but I couldn’t find it and I needed something that I could quickly put in cron.

The script I wrote is called oci_ocpu_scale.zsh and I can put it in cron and scale up or down the OCPUs at a certain time of the day without me doing it manually.

But first, since I hadn’t written about it. I will set up my ocicli environment.

To start, I create the RSA private/public keys and find the fingerprint of the RSA key.  I will make sure that I copy the public key once I had created it.

Rene@Eclipsyss-MacBook-Pro ~ % mkdir ~/.oci
Rene@Eclipsyss-MacBook-Pro ~ % cd ~/.oci
Rene@Eclipsyss-MacBook-Pro .oci % openssl genrsa -out ~/.oci/oci_api_key.pem 2048
Generating RSA private key, 2048 bit long modulus
...................................................................+++
....................+++
e is 65537 (0x10001)
Rene@Eclipsyss-MacBook-Pro .oci % chmod go-rwx ~/.oci/oci_api_key.pem
Rene@Eclipsyss-MacBook-Pro .oci % openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem
writing RSA key
Rene@Eclipsyss-MacBook-Pro .oci % openssl rsa -pubout -outform DER -in ~/.oci/oci_api_key.pem | openssl md5 -c
writing RSA key
78:**:**:**:**:**:**:**:**:**:**:**:**:**:**:8g
Rene@Eclipsyss-MacBook-Pro .oci % cat ~/.oci/oci_api_key_public.pem | pbcopy 

 Now that I have created the keys, I log in to https://cloud.oracle.com/ and go to my user settings as the picture below. 

Oracle Cloud User Settings

In my user settings, I will add the public key in the Resources section called API Keys 

Oracle Cloud API Keys

In this screen, I choose Paste Public Key, paste the public key I copied above and click on Add.

Add API Key to Oracle Cloud

After the key has been added, it will generate a Configuration File called config that I will use in my local ~/.oci directory. I just substitute the key_file part of it to use my private RSA key that I generated.

Oracle Cloud Configuration File Preview

The config file will look something like this

Rene@Eclipsyss-MacBook-Pro .oci % pwd
/Users/*****/.oci
Rene@Eclipsyss-MacBook-Pro .oci % cat config
[DEFAULT]
user=ocid1.user.*************************************
fingerprint=12*************************************:2c
tenancy=ocid1.tenancy.oc1.*************************************
region=ca-toronto-1
key_file=/Users/****/.oci/oci_api_key.pem

The next thing I did was install oci-cli with a brew as I’m using a Mac. I have also installed jq which is json parser.

Rene@Eclipsyss-MacBook-Pro ~ % brew update && brew install oci-cli
Updated 1 tap (homebrew/core).
....
==> Pouring pyenv--1.2.27.arm64_big_sur.bottle.tar.gz
🍺 /opt/homebrew/Cellar/pyenv/1.2.27: 750 files, 2.6MB
Removing: /opt/homebrew/Cellar/pyenv/1.2.24.1... (742 files, 2.6MB)
==> Checking for dependents of upgraded formulae...
==> No broken dependents found!
Rene@Eclipsyss-MacBook-Pro scripts % brew install jq
Updating Homebrew...
==> Downloading https://ghcr.io/v2/homebrew/core/oniguruma/manifests/6.9.7.1
######################################################################## 100.0%
...
🍺 /opt/homebrew/Cellar/oniguruma/6.9.7.1: 14 files, 1.4MB
==> Installing jq
==> Pouring jq--1.6.arm64_big_sur.bottle.1.tar.gz
🍺 /opt/homebrew/Cellar/jq/1.6: 18 files, 1.2MB

Now that I have done all the steps above, I can test out if the connectivity is working towards my OCI Tenancy

Rene@Eclipsyss-MacBook-Pro .oci % oci iam region list --output table
+-----+----------------+
| key | name           |
+-----+----------------+
| AMS | eu-amsterdam-1 |
| BOM | ap-mumbai-1    |
| CWL | uk-cardiff-1   |
| DXB | me-dubai-1     |
| FRA | eu-frankfurt-1 |
| GRU | sa-saopaulo-1  |
| HYD | ap-hyderabad-1 |
| IAD | us-ashburn-1   |
| ICN | ap-seoul-1     |
| JED | me-jeddah-1    |
| KIX | ap-osaka-1     |
| LHR | uk-london-1    |
| MEL | ap-melbourne-1 |
| NRT | ap-tokyo-1     |
| PHX | us-phoenix-1   |
| SCL | sa-santiago-1  |
| SJC | us-sanjose-1   |
| SYD | ap-sydney-1    |
| YNY | ap-chuncheon-1 |
| YUL | ca-montreal-1  |
| YYZ | ca-toronto-1   |
| ZRH | eu-zurich-1    |
+-----+----------------+

Once I had finished setting up the oci cli and connectivity to OCI, I proceeded to create the script called oci_ocpu_scale.

This script uses a control file called oci_inputs.ctl which will have the following 3 parameters. It is important to know that the value of DEFAULT_OCPU can be overridden if a value is passed as a first parameter to the script.

VM_CLUSTER_OCID:CHANGE_OCID_FOR_ACTUAL_VALUE # EXACC VM CLUSTER OCID
DEFAULT_OCPU:32 # VALUE OF OCPUs CONSIDERED AS DEFAULT
HIGHEST_OCPU_VAL:100 # MAXIMUM VALUE OF OCPUS THAT CAN BE SCALED UP TO

The script will validate if the number of OCPUs will be between the MIN (which is two) and MAX ( which is defined by HIGHEST_OCPU_VAL) value of OCPUs.

################################################################################
# validate_ocpu_values ()
# This function validates the minimum and maximum OCPU values that 
# the customer can scale up or down
################################################################################
validate_ocpu_values ()
{
if [ $1 -gt 1 -a $1 -le ${2} ];
then
   echo "====> New VM Cluster OCPU count of ${1} is : VALID"
else
   echo "====> New VM Cluster OCPU count of ${1} is : INVALID" 
   echo "Valid OCPU Range is 2 - ${2} "
   echo "Exiting..."
   exit 0
fi
}

It will also get the latest value of the OCPUs from the json file returned by oci db vm-cluster get

################################################################################
# get_ocpu_curr_value ()
# This function will read from OCI the current OCPU value
################################################################################
get_ocpu_curr_value ()
{
#oci db vm-cluster get --vm-cluster-id ${VM_CLUSTER_OCID} | jq '.["data"]' | jq '.["cpus-enabled"]'
oci db vm-cluster get --vm-cluster-id ${VM_CLUSTER_OCID} | jq -r .data.\"cpus-enabled\"
}

The script will check for a lock file, called do_not_change_ocpu, in case you don’t want the script to override the current OCPU values of your ExaCC VM Cluster and also will not execute the update command if the new OCPU value is equal to the current OCPU value.

################################################################################
# Execution of Script
################################################################################
validate_ocpu_values ${DEFAULT_OCPU} ${HIGHEST_OCPU_VAL}
CURRENT_OCPU=$(get_ocpu_curr_value)
echo "====> Current VM Cluster OCPU Value is : ${CURRENT_OCPU}"

if [[ -f "${DO_NOT_CHANGE_OCPU_LOCK}" ]]
then
   echo "====> Currently file ${DO_NOT_CHANGE_OCPU_LOCK} "
   echo " is not allowing OCPU Modifications"
   echo " Remove lock before re-execution"
   echo " Exiting..."
   exit 1
>fi

if [[ ${DEFAULT_OCPU} -ne ${CURRENT_OCPU} ]]
then
   echo "====> Changing VM Cluster value to ${DEFAULT_OCPU} OCPUs"
   update_ocpu_curr_value
   CURRENT_OCPU=$(get_ocpu_curr_value)
   echo "====> Current VM Cluster OCPU Value is : ${CURRENT_OCPU}"
else
   echo "====> Current VM Cluster OCPU Value is equal to ${DEFAULT_OCPU}"
   echo " Will not execute any scale up or scale down commands"
fi

When you run the script, it will look something like this.

Rene@Eclipsyss-MacBook-Pro ~% ./oci_scripts/oci_ocpu_scale.zsh
************************************************************************
====>Script oci_ocpu_scale.zsh starting on Thu 6 May 2021 15:00:06 CDT
************************************************************************
====> New VM Cluster OCPU count of 32 is : VALID
====> Current VM Cluster OCPU Value is : 64
====> Changing VM Cluster value to 32 OCPUs
Action completed. Waiting until the resource has entered state: ('AVAILABLE',)
{
   "data": {
       redacted
}
====> Current VM Cluster OCPU Value is : 32
************************************************************************
====>Script oci_ocpu_scale.zsh ending on Thu 6 May 2021 15:03:39 CDT
************************************************************************

This script will allow you to scale up or down from cron if it is a certain time you want to scale or use an EM corrective action and use it with the proper parameters. 

Share on:

More from this Author

OCI, Terraform & IaC Creating Compartments for CIS Foundation Architecture by Gustavo

OCI, Terraform & IaC: Creating Compartments for CIS Foundation Architecture

In this third blog post series, we will be creating four main compartments Security Compartment Network Compartment App/Dev Compartment Database ... Read More

OCI, Terraform & IaC Creating a Compartment

OCI, Terraform & IaC: Creating a Compartment

In my previous post, I talked about the setup of Terraform and a primer on what it is. In this blog post, I will create a simple resource in OCI. One ... Read More

Back to Top