Definitions
define main(@servers, $wait) return @instances, $duration do
$now = now()
@instances = @servers.launch()
if $wait
sleep_until(all?(@instances.state, "operational"))
end
$duration = $now - now()
end
A cloud workflow may contain any number of definitions.
Definitions are scoped by the file that contain them: two definitions in the same file cannot have the same name.
Definitions include the following components:
• | Inputs and Outputs |
• | Structure |
• | Expressions |
As shown in the snippet above a cloud workflow may specify arguments and return values. There can be zero or more arguments and zero or more return values. Arguments and return values can either be references (@server and @instance) or variables ($wait and $duration), the difference being that references contain resource collections while variables may contain a number, a string, a boolean, a time value, the special null value, an array or a hash (references are described in Cloud Workflow Resources and variables are described in Cloud Workflow Variables).
A cloud workflow may execute children definitions using the call keyword:
define run_application() do
call get_servers_by_tag(["my_site:role=app"]) retrieve @servers
call launch_servers(@servers, true) retrieve @launched_instances, $duration
end
define launch_servers(@servers, $wait) return @instances, $duration
$now = now()
@instances = @servers.launch()
if $wait
sleep_until(all?(@instances.state, "operational"))
end
$duration = $now - now()
end
define get_servers_by_tag($tags) return @servers do
$servers = rs_cm.tags.by_tag(resource_type: "servers", tags: $tags)
# by_tag returns an array of an array of a hash and the hrefs for the resources are in the "links"
$servers = first(first($servers))["links"]
# initialize a collection of servers
@servers = rs_cm.servers.empty()
foreach $server in $servers do
@servers = rs_cm.get(href: $server["href"]) + @servers
end
end
This run_application definition first initializes the @servers resources collection using a tag query then passes the collection of retrieved servers to the launch_servers definition above. The list of references and variables specified after the retrieve keyword matches the list after the return keyword of the definition. These references and variables are initialized with the return values of the definition (so in this example the reference @launched_instances of the caller is initialized with the value of the @instances reference returned by launch_servers). The names can match, but don't have
Note:Using **retrieve* is optional and can be skipped if the caller does not need the return values.*
A cloud workflow is composed of statements. Each statement is in turn made of expressions. Statements are delimited by newlines, semi-colons or both. Comments all start with the # symbol. Any character following that symbol on the same line is part of the comment. Blank characters include tabs, spaces and newlines. There can be zero or more blank characters before and after a statement (pending there is a semi-colon if there is no newline). There can also be zero or more blank characters between arguments, operands, and keywords:
@instances = @servers.launch() # Comments can appear at the end of a line
sleep_until(all?(@instances.state[], "operational"))
# is equivalent to:
@instances = @servers.launch();
sleep_until(all?(@instances.state[], "operational"));
# and to:
@instances = @servers.launch(); sleep_until(all?(@instances.state[], "operational"))
Sequences of statements are encapsulated in blocks. The outer block is the define...end block however blocks can be defined at any time using the sub keyword:
sub do
@servers = rs_cm.servers.get(filter: ["name==my_server"])
@servers.launch()
end
sub do
@servers = rs_cm.servers.get(filter: ["name==my_other_server"])
@servers.launch()
end
Using blocks as in the snippet above does not change anything to the execution of the Cloud Workflow (and is thus not that useful...). However, blocks can be used to define scopes for error handlers, timeouts, etc. Blocks also allow defining concurrent activities. For more information about blocks, see the Cloud Workflow Processes section.
As mentioned above, a statement consists of a series of expressions. There are four categories of expressions:
• | Resource expressions include calls to resource actions and retrieval of resource links and fields, see Cloud Workflow Definitions. |
• | Flow control expressions include block definitions (define, sub, concurrent), conditional expressions (if, else, elsif), and all looping constructs (while, foreach, map, concurrent foreach, concurrent map), see Branching and Looping. |
• | Operators (+, -, **, */, %, [], ==, =~, >=, <=, &, |, !) |
• | Assignments (= and <<) |
Expressions can be adorned with attributes. Attributes do not constitute an expression by themselves. For more information about attributes, see Attributes and Error Handling.