Ejemplo n.º 1
0
    def parse(self):
        ''' create an options parser for bin/ansible '''

        self.parser = CLI.base_parser(
            usage="usage: %%prog [%s] [--help] [options] ..." %
            "|".join(self.VALID_ACTIONS),
            epilog=
            "\nSee '%s <command> --help' for more information on a specific command.\n\n"
            % os.path.basename(sys.argv[0]))

        # common
        self.parser.add_option('-s',
                               '--server',
                               dest='api_server',
                               default=C.GALAXY_SERVER,
                               help='The API server destination')
        self.parser.add_option(
            '-c',
            '--ignore-certs',
            action='store_true',
            dest='ignore_certs',
            default=C.GALAXY_IGNORE_CERTS,
            help='Ignore SSL certificate validation errors.')
        self.set_action()

        super(GalaxyCLI, self).parse()

        display.verbosity = self.options.verbosity
        self.galaxy = Galaxy(self.options)
Ejemplo n.º 2
0
    def run(self):

        super(GalaxyCLI, self).run()

        self.galaxy = Galaxy()

        self.api = GalaxyAPI(self.galaxy)
        self.execute()
Ejemplo n.º 3
0
def get_galaxy(tmp_dir, token):
    return Galaxy(
        AttrDict(api_server=C.GALAXY_SERVER,
                 ignore_certs=C.GALAXY_IGNORE_CERTS,
                 ignore_errors=False,
                 no_deps=False,
                 roles_path=[tmp_dir],
                 token=token))
Ejemplo n.º 4
0
    def run(self):

        super(GalaxyCLI, self).run()

        self.galaxy = Galaxy()

        self.api = GalaxyAPI(self.galaxy)
        context.CLIARGS['func']()
Ejemplo n.º 5
0
    def __init__(self, repo, options=None):
        options = options or {}
        self.repo = repo
        opts = self.default_options()
        opts.update(options)

        Options = namedtuple('Options', sorted(opts))
        self.options = Options(**opts)
        self.galaxy = Galaxy(self.options)
Ejemplo n.º 6
0
def fetch_role(playbook_path, role_name):
    options = Options()
    options.roles_path = '{playbook_path}/provision/roles'.format(
        playbook_path=playbook_path, )
    if not path.exists(options.roles_path):
        makedirs(options.roles_path)
    galaxy = Galaxy(options)
    role = GalaxyRole(galaxy, role_name, path=options.roles_path)
    role.install()
    dependencies = get_application_dependencies(
        playbook_path,
        role_name,
    )
    for role in dependencies:
        fetch_role(playbook_path, role)
def test_role_download_github_default_version(mocker, galaxy_server,
                                              mock_role_download_api,
                                              monkeypatch):
    mock_api = mocker.MagicMock()
    mock_api.side_effect = [
        StringIO(u'{"available_versions":{"v1":"v1/"}}'),
        StringIO(
            u'{"results":[{"id":"123","github_user":"******","github_repo": "test_role"}]}'
        ),
        StringIO(u'{"results":[{"name": "0.0.1"},{"name": "0.0.2"}]}'),
    ]
    monkeypatch.setattr(api, 'open_url', mock_api)

    role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role').install()

    assert mock_role_download_api.call_count == 1
    assert mock_role_download_api.mock_calls[0][1][
        0] == 'https://github.com/test_owner/test_role/archive/0.0.2.tar.gz'
Ejemplo n.º 8
0
def test_role_download_url(init_mock_temp_file, mocker, galaxy_server,
                           mock_role_download_api, monkeypatch):
    mock_api = mocker.MagicMock()
    mock_api.side_effect = [
        StringIO(u'{"available_versions":{"v1":"v1/"}}'),
        StringIO(
            u'{"results":[{"id":"123","github_user":"******","github_repo": "test_role"}]}'
        ),
        StringIO(
            u'{"results":[{"name": "0.0.1","download_url":"http://localhost:8080/test_owner/test_role/0.0.1.tar.gz"},'
            u'{"name": "0.0.2","download_url":"http://localhost:8080/test_owner/test_role/0.0.2.tar.gz"}]}'
        ),
    ]
    monkeypatch.setattr(api, 'open_url', mock_api)

    role.GalaxyRole(Galaxy(),
                    galaxy_server,
                    'test_owner.test_role',
                    version="0.0.1").install()

    assert mock_role_download_api.call_count == 1
    assert mock_role_download_api.mock_calls[0][1][
        0] == 'http://localhost:8080/test_owner/test_role/0.0.1.tar.gz'
Ejemplo n.º 9
0
 def install(self, roles):
     roles_to_install = list(roles)
     with MakeTempDir() as temp_dir:
         self._galaxy = Galaxy(
             AttrDict(api_server=C.GALAXY_SERVER,
                      ignore_certs=C.GALAXY_IGNORE_CERTS,
                      ignore_errors=False,
                      no_deps=False,
                      roles_path=[temp_dir],
                      token=None))  # FIXME: support tokens
         roles_processed = []
         role_failure = False
         with InCaseOfFail(temp_dir):
             while roles_to_install:
                 try:
                     role_to_install = roles_to_install.pop()
                     role_obj, installed = self._role_to_temp_space(
                         role_to_install)
                     if installed:
                         deps = role_obj.metadata.get('dependencies', [])
                         for dep in deps:
                             if dep not in roles_to_install + roles_processed:
                                 roles_to_install.append(dep)
                         self._update_container_yml(role_obj)
                         self._update_requirements_yml(role_obj)
                     roles_processed.append(role_to_install)
                 except exceptions.AnsibleContainerGalaxyFatalException as exc:
                     logger.error(exc)
                     raise
                 except exceptions.AnsibleContainerGalaxyRoleException as exc:
                     logger.error(exc)
                     role_failure = True
                     continue
     if role_failure:
         raise exceptions.AnsibleContainerGalaxyRoleException(
             'One or more roles failed.')
Ejemplo n.º 10
0
    def parse(self):
        ''' create an options parser for bin/ansible '''

        self.parser = CLI.base_parser(
            usage="usage: %%prog [%s] [--help] [options] ..." %
            "|".join(self.VALID_ACTIONS),
            epilog=
            "\nSee '%s <command> --help' for more information on a specific command.\n\n"
            % os.path.basename(sys.argv[0]))

        self.set_action()

        # options specific to actions
        if self.action == "info":
            self.parser.set_usage(
                "usage: %prog info [options] role_name[,version]")
        elif self.action == "init":
            self.parser.set_usage("usage: %prog init [options] role_name")
            self.parser.add_option(
                '-p',
                '--init-path',
                dest='init_path',
                default="./",
                help=
                'The path in which the skeleton role will be created. The default is the current working directory.'
            )
            self.parser.add_option(
                '--offline',
                dest='offline',
                default=False,
                action='store_true',
                help="Don't query the galaxy API when creating roles")
        elif self.action == "install":
            self.parser.set_usage(
                "usage: %prog install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]"
            )
            self.parser.add_option(
                '-i',
                '--ignore-errors',
                dest='ignore_errors',
                action='store_true',
                default=False,
                help='Ignore errors and continue with the next specified role.'
            )
            self.parser.add_option(
                '-n',
                '--no-deps',
                dest='no_deps',
                action='store_true',
                default=False,
                help='Don\'t download roles listed as dependencies')
            self.parser.add_option(
                '-r',
                '--role-file',
                dest='role_file',
                help='A file containing a list of roles to be imported')
        elif self.action == "remove":
            self.parser.set_usage("usage: %prog remove role1 role2 ...")
        elif self.action == "list":
            self.parser.set_usage("usage: %prog list [role_name]")
        elif self.action == "search":
            self.parser.add_option('--platforms',
                                   dest='platforms',
                                   help='list of OS platforms to filter by')
            self.parser.add_option('--galaxy-tags',
                                   dest='tags',
                                   help='list of galaxy tags to filter by')
            self.parser.set_usage(
                "usage: %prog search [<search_term>] [--galaxy-tags <galaxy_tag1,galaxy_tag2>] [--platforms platform]"
            )

        # options that apply to more than one action
        if self.action != "init":
            self.parser.add_option(
                '-p',
                '--roles-path',
                dest='roles_path',
                default=C.DEFAULT_ROLES_PATH,
                help='The path to the directory containing your roles. '
                'The default is the roles_path configured in your '
                'ansible.cfg file (/etc/ansible/roles if not configured)')

        if self.action in ("info", "init", "install", "search"):
            self.parser.add_option('-s',
                                   '--server',
                                   dest='api_server',
                                   default="https://galaxy.ansible.com",
                                   help='The API server destination')
            self.parser.add_option(
                '-c',
                '--ignore-certs',
                action='store_false',
                dest='validate_certs',
                default=True,
                help='Ignore SSL certificate validation errors.')

        if self.action in ("init", "install"):
            self.parser.add_option('-f',
                                   '--force',
                                   dest='force',
                                   action='store_true',
                                   default=False,
                                   help='Force overwriting an existing role')

        # get options, args and galaxy object
        self.options, self.args = self.parser.parse_args()
        self.display.verbosity = self.options.verbosity
        self.galaxy = Galaxy(self.options, self.display)

        return True
Ejemplo n.º 11
0
    def parse(self):
        ''' create an options parser for bin/ansible '''

        self.parser = CLI.base_parser(
            usage="usage: %%prog [%s] [--help] [options] ..." %
            "|".join(self.VALID_ACTIONS),
            epilog=
            "\nSee '%s <command> --help' for more information on a specific command.\n\n"
            % os.path.basename(sys.argv[0]))

        self.set_action()

        # common
        self.parser.add_option('-s',
                               '--server',
                               dest='api_server',
                               default=C.GALAXY_SERVER,
                               help='The API server destination')
        self.parser.add_option(
            '-c',
            '--ignore-certs',
            action='store_true',
            dest='ignore_certs',
            default=C.GALAXY_IGNORE_CERTS,
            help='Ignore SSL certificate validation errors.')

        # specific to actions
        if self.action == "delete":
            self.parser.set_usage(
                "usage: %prog delete [options] github_user github_repo")
        elif self.action == "import":
            self.parser.set_usage(
                "usage: %prog import [options] github_user github_repo")
            self.parser.add_option('--no-wait',
                                   dest='wait',
                                   action='store_false',
                                   default=True,
                                   help='Don\'t wait for import results.')
            self.parser.add_option(
                '--branch',
                dest='reference',
                help=
                'The name of a branch to import. Defaults to the repository\'s default branch (usually master)'
            )
            self.parser.add_option(
                '--role-name',
                dest='role_name',
                help=
                'The name the role should have, if different than the repo name'
            )
            self.parser.add_option(
                '--status',
                dest='check_status',
                action='store_true',
                default=False,
                help=
                'Check the status of the most recent import request for given github_user/github_repo.'
            )
        elif self.action == "info":
            self.parser.set_usage(
                "usage: %prog info [options] role_name[,version]")
        elif self.action == "init":
            self.parser.set_usage("usage: %prog init [options] role_name")
            self.parser.add_option(
                '-p',
                '--init-path',
                dest='init_path',
                default="./",
                help=
                'The path in which the skeleton role will be created. The default is the current working directory.'
            )
            self.parser.add_option(
                '--container-enabled',
                dest='container_enabled',
                action='store_true',
                default=False,
                help=
                'Initialize the skeleton role with default contents for a Container Enabled role.'
            )
        elif self.action == "install":
            self.parser.set_usage(
                "usage: %prog install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]"
            )
            self.parser.add_option(
                '-i',
                '--ignore-errors',
                dest='ignore_errors',
                action='store_true',
                default=False,
                help='Ignore errors and continue with the next specified role.'
            )
            self.parser.add_option(
                '-n',
                '--no-deps',
                dest='no_deps',
                action='store_true',
                default=False,
                help='Don\'t download roles listed as dependencies')
            self.parser.add_option(
                '-r',
                '--role-file',
                dest='role_file',
                help='A file containing a list of roles to be imported')
        elif self.action == "remove":
            self.parser.set_usage("usage: %prog remove role1 role2 ...")
        elif self.action == "list":
            self.parser.set_usage("usage: %prog list [role_name]")
        elif self.action == "login":
            self.parser.set_usage("usage: %prog login [options]")
            self.parser.add_option(
                '--github-token',
                dest='token',
                default=None,
                help=
                'Identify with github token rather than username and password.'
            )
        elif self.action == "search":
            self.parser.set_usage(
                "usage: %prog search [searchterm1 searchterm2] [--galaxy-tags galaxy_tag1,galaxy_tag2] [--platforms platform1,platform2] [--author username]"
            )
            self.parser.add_option('--platforms',
                                   dest='platforms',
                                   help='list of OS platforms to filter by')
            self.parser.add_option('--galaxy-tags',
                                   dest='galaxy_tags',
                                   help='list of galaxy tags to filter by')
            self.parser.add_option('--author',
                                   dest='author',
                                   help='GitHub username')
        elif self.action == "setup":
            self.parser.set_usage(
                "usage: %prog setup [options] source github_user github_repo secret"
            )
            self.parser.add_option(
                '--remove',
                dest='remove_id',
                default=None,
                help=
                'Remove the integration matching the provided ID value. Use --list to see ID values.'
            )
            self.parser.add_option('--list',
                                   dest="setup_list",
                                   action='store_true',
                                   default=False,
                                   help='List all of your integrations.')

        # options that apply to more than one action
        if self.action in ['init', 'info']:
            self.parser.add_option(
                '--offline',
                dest='offline',
                default=False,
                action='store_true',
                help="Don't query the galaxy API when creating roles")

        if self.action not in ("delete", "import", "init", "login", "setup"):
            # NOTE: while the option type=str, the default is a list, and the
            # callback will set the value to a list.
            self.parser.add_option(
                '-p',
                '--roles-path',
                dest='roles_path',
                action="callback",
                callback=CLI.expand_paths,
                type=str,
                default=C.DEFAULT_ROLES_PATH,
                help=
                'The path to the directory containing your roles. The default is the roles_path configured in your ansible.cfg file (/etc/ansible/roles if not configured)'
            )

        if self.action in ("init", "install"):
            self.parser.add_option('-f',
                                   '--force',
                                   dest='force',
                                   action='store_true',
                                   default=False,
                                   help='Force overwriting an existing role')

        super(GalaxyCLI, self).parse()

        display.verbosity = self.options.verbosity
        self.galaxy = Galaxy(self.options)
Ejemplo n.º 12
0
collection_requirements = [Requirement('amazon.aws', '*', None, None)]
#collection_requirements = [Requirement('amazon.aws', '1.2.0', None, None)]
#collection_requirements = [Requirement('amazon.aws', '1.2.1-dev3', None, None)]
print()
print('Given collection requirements:')
#print(f'{collection_requirements=}')
for abstract_req in collection_requirements:
    print(f'\t* {abstract_req.fqcn}\t"{abstract_req.ver}"')
print()

context.CLIARGS = {  # patch a value normally populated by the CLI
    'ignore_certs': False,
    'type': 'collection',
}
galaxy_api = GalaxyAPI(Galaxy(), 'default_galaxy', C.GALAXY_SERVER)
resolver = Resolver(
    AnsibleGalaxyProvider(api=galaxy_api),
    BaseReporter(),
)
print()
print('Computing the dependency tree...')
print()
concrete_requirements = resolver.resolve(
    collection_requirements,
    max_rounds=2_000_000,  # avoid too deep backtracking; taken from pip
)
print()
print('Resolved concrete transitive dependencies:')
#print(f'{concrete_requirements=}')
#print(f'{concrete_requirements.mapping=}')