In this previous article ‘Pattern-type (Part 6): How to add operation with SmartCloud Application Services‘, I explained how to add operation on a pattern-type. Operations provide you the ability to take actions on a deployed pattern/virtual application. These actions can be setting parameters and take actions based on these parameters by launching a specified script and this is done by creating an operation.json file in the appmodel.
Tweak provides the same functionality but the main difference is that the parameters are persisted in the deployed pattern, that’s mean if a server crashes and has to be regenerated, you can use the tweak parameter to regenerate this server and setup it as the previous one. The well known example is the WAR/EAR file in the WebApp pattern. When you set the WAR/EAR file, the path to this file is persisted in the deployed pattern and then when a server have to be restarted (or started in case of scaling-out), the server can load the WAR/EAR file because its location was persisted which is not the case with operation.
Tweaked parameters can be linked or not to component attributes.
The AppModel
In the appmodel, we will create a tweak.json and operation files. I will explain later why we need also an operation.json file.
You can see we have in the metadata.json a role called AppManager and this AppManager has an attribute called ‘cacheSize’.
[ { "id": "AppManager", "type": "component", "label": "Application Manager", "attributes": [ { "id": "cacheSize", "type": "number", "required": false, "label": "Cache Size" } ] } ]
Let’s look at the tweak.json.
[ { "id": "AppManager.logSize", "label": "Log Size", "type": "number", "description": "Log Size value" }, { "id": "AppManager.cachesize", "ref-component": "AppManager", "label": "Cache Size", "ref-id": "cacheSize" } ]
You can see, I defined two tweaked parameters, the first one ‘logSize’ and the second one, which is link to the ‘AppManager’ component attribute ‘cacheSize’ called here ‘cachesize’.
For the ‘cacheSize’, I don’t need to specify the type as the referenced component attribute already have this information.
In order for the UI to show the tweak ‘operation’, we have to create a specific operation. It is important that the ‘id’ of this operation is ‘configuration’.
{ "AppManager": [ { "id": "configuration", "label": "Configuration", "description" : "Parameters that can be tweaked", "script": "change.py", } ] }
This operation is linked to the ‘AppManager’ role, the ‘id’ is ‘configuration’ and the script to be launched is specified by the ‘script’ attribute, here ‘change.py’.
Let’s look now at the ‘change.py’:
import subprocess import maestro import os import logging import gettext import sys logger = logging.getLogger('AppManager/change.py') logger.debug("Change sampleTweak"); cacheSize = maestro.operation['parms']['cachesize'] logger.debug("Tweak CacheSize %s" % cacheSize); logSize = maestro.operation['parms']['logSize'] logger.debug("Tweak LogSize %s" % logSize);
You can see, the tweaked parameter values can be retreived using the maestro.operation[‘parms’][‘parameter_name’].
The persistance is done in the parameters.json located in the deployed pattern in the storehouse.
{ "$$1": { "value": 10, "role": "AppManager-sampleTweak.AppManager", "id": "AppManager.cachesize" }, "$$2": { "value": 11, "role": "AppManager-sampleTweak.AppManager", "id": "AppManager.logSize" } }
As I already tweak the parameters with value ’10’ and ’11’, we can see these values in the ‘value’ attribute of the parameter.json.
Topology
Nothing special as to be done in the *.vm, except if you want to retreive the ‘cacheSize’ value using ‘maestro.parms’ function. In that case, you will have to add in the parms section of your *.vm a line such as:
"roles": [ { "parms": { "CacheSize": "$attributes.cacheSize" }, "type": "AppManager", "name": "AppManager" } ],
Test
I created a pattern which just contains my ‘AppManager’ component and deployed it.
Going to ‘manage’ and ‘operation’, we can see our 2 tweak parameters:
and put value ’10’ and ’11’:
Now, let’s hit the ‘submit’ button, the operation is launched. Behind the scene the script ‘change.py’ is executed.
After few seconds, the operation is completed:
Now, let’s look at the log files:
We can see, now, the cacheSize and logSize have respectively the value 10 and 11 and the parameter.json file has been updated:
{ "$$1": { "value": 10, "role": "AppManager-sampleTweak.AppManager", "id": "AppManager.cachesize" }, "$$2": { "value": 11, "role": "AppManager-sampleTweak.AppManager", "id": "AppManager.logSize" } }
Conclusion:
There is two ways to launch scripts with parameters on a running pattern, Operation and Tweak and the main difference between them is that the tweaked parameters are persisted in the storehouse and thus can be reused at any time during the running pattern lifecycle.
References
– Pattern-type (Part 8): How to extend an existing pattern-type with SmartCloud Application Services
– Pattern-type (Part 7): Scaling in/out based on OS metrics with SmartCloud Application Services
– Pattern-type (Part 6): How to add operation with SmartCloud Application Services