# Path: cloudify_agent/api/utils.py
# class _Internal(object):
#     CLOUDIFY_DAEMON_STORAGE_DIRECTORY_KEY = 'CLOUDIFY_DAEMON_STORAGE_DIRECTORY'
#     CLOUDIFY_DAEMON_USER_KEY = 'CLOUDIFY_DAEMON_USER'
#     def get_daemon_storage_dir(cls):
#     def get_daemon_user(cls):
#     def get_storage_directory(cls, username=None):
#     def generate_agent_name():
#     def generate_new_agent_name(old_agent_name):
#     def daemon_to_dict(daemon):
#     def get_broker_url(agent):
# def get_agent_registered(name,
#                          celery_client,
#                          timeout=workflows_tasks.INSPECT_TIMEOUT):
# def get_windows_home_dir(username):
# def get_home_dir(username=None):
# def render_template_to_file(template_path, file_path=None, **values):
# def resource_to_tempfile(resource_path, executable=False):
# def get_resource(resource_path):
# def get_absolute_resource_path(resource_path):
# def content_to_file(content, file_path=None, executable=False):
# def env_to_file(env_variables, destination_path=None, posix=True):
# def stringify_values(dictionary):
# def purge_none_values(dictionary):
# def json_load(file_path):
# def json_loads(content):
# def safe_create_dir(path):
# def get_rest_client(rest_host,
#                     rest_port,
#                     rest_token,
#                     rest_tenant,
#                     ssl_cert_path,
#                     bypass_maintenance_mode=False):
# def _parse_comma_separated(ctx, param, value):
# def get_manager_file_server_url(hostname, port, scheme=None):
# def get_agent_version():
# def get_windows_basedir():
# def get_linux_basedir():
# def get_agent_basedir(is_windows=False):


import logging
import time
from cloudify.amqp_client import get_client
from cloudify.utils import setup_logger
from cloudify_agent.api import utils as agent_utils
logger = setup_logger(
    'cloudify-agent.tests.daemon',
    logger_level=logging.DEBUG)


def _is_agent_alive(name, timeout=10):
    return agent_utils.is_agent_alive(
        name,
        get_client(),
        timeout=timeout)


def assert_daemon_alive(name):
    assert _is_agent_alive(name)


def assert_daemon_dead(name):
    assert not _is_agent_alive(name)


def wait_for_daemon_alive(name, timeout=10):
    deadline = time.time() + timeout

    while time.time() < deadline:
        if _is_agent_alive(name, timeout=5):
            return
        logger.info('Waiting for daemon {0} to start...'
                    .format(name))
        time.sleep(1)
    raise RuntimeError('Failed waiting for daemon {0} to start. Waited '

# Path: cloudify_agent/api/utils.py
# class _Internal(object):
#     CLOUDIFY_DAEMON_STORAGE_DIRECTORY_KEY = 'CLOUDIFY_DAEMON_STORAGE_DIRECTORY'
#     CLOUDIFY_DAEMON_USER_KEY = 'CLOUDIFY_DAEMON_USER'
#     def get_daemon_storage_dir(cls):
#     def get_daemon_user(cls):
#     def get_storage_directory(cls, username=None):
#     def generate_agent_name():
#     def generate_new_agent_name(old_agent_name):
#     def daemon_to_dict(daemon):
#     def get_broker_url(agent):
# def get_agent_registered(name,
#                          celery_client,
#                          timeout=workflows_tasks.INSPECT_TIMEOUT):
# def get_windows_home_dir(username):
# def get_home_dir(username=None):
# def render_template_to_file(template_path, file_path=None, **values):
# def resource_to_tempfile(resource_path, executable=False):
# def get_resource(resource_path):
# def get_absolute_resource_path(resource_path):
# def content_to_file(content, file_path=None, executable=False):
# def env_to_file(env_variables, destination_path=None, posix=True):
# def stringify_values(dictionary):
# def purge_none_values(dictionary):
# def json_load(file_path):
# def json_loads(content):
# def safe_create_dir(path):
# def get_rest_client(rest_host,
#                     rest_port,
#                     rest_token,
#                     rest_tenant,
#                     ssl_cert_path,
#                     bypass_maintenance_mode=False):
# def _parse_comma_separated(ctx, param, value):
# def get_manager_file_server_url(hostname, port, scheme=None):
# def get_agent_version():
# def get_windows_basedir():
# def get_linux_basedir():
# def get_agent_basedir(is_windows=False):


# Path: cloudify_agent/tests/daemon.py
import logging
import time
from cloudify.amqp_client import get_client
from cloudify.utils import setup_logger
from cloudify_agent.api import utils as agent_utils

logger = setup_logger(
    'cloudify-agent.tests.daemon',
    logger_level=logging.DEBUG)


def _is_agent_alive(name, timeout=10):
    return agent_utils.is_agent_alive(
        name,
        get_client(),
        timeout=timeout)


def assert_daemon_alive(name):
    assert _is_agent_alive(name)


def assert_daemon_dead(name):
    assert not _is_agent_alive(name)


def wait_for_daemon_alive(name, timeout=10):
    deadline = time.time() + timeout

    while time.time() < deadline:
        if _is_agent_alive(name, timeout=5):
            return
        logger.info('Waiting for daemon {0} to start...'
                    .format(name))
        time.sleep(1)
    raise RuntimeError('Failed waiting for daemon {0} to start. Waited '

                       'for {1} seconds'.format(name, timeout))