{"id":1741,"date":"2018-05-01T12:32:52","date_gmt":"2018-05-01T10:32:52","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?page_id=1741"},"modified":"2018-05-01T12:33:04","modified_gmt":"2018-05-01T10:33:04","slug":"ohctl","status":"publish","type":"page","link":"https:\/\/www.ludovicocaldara.net\/dba\/source-code\/ohctl\/","title":{"rendered":"ohctl"},"content":{"rendered":"<pre class=\"lang:sh decode:true  \">#!\/bin\/bash\r\n\r\nF_usage() {\r\n        cat &lt;&lt;EOF\r\n\r\n        Purpose : Management of Golden Images (Oracle Homes)\r\n\r\n        Usage   : To list the available images:\r\n                    $0 -l\r\n                  To install an image on the localhost:\r\n                    $0 -i goldenimage [-n newname] [-r]\r\n                  To create an image based on the current OH:\r\n                    $0 -c [-n newname] [ -f ]\r\n                  To remove a golden image from the repository:\r\n                    $0 -d goldenimage [ -f ]\r\n\r\n        Options : -l                    List the available Oracle Homes in the golden image repository\r\n                  -i goldenimage        Installs locally the specified golden image. (If already deployed, an error is thrown)\r\n                                        if the option -l is given, the list action has the priority over the deploy.\r\n                  -n newname            Specify a new name for the Oracle Home: use it in case you need to patch\r\n                                        and create a new Golden Image from it or if you want to change the Golden Image name\r\n                                        for the current Oracle Home you are converting to Image.\r\n                                        When creating a new Image (-c), it takes the basename of the OH by default, and not the\r\n                                        OHname inside the inventory.\r\n                  -c                    Creates a new Golden Image from the current Oracle Home.\r\n                  -d goldenimage        Removes the golden image from the repository\r\n                  -f                    If the Golden Image to be created exists, force the overwrite.\r\n                  -r                    Link with RAC option (install only)\r\n\r\n        Example : $0 -i DB12_1_0_2_BP170718_home1 -n DB12_1_0_2_BP171018_home1\r\n                                        installs the Oracle Home DB12_1_0_2_BP170718_home1 with new name DB12_1_0_2_BP171018_home1\r\n                                        in order to apply the Bundle Patch 171018 on it\r\n\r\n                  $0 -i DB12_1_0_2_BP170718_home1\r\n                                        installs the Oracle Home DB12_1_0_2_BP170718_home1 for normal usage\r\n\r\n                  $0 -c -n DB12_1_0_2_BP180116\r\n                                        Creates a new Golden Image named DB12_1_0_2_BP180116 from the current ORACLE_HOME\r\n\r\n                  $0 -c -f\r\n                                        Creates a new Golden Image with the name of the current OH basename, overwriting\r\n                                        the eventual current image.\r\n                                        E.g. if the current OH is \/ccv\/app\/oracle\/product\/DB12_1_0_2_BP180116_RAC, the new GI name\r\n                                         will be \"DB12_1_0_2_BP180116_RAC\"\r\n\r\nEOF\r\n}\r\n\r\nfunction F_colordef () {\r\n        #######################################\r\n        # define colors for bash candiness\r\n        # colxxx = foreground plain\r\n        # colbxxx = foreground bold\r\n        # coluxxx = foreground underline\r\n        # colbgxxx = background\r\n        # blk=black grn=green ylw=yellow pur=purple cyn=cyan wht=white\r\n        # They must be used with echo -e $colxxx\r\n        colblk='\\e[0;30m' # Black - Regular\r\n        colred='\\e[0;31m' # Red\r\n        colgrn='\\e[0;32m' # Green\r\n        colylw='\\e[0;33m' # Yellow\r\n        colblu='\\e[0;34m' # Blue\r\n        colpur='\\e[0;35m' # Purple\r\n        colcyn='\\e[0;36m' # Cyan\r\n        colwht='\\e[0;37m' # White\r\n        colbblk='\\e[1;30m' # Black - Bold\r\n        colbred='\\e[1;31m' # Red\r\n        colbgrn='\\e[1;32m' # Green\r\n        colbylw='\\e[1;33m' # Yellow\r\n        colbblu='\\e[1;34m' # Blue\r\n        colbpur='\\e[1;35m' # Purple\r\n        colbcyn='\\e[1;36m' # Cyan\r\n        colbwht='\\e[1;37m' # White\r\n        colublk='\\e[4;30m' # Black - Underline\r\n        colured='\\e[4;31m' # Red\r\n        colugrn='\\e[4;32m' # Green\r\n        coluylw='\\e[4;33m' # Yellow\r\n        colublu='\\e[4;34m' # Blue\r\n        colupur='\\e[4;35m' # Purple\r\n        colucyn='\\e[4;36m' # Cyan\r\n        coluwht='\\e[4;37m' # White\r\n        colbgblk='\\e[40m'   # Black - Background\r\n        colbgred='\\e[41m'   # Red\r\n        colbggrn='\\e[42m'   # Green\r\n        colbgylw='\\e[43m'   # Yellow\r\n        colbgblu='\\e[44m'   # Blue\r\n        colbgpur='\\e[45m'   # Purple\r\n        colbgcyn='\\e[46m'   # Cyan\r\n        colbgwht='\\e[47m'   # White\r\n        colrst='\\e[0m'    # Text Reset\r\n}\r\n\r\nF_list_OH () {\r\n\r\n        F_colordef\r\n        echo\r\n        echo \"Listing existing golden images:\"\r\n\r\n        RESULT=`$SQLPLUS -S -L ${REPO_CREDENTIALS} &lt;&lt;EOF  | grep \";\"\r\n        set line 200 pages 1000\r\n        set feed off head off\r\n        col name format a32\r\n        alter session set nls_timestamp_format='YYYY-MM-DD';\r\n        select name||';'||created||';'||fullpath from oh_golden_images order by created desc;\r\nEOF\r\n`\r\n        echo\r\n        printf \"%-35s %-10s %-18s\\n\" \"OH_Name\" \"Created\" \"Installed locally?\"\r\n        echo \"----------------------------------- ---------- ------------------\"\r\n\r\n        for line in $RESULT ; do\r\n                L_GI_Name=`echo $line | awk -F\\; '{print $1}'`\r\n                L_GI_Date=`echo $line | awk -F\\; '{print $2}'`\r\n                L_GI_Path=`echo $line | awk -F\\; '{print $3}'`\r\n                L_Installed=`F_OH_Installed \"$L_GI_Name\"`\r\n                printf \"%-35s %-10s %-18s\\n\" \"$L_GI_Name\" \"$L_GI_Date\" \"$L_Installed\"\r\n        done\r\n}\r\n\r\nF_OH_Installed () {\r\n        CENTRAL_ORAINV=`grep ^inventory_loc \/etc\/oraInst.loc | awk -F= '{print $2}'`\r\n        grep \"&lt;HOME NAME=\\\"$1\\\"\" $CENTRAL_ORAINV\/ContentsXML\/inventory.xml | grep -v \"REMOVED=\\\"T\\\"\" &gt;\/dev\/null\r\n        if [ $? -eq 0 ] ; then\r\n                echo -e \"${colgrn}Installed${colrst}\"\r\n        else\r\n                echo \"Not installed\"\r\n        fi\r\n}\r\n\r\nF_OH_Component () {\r\n        comp_file=$ORACLE_HOME\/inventory\/ContentsXML\/comps.xml\r\n        comp_xml=`grep \"COMP NAME\" $comp_file | head -1`;\r\n        comp_name=`echo $comp_xml | tr ' ' '\\n' | grep ^NAME= | awk -F\\\" '{print $2}'`;\r\n        echo $comp_name\r\n}\r\n\r\nF_OH_Version () {\r\n        comp_file=$ORACLE_HOME\/inventory\/ContentsXML\/comps.xml\r\n        comp_xml=`grep \"COMP NAME\" $comp_file | head -1`;\r\n        comp_name=`echo $comp_xml | tr ' ' '\\n' | grep ^NAME= | awk -F\\\" '{print $2}'`;\r\n        comp_vers=`echo $comp_xml | tr ' ' '\\n' | grep ^VER= | awk -F\\\" '{print $2}'`;\r\n\r\n        if [ \"$comp_name\" == \"oracle.server\" ] ; then\r\n                comp_vers=`grep \"PATCH NAME=\\\"oracle.server\\\"\" $comp_file 2&gt;\/dev\/null | tr ' ' '\\n' | grep ^VER= | awk -F\\\" '{print $2}'`;\r\n        fi\r\n        echo $comp_vers\r\n}\r\n\r\nF_Image_Exists () {\r\n        L_search=$1\r\n        echo \"Checking existence of image $L_search\"\r\n\r\n        $SQLPLUS -S -L ${REPO_CREDENTIALS} &lt;&lt;EOF  | grep -e \";\\|false\"\r\n        set line 200 pages 1000\r\n        set feed off head off\r\n        col name format a32\r\n        alter session set nls_timestamp_format='YYYY-MM-DD';\r\n        select name||';'||created||';'||fullpath from oh_golden_images where name='${L_search}' union select 'false' from dual where not exists (select 1 from oh_golden_images where name='${L_search}');\r\nEOF\r\n\r\n        #select name||';'||created from oh_golden_images where name='${L_search}' ;\r\n}\r\n\r\n\r\nunset JAVA_HOME\r\n\r\nREPO_OWNER=scott\r\nREPO_PWD=tiger\r\nREPO_CONN=\"\/\/localhost:1521\/ORCL\"\r\nREPO_CREDENTIALS=${REPO_OWNER}\/${REPO_PWD}@${REPO_CONN}\r\n\r\nPRODUCT_INSTALL_PATH=\"\/u01\/app\/oracle\/product\"\r\nGOLDEN_IMAGE_DEST=\"\/share\/oracle\/oh_repository\/golden_images\"\r\nWORKING_COPY_DEST=\"\/share\/oracle\/oh_repository\/working_copies\"\r\n\r\n\r\nwhile getopts \"hli:n:cd:fr\" OPTION ; do\r\n        case \"${OPTION}\" in\r\n                l)      L_Action=list\r\n                        ;;\r\n                i)      L_Golden_Image=${OPTARG}\r\n                        L_Action=${L_Action:-install}\r\n                        if [[ \"$L_Golden_Image\" =~ [^a-zA-Z0-9_] ]] ; then\r\n                                echo \"The OH name can contain only alphanumeric and underscores\"\r\n                                exit 1\r\n                        fi\r\n                        ;;\r\n                n)      L_New_Name=${OPTARG}\r\n                        if [[ \"$L_New_Name\" =~ [^a-zA-Z0-9_] ]] ; then\r\n                                echo \"The OH name can contain only alphanumeric and underscores\"\r\n                                exit 1\r\n                        fi\r\n                        ;;\r\n                c)      L_Action=${L_Action:-create}\r\n                        ;;\r\n                d)      L_Golden_Image=${OPTARG}\r\n                        L_Action=${L_Action:-remove}\r\n                        ;;\r\n                f)      L_GI_Overwrite=yes\r\n                        ;;\r\n                r)      L_Link_RAC=yes\r\n                        ;;\r\n                h)      F_usage\r\n                        exit 1\r\n                        ;;\r\n                ?)      F_usage\r\n                        exit 1\r\n                        ;;\r\n        esac\r\ndone\r\n\r\n\r\nL_Action=${L_Action:-list}\r\n\r\nif [ $L_Action == \"list\" ] ; then\r\n        setoh ic\r\n        SQLPLUS=$ORACLE_HOME\/sqlplus\r\n        F_list_OH\r\nfi\r\n\r\nif [ $L_Action == \"install\" ] ; then\r\n\r\n        setoh ic\r\n        SQLPLUS=$ORACLE_HOME\/sqlplus\r\n        # if no new OH name specified, get the golden image name\r\n        L_New_Name=${L_New_Name:-$L_Golden_Image}\r\n\r\n        # - check if image to install exists\r\n        RESULT=`F_Image_Exists $L_Golden_Image`\r\n        if [ \"$RESULT\" == \"false\" ] ; then\r\n                echo \"The image $L_Golden_Image does not exist\"\r\n                exit 1\r\n        else\r\n                echo \"OK, the image exists.\"\r\n                L_Image_Created=`echo $RESULT | awk -F\\; '{print $2}'`\r\n                L_Image_Path=`echo \"$RESULT\" | awk -F\\; '{print $3}'`\r\n        fi\r\n\r\n        # - check if OH name to install is not already installed\r\n        L_Installed=`F_OH_Installed \"$L_New_Name\"`\r\n        if [ \"$L_Installed\" == \"Installed\" ] ; then\r\n                echo \"The image $L_New_Name is already installed on the local host. The script will exit.\"\r\n                exit 1\r\n        fi\r\n\r\n        # - check if the zip exists\r\n        if [ -f $L_Image_Path ] ; then\r\n                echo \"Zip file exists.\"\r\n        else\r\n                echo \"The image Zip file $L_Image_Path does NOT exist. The script will exit.\"\r\n                exit 1\r\n        fi\r\n\r\n        # - check if the destination directory exist\r\n        L_Install_Path=\"${PRODUCT_INSTALL_PATH}\/$L_New_Name\"\r\n        if [ -d $L_Install_Path ] ; then\r\n                echo \"The installation directory $l_Install_Path already exist. The script will exit.\"\r\n                exit 1\r\n        fi\r\n\r\n        # - create dest directory\r\n        mkdir -p $L_Install_Path\r\n        if [ $? -ne 0 ] ; then\r\n                echo \"Cannot create installation directory $l_Install_Path. The script will exit.\"\r\n                exit 1\r\n        fi\r\n\r\n        # - unzip the image from the zip\r\n        unzip -q -d $L_Install_Path $L_Image_Path\r\n        if [ $? -ne 0 ] ; then\r\n                echo \"The unzip failed. The script will exit.\"\r\n                exit 1\r\n        else\r\n                echo \"The unzip completed successfully.\"\r\n        fi\r\n\r\n        # - clone the home with newname\r\n        export ORACLE_HOME=$L_Install_Path\r\n        SQLPLUS=$ORACLE_HOME\/bin\/sqlplus\r\n        RUNINST=$ORACLE_HOME\/oui\/bin\/runInstaller\r\n\r\n        L_Clone_Command=\"$RUNINST -clone -waitForCompletion -silent ORACLE_HOME=$ORACLE_HOME ORACLE_BASE=$ORACLE_BASE ORACLE_HOME_NAME=$L_New_Name\"\r\n\r\n        echo $L_Clone_Command\r\n        $L_Clone_Command\r\n\r\n        if [ $? -eq 0 ] ; then\r\n                echo \"Clone command completed successfully.\"\r\n        else\r\n                echo \"There was a problem during the clone command. The script will exit.\"\r\n                exit 1\r\n        fi\r\n\r\n        if [ \"${L_Link_RAC}\" == \"yes\" ] ; then\r\n                pushd $ORACLE_HOME\/rdbms\/lib\r\n                make -f ins_rdbms.mk rac_on\r\n                make -f ins_rdbms.mk ioracle\r\n                popd\r\n        fi\r\n\r\n        # - run setasmgid\r\n        if [ -x \/etc\/oracle\/setasmgid ] ; then\r\n                echo \"setasmgid found: running it on Oracle binary\"\r\n                \/etc\/oracle\/setasmgid oracle_binary_path=$ORACLE_HOME\/bin\/oracle\r\n        else\r\n                echo \"setasmgid not found: ignoring\"\r\n        fi\r\n\r\n        # - create symlinks for ldap, sqlnet and tnsnames.ora\r\n        TNS_ADMIN=${TNS_ADMIN:-\/var\/opt\/oracle}\r\n        ln -s $TNS_ADMIN\/sqlnet.ora   $ORACLE_HOME\/network\/admin\/sqlnet.ora\r\n        ln -s $TNS_ADMIN\/tnsnames.ora $ORACLE_HOME\/network\/admin\/tnsnames.ora\r\n        ln -s $TNS_ADMIN\/ldap.ora     $ORACLE_HOME\/network\/admin\/ldap.ora\r\n\r\n        # - check if OH name installed exist in the central inventory\r\n        L_Installed=`F_OH_Installed \"$L_New_Name\"`\r\n        if [ \"$L_Installed\" != \"Installed\" ] ; then\r\n                echo \"The image $L_New_Name has been installed but it is not in the inventory. Please check.\"\r\n        else\r\n                echo \"The image $L_New_Name has been installed and exists in the inventory.\"\r\n        fi\r\n\r\n        # - prompt to run root.sh to the user\r\n        echo\r\n        echo \"Installation completed. Please run $ORACLE_HOME\/root.sh as root before using the new home.\"\r\n\r\n\r\nfi\r\n\r\nif [ $L_Action == \"create\" ] ; then\r\n        SQLPLUS=$ORACLE_HOME\/bin\/sqlplus\r\n\r\n        # check that a Oracle Home is set\r\n        if [ -z \"ORACLE_HOME\" ] ; then\r\n                echo \"ORACLE_HOME not set\"\r\n                exit 1\r\n        fi\r\n        L_Component=`F_OH_Component $ORACLE_HOME`\r\n        if [ \"$L_Component\" != \"oracle.server\" ] ; then\r\n                echo \"Please set a database ORACLE_HOME to create the Golden Image\"\r\n                exit 1\r\n        fi\r\n\r\n        # if no new OH name specified, get the basename\r\n        L_OH_Basename=`basename $ORACLE_HOME`\r\n        L_New_Name=${L_New_Name:-$L_OH_Basename}\r\n\r\n        # if golden image already exist, force is required\r\n        RESULT=`F_Image_Exists $L_New_Name`\r\n        if [ \"$RESULT\" != \"false\" ] ; then\r\n                if [ \"$L_GI_Overwrite\" != \"yes\" ] ; then\r\n                        echo \"Image $L_New_Name already exists but -f not specified. Exiting.\"\r\n                        exit 1\r\n                else\r\n                        echo \"Image $L_New_Name already exists but -f specified. The script will continue.\"\r\n                fi\r\n        fi\r\n\r\n        # Register Oracle Home in OH_REGISTRY\r\n        # $SCRIPT_DIR\/DB_OH_ohregistry.sh register name=$L_New_Name\r\n\r\n        echo\r\n        echo\r\n        echo \"Creating the new Golden Image $L_New_Name\"\r\n        echo\r\n\r\n\r\n        # Copy to NFS working copy\r\n        echo \"Cleaning previous working copy\"\r\n        WC=$WORKING_COPY_DEST\/$L_New_Name\r\n        [ -d $WC ] &amp;&amp; rm -rf $WC\r\n\r\n        echo \"Copying the OH to the working copy\"\r\n        mkdir -p  $WC\r\n        cp -rp $ORACLE_HOME\/* $WC\/ 2&gt;\/tmp\/ohctl.err\r\n\r\n        # Cleanup files\r\n        echo \"Cleansing files in Working Copy\"\r\n        rm -rf $WC\/log\/$HOSTNAME\r\n        rm -rf $WC\/log\/diag\/rdbms\/*\r\n        rm -rf $WC\/gpnp\/$HOSTNAME\r\n        find $WC\/gpnp -type f -exec rm {} \\; 2&gt;\/dev\/null\r\n        rm -rf $WC\/cfgtoollogs\/*\r\n        rm -rf $WC\/crs\/init\/*\r\n        rm -rf $WC\/cdata\/*\r\n        rm -rf $WC\/crf\/*\r\n        rm -rf $WC\/admin\/*\r\n        rm -rf $WC\/network\/admin\/*.ora\r\n        rm -rf $WC\/crs\/install\/crsconfig_params\r\n        find $WC -name '*.ouibak' -exec rm {} \\; 2&gt;\/dev\/null\r\n        find $WC -name '*.ouibak.1' -exec rm {} \\; 2&gt;\/dev\/null\r\n        # rm -rf $WC\/root.sh\r\n        find $WC\/rdbms\/audit -name '*.aud' -exec rm {} \\; 2&gt;\/dev\/null\r\n        rm -rf $WC\/rdbms\/log\/*\r\n        rm -rf $WC\/inventory\/backup\/*\r\n        rm -rf $WC\/dbs\/*\r\n\r\n        # create zip\r\n        echo \"Creating the Golden Image zip file\"\r\n        [ -f $GOLDEN_IMAGE_DEST\/$L_New_Name.zip ] &amp;&amp; rm $GOLDEN_IMAGE_DEST\/$L_New_Name.zip\r\n        cd $WC\r\n        zip -r $GOLDEN_IMAGE_DEST\/$L_New_Name.zip . &gt;\/dev\/null\r\n        cd $OLDPWD\r\n\r\n        # add image to OH_GOLDEN_IMAGES\r\n        $SQLPLUS -S -L ${REPO_CREDENTIALS} &lt;&lt;EOF\r\n        set feed off\r\n        delete from oh_golden_images where name='${L_New_Name}';\r\n        insert into oh_golden_images (name, created, fullpath) values ( '${L_New_Name}', systimestamp, '$GOLDEN_IMAGE_DEST\/$L_New_Name.zip');\r\n        commit;\r\n        exit\r\nEOF\r\n\r\nfi\r\n\r\nif [ $L_Action == \"remove\" ] ; then\r\n        echo \"Remove\"\r\n        RESULT=`F_Image_Exists $L_Golden_Image`\r\n        if [ \"$RESULT\" == \"false\" ] ; then\r\n                echo \"The image $L_Golden_Image does not exist\"\r\n                exit\r\n        else\r\n                echo \"OK, the image exists.\"\r\n                if [ \"$L_GI_Overwrite\" != \"yes\" ] ; then\r\n                        echo \"Image $L_New_Name marked for delete but -f not specified. Exiting.\"\r\n                        exit 1\r\n                fi\r\n                L_Image_Path=`echo \"$RESULT\" | awk -F\\; '{print $3}'`\r\n                [ -f \"$L_Image_Path\" ] &amp;&amp; rm $L_Image_Path\r\n                $SQLPLUS -S -L ${REPO_CREDENTIALS} &lt;&lt;EOF\r\n                delete from oh_golden_images where name='${L_Golden_Image}';\r\n                commit;\r\n                exit\r\nEOF\r\n        exit\r\n\r\n        fi\r\nfi<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash F_usage() { cat &lt;&lt;EOF Purpose : Management of Golden Images (Oracle Homes) Usage : To list the available images: $0 -l To install an image on the localhost: $0 -i goldenimage [-n newname] [-r] To create an image based &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/source-code\/ohctl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1739,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1741","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/pages\/1741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=1741"}],"version-history":[{"count":1,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/pages\/1741\/revisions"}],"predecessor-version":[{"id":1742,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/pages\/1741\/revisions\/1742"}],"up":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/pages\/1739"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=1741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}