Welcome to cloudify-plugin-common’s documentation!¶
This is the API reference to the cloudify-plugins-common module which is required when writing any cloudify plugin (and workflow).
Contents:
Context¶
- class cloudify.context.ContextCapabilities(endpoint, instance)[source]¶
Bases: object
Maps from instance relationship target ids to their respective runtime properties
- class cloudify.context.BootstrapContext(bootstrap_context)[source]¶
Bases: object
Holds bootstrap context that was posted to the rest service. (usually during the bootstrap process).
- class PolicyEngine(policy_engine)[source]¶
Bases: object
Cloudify policy engine related configuration
- class BootstrapContext.CloudifyAgent(cloudify_agent)[source]¶
Bases: object
Cloudify agent related bootstrap context properties.
- BootstrapContext.cloudify_agent[source]¶
Returns Cloudify agent related bootstrap context data
Return type: CloudifyAgent
- class cloudify.context.NodeInstanceContext(*args, **kwargs)[source]¶
Bases: cloudify.context.EntityContext
- runtime_properties[source]¶
The node instance runtime properties as a dict (read-only).
Runtime properties are properties set during the node instance’s lifecycle. Retrieving runtime properties involves a call to Cloudify’s storage.
- update()[source]¶
Stores new/updated runtime properties for the node instance in context in Cloudify’s storage.
This method should be invoked only if its necessary to immediately update Cloudify’s storage with changes. Otherwise, the method is automatically invoked as soon as the task execution is over.
- class cloudify.context.RelationshipContext(relationship_context, endpoint, node)[source]¶
Bases: cloudify.context.EntityContext
Holds relationship instance data
- class cloudify.context.RelationshipSubjectContext(context, endpoint, modifiable)[source]¶
Bases: object
Holds reference to node and node instance.
Obtained in relationship operations by ctx.source and ctx.target, and by iterating instance relationships and for each relationship, reading relationship.target
- class cloudify.context.CloudifyContext(ctx=None)[source]¶
Bases: cloudify.context.CommonContext
A context object passed to plugins tasks invocations. The context object is used in plugins when interacting with the Cloudify environment:
from cloudify import ctx @operation def my_start(**kwargs): # port is a property that was configured on the current instance's # node port = ctx.node.properties['port'] start_server(port=port)
- instance[source]¶
The node instance the operation is executed for.
This property is only relevant for NODE_INSTANCE context operations.
- node[source]¶
The node the operation is executed for.
This property is only relevant for NODE_INSTANCE context operations.
- source[source]¶
Provides access to the relationship’s operation source node and node instance.
This property is only relevant for relationship operations.
- target[source]¶
Provides access to the relationship’s operation target node and node instance.
This property is only relevant for relationship operations.
- type[source]¶
The type of this context.
Available values:
- DEPLOYMENT
- NODE_INSTANCE
- RELATIONSHIP_INSTANCE
- execution_id[source]¶
The workflow execution id the plugin invocation was requested from. This is a unique value which identifies a specific workflow execution.
- workflow_id[source]¶
The workflow id the plugin invocation was requested from. For example:
install, uninstall etc...
- operation[source]¶
The node operation name which is mapped to this task invocation. For example: cloudify.interfaces.lifecycle.start
- capabilities[source]¶
Maps from instance relationship target ids to their respective runtime properties
NOTE: This feature is deprecated, use ‘instance.relationships’ instead.
- logger[source]¶
A Cloudify context aware logger.
Use this logger in order to index logged messages in ElasticSearch using logstash.
- bootstrap_context[source]¶
System context provided during the bootstrap process
Return type: BootstrapContext
- get_resource(resource_path)[source]¶
Retrieves a resource bundled with the blueprint as a string.
Parameters: resource_path – the path to the resource. Note that this path is relative to the blueprint file which was uploaded.
- download_resource(resource_path, target_path=None)[source]¶
Retrieves a resource bundled with the blueprint and saves it under a local file.
Parameters: - resource_path – the path to the resource. Note that this path is relative to the blueprint file which was uploaded.
- target_path – optional local path (including filename) to store the resource at on the local file system. If missing, the location will be a tempfile with a generated name.
Returns: The path to the resource on the local file system (identical to target_path parameter if used).
raises an cloudify.exceptions.HttpException
Raises: cloudify.exceptions.HttpException on any kind of HTTP Error.
Raises: IOError if the resource failed to be written to the local file system.
Decorators¶
- cloudify.decorators.operation(func=None, **arguments)[source]¶
Decorate plugin operation function with this decorator. Internally, if celery is installed, will also wrap the function with a @celery.task decorator
The ctx injected to the function arguments is of type cloudify.context.CloudifyContext
The ctx object can also be accessed by importing cloudify.ctx
Example:
from cloudify import ctx @operations def start(**kwargs): pass
- cloudify.decorators.workflow(func=None, **arguments)[source]¶
Decorate workflow functions with this decorator. Internally, if celery is installed, will also wrap the function with a @celery.task decorator
The ctx injected to the function arguments is of type cloudify.workflows.workflow_context.CloudifyWorkflowContext
The ctx object can also be accessed by importing cloudify.workflows.ctx
Example:
from cloudify.workflows import ctx @workflow def reinstall(**kwargs): pass
Exceptions¶
- exception cloudify.exceptions.NonRecoverableError[source]¶
Bases: exceptions.Exception
An error raised by plugins to denote that no retry should be attempted by by the executing workflow engine.
- exception cloudify.exceptions.RecoverableError(message=None, retry_after=None)[source]¶
Bases: exceptions.Exception
An error raised by plugins to explicitly denote that this is a recoverable error (note that this is the default behavior). It is possible specifying how many seconds should pass before a retry is attempted thus overriding the bootstrap context configuration parameter: cloudify.workflows.retry_interval
Parameters: retry_after – How many seconds should the workflow engine wait before re-executing the task the raised this exception. (only applies when the workflow engine decides that this task should be retried)
- exception cloudify.exceptions.HttpException(url, code, message)[source]¶
Bases: cloudify.exceptions.NonRecoverableError
Wraps HTTP based exceptions that may be raised.
Parameters: - url – The url the request was made to.
- code – The response status code.
- message – The underlying reason for the error.
- exception cloudify.exceptions.CommandExecutionException(command, error, output, code)[source]¶
Bases: exceptions.Exception
Indicates a failure to execute a command.
Parameters: - command – The command executed
- error – process stderr output
- output – process stdout output
- code – process exit code
Manager¶
- class cloudify.manager.NodeInstance(node_instance_id, node_id, runtime_properties=None, state=None, version=None, host_id=None, relationships=None)[source]¶
Bases: object
Represents a deployment node instance. An instance of this class contains runtime information retrieved from Cloudify’s runtime storage as well as the node’s state.
- runtime_properties[source]¶
The node instance runtime properties.
To update the properties, make changes on the returned dict and call update_node_instance with the modified instance.
- cloudify.manager.get_rest_client()[source]¶
Returns: A REST client configured to connect to the manager in context Return type: cloudify_rest_client.CloudifyClient
- cloudify.manager.download_resource(resource_path, logger, target_path=None)[source]¶
Download resource from the manager file server.
Parameters: - resource_path – path to resource on the file server
- logger – logger to use for info output
- target_path – optional target path for the resource
Returns: path to the downloaded resource
- cloudify.manager.download_blueprint_resource(blueprint_id, resource_path, logger, target_path=None)[source]¶
Download resource from the manager file server with path relative to the blueprint denoted by blueprint_id.
Parameters: - blueprint_id – the blueprint id of the blueprint to download the resource from
- resource_path – path to resource relative to blueprint folder
- logger – logger to use for info output
- target_path – optional target path for the resource
Returns: path to the downloaded resource
- cloudify.manager.get_resource(resource_path, base_url=None)[source]¶
Get resource from the manager file server.
Parameters: resource_path – path to resource on the file server Returns: resource content
- cloudify.manager.get_blueprint_resource(blueprint_id, resource_path)[source]¶
Get resource from the manager file server with patch relative to the blueprint denoted by blueprint_id.
Parameters: - blueprint_id – the blueprint id of the blueprint to download the resource from
- resource_path – path to resource relative to blueprint folder
Returns: resource content
- cloudify.manager.get_node_instance(node_instance_id)[source]¶
Read node instance data from the storage.
Parameters: node_instance_id – the node instance id Return type: NodeInstance
- cloudify.manager.update_node_instance(node_instance)[source]¶
Update node instance data changes in the storage.
Parameters: node_instance – the node instance with the updated data
- cloudify.manager.get_node_instance_ip(node_instance_id)[source]¶
Get the IP address of the host the node instance denoted by node_instance_id is contained in.
Mocks¶
- class cloudify.mocks.MockNodeInstanceContext(id=None, runtime_properties=None)[source]¶
Bases: object
- class cloudify.mocks.MockCloudifyContext(node_id=None, node_name=None, blueprint_id=None, deployment_id=None, execution_id=None, properties=None, runtime_properties=None, capabilities=None, related=None, source=None, target=None, operation=None, resources=None, provider_context=None, bootstrap_context=None)[source]¶
Bases: cloudify.context.CloudifyContext
Cloudify context mock that can be used when testing Cloudify plugins.
Utils¶
- cloudify.utils.get_local_ip()[source]¶
Return the IP address used to connect to this machine by the management. machine
- cloudify.utils.get_manager_ip()[source]¶
Returns the IP address of manager inside the management network.
- cloudify.utils.get_manager_file_server_blueprints_root_url()[source]¶
Returns the blueprints root url in the file server.
- cloudify.utils.get_manager_rest_service_port()[source]¶
Returns the port the manager REST service is running on.
- cloudify.utils.id_generator(size=6, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]¶
Generate and return a random string using upper case letters and digits.
- class cloudify.utils.LocalCommandRunner(logger=None, host='localhost')[source]¶
Bases: object
- run(command, exit_on_failure=True, stdout_pipe=True, stderr_pipe=True)[source]¶
Runs local commands.
Parameters: - command – The command to execute.
- exit_on_failure – False to ignore failures.
- stdout_pipe – False to not pipe the standard output.
- stderr_pipe – False to not pipe the standard error.
Returns: A wrapper object for all valuable info from the execution.
Return type: CommandExecutionResponse
- class cloudify.utils.CommandExecutionResponse(command, std_out, std_err, return_code)[source]¶
Bases: object
Wrapper object for info returned when running commands.
Parameters: - command – The command that was executed.
- std_out – The output from the execution.
- std_err – The error message from the execution.
- return_code – The return code from the execution.
Logs¶
- cloudify.logs.message_context_from_cloudify_context(ctx)[source]¶
Build a message context from a CloudifyContext instance
- cloudify.logs.message_context_from_workflow_context(ctx)[source]¶
Build a message context from a CloudifyWorkflowContext instance
- cloudify.logs.message_context_from_workflow_node_instance_context(ctx)[source]¶
Build a message context from a CloudifyWorkflowNode instance
- class cloudify.logs.CloudifyBaseLoggingHandler(ctx, out_func, message_context_builder)[source]¶
Bases: logging.Handler
A base handler class for writing log messages to RabbitMQ
- class cloudify.logs.CloudifyPluginLoggingHandler(ctx, out_func=None)[source]¶
Bases: cloudify.logs.CloudifyBaseLoggingHandler
A handler class for writing plugin log messages to RabbitMQ
- class cloudify.logs.CloudifyWorkflowLoggingHandler(ctx, out_func=None)[source]¶
Bases: cloudify.logs.CloudifyBaseLoggingHandler
A Handler class for writing workflow log messages to RabbitMQ
- class cloudify.logs.CloudifyWorkflowNodeLoggingHandler(ctx, out_func=None)[source]¶
Bases: cloudify.logs.CloudifyBaseLoggingHandler
A Handler class for writing workflow nodes log messages to RabbitMQ
- cloudify.logs.init_cloudify_logger(handler, logger_name, logging_level=20)[source]¶
Instantiate an amqp backed logger based on the provided handler for sending log messages to RabbitMQ
Parameters: - handler – A logger handler based on the context
- logger_name – The logger name
- logging_level – The logging level
Returns: An amqp backed logger
- cloudify.logs.send_workflow_event(ctx, event_type, message=None, args=None, additional_context=None, out_func=None)[source]¶
Send a workflow event to RabbitMQ
Parameters: - ctx – A CloudifyWorkflowContext instance
- event_type – The event type
- message – The message
- args – additional arguments that may be added to the message
- additional_context – additional context to be added to the context
- cloudify.logs.send_workflow_node_event(ctx, event_type, message=None, args=None, additional_context=None, out_func=None)[source]¶
Send a workflow node event to RabbitMQ
Parameters: - ctx – A CloudifyWorkflowNode instance
- event_type – The event type
- message – The message
- args – additional arguments that may be added to the message
- additional_context – additional context to be added to the context
- cloudify.logs.send_plugin_event(ctx, message=None, args=None, additional_context=None, out_func=None)[source]¶
Send a plugin event to RabbitMQ
Parameters: - ctx – A CloudifyContext instance
- message – The message
- args – additional arguments that may be added to the message
- additional_context – additional context to be added to the context
- cloudify.logs.send_task_event(cloudify_context, event_type, message=None, args=None, additional_context=None, out_func=None)[source]¶
Send a task event to RabbitMQ
Parameters: - cloudify_context – a __cloudify_context struct as passed to operations
- event_type – The event type
- message – The message
- args – additional arguments that may be added to the message
- additional_context – additional context to be added to the context
Workflows¶
Workflow Tasks Graph¶
- class cloudify.workflows.tasks_graph.TaskDependencyGraph(workflow_context)[source]¶
Bases: object
A task graph builder
Parameters: workflow_context – A WorkflowContext instance (used for logging) - get_task(task_id)[source]¶
Get a task instance that was inserted to this graph by its id
Parameters: task_id – the task id Returns: a WorkflowTask instance for the requested task if found. None, otherwise.
- add_dependency(src_task, dst_task)[source]¶
Add a dependency between tasks. The source task will only be executed after the target task terminates. A task may depend on several tasks, in which case it will only be executed after all its ‘destination’ tasks terminate
Parameters: - src_task – The source task
- dst_task – The target task
- execute()[source]¶
Start executing the graph based on tasks and dependencies between them. Calling this method will block until one of the following occurs:
- all tasks terminated
- a task failed
- an unhandled exception is raised
- the execution is cancelled
Note: This method will return None unless the execution has been cancelled, in which case the return value will be api.EXECUTION_CANCELLED_RESULT. Callers of this method should check the return value and propagate the result in the latter case.
Also note that for the time being, if such a cancelling event occurs, the method might return even while there’s some operations still being executed.
- class cloudify.workflows.tasks_graph.forkjoin(*tasks)[source]¶
Bases: object
A simple wrapper for tasks. Used in conjunction with TaskSequence. Defined to make the code easier to read (instead of passing a list) see TaskSequence.add for more details
- class cloudify.workflows.tasks_graph.TaskSequence(graph)[source]¶
Bases: object
Helper class to add tasks in a sequential manner to a task dependency graph
Parameters: graph – The TaskDependencyGraph instance - add(*tasks)[source]¶
Add tasks to the sequence.
Parameters: tasks – Each task might be:
- A WorkflowTask instance, in which case, it will be added to the graph with a dependency between it and the task previously inserted into the sequence
- A forkjoin of tasks, in which case it will be treated as a “fork-join” task in the sequence, i.e. all the fork-join tasks will depend on the last task in the sequence (could be fork join) and the next added task will depend on all tasks in this fork-join task
Workflow API¶
- cloudify.workflows.workflow_api.has_cancel_request()[source]¶
Checks for requests to cancel the workflow execution. This should be used to allow graceful termination of workflow executions.
If this method is not used and acted upon, a simple ‘cancel’ request for the execution will have no effect - ‘force-cancel’ will have to be used to abruptly terminate the execution instead.
Note: When using this method, the workflow must return EXECUTION_CANCELLED_RESULT if indeed the execution gets cancelled.
Returns: whether there was a request to cancel the workflow execution
Workflow Context¶
- class cloudify.workflows.workflow_context.CloudifyWorkflowRelationshipInstance(ctx, node_instance, nodes_and_instances, relationship_instance)[source]¶
Bases: object
A node instance relationship instance
Parameters: - ctx – a CloudifyWorkflowContext instance
- node_instance – a CloudifyWorkflowNodeInstance instance
- nodes_and_instances – a WorkflowNodesAndInstancesContainer instance
- relationship_instance – A relationship dict from a NodeInstance instance (of the rest client model)
- class cloudify.workflows.workflow_context.CloudifyWorkflowRelationship(ctx, node, nodes_and_instances, relationship)[source]¶
Bases: object
A node relationship
Parameters: - ctx – a CloudifyWorkflowContext instance
- node – a CloudifyWorkflowNode instance
- nodes_and_instances – a WorkflowNodesAndInstancesContainer instance
- relationship – a relationship dict from a Node instance (of the rest client mode)
- class cloudify.workflows.workflow_context.CloudifyWorkflowNodeInstance(ctx, node, node_instance, nodes_and_instances)[source]¶
Bases: object
A plan node instance
Parameters: - ctx – a CloudifyWorkflowContext instance
- node – a CloudifyWorkflowContextNode instance
- node_instance – a NodeInstance (rest client response model)
- nodes_and_instances – a WorkflowNodesAndInstancesContainer instance
- set_state(state)[source]¶
Set the node state
Parameters: state – The node state Returns: the state set
- send_event(event, additional_context=None)[source]¶
Sends a workflow node event to RabbitMQ
Parameters: - event – The event
- additional_context – additional context to be added to the context
- class cloudify.workflows.workflow_context.CloudifyWorkflowNode(ctx, node, nodes_and_instances)[source]¶
Bases: object
A plan node instance
Parameters: - ctx – a CloudifyWorkflowContext instance
- node – a Node instance (rest client response model)
- nodes_and_instances – a WorkflowNodesAndInstancesContainer instance
- class cloudify.workflows.workflow_context.WorkflowNodesAndInstancesContainer(workflow_context, raw_nodes, raw_node_instances)[source]¶
Bases: object
- class cloudify.workflows.workflow_context.CloudifyWorkflowContext(ctx)[source]¶
Bases: cloudify.workflows.workflow_context.WorkflowNodesAndInstancesContainer
A context used in workflow operations
Parameters: ctx – a cloudify_context workflow dict - graph_mode()[source]¶
Switch the workflow context into graph mode
Returns: A task dependency graph instance
- send_event(event, event_type='workflow_stage', args=None, additional_context=None)[source]¶
Sends a workflow event to RabbitMQ
Parameters: - event – The event
- event_type – The event type
- args – additional arguments that may be added to the message
- additional_context – additional context to be added to the context
- update_execution_status(new_status)[source]¶
Updates the execution status to new_status. Note that the workflow status gets automatically updated before and after its run (whether the run succeeded or failed)
- execute_task(task_name, task_queue=None, kwargs=None, node_context=None, send_task_events=True)[source]¶
Execute a task
Parameters: - task_name – the task named
- task_queue – the task queue, if None runs the task locally
- kwargs – optional kwargs to be passed to the task
- node_context – Used internally by node.execute_operation
- local_task(local_task, node=None, info=None, kwargs=None, task_id=None, name=None, send_task_events=True, override_task_config=False)[source]¶
Create a local workflow task
Parameters: - local_task – A callable implementation for the task
- node – A node if this task is called in a node context
- info – Additional info that will be accessed and included in log messages
- kwargs – kwargs to pass to the local_task when invoked
- task_id – The task id
- class cloudify.workflows.workflow_context.LocalTasksProcessing(thread_pool_size=1)[source]¶
Bases: object
- class cloudify.workflows.workflow_context.CloudifyWorkflowContextHandler(workflow_ctx)[source]¶
Bases: object
- class cloudify.workflows.workflow_context.RemoteCloudifyWorkflowContextHandler(workflow_ctx)[source]¶
Bases: cloudify.workflows.workflow_context.CloudifyWorkflowContextHandler
- class cloudify.workflows.workflow_context.LocalCloudifyWorkflowContextHandler(workflow_ctx, storage)[source]¶
Bases: cloudify.workflows.workflow_context.CloudifyWorkflowContextHandler
- class cloudify.workflows.workflow_context.Modification(workflow_ctx, modification)[source]¶
Bases: object
- class cloudify.workflows.workflow_context.ModificationNodes(modification, raw_nodes, raw_node_instances)[source]¶
Bases: cloudify.workflows.workflow_context.WorkflowNodesAndInstancesContainer