A lightning-fast Command-Line OCI UI to Manage your resources
When you are developing on Oracle OCI, you often need to perform some small tasks on the OCI Console: start your VM’s and DB’s in the morning, check the size of a Block Volume, stop all consuming services in the evening.
Of course the standard browser-based OCI Console allows you to do all those things, but it might take a lot of clicks, especially with 2-Factor authentication and mandatory long passwords imposed, and all that for just clicking the start button of the VM and then leaving the console.
What if you could do this with a simple interactive command-line script, with no need to log in, with just a few keystrokes?
You could use the standard OCI Command-line utility, but even then you need to actually have a series of OCIDs at the ready to be copied over in order to manage your resources, and it will take more than a few clicks to get things done …
In this article I’m presenting a basic 200 lines Python script that gives you your OCI environment at your fingertips, on the command line of your laptop, with automatic login, listing of relevant resources in your environment, and easy starting and stopping of the key resources like VM’s and DB’s.
What this script is NOT allowing you is to actually create new resources: that would require a lot more input, and validation of all the various parameters, so for this I refer you to the normal web-based OCI Console.
And as this is just a simple python script, you can easily extend it to include other OCI resources you want to manipulate!
Preparing your environment
So what do you need to do in order to get going with this script?
- Install the OCI CLI on your machine
- Add an API key to your OCI profile and download the private key to your PC
- Configure the OCI CLI by creating the ~/.oci/config file as proposed in the API Configuration wizard of the standard OCI console
- Install Python on your machine (I use 3.11.7), and install the oci and readchar python packages
- Download the python script called coci.py on this github repo — more details in the readme file.
You’re all set ! Optionally, you can define an alias called coci to “python ~/coci.py” so you only need to type that single command.
If you work in a larger shared tenancy, you probably do most of your work in your own compartment, so you can add a parameter in the ~/.oci/config file to specify the compartment to start the script in — the default would be the root compartment — the details are in the github Readme file.
Running the script and working with your OCI resources
When you run the script, you first get some key elements so you know you are indeed on the right environment : tenancy name, compartment name and the username used to connect to the instance.
The commands available are displays right away:
- g: Go to a different compartment
- c: Compute instances: list, start or stop
- n: Networks: list available VCN’s
- s: Storage: list storage buckets and block volumes
- d: Databases — only autonomous for now: list, start and stop
- i: CN objects: list containers
- q: quit
That’s the basics covered for you!
Once you choose a specific command, you either get a simple list of resources of that type, or you enter in a sub-menu where you can manipulate the resources, like in the case of compute and database. You get also a list of available resources, but now you can start, stop, or see all the attributes of a resource through the following menu of commands:
- u: bring an instance UP
- d: bring an instance DOWN
- a: print all attributes
- space: re-list instances
- -: back to main menu
Relisting the same list is usefull after for example starting a VM instance, as you will see the state go from “Starting” to “Started. And the IP address of the instances are also listed, so you can simply copy it in your shell to start for example an ssh session!
The “Go” command allows you to change compartment, allowing you to “walk” across the tenancy environment, at least in those compartments where you have read access.
An example
You want to spin up that compute instance you want to work with ?
- Launch coci (4 letters plus enter)
% coci
----------------------------------------
- Instance : myociinstance
- Compartment: test-compartment-1
- Username : john.doe@a-company.com
--------------
Actions:
g: Go to a different compartment
c: Compute instances: list, start or stop
n: Networks: list available VCN's
s: Storage
d: Databases - only autonomous for now
i: CN objects: containers
q: quit
- Type the letter “c” ==> you get the list of instances in your compartment
----------------------------------------
Instances:
Instance 0 : RUNNING , Name: my-instance-1 , IP= 131.1.2.3
Instance 1 : STOPPED , Name: second-instance , IP= 132.4.5.6
----------------------------------------
Instance Actions:
u: bring an instance UP
d: bring an instance DOWN
a: print all attributes
space: re-list instances
-: back to main menu
- type the letter “u”
- type the sequence number of the instance (that is 0, 1, 2, etc, NOT the OCID !) followed by “Enter”
Enter Instance ID to start: 1
- The list of instances is re-displayed, check your instance is now in the “Starting” state
Starting instance: second-instance
--> response: STARTING
----------------------------------------
Instances:
Instance 0 : RUNNING , Name: my-instance-1 , IP= 131.1.2.3
Instance 1 : STARTING , Name: second-instance , IP= 132.4.5.6
----------------------------------------
Instance Actions:
u: bring an instance UP
d: bring an instance DOWN
a: print all attributes
space: re-list instances
-: back to main menu
- Type “q” to quit
That is exactly 10 keystrokes, including 2 times the “Enter” key!
Conclusion
The coci script allows you to spin up your resources really fast in the morning, and shut them down when finished, with a minimum of keystrokes, no need to copy/paste any OCID’s. You can also list useful other resources like networks, storage and compartments to check their status.
Stay tuned for more features to be added to the script — or submit your own improvements through a pull request on the github page.