The following example shows how to create a software component (e.g., a python script) and run it a on compute node.
Import the TOSCA type definition:
imports:
- tosca-normative-types:1.0.0-ALIEN20
The imported definition contains some default TOSCA types to use (e.g., tosca.nodes.SoftwareComponent
).
To define a new node type (e.g., otc.nodes.SoftwareComponent.Python
) for our python script, we derive from an existing
node type tosca.nodes.SoftwareComponent
:
node_types:
# This is the node name, can be arbitrary
otc.nodes.SoftwareComponent.Python:
derived_from: tosca.nodes.SoftwareComponent
By deriving from the tosca.nodes.SoftwareComponent
, the python component can be hosted on a compute node as in Figure 1.
Figure 1: A Python component
The tosca.nodes.SoftwareComponent
also comes up with the default property component_version
. Therefore, the Python
component also inherits this property (see Figure 1). In the editor, users can specify the value for the property.
The below example shows how to define a custom property hello_message
from type string
for our node. The property is
set to the default value Hello World!
. The required
field indicates that users have to specify a value for this
property in the editor.
otc.nodes.SoftwareComponent.Python:
...
properties:
hello_message:
description: A simple message to print
type: string
required: true
default: "Hello World!"
TOSCA also supports the following data types:
- YAML Types:
integer
,string
,boolean
,float
- TOSCA types:
version
: 2.0.1range
: [0, 100], [ 0, UNBOUNDED ]list
: [ 80, 8080 ]map
: { user1: 1001, user2: 1002 }
The interfaces are the main implementations, where we can control the lifecycle of our node. The TOSCA orchestration
engine will call the interfaces at runtime during the lifecycle of the node: create
, start
, update
, delete
.
In the following example, the orchestrator calls a python script start.py
to start
the python node. The script has
the input variable msp
, which value is set from the property hello_message
of the node.
otc.nodes.SoftwareComponent.Python:
...
properties:
hello_message:
...
interfaces:
Standard:
start:
inputs:
# the keyword SELF indicates the node itself.
msg: {get_property: [SELF, hello_message]}
implementation: scripts/start.py
Notice:
- We use the
SELF
keyword to reference to the current node itself. - We use
get_property
to get the propertyhello_message
of the current node.
In the example, the input param msp
of the start
interface is available as an environment variable in the script
start.py
as follows:
# scripts/start.py
print(msg)
Notice:
- During the deployment, the orchestrator takes care of sending the
implementation
script to the target compute node and apply it. - TOSCA supports the following implementation scripts: python, shell script, ansible.
We can define the output result for our python component as follows:
interfaces:
Standard:
create:
inputs:
var1: {get_property: [SELF, outputVar1]}
implementation: scripts/create.py
attributes:
resolvedOutput1: { get_operation_output: [SELF, Standard, create, myVar1]}
Notice:
- We use
attributes
to define output result of the python node. - We use
get_operation_output
to get output from an interface implementation (e.g.,create
).
In the above example, the python node outputs resolvedOutput1
. It is the global variable myVar1
of the create
interface. In particular, myVar1
is an environment variable set in the python script of the create
interface as
follows:
# scripts/create.py
from os import environ
# Set output val using two different ways
myVar1="Resolved {0}".format(var1)
- To package the TOSCA definition: zip all contents inside the folder examples/python/*
- Upload the zip file to the service catalog.