Example #1
0
def search(settings_file, url, repo, pattern):
    """Return of list of images in the repo matching the pattern.

    :arg str settings_file: Path to yaml file with Nexus settings.
    :arg str url: Nexus URL. Overrides settings.yaml.
    :arg str repo: The Nexus repository to audit.
    :arg str pattern: The pattern to search for in repo.
    """
    if not url and settings_file:
        url = get_url(settings_file)
    if not url:
        log.error(
            "ERROR: No Nexus URL provided. Please provide Nexus URL in " +
            "settings file or with the --server parameter.")
        sys.exit(1)

    _nexus = Nexus(url)

    # Check for NoneType, remove CLI escape characters from pattern
    if not pattern:
        pattern = ""
    pattern = pattern.replace("\\", "")

    all_images = _nexus.search_images(repo, pattern)

    # Ensure all of our images has a value for each of the keys we will use
    included_keys = ["name", "version", "id"]
    images = []
    for image in all_images:
        if set(included_keys).issubset(image):
            # Keep only the keys we're using
            restricted_image = {}
            for key in included_keys:
                restricted_image[key] = image[key]
            images.append(restricted_image)
    return images
Example #2
0
def create_repos(config_file, settings_file):
    """Create repositories as defined by configuration file.

    :arg str config: Configuration file containing repository definitions that
        will be used to create the new Nexus repositories.
    :arg str settings: Settings file containing administrative credentials and
        information.
    """
    with open(config_file, 'r') as f:
        config = yaml.safe_load(f)
    with open(settings_file, 'r') as f:
        settings = yaml.safe_load(f)

    for setting in ['nexus', 'user', 'password', 'email_domain']:
        if not setting in settings:
            log.error('{} needs to be defined'.format(setting))
            sys.exit(1)

    _nexus = Nexus(settings['nexus'], settings['user'], settings['password'])

    def create_nexus_perms(name, targets, email, password, extra_privs=[]):
        # Create target
        try:
            target_id = _nexus.get_target(name)
        except LookupError as e:
            target_id = _nexus.create_target(name, targets)

        # Create privileges
        privs_set = [
            'create',
            'delete',
            'read',
            'update',
        ]

        privs = {}
        for priv in privs_set:
            try:
                privs[priv] = _nexus.get_priv(name, priv)
                log.info('Creating {} privileges.'.format(priv))
            except LookupError as e:
                privs[priv] = _nexus.create_priv(name, target_id, priv)

        # Create Role
        try:
            role_id = _nexus.get_role(name)
            log.info('Creating {} role.'.format(role_id))
        except LookupError as e:
            role_id = _nexus.create_role(name, privs)

        # Create user
        try:
            _nexus.get_user(name)
            log.info('Creating {} user.'.format(name))
        except LookupError as e:
            _nexus.create_user(name, email, role_id, password, extra_privs)

    def build_repo(repo, repoId, config, base_groupId):
        log.info('-> Building for {}.{} in Nexus'.format(base_groupId, repo))
        groupId = '{}.{}'.format(base_groupId, repo)
        target = util.create_repo_target_regex(groupId)

        if 'extra_privs' in config:
            extra_privs = config['extra_privs']
            log.info('Privileges for this repo:' + ', '.join(extra_privs))
        else:
            extra_privs = []

        create_nexus_perms(repoId, [target], settings['email_domain'],
                           config['password'], extra_privs)

        log.info('-> Finished successfully for {}.{}!!\n'.format(
            base_groupId, repo))

        if 'repositories' in config:
            for sub_repo in config['repositories']:
                sub_repo_id = "{}-{}".format(repoId, sub_repo)
                build_repo(sub_repo, sub_repo_id,
                           config['repositories'][sub_repo], groupId)

    log.warning(
        'Nexus repo creation started. Aborting now could leave tasks undone!')
    for repo in config['repositories']:
        build_repo(repo, repo, config['repositories'][repo],
                   config['base_groupId'])