Пример #1
0
 def _update_requirements_yml(self, role_obj):
     requirements_yml_path = os.path.join(ANSIBLE_CONTAINER_PATH,
                                          'requirements.yml')
     requirements = None
     if os.path.exists(requirements_yml_path):
         try:
             requirements = ruamel.yaml.round_trip_load(
                 open(requirements_yml_path)) or []
         except Exception as exc:
             raise exceptions.AnsibleContainerGalaxyFatalException(
                 'Could not load project requirements.yml - %s' % str(exc))
     if not requirements:
         requirements = []
     for req in requirements:
         if req.get('src', '') == role_obj.src:
             logger.warning(
                 'Requirement %s already found in requirements.yml' %
                 role_obj.name)
             return
     role_def = {}
     role_def[u'src'] = role_obj.src
     if role_obj.version and role_obj.version != 'master':
         role_def[u'version'] = role_obj.version
     if role_obj.scm:
         role_def[u'scm'] = role_obj.scm
     if role_obj.name and role_obj.name != role_obj.src:
         role_def[u'name'] = role_obj.name
     requirements.append(role_def)
     try:
         ruamel.yaml.round_trip_dump(requirements,
                                     stream=open(requirements_yml_path,
                                                 'w'))
     except Exception as exc:
         raise exceptions.AnsibleContainerGalaxyFatalException(
             'Error updating requirements.yml')
Пример #2
0
    def _update_container_yml(self, role_obj):
        snippet = self._get_container_yml_snippet(role_obj)
        if not snippet:
            return None
        container_yml_path = os.path.join(ANSIBLE_CONTAINER_PATH,
                                          'container.yml')
        try:
            container_yml = ruamel.yaml.round_trip_load(
                open(container_yml_path))
        except Exception as exc:
            raise exceptions.AnsibleContainerGalaxyFatalException(
                'Failed to load container.yml: %s' % str(exc))

        if not container_yml['services']:
            container_yml['services'] = {}
        services = container_yml['services']
        # The snippet should be a dictionary with one key
        new_service_key = snippet.keys()[0]
        if new_service_key in services:
            raise exceptions.AnsibleContainerGalaxyRoleException(
                'Role defines service %s, but container.yml already has a service with this name'
                % new_service_key)

        # Add role name to the service's list of roles
        services[new_service_key] = snippet[new_service_key]
        if not services[new_service_key].get('roles'):
            services[new_service_key]['roles'] = []
        if role_obj.name not in services[new_service_key]['roles']:
            services[new_service_key]['roles'].append(role_obj.name)

        try:
            ruamel.yaml.round_trip_dump(container_yml,
                                        stream=open(container_yml_path, 'w'))
        except Exception as exc:
            raise exceptions.AnsibleContainerGalaxyFatalException(
                'Error updating container.yml - %s' % str(exc))
        return new_service_key