-
Notifications
You must be signed in to change notification settings - Fork 8
Using Parameters for Customization
You want to develop templates that can be used to provision multiple environment instances in dev, test and production.
Different environments will have to run on different hosts, use different resources, servers, and services. Such differences can be accounted for by designing flexible templates that use variables which can be set as input parameters at the time of template application.
- Open
templates/tutorial/template.yaml
in the text editor and modifyversion:
to be0.2
to denote the change we're making - Add new
changes:
list and add a note about introduction of input parameters
alias: tutorial
description: How to add parameters
version: 0.2
changes:
- Added input parameters
Parameters are optional but essential for designing flexible and reusable templates
- Add
environments:
section withdefault:
subsection where we can add our variables that can be controlled via input parameters - Add
name
andgreeting
variables as the following:
alias: tutorial
description: How to add parameters
version: 0.2
changes:
- Added input parameters
environments:
default: # global defaults defined here
foo: bar # basic variable with default value
name: ${} # required parameter, must be provided by the client
greeting: "Hello ${name}!" # parameter value can reference other parameters
NOTES:
- To improve usability always try to provide sensible default values for defined variables. This way the user does not have to specify the value unless he/she wants to overwrite the default. foo variable with default value bar is an example of that.
- Variable values can reference other variable values using
${otherVarName}
. Example of that isgreeting
variable usingname
variable. - If the default value is not possible and you want the user to provide the value at the template execution time then the variable value must use this special value of
${}
Run ant up
command to apply the template:
[user@linuxbox tutorial]$ ant up
...
BUILD FAILED
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:86: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:88: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:76: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:138: Job execution failed with 'ERROR' status: [SPMCOME0007] Un-handled internal server error. There are unresolved variables in the provided properties: [name, greeting] >> RuntimeException: There are unresolved variables in the provided properties: [name, greeting]
The error indicates the names of the variables that could not be resolved: both name
and greeting
. To resolve this problem you need to provide either both variable values or if you know that you really need to provide only name
and the greeting
default value will allow to resolve it using name
, you can provide only name
.
The input parameter values are specified as simple name=value
pairs in the environment/default/env.properties
.
Open the file add the following line
name=foo
Apply the template:
[user@linuxbox tutorial]$ ant up
Buildfile: /home/user/Tutorial/tutorial/build.xml
apply:
[cc] REST: POST http://linuxbox.user:8090/cce/templates/composite/apply/tutorial {includeHeaders=[false], properties=[id]}
[cc] REST: GET http://linuxbox.user:8090/cce/jobmanager/jobs/84 {includeHeaders=[false]}
[cc] 84 0 DONE 100 Applying template tutorial Finished applying template tutorial. DONE http://linuxbox.user:8090/cce/diagnostics/logs/local/OSGI-CCE/default.log/full?includeHeaders=false®ex=.%2A%3FVLD28132806.%2A ValidateCompositeTemplateJob:default.log
[cc] The expected values were successfully retrieved after 1 call within 1 second.
_jobresult:
[cc] REST: GET http://linuxbox.user:8090/cce/jobmanager/jobs/84 {includeHeaders=[false], properties=[status]}
[cc] REST: GET http://linuxbox.user:8090/cce/jobmanager/jobs/84 {includeHeaders=[false], properties=[statusAsString]}
BUILD SUCCESSFUL
Total time: 3 seconds
If you have not achieved the goals of this tutorial stage, use the following command to checkout everything that needed to be done so far:
[user@linuxbox tutorial]$ git checkout stage-02a -f
Switched to branch 'stage-02a'
It often makes sense to have different defaults for different environment types. An environment type is just a concept that is easy to understand if you take dev, test and production environment types, but you can come up with different types of environments in your design. Command Central allows you to control variables definition and resolution based on specified environment type. This is how it works.
Open templates/tutorial/template.yaml
in the editor and add additional dev
and test
environment types with new variables as shown below.
alias: tutorial
description: How to add parameters
version: 0.2
changes:
- Added input parameters
environments:
default: # global defaults
foo: bar # basic variable with default value
name: ${} # required parameter, must be provided by the client
greeting: "Hello ${name}!" # parameter can reference other parameters
dev: # parameters for environment.type=dev
hello: Hi # new default value for existing parameter
name: Developer # default value for previously required parameter
test: # parameters for environment.type=test
hello: ${} # now hello parameter is required
prev.name: Developer # new optional parameter
greeting: "Goodbye ${prev.name}, ${hello}, ${name}!!!" # different default
Edit environment/default/env.properties
and add the following lines:
#default
name=foo
#dev
#environment.type=dev
#test
#environment.type=test
#hello=foo
#name=bar
When no environment.type
variable is not explicitly specified then it will to the value of default
. If you want to choose a different environment type, uncomment the respective environment.type=*
line. Experiment with the different environment types and observe which variables will get resolved.
Apply the template. If the variables are not resolved the run will fail:
[user@linuxbox tutorial]$ ant up
Buildfile: /home/user/Tutorial/tutorial/build.xml
BUILD FAILED
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:86: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:88: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:76: The following error occurred while executing this line:
/home/user/Tutorial/tutorial/antcc/lib/sagenv.xml:138: Job execution failed with 'ERROR' status: [SPMCOME0007] Un-handled internal server error. There are unresolved variables in the provided properties: [name, greeting, hello] >> RuntimeException: There are unresolved variables in the provided properties: [name, greeting, hello]
Total time: 3 seconds
Experiment with the different environment.type
values and rerun the template using ant up
command.
NOTES:
- Specialized environment type can overwrite defaults defined in the
default
environment section - Specialized environment type can add new variables with or without default values
- All default section variables are still available for specialized environment type
Refer to Command Central Documentation for more details on parameters resolution logic.
If you have not achieved the goals of this tutorial stage, use the following command to checkout everything that needed to be done so far:
[user@linuxbox tutorial]$ git checkout stage-02b -f
Switched to branch 'stage-02b'
You want to capture your environment instance configuration input parameters into a file that can be placed under version control.
- Under the
environments
folder create a new subfolder calledtest1
and then anenv.properties
file in this folder. Add the following lines of theenvironment/test1/env.properties
file:
# Test1
environment.type=test
hello=foo1
name=bar1
- Create
environment/test2/env.properties
with the following content:
# Test2
environment.type=test
hello=foo2
name=bar2
- Apply template to provision environment instances
test1
andtest2
using their instance configuration files:
[user@linuxbox tutorial]$ ant up -Denv=test1
...
BUILD SUCCESSFUL
Total time: 3 seconds
[user@linuxbox tutorial]$ ant up -Denv=test2
...
BUILD SUCCESSFUL
Total time: 3 seconds
If you have not achieved the goals of this tutorial stage, use the following command to checkout everything that needed to be done so far:
[user@linuxbox tutorial]$ git checkout stage-02c -f
Switched to branch 'stage-02c'
Congratulations! You have successfully developed a template that supports customization via parameters for different environment types. You also defined two environment instance configurations.
- Overview
- Project Structure and Navigation Through the Tutorial Steps
- Hello World Basic Development Life Cycle
- Using Parameters for Customization
- Bootstrapping Managed Installations
- Installing Products
- Installing Fixes
- Installing and Updating Core Products
- Creating Run time Instances
- Configuring Instances
- End 2 End Remote Test Environment Setup
- What's Next and Additional Resources