Example #1
0
    def init_parser(self):
        ''' create an options parser for bin/ansible '''

        super(GalaxyCLI, self).init_parser(
            desc="Perform various Role related operations.",
        )

        # common
        common = opt_help.argparse.ArgumentParser(add_help=False)
        common.add_argument('-s', '--server', dest='api_server', default=C.GALAXY_SERVER, help='The API server destination')
        common.add_argument('-c', '--ignore-certs', action='store_true', dest='ignore_certs', default=C.GALAXY_IGNORE_CERTS,
                            help='Ignore SSL certificate validation errors.')
        opt_help.add_verbosity_options(common)

        # options that apply to more than one action
        user_repo = opt_help.argparse.ArgumentParser(add_help=False)
        user_repo.add_argument('github_user', help='GitHub username')
        user_repo.add_argument('github_repo', help='GitHub repository')

        offline = opt_help.argparse.ArgumentParser(add_help=False)
        offline.add_argument('--offline', dest='offline', default=False, action='store_true',
                             help="Don't query the galaxy API when creating roles")

        default_roles_path = C.config.get_configuration_definition('DEFAULT_ROLES_PATH').get('default', '')
        roles_path = opt_help.argparse.ArgumentParser(add_help=False)
        roles_path.add_argument('-p', '--roles-path', dest='roles_path', type=opt_help.unfrack_path(pathsep=True),
                                default=C.DEFAULT_ROLES_PATH, action=opt_help.PrependListAction,
                                help='The path to the directory containing your roles. The default is the first writable one'
                                     'configured via DEFAULT_ROLES_PATH: %s ' % default_roles_path)

        force = opt_help.argparse.ArgumentParser(add_help=False)
        force.add_argument('-f', '--force', dest='force', action='store_true', default=False,
                           help='Force overwriting an existing role or collection')

        # Add sub parser for the Galaxy role type (role or collection)
        type_parser = self.parser.add_subparsers(metavar='TYPE', dest='type')
        type_parser.required = True

        # Define the actions for the collection object type
        collection = type_parser.add_parser('collection',
                                            parents=[common],
                                            help='Manage an Ansible Galaxy collection.')

        collection_parser = collection.add_subparsers(metavar='ACTION', dest='collection')
        collection_parser.required = True

        build_parser = collection_parser.add_parser(
            'build', help='Build an Ansible collection artifact that can be published to Ansible Galaxy.',
            parents=[common, force])
        build_parser.set_defaults(func=self.execute_build)
        build_parser.add_argument(
            'args', metavar='collection', nargs='*', default=('./',),
            help='Path to the collection(s) directory to build. This should be the directory that contains the '
                 'galaxy.yml file. The default is the current working directory.')

        build_parser.add_argument(
            '--output-path', dest='output_path', default='./',
            help='The path in which the collection is built to. The default is the current working directory.')

        self.add_init_parser(collection_parser, [common, force])

        cinstall_parser = collection_parser.add_parser('install', help='Install collection from Ansible Galaxy',
                                                       parents=[force, common])
        cinstall_parser.set_defaults(func=self.execute_install)
        cinstall_parser.add_argument('args', metavar='collection_name', nargs='*',
                                     help='The collection(s) name or path/url to a tar.gz collection artifact. This '
                                          'is mutually exclusive with --requirements-file.')
        cinstall_parser.add_argument('-p', '--collections-path', dest='collections_path', default='./',
                                     help='The path to the directory containing your collections.')
        cinstall_parser.add_argument('-i', '--ignore-errors', dest='ignore_errors', action='store_true', default=False,
                                     help='Ignore errors during installation and continue with the next specified '
                                          'collection. This will not ignore dependency conflict errors.')
        cinstall_parser.add_argument('-r', '--requirements-file', dest='requirements',
                                     help='A file containing a list of collections to be installed.')

        cinstall_exclusive = cinstall_parser.add_mutually_exclusive_group()
        cinstall_exclusive.add_argument('-n', '--no-deps', dest='no_deps', action='store_true', default=False,
                                        help="Don't download collections listed as dependencies")
        cinstall_exclusive.add_argument('--force-with-deps', dest='force_with_deps', action='store_true', default=False,
                                        help="Force overwriting an existing collection and its dependencies")

        publish_parser = collection_parser.add_parser(
            'publish', help='Publish a collection artifact to Ansible Galaxy.',
            parents=[common])
        publish_parser.set_defaults(func=self.execute_publish)
        publish_parser.add_argument(
            'args', metavar='collection_path', help='The path to the collection tarball to publish.')
        publish_parser.add_argument(
            '--api-key', dest='api_key',
            help='The Ansible Galaxy API key which can be found at https://galaxy.ansible.com/me/preferences. '
                 'You can also use ansible-galaxy login to retrieve this key.')
        publish_parser.add_argument(
            '--no-wait', dest='wait', action='store_false', default=True,
            help="Don't wait for import validation results.")

        # Define the actions for the role object type
        role = type_parser.add_parser('role',
                                      parents=[common],
                                      help='Manage an Ansible Galaxy role.')
        role_parser = role.add_subparsers(metavar='ACTION', dest='role')
        role_parser.required = True

        delete_parser = role_parser.add_parser('delete', parents=[user_repo, common],
                                               help='Removes the role from Galaxy. It does not remove or alter the actual GitHub repository.')
        delete_parser.set_defaults(func=self.execute_delete)

        import_parser = role_parser.add_parser('import', help='Import a role', parents=[user_repo, common])
        import_parser.set_defaults(func=self.execute_import)
        import_parser.add_argument('--no-wait', dest='wait', action='store_false', default=True, help="Don't wait for import results.")
        import_parser.add_argument('--branch', dest='reference',
                                   help='The name of a branch to import. Defaults to the repository\'s default branch (usually master)')
        import_parser.add_argument('--role-name', dest='role_name', help='The name the role should have, if different than the repo name')
        import_parser.add_argument('--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.')

        info_parser = role_parser.add_parser('info', help='View more details about a specific role.',
                                             parents=[offline, common, roles_path])
        info_parser.set_defaults(func=self.execute_info)
        info_parser.add_argument('args', nargs='+', help='role', metavar='role_name[,version]')

        rinit_parser = self.add_init_parser(role_parser, [offline, force, common])
        rinit_parser.add_argument('--type',
                                  dest='role_type',
                                  action='store',
                                  default='default',
                                  help="Initialize using an alternate role type. Valid types include: 'container', 'apb' and 'network'.")

        install_parser = role_parser.add_parser('install', help='Install Roles from file(s), URL(s) or tar file(s)',
                                                parents=[force, common, roles_path])
        install_parser.set_defaults(func=self.execute_install)
        install_parser.add_argument('-i', '--ignore-errors', dest='ignore_errors', action='store_true', default=False,
                                    help='Ignore errors and continue with the next specified role.')
        install_parser.add_argument('-r', '--role-file', dest='role_file', help='A file containing a list of roles to be imported')
        install_parser.add_argument('-g', '--keep-scm-meta', dest='keep_scm_meta', action='store_true',
                                    default=False, help='Use tar instead of the scm archive option when packaging the role')
        install_parser.add_argument('args', help='Role name, URL or tar file', metavar='role', nargs='*')
        install_exclusive = install_parser.add_mutually_exclusive_group()
        install_exclusive.add_argument('-n', '--no-deps', dest='no_deps', action='store_true', default=False,
                                       help="Don't download roles listed as dependencies")
        install_exclusive.add_argument('--force-with-deps', dest='force_with_deps', action='store_true', default=False,
                                       help="Force overwriting an existing role and it's dependencies")

        remove_parser = role_parser.add_parser('remove', help='Delete roles from roles_path.', parents=[common, roles_path])
        remove_parser.set_defaults(func=self.execute_remove)
        remove_parser.add_argument('args', help='Role(s)', metavar='role', nargs='+')

        list_parser = role_parser.add_parser('list', help='Show the name and version of each role installed in the roles_path.',
                                             parents=[common, roles_path])
        list_parser.set_defaults(func=self.execute_list)
        list_parser.add_argument('role', help='Role', nargs='?', metavar='role')

        login_parser = role_parser.add_parser('login', parents=[common],
                                              help="Login to api.github.com server in order to use ansible-galaxy role "
                                                   "sub command such as 'import', 'delete', 'publish', and 'setup'")
        login_parser.set_defaults(func=self.execute_login)
        login_parser.add_argument('--github-token', dest='token', default=None,
                                  help='Identify with github token rather than username and password.')

        search_parser = role_parser.add_parser('search', help='Search the Galaxy database by tags, platforms, author and multiple keywords.',
                                               parents=[common])
        search_parser.set_defaults(func=self.execute_search)
        search_parser.add_argument('--platforms', dest='platforms', help='list of OS platforms to filter by')
        search_parser.add_argument('--galaxy-tags', dest='galaxy_tags', help='list of galaxy tags to filter by')
        search_parser.add_argument('--author', dest='author', help='GitHub username')
        search_parser.add_argument('args', help='Search terms', metavar='searchterm', nargs='*')

        setup_parser = role_parser.add_parser('setup', help='Manage the integration between Galaxy and the given source.',
                                              parents=[roles_path, common])
        setup_parser.set_defaults(func=self.execute_setup)
        setup_parser.add_argument('--remove', dest='remove_id', default=None,
                                  help='Remove the integration matching the provided ID value. Use --list to see ID values.')
        setup_parser.add_argument('--list', dest="setup_list", action='store_true', default=False, help='List all of your integrations.')
        setup_parser.add_argument('source', help='Source')
        setup_parser.add_argument('github_user', help='GitHub username')
        setup_parser.add_argument('github_repo', help='GitHub repository')
        setup_parser.add_argument('secret', help='Secret')
Example #2
0
    def init_parser(self):
        super(VaultCLI, self).init_parser(
            desc="encryption/decryption utility for Ansible data files",
            epilog="\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0])
        )

        common = opt_help.argparse.ArgumentParser(add_help=False)
        opt_help.add_vault_options(common)
        opt_help.add_verbosity_options(common)

        subparsers = self.parser.add_subparsers(dest='action')
        subparsers.required = True

        output = opt_help.argparse.ArgumentParser(add_help=False)
        output.add_argument('--output', default=None, dest='output_file',
                            help='output file name for encrypt or decrypt; use - for stdout',
                            type=opt_help.unfrack_path())

        # For encrypting actions, we can also specify which of multiple vault ids should be used for encrypting
        vault_id = opt_help.argparse.ArgumentParser(add_help=False)
        vault_id.add_argument('--encrypt-vault-id', default=[], dest='encrypt_vault_id',
                              action='store', type=str,
                              help='the vault id used to encrypt (required if more than one vault-id is provided)')

        create_parser = subparsers.add_parser('create', help='Create new vault encrypted file', parents=[vault_id, common])
        create_parser.set_defaults(func=self.execute_create)
        create_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')

        decrypt_parser = subparsers.add_parser('decrypt', help='Decrypt vault encrypted file', parents=[output, common])
        decrypt_parser.set_defaults(func=self.execute_decrypt)
        decrypt_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')

        edit_parser = subparsers.add_parser('edit', help='Edit vault encrypted file', parents=[vault_id, common])
        edit_parser.set_defaults(func=self.execute_edit)
        edit_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')

        view_parser = subparsers.add_parser('view', help='View vault encrypted file', parents=[common])
        view_parser.set_defaults(func=self.execute_view)
        view_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')

        encrypt_parser = subparsers.add_parser('encrypt', help='Encrypt YAML file', parents=[common, output, vault_id])
        encrypt_parser.set_defaults(func=self.execute_encrypt)
        encrypt_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')

        enc_str_parser = subparsers.add_parser('encrypt_string', help='Encrypt a string', parents=[common, output, vault_id])
        enc_str_parser.set_defaults(func=self.execute_encrypt_string)
        enc_str_parser.add_argument('args', help='String to encrypt', metavar='string_to_encrypt', nargs='*')
        enc_str_parser.add_argument('-p', '--prompt', dest='encrypt_string_prompt',
                                    action='store_true',
                                    help="Prompt for the string to encrypt")
        enc_str_parser.add_argument('--show-input', dest='show_string_input', default=False, action='store_true',
                                    help='Do not hide input when prompted for the string to encrypt')
        enc_str_parser.add_argument('-n', '--name', dest='encrypt_string_names',
                                    action='append',
                                    help="Specify the variable name")
        enc_str_parser.add_argument('--stdin-name', dest='encrypt_string_stdin_name',
                                    default=None,
                                    help="Specify the variable name for stdin")

        rekey_parser = subparsers.add_parser('rekey', help='Re-key a vault encrypted file', parents=[common, vault_id])
        rekey_parser.set_defaults(func=self.execute_rekey)
        rekey_new_group = rekey_parser.add_mutually_exclusive_group()
        rekey_new_group.add_argument('--new-vault-password-file', default=None, dest='new_vault_password_file',
                                     help="new vault password file for rekey", type=opt_help.unfrack_path())
        rekey_new_group.add_argument('--new-vault-id', default=None, dest='new_vault_id', type=str,
                                     help='the new vault identity to use for rekey')
        rekey_parser.add_argument('args', help='Filename', metavar='file_name', nargs='*')
Example #3
0
    def init_parser(self):
        ''' create an options parser for bin/ansible '''

        super(GalaxyCLI, self).init_parser(
            desc="Perform various Role related operations.", )

        # common
        common = opt_help.argparse.ArgumentParser(add_help=False)
        common.add_argument('-s',
                            '--server',
                            dest='api_server',
                            default=C.GALAXY_SERVER,
                            help='The API server destination')
        common.add_argument('-c',
                            '--ignore-certs',
                            action='store_true',
                            dest='ignore_certs',
                            default=C.GALAXY_IGNORE_CERTS,
                            help='Ignore SSL certificate validation errors.')
        opt_help.add_verbosity_options(common)

        # options that apply to more than one action
        user_repo = opt_help.argparse.ArgumentParser(add_help=False)
        user_repo.add_argument('github_user', help='GitHub username')
        user_repo.add_argument('github_repo', help='GitHub repository')

        offline = opt_help.argparse.ArgumentParser(add_help=False)
        offline.add_argument(
            '--offline',
            dest='offline',
            default=False,
            action='store_true',
            help="Don't query the galaxy API when creating roles")

        roles_path = opt_help.argparse.ArgumentParser(add_help=False)
        roles_path.add_argument(
            '-p',
            '--roles-path',
            dest='roles_path',
            type=opt_help.unfrack_path(pathsep=True),
            default=C.DEFAULT_ROLES_PATH,
            action=opt_help.PrependListAction,
            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)'
        )

        force = opt_help.argparse.ArgumentParser(add_help=False)
        force.add_argument('-f',
                           '--force',
                           dest='force',
                           action='store_true',
                           default=False,
                           help='Force overwriting an existing role')

        subparsers = self.parser.add_subparsers(dest='action')
        subparsers.required = True

        delete_parser = subparsers.add_parser(
            'delete',
            parents=[user_repo, common],
            help=
            'Removes the role from Galaxy. It does not remove or alter the actual GitHub repository.'
        )
        delete_parser.set_defaults(func=self.execute_delete)

        import_parser = subparsers.add_parser('import',
                                              help='Import a role',
                                              parents=[user_repo, common])
        import_parser.set_defaults(func=self.execute_import)
        import_parser.add_argument('--no-wait',
                                   dest='wait',
                                   action='store_false',
                                   default=True,
                                   help="Don't wait for import results.")
        import_parser.add_argument(
            '--branch',
            dest='reference',
            help=
            'The name of a branch to import. Defaults to the repository\'s default branch (usually master)'
        )
        import_parser.add_argument(
            '--role-name',
            dest='role_name',
            help=
            'The name the role should have, if different than the repo name')
        import_parser.add_argument(
            '--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.'
        )

        info_parser = subparsers.add_parser(
            'info',
            help='View more details about a specific role.',
            parents=[offline, common, roles_path])
        info_parser.set_defaults(func=self.execute_info)
        info_parser.add_argument('args',
                                 nargs='+',
                                 help='role',
                                 metavar='role_name[,version]')

        init_parser = subparsers.add_parser(
            'init',
            help='Initialize new role with the base structure of a role.',
            parents=[offline, force, common])
        init_parser.set_defaults(func=self.execute_init)
        init_parser.add_argument(
            '--init-path',
            dest='init_path',
            default="./",
            help=
            'The path in which the skeleton role will be created. The default is the current working directory.'
        )
        init_parser.add_argument(
            '--type',
            dest='role_type',
            action='store',
            default='default',
            help=
            "Initialize using an alternate role type. Valid types include: 'container', 'apb' and 'network'."
        )
        init_parser.add_argument(
            '--role-skeleton',
            dest='role_skeleton',
            default=C.GALAXY_ROLE_SKELETON,
            help=
            'The path to a role skeleton that the new role should be based upon.'
        )
        init_parser.add_argument('role_name', help='Role name')

        install_parser = subparsers.add_parser(
            'install',
            help='Install Roles from file(s), URL(s) or tar file(s)',
            parents=[force, common, roles_path])
        install_parser.set_defaults(func=self.execute_install)
        install_parser.add_argument(
            '-i',
            '--ignore-errors',
            dest='ignore_errors',
            action='store_true',
            default=False,
            help='Ignore errors and continue with the next specified role.')
        install_parser.add_argument(
            '-r',
            '--role-file',
            dest='role_file',
            help='A file containing a list of roles to be imported')
        install_parser.add_argument(
            '-g',
            '--keep-scm-meta',
            dest='keep_scm_meta',
            action='store_true',
            default=False,
            help=
            'Use tar instead of the scm archive option when packaging the role'
        )
        install_parser.add_argument('args',
                                    help='Role name, URL or tar file',
                                    metavar='role',
                                    nargs='*')
        install_exclusive = install_parser.add_mutually_exclusive_group()
        install_exclusive.add_argument(
            '-n',
            '--no-deps',
            dest='no_deps',
            action='store_true',
            default=False,
            help="Don't download roles listed as dependencies")
        install_exclusive.add_argument(
            '--force-with-deps',
            dest='force_with_deps',
            action='store_true',
            default=False,
            help="Force overwriting an existing role and it's dependencies")

        remove_parser = subparsers.add_parser(
            'remove',
            help='Delete roles from roles_path.',
            parents=[common, roles_path])
        remove_parser.set_defaults(func=self.execute_remove)
        remove_parser.add_argument('args',
                                   help='Role(s)',
                                   metavar='role',
                                   nargs='+')

        list_parser = subparsers.add_parser(
            'list',
            help=
            'Show the name and version of each role installed in the roles_path.',
            parents=[common, roles_path])
        list_parser.set_defaults(func=self.execute_list)
        list_parser.add_argument('role',
                                 help='Role',
                                 nargs='?',
                                 metavar='role')

        login_parser = subparsers.add_parser(
            'login',
            parents=[common],
            help=
            "Login to api.github.com server in order to use ansible-galaxy sub "
            "command such as 'import', 'delete' and 'setup'")
        login_parser.set_defaults(func=self.execute_login)
        login_parser.add_argument(
            '--github-token',
            dest='token',
            default=None,
            help='Identify with github token rather than username and password.'
        )

        search_parser = subparsers.add_parser(
            'search',
            help=
            'Search the Galaxy database by tags, platforms, author and multiple keywords.',
            parents=[common])
        search_parser.set_defaults(func=self.execute_search)
        search_parser.add_argument('--platforms',
                                   dest='platforms',
                                   help='list of OS platforms to filter by')
        search_parser.add_argument('--galaxy-tags',
                                   dest='galaxy_tags',
                                   help='list of galaxy tags to filter by')
        search_parser.add_argument('--author',
                                   dest='author',
                                   help='GitHub username')
        search_parser.add_argument('args',
                                   help='Search terms',
                                   metavar='searchterm',
                                   nargs='*')

        setup_parser = subparsers.add_parser(
            'setup',
            help='Manage the integration between Galaxy and the given source.',
            parents=[roles_path, common])
        setup_parser.set_defaults(func=self.execute_setup)
        setup_parser.add_argument(
            '--remove',
            dest='remove_id',
            default=None,
            help=
            'Remove the integration matching the provided ID value. Use --list to see ID values.'
        )
        setup_parser.add_argument('--list',
                                  dest="setup_list",
                                  action='store_true',
                                  default=False,
                                  help='List all of your integrations.')
        setup_parser.add_argument('source', help='Source')
        setup_parser.add_argument('github_user', help='GitHub username')
        setup_parser.add_argument('github_repo', help='GitHub repository')
        setup_parser.add_argument('secret', help='Secret')