September 26, 2017

Install oracle bmcs (oraclebmc-cli)

Here is a quick example how to install Oracle Braremetal cloud cli on MacOS or Linux.
Please note what oracle just renamed Braremetal cloud to Oracle Cloud Infrastructure (OCI)

First thing first – python.

I recommend to start with Python 3.6 or later. Oracle says python 2 is also supported, but when I tried bmcs installed on 2.7.13, I got error:

$ bmcs compute image list --compartment-id ocid1.compartment.oc1..aaaaaaaaforqbvs3vdq2a73tfkih4ntll35dobofj4q2qjjk27l5p5gh3xna  

SSLError: ("bad handshake: SysCallError(54, 'Connection reset by peer')",)  

Which links to a problem what old python uses TLS 1.1, but minimum TLS 1.2 connection is required by Oracle OCI. There are ways to disable TLS 1.1, but if things start like this, its good sign to start from new python.

Install python 3.6+ on MacOS:
Just download package from https://www.python.org/downloads and install.

Install python 3.6+ on linux (will use ubuntu in example):
Better build from source:

sudo apt-get install python3-dev libffi-dev libssl-dev   
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz   
tar xvf Python-3.6.0.tgz   
cd Python-3.6.0   
./configure --enable-optimizations   
make -j8   
sudo make altinstall   
python3.6  
sudo apt-get install zlib1g-devMac  

Generate Key

Generate the fingerprint, public and private key for accessing the API:

$ mkdir ~/.oraclebmc $ openssl genrsa -out ~/.oraclebmc/OCI_api_key.pem 2048 $ chmod 0700 ~/.oraclebmc $ chmod 0600 ~/.oraclebmc/OCI_api_key.pem $ openssl rsa -pubout -in ~/.oraclebmc/OCI_api_key.pem -out ~/.oraclebmc/OCI_api_key_public.pem $ openssl rsa -pubout -outform DER -in ~/.oraclebmc/OCI_api_key.pem | openssl md5 -c  
writing RSA key  
(stdin)= ae:be:ed:ce:d5:48:......:c3:f1:9c:91:9a  
$ cat ~/.oraclebmc/OCI_api_key_public.pem   
-----BEGIN PUBLIC KEY----- 
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwhNgleEQKVIptgM239x8 
TkO5Og4F1QHPu50TK/WFqP4wzdcKcXiO1nsw2m2nrZ/jOI4L/U8COr77Q6cAKCtU 
OqPrJ/Hy9.................................WyDauBpmEg5Emigy 
q0PX9jEv3d8CaabTO7hieimLCGMob5Agb0WK4HyKcQL0zmzVD4xnquOC8bHnHV5U VQIDAQAB 
-----END PUBLIC KEY-----  

Go to the Oracle OCI Web console: Identity -> Users -> Click user you need to run commands as -> Add Public Key.
Copy/paste public key from ~/.oraclebmc/OCI_api_key_public.pem:

Make sure fingerprint generated in GUI matches fingerprint from command line above.

Install and configure baremetal cli:

$ sudo pip-3.6 install oraclebmc-cli

Note:

Every resource in OCI has Oracle Cloud Identifier (OCID). Command below will prompt you for couple OCID. For this example easiest way you can find them is in OCI web interface.

$ bmcs setup config

This command provides a walkthrough of creating a valid CLI config file.
The following links explain where to find the information required by this script:

User OCID and Tenancy OCID:
https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/apisigningkey.htm#Other
Region:
https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/regions.htm
General config documentation:
https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm

$ bmcs setup config

Enter a location for your config [/Users/amoseyev/.oraclebmc/config]:   
Enter a user OCID: ocid1.user.oc1......aaaaxxxxxxxxxxxxxxvqvxbhf2nafiy7tzl5vukjwepfdea  
Enter a tenancy OCID: ocid1.tenancy.oc1......aaaar7mtfjzpqqeaxxxxxxxxxxxxipgacnfg3baa5pxkblvyp7xfvq  
Enter a region: us-phoenix-1  
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]: n  
Enter the location of your private key file: /Users/amoseyev/.oraclebmc/OCI_api_key.pem  
Fingerprint: ae:be:ed:ce:d5:48:......:c3:f1:9c:91:9a  
Config written to /Users/amoseyev/.oraclebmc/config  

If you haven’t already uploaded your public key through the console, follow the instructions on the page linked below in the section ‘How to upload the public key’:
https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/apisigningkey.htm#How2

Run test commands:

List instances available in specific compartment:

$ bmcs compute instance list -c "ocid1.compartment.oc1..aaaaaaaaforqbvs3vdq2a73tfkihxxxxxxxxxxxfj4q2qjjk27l5p5gh3xna"  
{  
 "data": [  
 {  
 "availability-domain": "XowL:PHX-AD-1",   
 "compartment-id": "ocid1.compartment.oc1..aaaaaaaaforqbvsxxxxxxx4ntll35dobofj4q2qjjk27l5p5gh3xna",   
 "display-name": "Instance 1 for sandbox",   
 "extended-metadata": {},   
 "id": "ocid1.instance.oc1.phx.abyhqljtldip4b3s3t63e7iixxxxxxxxxxxx4reik47yszaepkp2zsb7aq",   
 "image-id": "ocid1.image.oc1.phx.aaaaaaaahmts5c5nktcnqxxxxxxxxxxxnvkmqsoaavpsiklamn7qd3a7szq",   
 "ipxe-script": null,   
 "lifecycle-state": "RUNNING",   
 "metadata": {},   
 "region": "phx",   
 "shape": "VM.Standard1.1",   
 "time-created": "2017-09-10T02:50:35.610000+00:00"  
 }  
 ]  
}  

Spin new instance in compartment:

$ bmcs compute instance launch --availability-domain "EMIr:PHX-AD-1" -c ocid1.compartment.oc1..aaaaaaaaforqbvs3vdxxxxxxxxobofj4q2qjjk27l5p5gh3xna --shape "VM.Standard1.1" --display-name "Instance 2 test" --image-id ocid1.image.oc1.phx.aaaaaaaahmts5c5nktcnqxxxxxxxxxxsoaavpsiklamn7qd3a7szq --subnet-id ocid1.vcn.oc1.phx.aaaaaaaanqw3vvyxxxxxxxxw7juhichllj5cep5uuq5itctn6rcd5spfmq  

Hope it helped.
And ping us if you need more help.