Oracle by Example brandingCreating a Custom Action in an Oracle Cloud Stack Template

section 0Before You Begin

This 25-minute tutorial shows you how to add a custom action to a cloud resource in an existing Oracle Cloud Stack template.

Background

In Oracle Cloud Stack, custom actions enable template authors to run commands on a cloud resource's operating system, after the resource is provisioned. For example, you can use a custom action to create a database schema, deploy an application, or modify the default OS configuration. You can also use template functions to assign values to any required environment variables in your OS commands.

You cannot add custom actions to a template using the Oracle Cloud Stack console. You must edit the template file manually.

Custom actions are supported only for customer-managed cloud resources that provide a secure shell (SSH) interface. This tutorial executes a custom action on an Oracle Database Cloud Service instance.

What Do You Need?


section 1Export the Template

  1. Sign in to Oracle Cloud.
  2. From the Oracle Cloud Infrastructure Console, click the Navigation menu Dashboard Menu Icon at the top left corner of the page, expand Platform Services, and then select Java.

    Note: On some older Oracle Cloud accounts, you use the Oracle Cloud Infrastructure Classic Console. Click the Navigation menu Dashboard Menu Icon, and then select Java.

    Tip: You can access Cloud Stack from any Oracle Platform service.

  3. From the Oracle Java Cloud Service console, click the Navigation menu Dashboard Menu Icon at the top left corner of the page, and then select Cloud Stack.
  4. From the Oracle Cloud Stack console, click Templates.
    Templates button
    Description of the illustration click_templates.png
  5. Click the Download icon Dashboard Menu Icon to the right of the MyStackTemplate template. Save the template file to your local computer.
  6. Open the file MyStackTemplate.yaml in a text editor.
  7. Increment the value of templateVersion. For example, if the current is value is 1.0.0, change it to 1.0.1.
    templateVersion: 1.0.1

section 2Create a Software Component Resource

Tip: You can also download the finished MyStackTemplate.yaml.

  1. Within resources, add a resource named dbactions and set its type to SoftwareComponent.
    resources:
      dbactions:
        type: SoftwareComponent
      oracledb1: ...
  2. Within this new resource, add the following configuration named createSchema.
    resources:
      dbactions:
        type: SoftwareComponent
        parameters:
          configs:
            - actions: [CREATE]
              name: createSchema
              config: 
                'Fn::Base64':
                  'Fn::Join':
                    - "\n"
                    -
                      - echo "Commands go here"
              runAsUser: oracle
              timeout: 1
              continueOnFailure: false

    Be sure to use the exact spacing shown here. If you prefer, you can copy code-snippet-1.txt.

  3. Add the following OS commands as a sequence of strings in the Fn::Join function.
    'Fn::Join':
      - "\n"
      -
        - scriptFile=~/createSchema.sh
        - scriptLog=~/createSchema.log
        - rm -f ${scriptFile}
        - touch ${scriptFile}
        - touch ${scriptLog}
        - echo "sqlplus / as sysdba << EOF" >> ${scriptFile}
        - echo "drop user c##myappuser cascade;" >> ${scriptFile}
        - echo "create user c##myappuser identified by ${schemaPassword};" >> ${scriptFile}
        - echo "grant create session, create table, unlimited tablespace to c##myappuser;" >> ${scriptFile}
        - echo "quit" >> ${scriptFile}
        - echo "EOF" >> ${scriptFile}
        - sh ${scriptFile} >> ${scriptLog}

    These commands create and run a script file. The script creates a new user in the local database. If you prefer, you can copy code-snippet-2.txt.

  4. Within the Software Component resource, below the configs node, add an input named schemaPassword.
    dbactions:
      type: SoftwareComponent
      parameters:
        configs:
          ...
        inputs:
          - name: schemaPassword

    The ${schemaPassword} environment variable is used in the OS commands for this custom action.


section 3Add the Software Component to the Database Resource

  1. Locate the existing oracledb1 resource in the template file.
    resources:
      dbactions:
        type: SoftwareComponent
        parameters:
          ...
      oracledb1:
        type: DBaaS
        parameters:
          ...
  2. Below the parameters for oracledb1, add the following configuration named oracledb1-dbactions.
    oracledb1:
      type: DBaaS
      parameters:
        ...
      configs:
        - name: oracledb1-dbactions
  3. Use the Fn::GetResource function to get the dbactions resource.
    oracledb1:
      type: DBaaS
      parameters:
        ...
      configs:
        - name: oracledb1-dbactions
          config:
            'Fn::GetResource': dbactions
  4. Assign a value to the schemaPassword input. Use the Fn::GetParam function to get the value of the dbPassword template parameter.
    oracledb1:
      type: DBaaS
      parameters:
        ...
      configs:
        - name: oracledb1-dbactions
          config:
            'Fn::GetResource': dbactions
          params:
            schemaPassword:
              'Fn::GetParam': dbPassword

    If you prefer, you can copy code-snippet-3.txt. Or you can download the finished MyStackTemplate.yaml.

  5. Save your changes.

section 4Import the Template

  1. Return to the Oracle Cloud Stack console.
  2. Click Templates.
  3. Click Import.
  4. Use your browser to select MyStackTemplate.yaml on your local computer. Then click Import.

section 5Test the Template

  1. To the right of MyStackTemplate, click Create a stack with this template Create Stack Icon.
  2. Configure the stack's parameters:
    • Name: MyCustomActionStack
    • Template: MyStackTemplate
    • On Failure Retain Resources: Yes
    • Database Password: Enter a password
    • Confirm Database Password: Enter a password

    The password must be 8 to 30 characters in length, and must contain at least one lowercase letter, one uppercase letter, one number, and one of these symbols: _, #, $. The password must begin with a letter.

  3. To the right of SSH Public Key, click Edit.
  4. Select the option Create a New Key, and then click Enter.
  5. Click Download, and then click Done.
  6. Click Next.
  7. Click Confirm.
  8. Refresh the Stacks page periodically until the stack is created.

section 6Verify the Custom Action

  1. From the Stacks page, click MyCustomActionStack.
  2. Under Resources, locate oracledb1-dbactions.
    Action resource
    Description of the illustration stack_resources_action.png
  3. To the right of oracledb1-dbactions, click the Success link. Use the log to verify that the custom action was executed.
    User script is executed successfully.
  4. Click MyCustomActionStackDB to view the Oracle Database Cloud Service instance.
  5. Within the Resources section, identify the Public IP address.
  6. Extract the contents of the archive file sshkeybundle.zip that you downloaded when you created the stack.
  7. Use an SSH client to connect to the public IP address as the opc user. Provide the location of the extracted private key file. For example:
    ssh -i privateKey opc@IP_address
  8. After connecting to the node, switch to the oracle user.
    sudo su oracle
  9. View the contents of the custom action's log file.
    $ cat ~/createSchema.log
    
    ...
    SQL>
    User created.
    
    SQL>
    Grant succeeded.
    ...
  10. Connect to the local database as myappuser. Enter the same password that you specified when you created the stack.
    sqlplus c##myappuser/Your_DB_password

    Note: If you make additional changes to your template, you will also need to change the templateVersion prior to importing it again.


more informationWant to Learn More?