Beispiel #1
0
def test_load_string():
    role_md = role_metadata.load(yaml_data1, role_name='apache')

    log.debug('role_md: %s', role_md)

    assert isinstance(role_md, RoleMetadata)
    assert role_md.name == 'apache'
    assert role_md.min_ansible_version == '2.2'
    assert isinstance(role_md.galaxy_tags, list)
    assert 'web' in role_md.galaxy_tags
    assert role_md.galaxy_tags == ['web', 'apache', 'webserver', 'html']
    assert isinstance(role_md.allow_duplicates, bool)
Beispiel #2
0
def test_load():
    yaml_fo = io.StringIO(initial_value=yaml_data1)
    role_md = role_metadata.load(yaml_fo, role_name='apache')

    assert isinstance(role_md, RoleMetadata)

    assert role_md.name == 'apache'
    assert role_md.min_ansible_version == '2.2'
    assert isinstance(role_md.galaxy_tags, list)
    assert 'web' in role_md.galaxy_tags
    assert role_md.galaxy_tags == ['web', 'apache', 'webserver', 'html']
    assert isinstance(role_md.allow_duplicates, bool)
Beispiel #3
0
def load_from_dir(content_dir, namespace, name, installed=True):
    # TODO: or artifact

    path_name = os.path.join(content_dir, namespace, name)
    # TODO: add trad role or collection detection rules here
    #       Or possibly earlier so we could call 'collection' loading
    #       code/class or trad-role-as-collection loading code/class
    #       and avoid intermingly the impls.
    #       Maybe:
    #       if more than one role in roles/ -> collection

    if not os.path.isdir(path_name):
        log.debug(
            'The directory %s does not exist, unable to load a Repository from it',
            path_name)
        return None

    requirements_list = []

    # Now look for any install_info for the repository
    install_info_data = None
    install_info_filename = os.path.join(path_name,
                                         'meta/.galaxy_install_info')
    try:
        with open(install_info_filename, 'r') as ifd:
            install_info_data = install_info.load(ifd)
    except EnvironmentError as e:
        log.warning(
            'Unable to find or load meta/.galaxy_install_info for repository %s.%s: %s',
            namespace, name, e)

    # TODO: figure out what to do if the version from install_info conflicts with version
    #       from galaxy.yml etc.
    install_info_version = getattr(install_info_data, 'version', None)

    # Try to load a MANIFEST.json if we have one

    manifest_filename = os.path.join(
        path_name, collection_artifact_manifest.COLLECTION_MANIFEST_FILENAME)
    manifest_data = None

    try:
        with open(manifest_filename, 'r') as mfd:
            manifest_data = collection_artifact_manifest.load(mfd)
    except EnvironmentError:
        # log.debug('No galaxy.yml collection info found for collection %s.%s: %s', namespace, name, e)
        pass

    # load galaxy.yml
    galaxy_filename = os.path.join(path_name,
                                   collection_info.COLLECTION_INFO_FILENAME)

    collection_info_data = None

    try:
        with open(galaxy_filename, 'r') as gfd:
            collection_info_data = collection_info.load(gfd)
    except EnvironmentError:
        # log.debug('No galaxy.yml collection info found for collection %s.%s: %s', namespace, name, e)
        pass

    # Now try the repository as a role-as-collection
    # FIXME: For a repository with one role that matches the collection name and doesn't
    #        have a galaxy.yml, that's indistinguishable from a role-as-collection
    # FIXME: But in theory, if there is more than one role in roles/, we should skip this
    role_meta_main_filename = os.path.join(path_name, 'roles', name, 'meta',
                                           'main.yml')
    role_meta_main = None
    role_name = '%s.%s' % (namespace, name)

    try:
        with open(role_meta_main_filename, 'r') as rmfd:
            # FIXME: kluge to avoid circular import on py2
            #        repository->role_metadata->dependencies->repository_spec->repository (loop)
            #        repository->requirements->repository_spec->repository (loop)
            from ansible_galaxy import role_metadata
            role_meta_main = role_metadata.load(rmfd, role_name=role_name)
    except EnvironmentError:
        # log.debug('No meta/main.yml was loaded for repository %s.%s: %s', namespace, name, e)
        pass

    # Prefer version from install_info, but for a editable installed, there may be only galaxy version
    installed_version = install_info_version
    if manifest_data:
        installed_version = manifest_data.collection_info.version
    elif collection_info_data:
        installed_version = collection_info_data.version
    # if role_meta_main:
    #    installed_version = installed_version or role_meta_main.version

    # TODO/FIXME: what takes precedence?
    #           - the dir names a collection lives in ~/.ansible/content/my_ns/my_name
    #           - Or the namespace/name from galaxy.yml?
    # log.debug('collection_info_data: %s', collection_info_data)

    # Build a repository_spec of the repo now so we can pass it things like requirements.load()
    # that need to know what requires something
    repository_spec = RepositorySpec(namespace=namespace,
                                     name=name,
                                     version=installed_version)

    # The current galaxy.yml 'dependencies' are actually 'requirements' in ansible/ansible terminology
    # (ie, install-time)
    if collection_info_data:
        collection_requires = requirements.from_dependencies_dict(
            collection_info_data.dependencies, repository_spec=repository_spec)
        requirements_list.extend(collection_requires)

    # TODO: add requirements loaded from galaxy.yml
    # TODO: should the requirements in galaxy.yml be plain strings or dicts?
    # TODO: should there be requirements in galaxy.yml at all? in liue of requirements.yml
    # collection_info_requirements = []

    requirements_filename = os.path.join(path_name, 'requirements.yml')

    try:
        with open(requirements_filename, 'r') as rfd:
            requirements_list.extend(
                requirements.load(rfd, repository_spec=repository_spec))
    except EnvironmentError:
        # log.debug('No requirements.yml was loaded for repository %s.%s: %s', namespace, name, e)
        pass

    # TODO: if there are other places to load dependencies (ie, runtime deps) we will need
    #       to load them and combine them with role_depenency_specs
    role_dependency_specs = []
    if role_meta_main:
        role_dependency_specs = role_meta_main.dependencies

    repository = Repository(repository_spec=repository_spec,
                            path=path_name,
                            installed=installed,
                            requirements=requirements_list,
                            dependencies=role_dependency_specs)

    log.debug('Repository %s loaded from %s', repository.repository_spec.label,
              path_name)

    return repository