# Path: cloudify/cluster.py
# def _get_cluster_settings_file(filename=None):
# def get_cluster_settings(filename=None):
# def set_cluster_settings(settings, filename=None):
# def get_cluster_nodes(filename=None):
# def set_cluster_nodes(nodes, filename=None):
# def get_cluster_active(filename=None):
# def set_cluster_active(node, filename=None):
# def get_cluster_amqp_settings():
# def delete_cluster_settings(filename=None):
# def is_cluster_configured(filename=None):
# def _parse_url(broker_url):
# def config_from_broker_urls(active, nodes):
#     def __init__(self, *args, **kwargs):
#     def do_request(self, *args, **kwargs):
#     def _use_node(self, node_ip):
# class ClusterHTTPClient(HTTPClient):
# class CloudifyClusterClient(CloudifyClient):
# 
# Path: cloudify/constants.py
# REST_HOST_KEY = 'REST_HOST'
# REST_PORT_KEY = 'REST_PORT'
# CELERY_WORK_DIR_KEY = 'CELERY_WORK_DIR'
# MANAGER_FILE_SERVER_URL_KEY = 'MANAGER_FILE_SERVER_URL'
# MANAGER_FILE_SERVER_ROOT_KEY = 'MANAGER_FILE_SERVER_ROOT'
# FILE_SERVER_RESOURCES_FOLDER = 'resources'
# FILE_SERVER_BLUEPRINTS_FOLDER = 'blueprints'
# FILE_SERVER_DEPLOYMENTS_FOLDER = 'deployments'
# FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER = 'uploaded-blueprints'
# FILE_SERVER_SNAPSHOTS_FOLDER = 'snapshots'
# FILE_SERVER_PLUGINS_FOLDER = 'plugins'
# FILE_SERVER_GLOBAL_RESOURCES_FOLDER = 'global-resources'
# FILE_SERVER_TENANT_RESOURCES_FOLDER = 'tenant-resources'
# FILE_SERVER_AUTHENTICATORS_FOLDER = 'authenticators'
# AGENT_INSTALL_METHOD_NONE = 'none'
# AGENT_INSTALL_METHOD_REMOTE = 'remote'
# AGENT_INSTALL_METHOD_INIT_SCRIPT = 'init_script'
# AGENT_INSTALL_METHOD_PROVIDED = 'provided'
# AGENT_INSTALL_METHOD_PLUGIN = 'plugin'
# AGENT_INSTALL_METHODS = [
#     AGENT_INSTALL_METHOD_NONE,
#     AGENT_INSTALL_METHOD_REMOTE,
#     AGENT_INSTALL_METHOD_INIT_SCRIPT,
#     AGENT_INSTALL_METHOD_PROVIDED,
#     AGENT_INSTALL_METHOD_PLUGIN
# ]
# AGENT_INSTALL_METHODS_SCRIPTS = [
#     AGENT_INSTALL_METHOD_INIT_SCRIPT,
#     AGENT_INSTALL_METHOD_PROVIDED,
#     AGENT_INSTALL_METHOD_PLUGIN
# ]
# COMPUTE_NODE_TYPE = 'cloudify.nodes.Compute'
# BROKER_PORT_NO_SSL = 5672
# BROKER_PORT_SSL = 5671
# CELERY_TASK_RESULT_EXPIRES = 600
# CLOUDIFY_TOKEN_AUTHENTICATION_HEADER = 'Authentication-Token'
# LOCAL_REST_CERT_FILE_KEY = 'LOCAL_REST_CERT_FILE'
# SECURED_PROTOCOL = 'https'
# BROKER_SSL_CERT_PATH = 'BROKER_SSL_CERT_PATH'
# BYPASS_MAINTENANCE = 'BYPASS_MAINTENANCE'
# LOGGING_CONFIG_FILE = '/etc/cloudify/logging.conf'
# CLUSTER_SETTINGS_PATH_KEY = 'CLOUDIFY_CLUSTER_SETTINGS_PATH'
# MGMTWORKER_QUEUE = 'cloudify.management'
# DEPLOYMENT = 'deployment'
# NODE_INSTANCE = 'node-instance'
# RELATIONSHIP_INSTANCE = 'relationship-instance'
# DEFAULT_NETWORK_NAME = 'default'
# 
# Path: cloudify/state.py
# @proxy
# def workflow_ctx():
#     return current_workflow_ctx.get_ctx()
# 
# @proxy
# def ctx():
#     return current_ctx.get_ctx()
# 
# Path: cloudify/exceptions.py
# class CommandExecutionException(Exception):
# 
#     """
#     Indicates a command was executed, however some sort of error happened,
#     resulting in a non-zero return value of the process.
# 
#     :param command: The command executed
#     :param code: process exit code
#     :param error: process stderr output
#     :param output: process stdout output
#     """
# 
#     def __init__(self, command, error, output, code):
#         self.command = command
#         self.error = error
#         self.code = code
#         self.output = output
#         Exception.__init__(self, self.__str__())
# 
#     def __str__(self):
#         return "Command '{0}' executed with an error." \
#                "\ncode: {1}" \
#                "\nerror: {2}" \
#                "\noutput: {3}" \
#             .format(self.command, self.code,
#                     self.error or None,
#                     self.output or None)


from contextlib import contextmanager
from distutils.version import LooseVersion
from cloudify import cluster, constants
from cloudify.state import workflow_ctx, ctx
from cloudify.exceptions import CommandExecutionException
import logging
import os
import random
import shlex
import ssl
import string
import subprocess
import sys
import tempfile
import traceback
import StringIO
    """
    Returns location of the broker certificate on the agent
    """
    if cluster.is_cluster_configured():
        active_node = cluster.get_cluster_active() or {}
        broker_ssl_cert_path = active_node.get('internal_cert_path')
        if broker_ssl_cert_path:
            return broker_ssl_cert_path
    return os.environ[constants.BROKER_SSL_CERT_PATH]


# maintained for backwards compatibility
get_manager_ip = get_manager_rest_service_host


def get_manager_rest_service_port():
    """
    Returns the port the manager REST service is running on.
    """
    return int(os.environ[constants.REST_PORT_KEY])


def get_local_rest_certificate():
    """
    Returns the path to the local copy of the server's public certificate
    """
    return os.environ[constants.LOCAL_REST_CERT_FILE_KEY]


def _get_current_context():

# Path: cloudify/cluster.py
# def _get_cluster_settings_file(filename=None):
# def get_cluster_settings(filename=None):
# def set_cluster_settings(settings, filename=None):
# def get_cluster_nodes(filename=None):
# def set_cluster_nodes(nodes, filename=None):
# def get_cluster_active(filename=None):
# def set_cluster_active(node, filename=None):
# def get_cluster_amqp_settings():
# def delete_cluster_settings(filename=None):
# def is_cluster_configured(filename=None):
# def _parse_url(broker_url):
# def config_from_broker_urls(active, nodes):
#     def __init__(self, *args, **kwargs):
#     def do_request(self, *args, **kwargs):
#     def _use_node(self, node_ip):
# class ClusterHTTPClient(HTTPClient):
# class CloudifyClusterClient(CloudifyClient):
# 
# Path: cloudify/constants.py
# REST_HOST_KEY = 'REST_HOST'
# REST_PORT_KEY = 'REST_PORT'
# CELERY_WORK_DIR_KEY = 'CELERY_WORK_DIR'
# MANAGER_FILE_SERVER_URL_KEY = 'MANAGER_FILE_SERVER_URL'
# MANAGER_FILE_SERVER_ROOT_KEY = 'MANAGER_FILE_SERVER_ROOT'
# FILE_SERVER_RESOURCES_FOLDER = 'resources'
# FILE_SERVER_BLUEPRINTS_FOLDER = 'blueprints'
# FILE_SERVER_DEPLOYMENTS_FOLDER = 'deployments'
# FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER = 'uploaded-blueprints'
# FILE_SERVER_SNAPSHOTS_FOLDER = 'snapshots'
# FILE_SERVER_PLUGINS_FOLDER = 'plugins'
# FILE_SERVER_GLOBAL_RESOURCES_FOLDER = 'global-resources'
# FILE_SERVER_TENANT_RESOURCES_FOLDER = 'tenant-resources'
# FILE_SERVER_AUTHENTICATORS_FOLDER = 'authenticators'
# AGENT_INSTALL_METHOD_NONE = 'none'
# AGENT_INSTALL_METHOD_REMOTE = 'remote'
# AGENT_INSTALL_METHOD_INIT_SCRIPT = 'init_script'
# AGENT_INSTALL_METHOD_PROVIDED = 'provided'
# AGENT_INSTALL_METHOD_PLUGIN = 'plugin'
# AGENT_INSTALL_METHODS = [
#     AGENT_INSTALL_METHOD_NONE,
#     AGENT_INSTALL_METHOD_REMOTE,
#     AGENT_INSTALL_METHOD_INIT_SCRIPT,
#     AGENT_INSTALL_METHOD_PROVIDED,
#     AGENT_INSTALL_METHOD_PLUGIN
# ]
# AGENT_INSTALL_METHODS_SCRIPTS = [
#     AGENT_INSTALL_METHOD_INIT_SCRIPT,
#     AGENT_INSTALL_METHOD_PROVIDED,
#     AGENT_INSTALL_METHOD_PLUGIN
# ]
# COMPUTE_NODE_TYPE = 'cloudify.nodes.Compute'
# BROKER_PORT_NO_SSL = 5672
# BROKER_PORT_SSL = 5671
# CELERY_TASK_RESULT_EXPIRES = 600
# CLOUDIFY_TOKEN_AUTHENTICATION_HEADER = 'Authentication-Token'
# LOCAL_REST_CERT_FILE_KEY = 'LOCAL_REST_CERT_FILE'
# SECURED_PROTOCOL = 'https'
# BROKER_SSL_CERT_PATH = 'BROKER_SSL_CERT_PATH'
# BYPASS_MAINTENANCE = 'BYPASS_MAINTENANCE'
# LOGGING_CONFIG_FILE = '/etc/cloudify/logging.conf'
# CLUSTER_SETTINGS_PATH_KEY = 'CLOUDIFY_CLUSTER_SETTINGS_PATH'
# MGMTWORKER_QUEUE = 'cloudify.management'
# DEPLOYMENT = 'deployment'
# NODE_INSTANCE = 'node-instance'
# RELATIONSHIP_INSTANCE = 'relationship-instance'
# DEFAULT_NETWORK_NAME = 'default'
# 
# Path: cloudify/state.py
# @proxy
# def workflow_ctx():
#     return current_workflow_ctx.get_ctx()
# 
# @proxy
# def ctx():
#     return current_ctx.get_ctx()
# 
# Path: cloudify/exceptions.py
# class CommandExecutionException(Exception):
# 
#     """
#     Indicates a command was executed, however some sort of error happened,
#     resulting in a non-zero return value of the process.
# 
#     :param command: The command executed
#     :param code: process exit code
#     :param error: process stderr output
#     :param output: process stdout output
#     """
# 
#     def __init__(self, command, error, output, code):
#         self.command = command
#         self.error = error
#         self.code = code
#         self.output = output
#         Exception.__init__(self, self.__str__())
# 
#     def __str__(self):
#         return "Command '{0}' executed with an error." \
#                "\ncode: {1}" \
#                "\nerror: {2}" \
#                "\noutput: {3}" \
#             .format(self.command, self.code,
#                     self.error or None,
#                     self.output or None)


# Path: cloudify/utils.py
from contextlib import contextmanager
from distutils.version import LooseVersion
from cloudify import cluster, constants
from cloudify.state import workflow_ctx, ctx
from cloudify.exceptions import CommandExecutionException
import logging
import os
import random
import shlex
import ssl
import string
import subprocess
import sys
import tempfile
import traceback
import StringIO

    """
    Returns location of the broker certificate on the agent
    """
    if cluster.is_cluster_configured():
        active_node = cluster.get_cluster_active() or {}
        broker_ssl_cert_path = active_node.get('internal_cert_path')
        if broker_ssl_cert_path:
            return broker_ssl_cert_path
    return os.environ[constants.BROKER_SSL_CERT_PATH]


# maintained for backwards compatibility
get_manager_ip = get_manager_rest_service_host


def get_manager_rest_service_port():
    """
    Returns the port the manager REST service is running on.
    """
    return int(os.environ[constants.REST_PORT_KEY])


def get_local_rest_certificate():
    """
    Returns the path to the local copy of the server's public certificate
    """
    return os.environ[constants.LOCAL_REST_CERT_FILE_KEY]


def _get_current_context():

    for context in [ctx, workflow_ctx]: