# Path: cloudify/broker_config.py
# DEFAULT_HEARTBEAT = 30
#     BROKER_USE_SSL = {
#         'ca_certs': broker_cert_path,
#         'cert_reqs': ssl.CERT_REQUIRED,
#     }
# URL_TEMPLATE = \
#     'amqp://{username}:{password}@{hostname}:{port}/{vhost}{options}'
#     BROKER_URL = ';'.join(URL_TEMPLATE.format(username=broker_username,
#                                               password=broker_password,
#                                               hostname=node_ip,
#                                               port=broker_port,
#                                               vhost=broker_vhost,
#                                               options=options)
#                           for node_ip in config['cluster'])
#     BROKER_URL = URL_TEMPLATE.format(
#         username=broker_username,
#         password=broker_password,
#         hostname=broker_hostname,
#         port=broker_port,
#         vhost=broker_vhost,
#         options=options
#     )
# CELERY_RESULT_BACKEND = BROKER_URL
# CELERY_TASK_RESULT_EXPIRES = 600
# CELERYD_PREFETCH_MULTIPLIER = 1
# CELERY_ACKS_LATE = False
# 
# Path: cloudify/utils.py
# CFY_EXEC_TEMPDIR_ENVVAR = 'CFY_EXEC_TEMP'
# class ManagerVersion(object):
# class LocalCommandRunner(object):
# class CommandExecutionResponse(object):
# class Internal(object):
#     def __init__(self, raw_version):
#     def greater_than(self, other):
#     def equals(self, other):
#     def __str__(self):
#     def __eq__(self, other):
#     def __gt__(self, other):
#     def __lt__(self, other):
#     def __ge__(self, other):
#     def __le__(self, other):
#     def __ne__(self, other):
# def setup_logger(logger_name,
#                  logger_level=logging.INFO,
#                  handlers=None,
#                  remove_existing_handlers=True,
#                  logger_format=None,
#                  propagate=True):
# def format_exception(e):
# def get_manager_file_server_url():
# def get_manager_file_server_root():
# def get_manager_rest_service_host():
# def get_broker_ssl_cert_path():
# def get_manager_rest_service_port():
# def get_local_rest_certificate():
# def _get_current_context():
# def get_rest_token():
# def get_tenant():
# def get_tenant_name():
# def get_is_bypass_maintenance():
# def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
# def get_exec_tempdir():
# def create_temp_folder():
# def exception_to_error_cause(exception, tb):
#     def __init__(self, logger=None, host='localhost'):
#     def run(self, command,
#             exit_on_failure=True,
#             stdout_pipe=True,
#             stderr_pipe=True,
#             cwd=None,
#             execution_env=None):
#     def __init__(self, command, std_out, std_err, return_code):
# def _shlex_split(command):
#     def get_install_method(properties):
#     def get_broker_ssl_options(ssl_enabled, cert_path):
#     def get_broker_credentials(cloudify_agent):
#     def _get_package_version(plugins_dir, package_name):
#     def plugin_prefix(package_name=None, package_version=None,
#                       deployment_id=None, plugin_name=None, tenant_name=None,
#                       sys_prefix_fallback=True):
#     def _change_tenant(ctx, tenant):
# 
# Path: cloudify/constants.py
# CELERY_TASK_RESULT_EXPIRES = 600
# 
# MGMTWORKER_QUEUE = 'cloudify.management'
# 
# BROKER_PORT_SSL = 5671
# 
# BROKER_PORT_NO_SSL = 5672


from cloudify import broker_config
from cloudify.utils import internal
from cloudify.constants import (CELERY_TASK_RESULT_EXPIRES,
                                MGMTWORKER_QUEUE,
                                BROKER_PORT_SSL,
                                BROKER_PORT_NO_SSL)
    from celery import Celery
    # the broker if multiple urls were passed.
    # If max_retries is provided and >0, we will raise an exception if we
    # can't connect; otherwise we'll keep retrying forever.
    # Need to raise an exception in the case of a cluster, so that the
    # next node can be tried
    celery_client.pool.connection.ensure_connection(max_retries=max_retries)
    return celery_client


def get_cluster_celery_app(broker_urls, cluster, ssl_enabled,
                           broker_ssl_cert_path=None):
    err = None
    for broker_url in broker_urls:
        try:
            return get_celery_app(
                broker_url=broker_url,
                broker_ssl_cert_path=broker_ssl_cert_path,
                broker_ssl_enabled=ssl_enabled,
                max_retries=1)
        except Exception as err:
            continue
    if err is not None:
        raise err


def _get_broker_url(tenant, target, broker_ssl_enabled):
    """
    If the target is the mgmtworker queue, or if no tenants was passed use
    the default broker URL. Otherwise, create a tenant-specific one
    """

# Path: cloudify/broker_config.py
# DEFAULT_HEARTBEAT = 30
#     BROKER_USE_SSL = {
#         'ca_certs': broker_cert_path,
#         'cert_reqs': ssl.CERT_REQUIRED,
#     }
# URL_TEMPLATE = \
#     'amqp://{username}:{password}@{hostname}:{port}/{vhost}{options}'
#     BROKER_URL = ';'.join(URL_TEMPLATE.format(username=broker_username,
#                                               password=broker_password,
#                                               hostname=node_ip,
#                                               port=broker_port,
#                                               vhost=broker_vhost,
#                                               options=options)
#                           for node_ip in config['cluster'])
#     BROKER_URL = URL_TEMPLATE.format(
#         username=broker_username,
#         password=broker_password,
#         hostname=broker_hostname,
#         port=broker_port,
#         vhost=broker_vhost,
#         options=options
#     )
# CELERY_RESULT_BACKEND = BROKER_URL
# CELERY_TASK_RESULT_EXPIRES = 600
# CELERYD_PREFETCH_MULTIPLIER = 1
# CELERY_ACKS_LATE = False
# 
# Path: cloudify/utils.py
# CFY_EXEC_TEMPDIR_ENVVAR = 'CFY_EXEC_TEMP'
# class ManagerVersion(object):
# class LocalCommandRunner(object):
# class CommandExecutionResponse(object):
# class Internal(object):
#     def __init__(self, raw_version):
#     def greater_than(self, other):
#     def equals(self, other):
#     def __str__(self):
#     def __eq__(self, other):
#     def __gt__(self, other):
#     def __lt__(self, other):
#     def __ge__(self, other):
#     def __le__(self, other):
#     def __ne__(self, other):
# def setup_logger(logger_name,
#                  logger_level=logging.INFO,
#                  handlers=None,
#                  remove_existing_handlers=True,
#                  logger_format=None,
#                  propagate=True):
# def format_exception(e):
# def get_manager_file_server_url():
# def get_manager_file_server_root():
# def get_manager_rest_service_host():
# def get_broker_ssl_cert_path():
# def get_manager_rest_service_port():
# def get_local_rest_certificate():
# def _get_current_context():
# def get_rest_token():
# def get_tenant():
# def get_tenant_name():
# def get_is_bypass_maintenance():
# def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
# def get_exec_tempdir():
# def create_temp_folder():
# def exception_to_error_cause(exception, tb):
#     def __init__(self, logger=None, host='localhost'):
#     def run(self, command,
#             exit_on_failure=True,
#             stdout_pipe=True,
#             stderr_pipe=True,
#             cwd=None,
#             execution_env=None):
#     def __init__(self, command, std_out, std_err, return_code):
# def _shlex_split(command):
#     def get_install_method(properties):
#     def get_broker_ssl_options(ssl_enabled, cert_path):
#     def get_broker_credentials(cloudify_agent):
#     def _get_package_version(plugins_dir, package_name):
#     def plugin_prefix(package_name=None, package_version=None,
#                       deployment_id=None, plugin_name=None, tenant_name=None,
#                       sys_prefix_fallback=True):
#     def _change_tenant(ctx, tenant):
# 
# Path: cloudify/constants.py
# CELERY_TASK_RESULT_EXPIRES = 600
# 
# MGMTWORKER_QUEUE = 'cloudify.management'
# 
# BROKER_PORT_SSL = 5671
# 
# BROKER_PORT_NO_SSL = 5672


# Path: cloudify/celery/app.py
from cloudify import broker_config
from cloudify.utils import internal
from cloudify.constants import (CELERY_TASK_RESULT_EXPIRES,
                                MGMTWORKER_QUEUE,
                                BROKER_PORT_SSL,
                                BROKER_PORT_NO_SSL)
    from celery import Celery

    # the broker if multiple urls were passed.
    # If max_retries is provided and >0, we will raise an exception if we
    # can't connect; otherwise we'll keep retrying forever.
    # Need to raise an exception in the case of a cluster, so that the
    # next node can be tried
    celery_client.pool.connection.ensure_connection(max_retries=max_retries)
    return celery_client


def get_cluster_celery_app(broker_urls, cluster, ssl_enabled,
                           broker_ssl_cert_path=None):
    err = None
    for broker_url in broker_urls:
        try:
            return get_celery_app(
                broker_url=broker_url,
                broker_ssl_cert_path=broker_ssl_cert_path,
                broker_ssl_enabled=ssl_enabled,
                max_retries=1)
        except Exception as err:
            continue
    if err is not None:
        raise err


def _get_broker_url(tenant, target, broker_ssl_enabled):
    """
    If the target is the mgmtworker queue, or if no tenants was passed use
    the default broker URL. Otherwise, create a tenant-specific one
    """

    if target == MGMTWORKER_QUEUE or not tenant: