Friday, 31 March 2017

Oracle Cloud: create Docker container with Java via CLI

This tutorial demonstrates, how to set up an instance of Oracle Application Container Cloud Service (ACCS, under the hood running Docker) with a pre-configured Java environment via the PaaS Service Manger (PSM) command line interface (CLI).
If you have followed my former postings on this
you should be ready to go when running a Windows client. Whatever OS and editor you are using, the tutorial expects that you have setup your psm, so it speaks to your Oracle Cloud Identity Domain.
The general idea of the Cloud Stack Manager is, that you define your wanted environment in a template first. That environment might consist of several instances and is called a stack. When you got your template, upload it to the Oracle Cloud Stack Manager and create as many instances, as you like.
I have always been a friend of starting with the simplest possible example and don't move on before that is understood and running. So this tutorial shows step-by-step how to set up a stack with a single ACCS Java instance.

1. Create a template

Pick your favorite editor and write your first template. Ideally, the editor supports running shell commands in the background and pipes the output to an editor window. 
Here is my template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
    template:
        templateName: Simple-Java-Template
        templateVersion: 42.0
        templateDescription: "Hello Template World"
        parameters:
            containerName:
                label: Container Name
                description: Please enter the name of the Java Container
                type: string
                mandatory: true
                sensitive: false
                minLength: 2
                allowedPattern: "[a-zA-Z]*"
            parameter1:
                label: Parameter1
                description: Some Parameter 1
                type: string
                mandatory: false
                sensitive: false
            parameter2:
                label: Parameter2
                description: Some Parameter 2
                type: string
                mandatory: false
                sensitive: false        
        parameterGroups:
          - label: Mandatory Parameters
            parameters: 
              - containerName
          - label: Some Other Parameters
            parameters: [ parameter1, parameter2 ]
        resources:
            abContainer: 
                type: apaas
                parameters:
                    name: { "Fn::GetParam":containerName }
                    subscription: MONTHLY
                    runtime: Java

The first general section is quite obvious: a name, version and description.
Then comes the parameters section. In fact, I only need one parameter to ask for the name of the container. But to demonstrate the Parameter Groups, I added two unused parameters.
The parameterGroups section covers the groups Mandatory Parameters and Some Other Parameters. These groups are for the UI to visually group the parameters.
The resources section is where the wanted resources are listed with their parameters. Here we only have one ACCS instance with Java taste. For the name parameter, we insert the value of the containerName parameter by using Fn::GetParam. The only other mandatory parameter is the chosen subscription.

2. Import the template

Now that we have a template, we are ready to go. It is a good idea to validate the template first by using PSM with the following line:

psm stack validate-template -f Simple-Java-Template.yaml

The output should look like this (from Notepad++)


As everything is OK with the template, it can be imported by using


D:\programme\python\Scripts\psm.cmd stack import-template -f "D:\Project\PSM\Simple-Java-Template.yaml"

and the output should look like this:


Switch to the UI to double check that the template is available in the Cloud Stack Manager


Of course this can also be done using the PSM command


psm stack list-templates

but the result gets quite a bit long. Just the relevant first lines:


3. Create an instance from the template

Now that we have a valid template imported into Cloud Stack Manager, we can start creating instances based on that.

When creating an instance via the UI, the following dialog comes up

That shows what the parameter groups are good for: they group the parameters.

But this tutorial is about using the command line. Create an instance using the PSM via

psm stack create -n ArneStack -t Simple-Java-Template -f ROLLBACK -p containerName:ArneJava

Copy the returned Job ID and check the status via

psm stack operation-status -j 4761735

and the output should look like


The same can be found in the UI when clicking on the status text.

Wait until psm stack operation-status returns something like


{
    "activityDate":"2017-03-31T13:37:31.896+0000",
    "message":"Creation of stack ArneStack successful."
}

Double check by executing


psm stack list

to see your newly created stack listed:


4. Test and delete the instance

Go the the UI and click on the name of the stack. This will bring you to the Stack Overview.

Click on the Application Web URL


That should bring up the test page. So with the instance is up and running this tutorial is finished. Delete the instance with


psm stack delete -n ArneStack

5. More information

Two more complex examples are delivered with your Oracle Cloud subscription, you will find them in the templates section of the Cloud Stack Manager UI. One with a MySQL DB and a PHP container (Oracle-LMP, a LAMP stack without Apache) and one with an Oracle DB and a WebLogic Server (Oracle-JCS-DBCS-Template).

Some Documentation: