Skip to content
Menu
ChaoTic
  • Home
  • Python
  • APEX
  • PL/SQL
  • Oralce DB
  • Docker
  • OCI
  • Nginx
  • C#
  • Linux
  • JavaScript
  • Privacy Policy
ChaoTic

OCI Operations Professional Learning Notes

Posted on January 29, 2023July 25, 2023

OCI CLI Authentication

  1. Session Token Based
    • You must use the --auth security_token or set the OCI_CLI_AUTH environment variable to security_token to authenticate CLI commands using the session token.
    • To validate, run ==> oci session validate –config-file ${file location} –profile ${profile name} –auth security_token
  2. API key based (less secure)

OCI CLI and Configure

on mac

brew install oci

After installation, run configuration command. This will collect your ocids for your user, tenancy , region and more. when all done, it will create 3 file in .oci folder. 1 config file and two .pem files.

oci setup config
.
.
.

Enter a location for your config [/Users/work/.oci/config]: 
Enter a user OCID: 
Enter a tenancy OCID: 
Enter a region by index or name:
.
.
.
# at the end you will have two a .pem files created on your system
# like this 
cd /Users/${Your username}/.oci
ls -l
#output
-rw-------  1 work  staff   316 Jan 29 13:34 config
-rw-------  1 work  staff  1704 Jan 29 13:34 oci_chaoyuim001_api_key.pem
-rw-------  1 work  staff   451 Jan 29 13:34 oci_chaoyuim001_api_key_public.pem

The oci_chaoyuim001_api_key_public.pem file will be used to upload to OCI console user.

Click on it to validate your local config file ( cat /Users/${Your username}/.oci/config )

A Quick Exmple of Displaying all buckets in Compartment

oci os bucket list --all -c ${your compartment ocid} --query 'data[*].[name,namespace]' --output table
+----------------------+--------------+
| Column1              | Column2      |
+----------------------+--------------+
| bucket-20230129-1938 | frpnibrn7ulj |
| test                 | frpnibrn7ulj |
+----------------------+--------------+

Example Create buckets with autotiering enabled

oci os bucket create -c ${your compartment ocid} --auto-tiering InfrequentAccess --name test22

How Start / Stop VMs and Autonomous Databases in OCI with OCI CLI

.sh to stop and start VMs once attached to a schedule cron job, this can act as agent to manage your resources.

#!/bin/bash
Help() {
   # Display Help
   echo "this .sh file takes 3 parameters \$compartment-id \$action_type \$skip"
   echo "\$compartment-id \$action_type are mandatory"
   echo "\$skip can be used if the start / stop action should skip one or more oci resources"
   echo "\$compartment-id is the ocid of a compartment"
   echo "\$action_type can be either START or STOP"
   echo "\$skip is the display-name of oci resouces, more than 1 can be supplied and seperated by "," "
   echo -e '\n'
   echo -e 'example \nbash vm_auto_start_or_stop.sh ${compartment_id} START vm001,vm002,v003'
   echo 'this will try to start any vms NOT named as "vm001,vm002,v003" if they are in stopped state.'
   echo -e '\n'
}

while getopts ":h" option; do
   case $option in
   h) # display Help
      Help
      exit
      ;;
   esac
done

#############################################

COMPARTMENT_ID="$1"
ACTION_TYPE="$2"
SKIP_VM_NAMES="$3"
TEMP_FILE_NAME='list_of_instance_status'
STATUS_RUNNING='RUNNING'
STATUS_STOPPED='STOPPED'
LOG_FILE_NAME='vms_log.txt'

echo " $(date +'%m/%d/%Y %T') ############## compartment_id => $COMPARTMENT_ID action_type => $ACTION_TYPE  skip => $SKIP_VM_NAMES ####################" >>$LOG_FILE_NAME

if [ -z "$COMPARTMENT_ID" ] | [ -z "$ACTION_TYPE" ]; then
   echo "\$COMPARTMENT_ID must NOT be empty"
   echo "\$ACTION TYPE must NOT be empty"
   echo -e "run: \nbash vm_auto_start_or_stop.sh -h\nfor help"
   exit 1
fi
if [ "$ACTION_TYPE" != "START" ] && [ "$ACTION_TYPE" != "STOP" ]; then
   echo -e "ACTION_TYPE can only be START or STOP"
   exit 1
fi

oci compute instance list \
   --compartment-id $COMPARTMENT_ID \
   --query 'data[*].["display-name","lifecycle-state","id"]' \
   --output table >$TEMP_FILE_NAME

cat $TEMP_FILE_NAME | while read line; do
   INSTANCE=$(echo $line | cut -d'|' -f 4 | xargs)
   INSTANCE_NAME=$(echo $line | cut -d'|' -f 2 | xargs)

   if [[ "$line" == *"$STATUS_RUNNING"* || "$line" == *"$STATUS_STOPPED"* ]]; then
      if [[ "$SKIP_VM_NAMES" == *"$INSTANCE_NAME"* ]]; then
         echo -e "\n"
         echo "${INSTANCE_NAME} is FOUND in '$SKIP_VM_NAMES'"
         echo "DO nothing with ${INSTANCE_NAME}"
      else
         echo -e "\n"
         echo "${INSTANCE_NAME} is NOT FOUND in '$SKIP_VM_NAMES'"
         echo "START or STOP ${INSTANCE_NAME}"

         if [[ "$line" == *"$STATUS_RUNNING"* && "$ACTION_TYPE" = "STOP" ]]; then
            echo "running instance found"
            sleep 1
            echo "issue STOP action to this instance ==>  $INSTANCE_NAME"
            sleep 5
            oci compute instance action \
               --action STOP \
               --instance-id $INSTANCE \
               --query 'data.["display-name","lifecycle-state","id"]' \
               --output table >>$LOG_FILE_NAME

            sleep 5
            echo "."
            echo ".."
            sleep 1

            echo "."
            echo ".."
         elif [[ "$line" == *"$STATUS_STOPPED"* && "$ACTION_TYPE" = "START" ]]; then
            echo "Stopped instance found"
            sleep 1
            echo "issue start action to this instance ==> $INSTANCE_NAME"
            sleep 5
            oci compute instance action \
               --action START \
               --instance-id $INSTANCE \
               --query 'data.["display-name","lifecycle-state","id"]' \
               --output table >>$LOG_FILE_NAME

            sleep 5
            echo "."
            echo ".."
            sleep 1
            echo "."
            echo ".."
         fi
      fi
   fi
done
#!/bin/bash
Help() {
   # Display Help
   echo "this .sh file takes 3 parameters \$compartment-id \$action_type \$skip"
   echo "\$compartment-id \$action_type are mandatory"
   echo "\$skip can be used if thie start / stop action should skip one or more oci resources"
   echo "\$compartment-id is the ocid of a compartment"
   echo "\$action_type can be either START or STOP"
   echo "\$skip is the display-name of oci resouces, more than 1 can be supplied and seperated by "," "
   echo -e '\n'
   echo -e 'example \nbash vm_auto_start_or_stop.sh ${compartment_id} START vm001,vm002,v003'
   echo 'this will try to start any vms NOT named as "vm001,vm002,v003" if they are in stopped state.'
   echo -e '\n'
}

while getopts ":h" option; do
   case $option in
   h) # display Help
      Help
      exit
      ;;
   esac
done
#############################################

COMPARTMENT_ID="$1"
ACTION_TYPE="$2"
SKIP_ATP_NAMES="$3"
TEMP_FILE_NAME='list_of_apt_status'
STATUS_AVAILABLE='AVAILABLE'
STATUS_STOPPED='STOPPED'
LOG_FILE_NAME='apt_log.txt'

echo " $(date +'%m/%d/%Y %T') ############## compartment_id => $COMPARTMENT_ID action_type => $ACTION_TYPE  skip => $SKIP_ATP_NAMES ####################" >>$LOG_FILE_NAME

if [ -z "$COMPARTMENT_ID" ] | [ -z "$ACTION_TYPE" ]; then
   echo "\$COMPARTMENT_ID must NOT be empty"
   echo "\$ACTION TYPE must NOT be empty"
   echo -e "run: \nbash vm_auto_start_or_stop.sh -h\nfor help"
   exit 1
fi

if [ "$ACTION_TYPE" != "START" ] && [ "$ACTION_TYPE" != "STOP" ]; then
   echo -e "ACTION_TYPE can only be START or STOP"
   exit 1
fi
oci db autonomous-database list \
   --compartment-id $COMPARTMENT_ID \
   --query 'data[*].["display-name","lifecycle-state","id"]' \
   --output table >$TEMP_FILE_NAME

cat $TEMP_FILE_NAME | while read line; do
   ATP=$(echo $line | cut -d'|' -f 4 | xargs)
   ATP_NAME=$(echo $line | cut -d'|' -f 2 | xargs)

   if [[ "$line" == *"$STATUS_AVAILABLE"* || "$line" == *"$STATUS_STOPPED"* ]]; then
      if [[ "$SKIP_ATP_NAMES" == *"$ATP_NAME"* ]]; then
         echo -e "\n"
         echo "${ATP_NAME} is FOUND in '$SKIP_ATP_NAMES'"
         echo "DO nothing with ${ATP_NAME}"
      else
         echo -e "\n"
         echo "${ATP_NAME} is NOT FOUND in '$SKIP_ATP_NAMES'"
         echo "START or STOP ${ATP_NAME}"

         if [[ "$line" == *"$STATUS_AVAILABLE"* && "$ACTION_TYPE" = "STOP" ]]; then
            echo "running ATP found"
            sleep 1
            echo "issue STOP action to this ATP ==>  $ATP_NAME"
            sleep 5
            oci db autonomous-database stop \
               --autonomous-database-id $ATP \
               --query 'data.["display-name","lifecycle-state","id"]' \
               --output table >>$LOG_FILE_NAME
            sleep 5
            echo "."
            echo ".."
            sleep 1
            echo "."
            echo ".."
         elif [[ "$line" == *"$STATUS_STOPPED"* && "$ACTION_TYPE" = "START" ]]; then
            echo "Stopped ATP found"
            sleep 1
            echo "issue start action to this ATP ==> $ATP_NAME"
            sleep 5
            oci db autonomous-database start \
               --autonomous-database-id $ATP \
               --query 'data.["display-name","lifecycle-state","id"]' \
               --output table >>$LOG_FILE_NAME
            sleep 5
            echo "."
            echo ".."
            sleep 1
            echo "."
            echo ".."
         fi

      fi

   fi

done

How to copy files from One Object storage to Another

  • Get a list of all the file you like to copy
  • Save the list into a txt file locally
  • loop through the file line by line to get the file name
  • Create COPY work request for each file
  • output console lines into a file to check status
# to list all objects in a bucket and output into a file 
 oci os object list --bucket-name MYBUCKET --all > file.txt

this policy is needed before COPY request can be created

  • Allow service objectstorage-eu-amsterdam-1 to manage object-family in compartment YOURCOMPRTMENT (see bucket replication options for this policy)
echo "start";
FILE_NAME="";
I=0;
while read p; do 
 FILE_NAME=$(echo $p | sed 's/I//g' | sed 's/ //g');
 echo $FILE_NAME;
 #((I++))
 #if [[ "$I" == '3' ]]
 #then
 # echo "Number $I";
 # break;
 #fi
 oci os object copy --bucket-name $YOURBUCKETNAME --destination-bucket $YOURDESTBUCKETNAME --source-object-name $FILE_NAME >> copy_log.txt
done < allfiles.txt

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Oracle APEX cheat sheets (on going)
  • Second Take on React Native – ReadCast
  • Switch Between APEX builder Authentication schemes
  • Use BitBucket Pipelines to Automate Oracle APEX deployment
  • MARKDown TEST

Categories

  • APEX
  • C#
  • chatgpt
  • Docker
  • JavaScript
  • Linux
  • Nginx
  • OCI
  • Oracle APEX
  • Oralce DB
  • PL/SQL
  • Python
  • Uncategorized
©2025 ChaoTic | Powered by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Scroll Up