{"id":1053,"date":"2023-01-29T12:35:32","date_gmt":"2023-01-29T12:35:32","guid":{"rendered":"http:\/\/wp.chaoyu.nl\/?p=1053"},"modified":"2023-07-25T00:15:06","modified_gmt":"2023-07-24T22:15:06","slug":"oci-operations-professional-learn-notes","status":"publish","type":"post","link":"https:\/\/chaoyu.nl\/?p=1053","title":{"rendered":"OCI Operations Professional Learning Notes"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">OCI CLI Authentication<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/en-us\/iaas\/Content\/API\/SDKDocs\/clitoken.htm\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/en-us\/iaas\/Content\/API\/SDKDocs\/clitoken.htm\" target=\"_blank\">Session Token Based<\/a> \n<ul class=\"wp-block-list\">\n<li>You must use the&nbsp;<code>--auth security_token<\/code>&nbsp;or set the&nbsp;<code>OCI_CLI_AUTH<\/code>&nbsp;environment variable to&nbsp;<code>security_token<\/code>&nbsp;to authenticate CLI commands using the session token. <\/li>\n\n\n\n<li>To validate, run ==&gt; <strong><em>oci session validate &#8211;config-file ${file location} &#8211;profile  ${profile name} &#8211;auth security_token<\/em><\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>API key based (less secure)<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>OCI CLI and Configure <\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">on mac<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash line-numbers\">brew install oci<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash line-numbers\">oci setup config\n.\n.\n.\n\nEnter a location for your config [\/Users\/work\/.oci\/config]: \nEnter a user OCID: \nEnter a tenancy OCID: \nEnter a region by index or name:\n.\n.\n.\n# at the end you will have two a .pem files created on your system\n# like this \ncd \/Users\/${Your username}\/.oci\nls -l\n#output\n-rw-------  1 work  staff   316 Jan 29 13:34 config\n-rw-------  1 work  staff  1704 Jan 29 13:34 oci_chaoyuim001_api_key.pem\n-rw-------  1 work  staff   451 Jan 29 13:34 oci_chaoyuim001_api_key_public.pem<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><em>oci_chaoyuim001_api_key_public.pem<\/em><\/strong> file will be used to upload to OCI console user. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1441\" height=\"635\" src=\"http:\/\/wp.chaoyu.nl\/wp-content\/uploads\/2023\/01\/image-1.png\" alt=\"\" class=\"wp-image-1062\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Click on it to validate your local config file ( <strong><em>cat \/Users\/${Your username}\/.oci\/config <\/em><\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1650\" height=\"1216\" src=\"http:\/\/wp.chaoyu.nl\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-29-at-19.39.40.png\" alt=\"\" class=\"wp-image-1064\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Quick Exmple of Displaying all buckets in Compartment<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">oci os bucket list --all -c ${your compartment ocid} --query 'data[*].[name,namespace]' --output table<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">+----------------------+--------------+\n| Column1              | Column2      |\n+----------------------+--------------+\n| bucket-20230129-1938 | frpnibrn7ulj |\n| test                 | frpnibrn7ulj |\n+----------------------+--------------+<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Create buckets with autotiering enabled<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">oci os bucket create -c ${your compartment ocid} --auto-tiering InfrequentAccess --name test22<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Start \/ Stop VMs and Autonomous Databases in OCI with OCI CLI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">.sh to stop and start VMs once attached to a schedule cron job, this can act as agent to manage your resources. <\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash line-numbers\">#!\/bin\/bash\nHelp() {\n   # Display Help\n   echo \"this .sh file takes 3 parameters \\$compartment-id \\$action_type \\$skip\"\n   echo \"\\$compartment-id \\$action_type are mandatory\"\n   echo \"\\$skip can be used if the start \/ stop action should skip one or more oci resources\"\n   echo \"\\$compartment-id is the ocid of a compartment\"\n   echo \"\\$action_type can be either START or STOP\"\n   echo \"\\$skip is the display-name of oci resouces, more than 1 can be supplied and seperated by \",\" \"\n   echo -e '\\n'\n   echo -e 'example \\nbash vm_auto_start_or_stop.sh ${compartment_id} START vm001,vm002,v003'\n   echo 'this will try to start any vms NOT named as \"vm001,vm002,v003\" if they are in stopped state.'\n   echo -e '\\n'\n}\n\nwhile getopts \":h\" option; do\n   case $option in\n   h) # display Help\n      Help\n      exit\n      ;;\n   esac\ndone\n\n#############################################\n\nCOMPARTMENT_ID=\"$1\"\nACTION_TYPE=\"$2\"\nSKIP_VM_NAMES=\"$3\"\nTEMP_FILE_NAME='list_of_instance_status'\nSTATUS_RUNNING='RUNNING'\nSTATUS_STOPPED='STOPPED'\nLOG_FILE_NAME='vms_log.txt'\n\necho \" $(date +'%m\/%d\/%Y %T') ############## compartment_id =&gt; $COMPARTMENT_ID action_type =&gt; $ACTION_TYPE  skip =&gt; $SKIP_VM_NAMES ####################\" &gt;&gt;$LOG_FILE_NAME\n\nif [ -z \"$COMPARTMENT_ID\" ] | [ -z \"$ACTION_TYPE\" ]; then\n   echo \"\\$COMPARTMENT_ID must NOT be empty\"\n   echo \"\\$ACTION TYPE must NOT be empty\"\n   echo -e \"run: \\nbash vm_auto_start_or_stop.sh -h\\nfor help\"\n   exit 1\nfi\nif [ \"$ACTION_TYPE\" != \"START\" ] &amp;&amp; [ \"$ACTION_TYPE\" != \"STOP\" ]; then\n   echo -e \"ACTION_TYPE can only be START or STOP\"\n   exit 1\nfi\n\noci compute instance list \\\n   --compartment-id $COMPARTMENT_ID \\\n   --query 'data[*].[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n   --output table &gt;$TEMP_FILE_NAME\n\ncat $TEMP_FILE_NAME | while read line; do\n   INSTANCE=$(echo $line | cut -d'|' -f 4 | xargs)\n   INSTANCE_NAME=$(echo $line | cut -d'|' -f 2 | xargs)\n\n   if [[ \"$line\" == *\"$STATUS_RUNNING\"* || \"$line\" == *\"$STATUS_STOPPED\"* ]]; then\n      if [[ \"$SKIP_VM_NAMES\" == *\"$INSTANCE_NAME\"* ]]; then\n         echo -e \"\\n\"\n         echo \"${INSTANCE_NAME} is FOUND in '$SKIP_VM_NAMES'\"\n         echo \"DO nothing with ${INSTANCE_NAME}\"\n      else\n         echo -e \"\\n\"\n         echo \"${INSTANCE_NAME} is NOT FOUND in '$SKIP_VM_NAMES'\"\n         echo \"START or STOP ${INSTANCE_NAME}\"\n\n         if [[ \"$line\" == *\"$STATUS_RUNNING\"* &amp;&amp; \"$ACTION_TYPE\" = \"STOP\" ]]; then\n            echo \"running instance found\"\n            sleep 1\n            echo \"issue STOP action to this instance ==&gt;  $INSTANCE_NAME\"\n            sleep 5\n            oci compute instance action \\\n               --action STOP \\\n               --instance-id $INSTANCE \\\n               --query 'data.[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n               --output table &gt;&gt;$LOG_FILE_NAME\n\n            sleep 5\n            echo \".\"\n            echo \"..\"\n            sleep 1\n\n            echo \".\"\n            echo \"..\"\n         elif [[ \"$line\" == *\"$STATUS_STOPPED\"* &amp;&amp; \"$ACTION_TYPE\" = \"START\" ]]; then\n            echo \"Stopped instance found\"\n            sleep 1\n            echo \"issue start action to this instance ==&gt; $INSTANCE_NAME\"\n            sleep 5\n            oci compute instance action \\\n               --action START \\\n               --instance-id $INSTANCE \\\n               --query 'data.[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n               --output table &gt;&gt;$LOG_FILE_NAME\n\n            sleep 5\n            echo \".\"\n            echo \"..\"\n            sleep 1\n            echo \".\"\n            echo \"..\"\n         fi\n      fi\n   fi\ndone\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash line-numbers\">#!\/bin\/bash\nHelp() {\n   # Display Help\n   echo \"this .sh file takes 3 parameters \\$compartment-id \\$action_type \\$skip\"\n   echo \"\\$compartment-id \\$action_type are mandatory\"\n   echo \"\\$skip can be used if thie start \/ stop action should skip one or more oci resources\"\n   echo \"\\$compartment-id is the ocid of a compartment\"\n   echo \"\\$action_type can be either START or STOP\"\n   echo \"\\$skip is the display-name of oci resouces, more than 1 can be supplied and seperated by \",\" \"\n   echo -e '\\n'\n   echo -e 'example \\nbash vm_auto_start_or_stop.sh ${compartment_id} START vm001,vm002,v003'\n   echo 'this will try to start any vms NOT named as \"vm001,vm002,v003\" if they are in stopped state.'\n   echo -e '\\n'\n}\n\nwhile getopts \":h\" option; do\n   case $option in\n   h) # display Help\n      Help\n      exit\n      ;;\n   esac\ndone\n#############################################\n\nCOMPARTMENT_ID=\"$1\"\nACTION_TYPE=\"$2\"\nSKIP_ATP_NAMES=\"$3\"\nTEMP_FILE_NAME='list_of_apt_status'\nSTATUS_AVAILABLE='AVAILABLE'\nSTATUS_STOPPED='STOPPED'\nLOG_FILE_NAME='apt_log.txt'\n\necho \" $(date +'%m\/%d\/%Y %T') ############## compartment_id =&gt; $COMPARTMENT_ID action_type =&gt; $ACTION_TYPE  skip =&gt; $SKIP_ATP_NAMES ####################\" &gt;&gt;$LOG_FILE_NAME\n\nif [ -z \"$COMPARTMENT_ID\" ] | [ -z \"$ACTION_TYPE\" ]; then\n   echo \"\\$COMPARTMENT_ID must NOT be empty\"\n   echo \"\\$ACTION TYPE must NOT be empty\"\n   echo -e \"run: \\nbash vm_auto_start_or_stop.sh -h\\nfor help\"\n   exit 1\nfi\n\nif [ \"$ACTION_TYPE\" != \"START\" ] &amp;&amp; [ \"$ACTION_TYPE\" != \"STOP\" ]; then\n   echo -e \"ACTION_TYPE can only be START or STOP\"\n   exit 1\nfi\noci db autonomous-database list \\\n   --compartment-id $COMPARTMENT_ID \\\n   --query 'data[*].[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n   --output table &gt;$TEMP_FILE_NAME\n\ncat $TEMP_FILE_NAME | while read line; do\n   ATP=$(echo $line | cut -d'|' -f 4 | xargs)\n   ATP_NAME=$(echo $line | cut -d'|' -f 2 | xargs)\n\n   if [[ \"$line\" == *\"$STATUS_AVAILABLE\"* || \"$line\" == *\"$STATUS_STOPPED\"* ]]; then\n      if [[ \"$SKIP_ATP_NAMES\" == *\"$ATP_NAME\"* ]]; then\n         echo -e \"\\n\"\n         echo \"${ATP_NAME} is FOUND in '$SKIP_ATP_NAMES'\"\n         echo \"DO nothing with ${ATP_NAME}\"\n      else\n         echo -e \"\\n\"\n         echo \"${ATP_NAME} is NOT FOUND in '$SKIP_ATP_NAMES'\"\n         echo \"START or STOP ${ATP_NAME}\"\n\n         if [[ \"$line\" == *\"$STATUS_AVAILABLE\"* &amp;&amp; \"$ACTION_TYPE\" = \"STOP\" ]]; then\n            echo \"running ATP found\"\n            sleep 1\n            echo \"issue STOP action to this ATP ==&gt;  $ATP_NAME\"\n            sleep 5\n            oci db autonomous-database stop \\\n               --autonomous-database-id $ATP \\\n               --query 'data.[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n               --output table &gt;&gt;$LOG_FILE_NAME\n            sleep 5\n            echo \".\"\n            echo \"..\"\n            sleep 1\n            echo \".\"\n            echo \"..\"\n         elif [[ \"$line\" == *\"$STATUS_STOPPED\"* &amp;&amp; \"$ACTION_TYPE\" = \"START\" ]]; then\n            echo \"Stopped ATP found\"\n            sleep 1\n            echo \"issue start action to this ATP ==&gt; $ATP_NAME\"\n            sleep 5\n            oci db autonomous-database start \\\n               --autonomous-database-id $ATP \\\n               --query 'data.[\"display-name\",\"lifecycle-state\",\"id\"]' \\\n               --output table &gt;&gt;$LOG_FILE_NAME\n            sleep 5\n            echo \".\"\n            echo \"..\"\n            sleep 1\n            echo \".\"\n            echo \"..\"\n         fi\n\n      fi\n\n   fi\n\ndone\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to copy files from One Object storage to Another<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get a list of all the file you like to copy<\/li>\n\n\n\n<li>Save the list into a txt file locally<\/li>\n\n\n\n<li>loop through the file line by line to get the file name<\/li>\n\n\n\n<li>Create COPY work request for each file<\/li>\n\n\n\n<li>output console lines into a file to check status<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\"># to list all objects in a bucket and output into a file \n oci os object list --bucket-name MYBUCKET --all &gt; file.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">this policy is needed before COPY request can be created<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allow service objectstorage-eu-amsterdam-1 to manage object-family in compartment YOURCOMPRTMENT (see bucket replication options for this policy)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash line-numbers\">echo \"start\";\nFILE_NAME=\"\";\nI=0;\nwhile read p; do \n FILE_NAME=$(echo $p | sed 's\/I\/\/g' | sed 's\/ \/\/g');\n echo $FILE_NAME;\n #((I++))\n #if [[ \"$I\" == '3' ]]\n #then\n # echo \"Number $I\";\n # break;\n #fi\n oci os object copy --bucket-name $YOURBUCKETNAME --destination-bucket $YOURDESTBUCKETNAME --source-object-name $FILE_NAME &gt;&gt; copy_log.txt\ndone &lt; allfiles.txt<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OCI CLI Authentication OCI CLI and Configure on mac 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. The oci_chaoyuim001_api_key_public.pem file will be used to upload to OCI&#8230;<\/p>\n","protected":false},"author":1,"featured_media":811,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,11],"tags":[46,47,48],"class_list":["post-1053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-oci","tag-oci-auto-start-and-auto-stop-vms","tag-oci-cli","tag-oci-stop-and-stop-atp"],"_links":{"self":[{"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1053"}],"version-history":[{"count":30,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1487,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions\/1487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=\/wp\/v2\/media\/811"}],"wp:attachment":[{"href":"https:\/\/chaoyu.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chaoyu.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}