def _make_venv(venv, python, force):
    """Handles the virtualenv.

    removes the virtualenv if required, else, notifies
    that it already exists. If it doesn't exist, it will be
    created.
    :param string venv: path of virtualenv to install in.
    :param string python: python binary path to use.
    :param bool force: whether to force creation or not if it
     already exists.
    """
    if utils.is_virtualenv(venv):
        if force:
            lgr.info('Installing within existing virtualenv: {0}'.format(venv))
        else:
            lgr.error('Virtualenv already exists at {0}. '
                      'You can use the -f flag to install within the '
                      'existing virtualenv.'.format(venv))
            sys.exit(codes.errors['virtualenv_already_exists'])
    else:
        lgr.debug('Creating virtualenv: {0}'.format(venv))
        utils.make_virtualenv(venv, python)
def _make_venv(venv, python, force):
    """Handles the virtualenv.

    removes the virtualenv if required, else, notifies
    that it already exists. If it doesn't exist, it will be
    created.
    :param string venv: path of virtualenv to install in.
    :param string python: python binary path to use.
    :param bool force: whether to force creation or not if it
     already exists.
    """
    if utils.is_virtualenv(venv):
        if force:
            lgr.info('Installing within existing virtualenv: {0}'.format(venv))
        else:
            lgr.error('Virtualenv already exists at {0}. '
                      'You can use the -f flag to install within the '
                      'existing virtualenv.'.format(venv))
            sys.exit(codes.errors['virtualenv_already_exists'])
    else:
        lgr.debug('Creating virtualenv: {0}'.format(venv))
        utils.make_virtualenv(venv, python)
def create(config=None, config_file=None, force=False, dryrun=False,
           no_validate=False, verbose=True):
    """Creates an agent package (tar.gz)

    This will try to identify the distribution of the host you're running on.
    If it can't identify it for some reason, you'll have to supply a
    `distribution` (e.g. Ubuntu) config object in the config.yaml.
    The same goes for the `release` (e.g. Trusty).

    A virtualenv will be created under cloudify/env.

    The order of the modules' installation is as follows:
    cloudify-rest-service
    cloudify-plugins-common
    cloudify-script-plugin
    cloudify-diamond-plugin
    cloudify-agent
    any additional modules specified under `additional_modules` in the yaml.
    any additional plugins specified under `additional_plugins` in the yaml.
    Once all modules are installed, excluded modules will be uninstalled;
    installation validation will occur; a tar.gz file will be created.
    The `output_tar` config object can be specified to determine the path to
    the output file. If omitted, a default path will be given with the
    format `DISTRIBUTION-RELEASE-agent.tar.gz`.
    """
    set_global_verbosity_level(verbose)

    # this will be updated with installed plugins and modules and used
    # to validate the installation
    final_set = {'modules': [], 'plugins': []}
    if not config:
        config = _import_config(config_file) if config_file else \
            _import_config()
        config = {} if not config else config

    name_params = {}
    try:
        (distro, release) = get_os_props()
        name_params['distro'] = config.get('distribution', distro)
        name_params['release'] = config.get('release', release)
        name_params['version'] = config.get(
            'version', os.environ.get('VERSION', None))
        name_params['milestone'] = config.get(
            'milestone', os.environ.get('PRERELEASE', None))
        name_params['build'] = config.get(
            'build', os.environ.get('BUILD', None))
    except Exception as ex:
        lgr.error(
            'Distribution not found in configuration '
            'and could not be retrieved automatically. '
            'please specify the distribution in the yaml. '
            '({0})'.format(ex.message))
        sys.exit(codes.errors['could_not_identify_distribution'])
    python = config.get('python_path', '/usr/bin/python')
    venv = DEFAULT_VENV_PATH
    venv_already_exists = utils.is_virtualenv(venv)
    destination_tar = config.get('output_tar', _name_archive(**name_params))

    lgr.debug('Distibution is: {0}'.format(name_params['distro']))
    lgr.debug('Distribution release is: {0}'.format(name_params['release']))
    lgr.debug('Python path is: {0}'.format(python))
    lgr.debug('Destination tarfile is: {0}'.format(destination_tar))

    if not dryrun:
        _make_venv(venv, python, force)

    _handle_output_file(destination_tar, force)

    modules = _set_defaults()
    modules = _merge_modules(modules, config)

    if dryrun:
        set_global_verbosity_level(True)
    lgr.debug('Modules and plugins to install: {0}'.format(json.dumps(
        modules, sort_keys=True, indent=4, separators=(',', ': '))))
    if dryrun:
        lgr.info('Dryrun complete')
        sys.exit(codes.notifications['dryrun_complete'])

    final_set = _install(modules, venv, final_set)
    _uninstall_excluded(modules, venv)
    if not no_validate:
        _validate(final_set, venv)
    utils.tar(venv, destination_tar)

    lgr.info('The following modules and plugins were installed '
             'in the agent:\n{0}'.format(utils.get_installed(venv)))

    # if keep_virtualenv is explicitly specified to be false, the virtualenv
    # will not be deleted.
    # if keep_virtualenv is not in the config but the virtualenv already
    # existed, it will not be deleted.
    if ('keep_virtualenv' in config and not config['keep_virtualenv']) \
            or ('keep_virtualenv' not in config and not venv_already_exists):
        lgr.info('Removing origin virtualenv...')
        shutil.rmtree(venv)

    # duh!
    lgr.info('Process complete!')
def create(config=None,
           config_file=None,
           force=False,
           dryrun=False,
           no_validate=False,
           verbose=True):
    """Creates an agent package (tar.gz)

    This will try to identify the distribution of the host you're running on.
    If it can't identify it for some reason, you'll have to supply a
    `distribution` (e.g. Ubuntu) config object in the config.yaml.
    The same goes for the `release` (e.g. Trusty).

    A virtualenv will be created under cloudify/env.

    The order of the modules' installation is as follows:
    cloudify-rest-service
    cloudify-plugins-common
    cloudify-script-plugin
    cloudify-diamond-plugin
    cloudify-agent
    any additional modules specified under `additional_modules` in the yaml.
    any additional plugins specified under `additional_plugins` in the yaml.
    Once all modules are installed, excluded modules will be uninstalled;
    installation validation will occur; a tar.gz file will be created.
    The `output_tar` config object can be specified to determine the path to
    the output file. If omitted, a default path will be given with the
    format `DISTRIBUTION-RELEASE-agent.tar.gz`.
    """
    set_global_verbosity_level(verbose)

    # this will be updated with installed plugins and modules and used
    # to validate the installation
    final_set = {'modules': [], 'plugins': []}
    if not config:
        config = _import_config(config_file) if config_file else \
            _import_config()
        config = {} if not config else config

    name_params = {}
    try:
        (distro, release) = get_os_props()
        name_params['distro'] = config.get('distribution', distro)
        name_params['release'] = config.get('release', release)
        name_params['version'] = config.get('version',
                                            os.environ.get('VERSION', None))
        name_params['milestone'] = config.get(
            'milestone', os.environ.get('PRERELEASE', None))
        name_params['build'] = config.get('build',
                                          os.environ.get('BUILD', None))
    except Exception as ex:
        lgr.error('Distribution not found in configuration '
                  'and could not be retrieved automatically. '
                  'please specify the distribution in the yaml. '
                  '({0})'.format(ex.message))
        sys.exit(codes.errors['could_not_identify_distribution'])
    python = config.get('python_path', '/usr/bin/python')
    venv = DEFAULT_VENV_PATH
    venv_already_exists = utils.is_virtualenv(venv)
    destination_tar = config.get('output_tar', _name_archive(**name_params))

    lgr.debug('Distibution is: {0}'.format(name_params['distro']))
    lgr.debug('Distribution release is: {0}'.format(name_params['release']))
    lgr.debug('Python path is: {0}'.format(python))
    lgr.debug('Destination tarfile is: {0}'.format(destination_tar))

    if not dryrun:
        _make_venv(venv, python, force)

    _handle_output_file(destination_tar, force)

    modules = _set_defaults()
    modules = _merge_modules(modules, config)

    if dryrun:
        set_global_verbosity_level(True)
    lgr.debug('Modules and plugins to install: {0}'.format(
        json.dumps(modules, sort_keys=True, indent=4, separators=(',', ': '))))
    if dryrun:
        lgr.info('Dryrun complete')
        sys.exit(codes.notifications['dryrun_complete'])

    final_set = _install(modules, venv, final_set)
    _uninstall_excluded(modules, venv)
    if not no_validate:
        _validate(final_set, venv)
    utils.tar(venv, destination_tar)

    lgr.info('The following modules and plugins were installed '
             'in the agent:\n{0}'.format(utils.get_installed(venv)))

    # if keep_virtualenv is explicitly specified to be false, the virtualenv
    # will not be deleted.
    # if keep_virtualenv is not in the config but the virtualenv already
    # existed, it will not be deleted.
    if ('keep_virtualenv' in config and not config['keep_virtualenv']) \
            or ('keep_virtualenv' not in config and not venv_already_exists):
        lgr.info('Removing origin virtualenv...')
        shutil.rmtree(venv)

    # duh!
    lgr.info('Process complete!')