Oracle Analytics Cloud (OAC) — Automatic Start/Stop using OCI CLI

Introduction

Oracle Analytics is an easy to use, highly visual way to understand what your data is telling you. In some scenarios, it is desirable to make this insight available to a larger audience, without the need for the viewer to log in.

This step by step tutorial will help you set up an automatic start and stop of your instance to save resources, if needed.

Prerequisites

  • Access to provision Instances (Virtual Machines or VMs) in Oracle Cloud Infrastructure (OCI)
  • Roles to create or to access a Virtual Cloud Network (VCN)

Basic Setup

The setup for these scripts is basic and all you need is an Always Free VM provisioned in OCI.

You can use a default VCN with public internet access, as an example:

Select your VM Shape

You will need a pre-defined VCN and a SSH key generated. SSH Keys can be generated on Win 10 machines by using the Powershell.

Generating SSH key pairs from Windows 10 using PowerShell

You can then open the .pub key using Notepad or any other text editor and copy the one-line key into the provisioning screen:

Adding SSH Keys for your VM during the provisioning process

Once the machine is provisioned you can SSH into it by using Putty or directly from the command line on any OS:

SSH From the Command Line Interface on Windows 10

The syntax is

ssh -i C:\path\to\private\key opc@public:ip

The command that you need to run to install the oci-cli is:

bash -c “$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

Or just copy-paste it from the documentation:

Documentation caption for installing the OCI CLI

Follow the prompts and proceed with the default install. The image that I used for the VM is Autonomous Linux and that comes with Python installed and it takes a couple of minutes to add the oci-cli on top of it.

Once the setup is complete you can proceed with the oci cli config setup by typing

oci setup config

Running the oci setup config command

You will need the tenancy and user OCID from your cloud account:

Use the Copy button to save your Tenancy OCID

User OCID:

Use the Copy button to save your User OCID

Once you complete this setup you will have to get the .pem key from the VM back to your local machine so you can upload it in Identity — Users — Select your User and click on API Keys.

This command will help you copy the key from the VM to your Desktop (open a new command prompt):

Copying the key from the VM to your Desktop to upload it in OCI

scp -i C:\path\to\key opc@public_ip:/home/opc/path/to/oci_api_key_public.pem C:\users\youruser\Desktop

Identity — Users — Select your User and click on API Keys

Proceed to upload this key as shown above. If you have followed the default installation as I did, you should be able to follow the screenshots as they are.

Once you have uploaded the public api_key, you can test the stop or start command:

Testing the OCI CLI setup

As per the oci-cli documentation for analytics, the syntax is:

oci analytics analytics-instance stop — analytics-instance-id OAC_INSTANCE_OCID

Finding the OAC OCID

The response should be like the one above. If this has been the case, you can proceed to setup the .sh files and the cronjob.

Sample start script

Use the vi text editor to create the file:

vi start_oac.sh will open a blank window

Press SHIFT+I to insert and type in the code:

#!/bin/sh

#Simple script to start the instance

oci analytics analytics-instance start — analytics-instance-id OAC_OCID

Once this is done, hit the ESC key and type :wq! (: included) to quit the text editor (write, quit, force)

Simple bash script for running the OCI CLI command to start an OAC instance

Make it executable chmod +x start_oac.sh

Changing the mod to +x to be able to run the script

Now you should be able to run it by typing ./start_oac.sh and it will run the script. This can be scripted in different ways; you can also create a python script with multiple log options and multiple instances. Something more dynamic than the example presented.

Proceed in a similar fashion with the stop_oac.sh and then you are good to setup the cronjob.

Make sure you consider the server’s time before you submit the final cronjob.

Checking the local server date

Type crontab -e to edit the cron jobs and type in something similar to (adjusted per your timezone):

0 18 * * 1–5 /home/opc/stop_oac.sh >> /home/opc/stop_script.log 2>&1

0 9 * * 1–5 /home/opc/start_oac.sh >> /home/opc/start_script.log 2>&1

This will run daily, Monday through Friday at 18:00 or 09:00 server time and save the log in /home/opc/.

:wq! to save it and you’re done.

I use crontab guru to help me with the setup as in the screenshot below:

Summary

Setting up the automatic start and stop of an instance can help you save resources or limit the access to a specific Oracle Analytics Cloud instance during working hours. This can be done for free by using the Always Free services from OCI.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mihai TALPIS

Mihai TALPIS

Cloud BI Specialist [All content and opinions are my own]