コード例 #1
0
ファイル: shell.py プロジェクト: Blackspan/python-novaclient
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
                options.os_compute_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse
        #                thinking usage-list --end is ambiguous; but it
        #                works fine with only --endpoint-type present
        #                Go figure.
        if '--endpoint_type' in argv:
            spot = argv.index('--endpoint_type')
            argv[spot] = '--endpoint-type'

        subcommand_parser = self.get_subcommand_parser(
                options.os_compute_api_version)
        self.parser = subcommand_parser

        if options.help and len(args) == 0:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        (os_username, os_password, os_tenant_name, os_auth_url,
                os_region_name, os_auth_system, endpoint_type, insecure,
                service_type, service_name, volume_service_name,
                username, apikey, projectid, url, region_name,
                bypass_url, no_cache) = (
                        args.os_username, args.os_password,
                        args.os_tenant_name, args.os_auth_url,
                        args.os_region_name, args.os_auth_system,
                        args.endpoint_type, args.insecure, args.service_type,
                        args.service_name, args.volume_service_name,
                        args.username, args.apikey, args.projectid,
                        args.url, args.region_name,
                        args.bypass_url, args.no_cache)

        if not endpoint_type:
            endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_NOVA_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        #FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if not os_username:
                if not username:
                    raise exc.CommandError("You must provide a username "
                            "via either --os-username or env[OS_USERNAME]")
                else:
                    os_username = username

            if not os_password:
                if not apikey:
                    raise exc.CommandError("You must provide a password "
                            "via either --os-password or via "
                            "env[OS_PASSWORD]")
                else:
                    os_password = apikey

            if not os_tenant_name:
                if not projectid:
                    raise exc.CommandError("You must provide a tenant name "
                            "via either --os-tenant-name or "
                            "env[OS_TENANT_NAME]")
                else:
                    os_tenant_name = projectid

            if not os_auth_url:
                if not url:
                    if os_auth_system and os_auth_system != 'keystone':
                        os_auth_url = \
                            client.get_auth_system_url(os_auth_system)
                else:
                    os_auth_url = url

            if not os_auth_url:
                    raise exc.CommandError("You must provide an auth url "
                            "via either --os-auth-url or env[OS_AUTH_URL] "
                            "or specify an auth_system which defines a "
                            "default url with --os-auth-system "
                            "or env[OS_AUTH_SYSTEM")

            if not os_region_name and region_name:
                os_region_name = region_name

        if (options.os_compute_api_version and
                options.os_compute_api_version != '1.0'):
            if not os_tenant_name:
                raise exc.CommandError("You must provide a tenant name "
                        "via either --os-tenant-name or env[OS_TENANT_NAME]")

            if not os_auth_url:
                raise exc.CommandError("You must provide an auth url "
                        "via either --os-auth-url or env[OS_AUTH_URL]")

        self.cs = client.Client(options.os_compute_api_version, os_username,
                os_password, os_tenant_name, os_auth_url, insecure,
                region_name=os_region_name, endpoint_type=endpoint_type,
                extensions=self.extensions, service_type=service_type,
                service_name=service_name, auth_system=os_auth_system,
                volume_service_name=volume_service_name,
                timings=args.timings, bypass_url=bypass_url,
                no_cache=no_cache, http_log_debug=options.debug)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("Invalid OpenStack Nova credentials.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user")

        args.func(self.cs, args)

        if args.timings:
            self._dump_timings(self.cs.get_timings())
コード例 #2
0
ファイル: shell.py プロジェクト: AlexeyKasatkin/openstack-tut
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
            options.os_compute_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse
        #                thinking usage-list --end is ambiguous; but it
        #                works fine with only --endpoint-type present
        #                Go figure.
        if '--endpoint_type' in argv:
            spot = argv.index('--endpoint_type')
            argv[spot] = '--endpoint-type'

        subcommand_parser = self.get_subcommand_parser(
            options.os_compute_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        (os_username, os_tenant_name, os_auth_url, os_region_name,
         os_auth_system, endpoint_type, insecure, service_type, service_name,
         volume_service_name, username, projectid, url, region_name,
         bypass_url, os_cache, cacert,
         timeout) = (args.os_username, args.os_tenant_name, args.os_auth_url,
                     args.os_region_name, args.os_auth_system,
                     args.endpoint_type, args.insecure, args.service_type,
                     args.service_name, args.volume_service_name,
                     args.username, args.projectid, args.url, args.region_name,
                     args.bypass_url, args.os_cache, args.os_cacert,
                     args.timeout)

        # Fetched and set later as needed
        os_password = None

        if not endpoint_type:
            endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_NOVA_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        #FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.
        if not utils.isunauthenticated(args.func):
            if not os_username:
                if not username:
                    raise exc.CommandError(
                        "You must provide a username "
                        "via either --os-username or env[OS_USERNAME]")
                else:
                    os_username = username

            if not os_tenant_name:
                if not projectid:
                    raise exc.CommandError("You must provide a tenant name "
                                           "via either --os-tenant-name or "
                                           "env[OS_TENANT_NAME]")
                else:
                    os_tenant_name = projectid

            if not os_auth_url:
                if not url:
                    if os_auth_system and os_auth_system != 'keystone':
                        os_auth_url = \
                            client.get_auth_system_url(os_auth_system)
                else:
                    os_auth_url = url

            if not os_auth_url:
                raise exc.CommandError(
                    "You must provide an auth url "
                    "via either --os-auth-url or env[OS_AUTH_URL] "
                    "or specify an auth_system which defines a "
                    "default url with --os-auth-system "
                    "or env[OS_AUTH_SYSTEM]")

            if not os_region_name and region_name:
                os_region_name = region_name

        if (options.os_compute_api_version
                and options.os_compute_api_version != '1.0'):
            if not os_tenant_name:
                raise exc.CommandError(
                    "You must provide a tenant name "
                    "via either --os-tenant-name or env[OS_TENANT_NAME]")

            if not os_auth_url:
                raise exc.CommandError(
                    "You must provide an auth url "
                    "via either --os-auth-url or env[OS_AUTH_URL]")

        self.cs = client.Client(options.os_compute_api_version,
                                os_username,
                                os_password,
                                os_tenant_name,
                                os_auth_url,
                                insecure,
                                region_name=os_region_name,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                auth_system=os_auth_system,
                                volume_service_name=volume_service_name,
                                timings=args.timings,
                                bypass_url=bypass_url,
                                os_cache=os_cache,
                                http_log_debug=options.debug,
                                cacert=cacert,
                                timeout=timeout)

        # Now check for the password/token of which pieces of the
        # identifying keyring key can come from the underlying client
        if not utils.isunauthenticated(args.func):
            helper = SecretsHelper(args, self.cs.client)
            use_pw = True
            tenant_id, auth_token, management_url = (helper.tenant_id,
                                                     helper.auth_token,
                                                     helper.management_url)
            if tenant_id and auth_token and management_url:
                self.cs.client.tenant_id = tenant_id
                self.cs.client.auth_token = auth_token
                self.cs.client.management_url = management_url
                # Try to auth with the given info, if it fails
                # go into password mode...
                try:
                    self.cs.authenticate()
                    use_pw = False
                except (exc.Unauthorized, exc.AuthorizationFailure):
                    # Likely it expired or just didn't work...
                    self.cs.client.auth_token = None
                    self.cs.client.management_url = None
            if use_pw:
                # Auth using token must have failed or not happened
                # at all, so now switch to password mode and save
                # the token when its gotten... using our keyring
                # saver
                os_password = helper.password
                if not os_password:
                    raise exc.CommandError(
                        'Expecting a password provided via either '
                        '--os-password, env[OS_PASSWORD], or '
                        'prompted response')
                self.cs.client.password = os_password
                self.cs.client.keyring_saver = helper

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("Invalid OpenStack Nova credentials.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user")

        args.func(self.cs, args)

        if args.timings:
            self._dump_timings(self.cs.get_timings())
コード例 #3
0
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
                options.os_compute_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse
        #                thinking usage-list --end is ambiguous; but it
        #                works fine with only --endpoint-type present
        #                Go figure.
        if '--endpoint_type' in argv:
            spot = argv.index('--endpoint_type')
            argv[spot] = '--endpoint-type'

        subcommand_parser = self.get_subcommand_parser(
                options.os_compute_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        (os_username, os_tenant_name, os_auth_url,
                os_region_name, os_auth_system, endpoint_type, insecure,
                service_type, service_name, volume_service_name,
                username, projectid, url, region_name,
                bypass_url, os_cache, cacert, timeout) = (
                        args.os_username,
                        args.os_tenant_name, args.os_auth_url,
                        args.os_region_name, args.os_auth_system,
                        args.endpoint_type, args.insecure, args.service_type,
                        args.service_name, args.volume_service_name,
                        args.username, args.projectid,
                        args.url, args.region_name,
                        args.bypass_url, args.os_cache,
                        args.os_cacert, args.timeout)

        # Fetched and set later as needed
        os_password = None

        if not endpoint_type:
            endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_NOVA_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        #FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.
        if not utils.isunauthenticated(args.func):
            if not os_username:
                if not username:
                    raise exc.CommandError("You must provide a username "
                            "via either --os-username or env[OS_USERNAME]")
                else:
                    os_username = username

            if not os_tenant_name:
                if not projectid:
                    raise exc.CommandError("You must provide a tenant name "
                            "via either --os-tenant-name or "
                            "env[OS_TENANT_NAME]")
                else:
                    os_tenant_name = projectid

            if not os_auth_url:
                if not url:
                    if os_auth_system and os_auth_system != 'keystone':
                        os_auth_url = \
                            client.get_auth_system_url(os_auth_system)
                else:
                    os_auth_url = url

            if not os_auth_url:
                    raise exc.CommandError("You must provide an auth url "
                            "via either --os-auth-url or env[OS_AUTH_URL] "
                            "or specify an auth_system which defines a "
                            "default url with --os-auth-system "
                            "or env[OS_AUTH_SYSTEM")

            if not os_region_name and region_name:
                os_region_name = region_name

        if (options.os_compute_api_version and
                options.os_compute_api_version != '1.0'):
            if not os_tenant_name:
                raise exc.CommandError("You must provide a tenant name "
                        "via either --os-tenant-name or env[OS_TENANT_NAME]")

            if not os_auth_url:
                raise exc.CommandError("You must provide an auth url "
                        "via either --os-auth-url or env[OS_AUTH_URL]")

        self.cs = client.Client(options.os_compute_api_version, os_username,
                os_password, os_tenant_name, os_auth_url, insecure,
                region_name=os_region_name, endpoint_type=endpoint_type,
                extensions=self.extensions, service_type=service_type,
                service_name=service_name, auth_system=os_auth_system,
                volume_service_name=volume_service_name,
                timings=args.timings, bypass_url=bypass_url,
                os_cache=os_cache, http_log_debug=options.debug,
                cacert=cacert, timeout=timeout)

        # Now check for the password/token of which pieces of the
        # identifying keyring key can come from the underlying client
        if not utils.isunauthenticated(args.func):
            helper = SecretsHelper(args, self.cs.client)
            use_pw = True
            tenant_id, auth_token, management_url = (helper.tenant_id,
                                                     helper.auth_token,
                                                     helper.management_url)
            if tenant_id and auth_token and management_url:
                self.cs.client.tenant_id = tenant_id
                self.cs.client.auth_token = auth_token
                self.cs.client.management_url = management_url
                # Try to auth with the given info, if it fails
                # go into password mode...
                try:
                    self.cs.authenticate()
                    use_pw = False
                except (exc.Unauthorized, exc.AuthorizationFailure):
                    # Likely it expired or just didn't work...
                    self.cs.client.auth_token = None
                    self.cs.client.management_url = None
            if use_pw:
                # Auth using token must have failed or not happened
                # at all, so now switch to password mode and save
                # the token when its gotten... using our keyring
                # saver
                os_password = helper.password
                if not os_password:
                    raise exc.CommandError(
                        'Expecting a password provided via either '
                        '--os-password, env[OS_PASSWORD], or '
                        'prompted response')
                self.cs.client.password = os_password
                self.cs.client.keyring_saver = helper

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("Invalid OpenStack Nova credentials.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user")

        args.func(self.cs, args)

        if args.timings:
            self._dump_timings(self.cs.get_timings())
コード例 #4
0
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
                options.os_compute_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse
        #                thinking usage-list --end is ambiguous; but it
        #                works fine with only --endpoint-type present
        #                Go figure.
        if '--endpoint_type' in argv:
            spot = argv.index('--endpoint_type')
            argv[spot] = '--endpoint-type'

        subcommand_parser = self.get_subcommand_parser(
                options.os_compute_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        (os_username, os_password, os_tenant_name, os_auth_url,
                os_region_name, os_auth_system, endpoint_type, insecure,
                service_type, service_name, volume_service_name,
                username, apikey, projectid, url, region_name,
                bypass_url, no_cache) = (
                        args.os_username, args.os_password,
                        args.os_tenant_name, args.os_auth_url,
                        args.os_region_name, args.os_auth_system,
                        args.endpoint_type, args.insecure, args.service_type,
                        args.service_name, args.volume_service_name,
                        args.username, args.apikey, args.projectid,
                        args.url, args.region_name,
                        args.bypass_url, args.no_cache)

        if not endpoint_type:
            endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_NOVA_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        #FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if not os_username:
                if not username:
                    raise exc.CommandError("You must provide a username "
                            "via either --os-username or env[OS_USERNAME]")
                else:
                    os_username = username

            if not os_password:
                if not apikey:
                    raise exc.CommandError("You must provide a password "
                            "via either --os-password or via "
                            "env[OS_PASSWORD]")
                else:
                    os_password = apikey

            if not os_tenant_name:
                if not projectid:
                    raise exc.CommandError("You must provide a tenant name "
                            "via either --os-tenant-name or "
                            "env[OS_TENANT_NAME]")
                else:
                    os_tenant_name = projectid

            if not os_auth_url:
                if not url:
                    if os_auth_system and os_auth_system != 'keystone':
                        os_auth_url = \
                            client.get_auth_system_url(os_auth_system)
                else:
                    os_auth_url = url

            if not os_auth_url:
                    raise exc.CommandError("You must provide an auth url "
                            "via either --os-auth-url or env[OS_AUTH_URL] "
                            "or specify an auth_system which defines a "
                            "default url with --os-auth-system "
                            "or env[OS_AUTH_SYSTEM")

            if not os_region_name and region_name:
                os_region_name = region_name

        if (options.os_compute_api_version and
                options.os_compute_api_version != '1.0'):
            if not os_tenant_name:
                raise exc.CommandError("You must provide a tenant name "
                        "via either --os-tenant-name or env[OS_TENANT_NAME]")

            if not os_auth_url:
                raise exc.CommandError("You must provide an auth url "
                        "via either --os-auth-url or env[OS_AUTH_URL]")

        self.cs = client.Client(options.os_compute_api_version, os_username,
                os_password, os_tenant_name, os_auth_url, insecure,
                region_name=os_region_name, endpoint_type=endpoint_type,
                extensions=self.extensions, service_type=service_type,
                service_name=service_name, auth_system=os_auth_system,
                volume_service_name=volume_service_name,
                timings=args.timings, bypass_url=bypass_url,
                no_cache=no_cache, http_log_debug=options.debug)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("Invalid OpenStack Nova credentials.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user")

        args.func(self.cs, args)

        if args.timings:
            self._dump_timings(self.cs.get_timings())