Context

class cloudify.context.ContextCapabilities(endpoint, instance)[source]

Bases: object

Maps from instance relationship target ids to their respective runtime properties

get_all()[source]

Returns all capabilities as dict.

class cloudify.context.DeploymentWorkdirMixin[source]

Bases: object

download_deployment_workdir()[source]

Download a copy of deployment-in-context’s working directory from the manager, but only if these files are not available locally.

upload_deployment_workdir()[source]

Upload a local copy of deployment-in-context’s working directory to the manager, but only if these files are not available locally.

sync_deployment_workdir()[source]

Sync a local copy of deployment-in-context’s working directory to the manager, but only if these files are not available locally. The method returns a contextmanager, so it should be used like:

``` with ctx.sync_deployment_workdir():

do_something()

```

local_deployment_workdir()[source]

Generate absolute path to deployment’s local working directory. The directory is a local copy of relevant directory on the manager.

upload_deployment_file(target_file_path, src_file, src_file_mtime=None)[source]
delete_deployment_file(file_path)[source]
class cloudify.context.CommonContext(ctx=None)[source]

Bases: DeploymentWorkdirMixin

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

property start_timeout

Returns the number of seconds to wait for the policy engine to start

class CloudifyAgent(cloudify_agent)[source]

Bases: object

Cloudify agent related bootstrap context properties.

property min_workers

Returns the minimum number of workers for agent hosts.

property max_workers

Returns the maximum number of workers for agent hosts.

property user

Returns the username used when SSH-ing during agent installation.

property remote_execution_port

Returns the port used when SSH-ing during agent installation.

property agent_key_path

Returns the path to the key file on the management machine used when SSH-ing during agent installation.

property broker_ip

Returns the host name or IP of the rabbit server. An empty string should result in clients using the manager IP.

property broker_user

Returns the username for connecting to rabbit.

property broker_pass

Returns the password for connecting to rabbit.

property broker_vhost

Returns the vhost for connecting to rabbit.

property broker_ssl_enabled

Returns whether SSL is enabled for connecting to rabbit.

property cluster

Returns the cluster configuration.

property networks
property cloudify_agent

Returns Cloudify agent related bootstrap context data

Return type:

CloudifyAgent

property policy_engine

Returns Cloudify policy engine related bootstrap context data

Return type:

PolicyEngine

property resources_prefix

Returns the resources prefix that was configured during bootstrap. An empty string is returned if the resources prefix was not configured.

broker_config(*args, **kwargs)[source]

Returns dictionary containing broker configuration.

class cloudify.context.EntityContext(context, **_)[source]

Bases: object

class cloudify.context.BlueprintContext(context, **_)[source]

Bases: EntityContext

property id

The blueprint id the plugin invocation belongs to.

class cloudify.context.DeploymentContext(context, **_)[source]

Bases: EntityContext

property id

The deployment id the plugin invocation belongs to.

property tenant_name

The deployment tenant’s name.

property runtime_only_evaluation
property display_name

The deployment’s display_name used e.g. in Cloudify UI.

property creator

The name of the user who owns the deployment.

property resource_tags

Resource tags associated with this deployment.

class cloudify.context.NodeContext(*args, **kwargs)[source]

Bases: EntityContext

property id

The node’s id

property name

The node’s name

property properties

The node properties as dict (read-only). These properties are the properties specified in the blueprint.

property type

The node’s type

property type_hierarchy

The node’s type hierarchy

property number_of_instances

The number of instances of that node.

class cloudify.context.NodeInstanceContext(*args, **kwargs)[source]

Bases: EntityContext

property id

The node instance id.

property node

The node that this instance was created from.

property scaling_groups

The list of scaling group instances this node instance belongs to

property runtime_properties

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(on_conflict=None)[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.

Updating the runtime properties might fail due to concurrent writes: use a handler function to merge properties, to retry quickly.

Parameters:

on_conflict (function(dict, dict) -> dict) – Optional function returning the runtime properties to store. It will be called with two arguments: locally modified runtime properties, and runtime properties refetched from storage. If the update raises a version conflict error (due to concurrent writes), the function will be called again, with the same locally modified runtime properties, and the newest runtime properties from storage.

refresh(force=False)[source]

Force fetching up-to-date instance data.

Useful for scripts that must reliably work in parallel, with each updating runtime properties.

Parameters:

force – Overwrite local changes

property host_ip

Returns the node instance host ip address.

This values is derived by reading the host_id from the relevant node instance and then reading its ip runtime property or its node_state ip property.

property relationships

Returns a list of this instance relationships

Returns:

list of RelationshipContext

Return type:

list

property index
property drift
class cloudify.context.RelationshipContext(relationship_context, endpoint, node)[source]

Bases: EntityContext

Holds relationship instance data

property target

Returns a holder for target node and target instance

Return type:

RelationshipSubjectContext

property type

The relationship type

property type_hierarchy

The relationship type hierarchy

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: 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)
get_managers(network='default')[source]

The managers, optionally for a non-default network.

get_brokers(network='default')[source]

The brokers, optionally for a non-default network.

property instance

The node instance the operation is executed for.

This property is only relevant for NODE_INSTANCE context operations.

property node

The node the operation is executed for.

This property is only relevant for NODE_INSTANCE context operations.

property source

Provides access to the relationship’s operation source node and node instance.

This property is only relevant for relationship operations.

property target

Provides access to the relationship’s operation target node and node instance.

This property is only relevant for relationship operations.

property task_type

The kind of task this context is for

Possible values include: operation, workflow, or hook.

property type

The type of this context.

Available values:

  • DEPLOYMENT

  • NODE_INSTANCE

  • RELATIONSHIP_INSTANCE

property timeout

After this many seconds, the operation process will be killed

property timeout_recoverable

If set, the operation timeouting will raise a RecoverableError

By default, if the operation is killed by a timeout, NonRecoverableError is raised instead. Recoverable allows for retries.

property execution_env

Additional envvars for the operation subprocess

property execution_id

The workflow execution id the plugin invocation was requested from. This is a unique value which identifies a specific workflow execution.

property execution_token

The token of the current execution

property workflow_id

The workflow id the plugin invocation was requested from. For example:

install, uninstall etc…

property rest_host

REST host

property rest_port

REST port

property rest_token

REST service token

property rest_ssl_cert

REST SSL Certificate Content

property bypass_maintenance

If true, all requests sent bypass maintenance mode.

property tenant_name

Cloudify tenant name

property tenant

Full Cloudify tenant.

This will go out to the REST API and fetch all the tenant details that the current user is allowed to obtain.

property task_id

The plugin’s task invocation unique id.

property task_name

The full task name of the invoked task.

property task_target

The task target (agent worker name).

property task_queue

The task target (agent queue name).

property plugin

The plugin context.

property operation

The current operation context.

property agent
property capabilities

Maps from instance relationship target ids to their respective runtime properties

NOTE: This feature is deprecated, use ‘instance.relationships’ instead.

property logger

A Cloudify context aware logger.

Use this logger in order to index logged messages in ElasticSearch using logstash.

property bootstrap_context

System context provided during the bootstrap process

Return type:

BootstrapContext

property execution_creator_username
property resume

This run of the operation is a resume of an interrupted run

get_config(name=None, scope=None)[source]

Get a subset of the stored configuration

send_event(event)[source]

Send an event to rabbitmq

Parameters:

event – the event message

property provider_context

Gets provider context which contains provider specific metadata.

property workflow_parameters

Get workflow parameters associated with this context.

get_operation()[source]

Get the operation object for the currently-executed task.

get_execution(execution_id=None)[source]

Ge the execution object for the current execution :param execution_id: The Id of the execution object :return: Instance of Execution object which holds all the needed info

update_operation(state)[source]

Update current operation state.

Parameters:

state – New operation state

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.

get_resource_and_render(resource_path, template_variables=None)[source]

Like get_resource, but also renders the resource according to template_variables. This context is added to template_variables.

Parameters:

template_variables – according to this dict the resource will be rendered.

download_directory(directory_path, target_path=None)[source]
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.

download_resource_and_render(resource_path, target_path=None, template_variables=None)[source]

Like download_resource, but also renders the resource according to template_variables. This context is added to template_variables.

Parameters:

template_variables – according to this dict the resource will be rendered.

class cloudify.context.OperationContext(operation_context)[source]

Bases: object

property name

The name of the operation.

property retry_number

The retry number (relevant for retries and recoverable errors).

property max_retries

The maximum number of retries the operation can have.

property relationship
retry(message=None, retry_after=None)[source]

Specifies that this operation should be retried.

Usage:

return ctx.operation.retry(message=’…’, retry_after=1000)

:param message A text message containing information about the reason

for retrying the operation.

:param retry_after How many seconds should the workflow engine wait

before re-executing the operation.

class cloudify.context.CloudifyAgentContext(context)[source]

Bases: object

property script_path
clean_script()[source]
init_script(agent_config=None)[source]
class cloudify.context.PluginContext(other='')[source]

Bases: str

property name

The plugin name.

property package_name

The plugin package name.

property package_version

The plugin package version.

property source

The source package of the plugin.

property properties

The properties of the plugin.

property prefix

The plugin prefix.

property workdir

The plugin workdir.

This directory is unique for each (deployment, plugin) combination.

Note: if this operation is executed not as part of a deployment or a plugin, None is returned.

cloudify.context.deployment_workdirs_sync_required()[source]

Returns True if mgmtworker and manager are running on different hosts. The test is just a comparison of environment variables: CFY_RESOURCES_ROOT and MANAGER_FILE_SERVER_ROOT.