Utils¶
- class cloudify.utils.ManagerVersion(raw_version)[source]¶
Bases:
object
Cloudify manager version helper class.
- cloudify.utils.format_exception(e)[source]¶
Human-readable representation of an exception, as a bytestring.
The canonical way to print an exception, str(e), also made to handle unicode exception messages in python 2. Additionally, if the exception message is incompatible with utf-8, (which should only happen in extreme cases, such as NUL bytes), fallback to repr().
- cloudify.utils.get_manager_name()[source]¶
Hostname/id of the current manager.
Available only on mgmtworkers, returns the hostname of the manager this is running on.
- cloudify.utils.get_manager_file_server_scheme()[source]¶
Returns the protocol to use for connecting to the fileserver
- cloudify.utils.get_manager_file_server_root()[source]¶
Returns the host the manager REST service is running on.
- cloudify.utils.get_manager_rest_service_host()[source]¶
Returns the host the manager REST service is running on.
- cloudify.utils.get_manager_rest_service_port()[source]¶
Returns the port the manager REST service is running on.
- cloudify.utils.get_broker_ssl_cert_path()[source]¶
Returns location of the broker certificate on the agent
- cloudify.utils.get_manager_ip()¶
Returns the host the manager REST service is running on.
- cloudify.utils.get_local_rest_certificate()[source]¶
Returns the path to the local copy of the server’s public certificate
- cloudify.utils.get_execution_creator_username()[source]¶
Returns the execution creator username to use in the logs
- cloudify.utils.get_rest_token()[source]¶
Returns the auth token to use when calling the REST service
- cloudify.utils.get_execution_token()[source]¶
Returns the execution token to use when calling the REST service
- cloudify.utils.get_instances_of_node(rest_client, deployment_id=None, node_id=None, _ctx=None)[source]¶
Get node instances of a node from a deployment.
- cloudify.utils.get_tenant_name()[source]¶
Returns the tenant name to use when calling the REST service
- cloudify.utils.get_is_bypass_maintenance()[source]¶
Returns true if workflow should run in maintenance mode.
- cloudify.utils.id_generator(size=6, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]¶
Generate and return a random string using upper case letters and digits.
- cloudify.utils.get_exec_tempdir()[source]¶
Returns the directory to use for temporary files, when the intention is to place an executable file there. This is needed because some production systems disallow executions from the default temporary directory.
- cloudify.utils.get_workflow_parameters()[source]¶
Get workflow parameters (except ctx key) as a dict.
- class cloudify.utils.LocalCommandRunner(logger=None, host='localhost')[source]¶
Bases:
object
- run(command, exit_on_failure=True, stdout_pipe=True, stderr_pipe=True, cwd=None, execution_env=None, encoding='utf-8')[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.
cwd – the working directory the command will run from.
execution_env – dictionary of environment variables that will be present in the command scope.
- Returns:
A wrapper object for all valuable info from the execution.
- Return type:
- 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.
- cloudify.utils.setup_default_logger(logger_name, logger_level=20, handlers=None, remove_existing_handlers=True, logger_format=None, propagate=True)¶
- Parameters:
logger_name – Name of the logger.
logger_level – Level for the logger (not for specific handler).
handlers – An optional list of handlers (formatter will be overridden); If None, only a StreamHandler for sys.stdout will be used.
remove_existing_handlers – Determines whether to remove existing handlers before adding new ones
logger_format – the format this logger will have.
propagate – propagate the message the parent logger.
- Returns:
A logger instance.
- Return type:
logging.Logger
- cloudify.utils.is_management_environment()[source]¶
Checks whether we’re currently running within a management worker.
- cloudify.utils.generate_user_password(password_length=32)[source]¶
Generate random string to use as user password.
- cloudify.utils.extract_plugins_to_install(plugin_list, filter_func)[source]¶
- Parameters:
plugin_list – list of plugins.
- Returns:
a list of plugins that are marked for installation (that potentially need installation) and pass the given filter function.
- cloudify.utils.extract_and_merge_plugins(deployment_plugins, workflow_plugins, filter_func=<function <lambda>>, with_repetition=False)[source]¶
- Parameters:
deployment_plugins – deployment plugins to install.
workflow_plugins – workflow plugins to install.
filter_func – predicate function to filter the plugins, should return True for plugins that don’t need to be filtered out.
with_repetition – can plugins appear twice in the list.
- Returns:
a list of merged plugins that were marked for installation.
- cloudify.utils.merge_plugins(deployment_plugins, workflow_plugins)[source]¶
Merge plugins to install.
- Parameters:
deployment_plugins – deployment plugins to install.
workflow_plugins – workflow plugins to install.
- Returns:
the merged plugins.
- cloudify.utils.add_plugins_to_install(ctx, plugins_to_install, sequence)[source]¶
Adds a task to the sequence that installs the plugins.
- cloudify.utils.add_plugins_to_uninstall(ctx, plugins_to_uninstall, sequence)[source]¶
Adds a task to the sequence that uninstalls the plugins.
- cloudify.utils.wait_for(callable_obj, callable_obj_key, value_attr, test_condition, exception_class, msg='', timeout=900)[source]¶
- cloudify.utils.get_executable_path(executable, venv)[source]¶
Lookup the path to the executable, os agnostic
- Parameters:
executable – the name of the executable
venv – the venv to look for the executable in
- cloudify.utils.plugin_prefix(name, tenant_name, version=None, deployment_id=None)[source]¶
Virtualenv for the specified plugin.
If version is not provided, the highest version is used.
- Parameters:
name – package name of the plugin
tenant_name – tenant name, or None for local executions
version – version of the plugin, or None for the newest version
deployment_id – deployment id, for source plugins
- Returns:
directory containing the plugin virtualenv, or None
- cloudify.utils.target_plugin_prefix(name, tenant_name, version=None, deployment_id=None)[source]¶
Target directory into which the plugin should be installed
- cloudify.utils.parse_utc_datetime(time_expression, timezone=None)[source]¶
- Parameters:
time_expression – a string representing date a and time. The following formats are possible: YYYY-MM-DD HH:MM, HH:MM, or a time delta expression such as ‘+2 weeks’ or ‘+1day+10min.
timezone – a string representing a timezone. Any timezone recognized by UNIX can be used here, e.g. ‘EST’ or Asia/Jerusalem.
- Returns:
A naive datetime object, in UTC time.
- cloudify.utils.parse_utc_datetime_relative(time_expression, base_datetime=None)[source]¶
- Parameters:
time_expression – a string representing a relative time delta, such as ‘+2 weeks’ or ‘+1day+10min.
base_datetime – a datetime object representing the absolute date and time to which we apply the time delta. By default: UTC now
- Returns:
A naive datetime object, in UTC time.
- cloudify.utils.parse_utc_datetime_absolute(time_expression, timezone=None)[source]¶
- Parameters:
time_expression – a string representing an absolute date and time. The following formats are possible: YYYY-MM-DD HH:MM, HH:MM
timezone – a string representing a timezone. Any timezone recognized by UNIX can be used here, e.g. ‘EST’ or Asia/Jerusalem.
- Returns:
A naive datetime object, in UTC time.
- cloudify.utils.unpack_timedelta_string(expr)[source]¶
- Parameters:
expr – a string representing a time delta, such as ‘+2 weeks’, ‘+1’, or ‘+10min’.
- Returns:
a tuple of int + period string, e.g. (2, ‘weeks’)
- cloudify.utils.parse_and_apply_timedelta(expr, date_time)[source]¶
- Parameters:
expr – a string representing a time delta, such as ‘+2 weeks’, ‘+1’, or ‘+10min’.
date_time – a datetime object
- Returns:
a datetime object with added time delta
- cloudify.utils.ipv6_url_compat(addr)[source]¶
Return URL-compatible version of IPv6 address (or just an address).
- cloudify.utils.uuid4()[source]¶
Generate a random UUID, and return a string representation of it.
This is pretty much a copy of the stdlib uuid4. We inline it here, because we’d like to avoid importing the stdlib uuid module on the operation dispatch critical path, because importing the stdlib uuid module runs some subprocesses (for detecting the uuid1-uuid3 MAC address), and that causes more memory pressure than we’d like.
- cloudify.utils.keep_trying_http(total_timeout_sec=3600, max_delay_sec=60)[source]¶
Keep (re)trying HTTP requests.
This is a wrapper function that will keep (re)running whatever function it wraps until it raises a “non-repairable” exception (where “repairable” exceptions are requests.exceptions.ConnectionError and requests.exceptions.Timeout), or total_timeout_sec is exceeded.
- Parameters:
total_timeout_sec – The amount of seconds to keep trying, if None, there is no timeout and the wrapped function will be re-tried indefinitely.
max_delay_sec – The maximum delay (in seconds) between consecutive calls to the wrapped function. The actual delay will be a pseudo-random number between 0 and max_delay_sec.