def get_test_plugins(workspace_path=None):
    """
    Find all wagons from the workspace (generated during previous build job)
    and return a tuple with the wagon path on the manager and the plugin
    YAML path on the manger.
    :return: list of tuples
    """

    plugin_yaml = copy_file_to_docker('plugin.yaml')
    return [(copy_file_to_docker(f), plugin_yaml)
            for f in get_workspace_files(workspace_path=workspace_path)
            if f.endswith('.wgn')]
def upload_test_plugins_dev(plugins,
                            execute_bundle_upload=True,
                            bundle_path=None):
    """
    Upload all plugins that we need to execute the test.
    :param plugins: A list of additional plugins to upload.
       (Like ones that are not in the bundle (Openstack 3, Host Pool).
    :param execute_bundle_upload: Whether to install a bundle.
    :param bundle_path: Path to plugins bundle.
    :return:
    """

    plugins = plugins or []
    bundle_path = bundle_path or ''
    if execute_bundle_upload:
        if os.path.isfile(bundle_path):
            logger.info("Using plugins bundle found at: {path}".format(
                path=bundle_path))
            cloudify_exec(
                'cfy plugins bundle-upload --path {bundle_path}'.format(
                    bundle_path=copy_file_to_docker(bundle_path)),
                get_json=False)
        else:
            cloudify_exec('cfy plugins bundle-upload', get_json=False)

    for plugin in plugins:
        sleep(3)
        output = plugins_upload(plugin[0], plugin[1])
        logger.info('Uploaded plugin: {0}'.format(output))
    logger.info('Plugins list: {0}'.format(cloudify_exec('cfy plugins list')))
def secrets_create(name, is_file=False):
    """
    Create a secret on the manager.
    :param name: The secret key.
    :param is_file: Whether to create the secret from a file.
    :return:
    """
    logger.info('Creating secret: {0}.'.format(name))
    try:
        value = (base64.b64decode(
            os.environ[name].encode('utf-8'))).decode('ascii')
    except KeyError:
        raise EcosystemTestException(
            'Secret env var not set {0}.'.format(name))
    if is_file:
        with NamedTemporaryFile(mode='w+', delete=True) as outfile:
            outfile.write(value)
            outfile.flush()
            cmd = 'cfy secrets create -u {0} -f {1}'.format(
                name, copy_file_to_docker(outfile.name))
            return cloudify_exec(cmd, get_json=False, log=False)

    return cloudify_exec('cfy secrets create -u {0} -s {1}'.format(
        name, value),
                         get_json=False,
                         log=False)
def upload_test_plugins(plugins,
                        plugin_test,
                        execute_bundle_upload=True,
                        workspace_path=None):
    """
    Upload all plugins that we need to execute the test.
    :param plugins: A list of additional plugins to upload.
       (Like ones that are not in the bundle (Openstack 3, Host Pool).
    :param plugin_test: Whether to install plugins from workspace.
    :param execute_bundle_upload: Whether to install a bundle.
    :return:
    """

    plugins = plugins or []
    if plugin_test:
        for plugin_pair in get_test_plugins():
            plugins.append(plugin_pair)
    if execute_bundle_upload:
        bundle_path = get_bundle_from_workspace(workspace_path=workspace_path)
        if bundle_path:
            cloudify_exec(
                'cfy plugins bundle-upload --path {bundle_path}'.format(
                    bundle_path=copy_file_to_docker(bundle_path)),
                get_json=False)
        else:
            cloudify_exec('cfy plugins bundle-upload', get_json=False)

    for plugin in plugins:
        sleep(3)
        output = plugins_upload(plugin[0], plugin[1])
        logger.info('Uploaded plugin: {0}'.format(output))
    logger.info('Plugins list: {0}'.format(cloudify_exec('cfy plugins list')))
def plugins_upload(wagon_path, yaml_path):
    """
    Upload a wagon and plugin YAML to the manager.
    :param wagon_path: Path to the wagon on the manager.
    :param yaml_path: Path to the YAML on the manager container.
    :return: Command output.
    """
    logger.info('Uploading plugin: {0} {1}'.format(wagon_path, yaml_path))
    if not plugin_already_uploaded(wagon_path):
        if os.path.exists(wagon_path):
            wagon_path = copy_file_to_docker(wagon_path)
        if os.path.exists(yaml_path):
            yaml_path = copy_file_to_docker(yaml_path)
        return cloudify_exec('cfy plugins upload {0} -y {1}'.format(
            wagon_path, yaml_path),
                             get_json=False)
def prepare_test(plugins=None,
                 secrets=None,
                 plugin_test=False,
                 pip_packages=None,
                 yum_packages=None,
                 execute_bundle_upload=True,
                 use_vpn=False,
                 workspace_path=None):
    """
    Prepare the environment for executing a plugin test.



    :param plugins: A list of plugins to install. `plugin_test` must be True.
    :param secrets: A list of secrets to create.
    :param plugin_test: Do want to use wagons in the workspace for this test?
    :param pip_packages: A list of packages to install (on manger) with pip.
    :param yum_packages: A list of packages to install (on manger) with yum.
    :param execute_bundle_upload: Whether to upload the plugins bundle.
    :param use_vpn:
    :param workspace_path: THe path to the build directory if not circleci
    :return:
    """

    pip_packages = pip_packages or []
    yum_packages = yum_packages or []
    use_cfy()
    license_upload()
    upload_test_plugins(plugins,
                        plugin_test,
                        execute_bundle_upload,
                        workspace_path=workspace_path)
    create_test_secrets(secrets)
    yum_command = 'yum install -y python-netaddr git '
    if use_vpn:
        yum_packages.append('openvpn')
    if yum_packages:
        yum_command = yum_command + ' '.join(yum_packages)
    docker_exec(yum_command)
    pip_command = '/opt/mgmtworker/env/bin/pip install netaddr ipaddr '
    if pip_packages:
        pip_command = pip_command + ' '.join(pip_packages)
    docker_exec(pip_command)
    if use_vpn:
        value = base64.b64decode(os.environ['vpn_config'])
        file_temp = NamedTemporaryFile(delete=False)
        with open(file_temp.name, 'w') as outfile:
            outfile.write(value)
        docker_path = copy_file_to_docker(file_temp.name)
        docker_exec('mv {0} {1}'.format(docker_path, VPN_CONFIG_PATH))
def prepare_inputs(inputs):
    logger.info("Preparing inputs...")
    if not inputs:
        yield
    elif type(inputs) is dict:
        with NamedTemporaryFile(mode='w+', delete=True) as outfile:
            yaml.dump(inputs, outfile, allow_unicode=False)
            logger.debug(
                "temporary inputs file path {p}".format(p=outfile.name))
            inputs_on_docker = copy_file_to_docker(outfile.name)
            try:
                yield inputs_on_docker
            finally:
                delete_file_from_docker(inputs_on_docker)
    elif os.path.isfile(inputs):
        inputs_on_docker = copy_file_to_docker(inputs)
        try:
            yield inputs_on_docker
        finally:
            delete_file_from_docker(inputs_on_docker)
    else:
        # It's input string or None so yield it as is.
        yield inputs
def license_upload():
    """
    Upload the license to the manager.
    :return: Command output.
    """

    logger.info('Uploading manager license.')
    try:
        license = base64.b64decode(os.environ[LICENSE_ENVAR_NAME])
    except KeyError:
        raise EcosystemTestException('License env var not set {0}.')
    file_temp = NamedTemporaryFile(delete=False)
    with open(file_temp.name, 'wb') as outfile:
        outfile.write(license)
    return cloudify_exec('cfy license upload {0}'.format(
        copy_file_to_docker(file_temp.name)),
                         get_json=False)