Exemple #1
0
def do_performance_metric_get_value(cs, args):
    """Gets the value of hyperstash performance metric by rbd id and type."""
    if not args.rbd_id:
        raise exceptions.CommandError("you need specify a rbd_id")
    if not args.type:
        raise exceptions.CommandError("you need specify a type")
    performance_metrics = \
        cs.performance_metrics.get_value(args.rbd_id, args.type)
    columns = ["Metric", "Value", "RBD Name", "TimeStamp"]
    if args.type == "rbd_basic_info":
        columns = [
            "ID", "Name", "Size", "Objects", "Order", "Object Size",
            "Block Name Prefix", "Format", "Features", "Flags"
        ]
    utils.print_list(performance_metrics, columns)
Exemple #2
0
def do_rbd_cache_config_update(cs, args):
    """Updates a rbd cache config by id."""

    rbd_cache_config_resource = [
        'cache_dir', 'clean_start', 'enable_memory_usage_tracker',
        'object_size', 'cache_total_size', 'cache_dirty_ratio_min',
        'cache_ratio_health', 'cache_ratio_max', 'cache_flush_interval',
        'cache_evict_interval', 'cache_flush_queue_depth', 'agent_threads_num',
        'cache_service_threads_num', 'messenger_port', 'log_to_file'
    ]

    config_kwargs = {}
    for resource in rbd_cache_config_resource:
        val = getattr(args, resource, None)
        if resource == "clean_start" and val:
            val = 1
        elif resource == "clean_start" and val:
            val = 0
        if resource == "enable_memory_usage_tracker" and val:
            val = True
        elif resource == "enable_memory_usage_tracker" and not val:
            val = False
        if val:
            config_kwargs[resource] = val
        elif resource in ["enable_memory_usage_tracker", "clean_start"]:
            config_kwargs[resource] = val
    try:
        cs.rbd_cache_configs.update(args.rbd_cache_config, **config_kwargs)
        print("Succeed to update a rbd cache config.")
    except Exception:
        raise exceptions.CommandError("Fail to update a rbd cache config.")
Exemple #3
0
def do_server_activate(cs, args):
    """Activates a server as a hyperstash instance."""

    try:
        cs.servers.activate(args.server)
        print("Succeed to activate a server as a hyperstash instance.")
    except Exception:
        raise exceptions.CommandError("Fail to activate a server.")
Exemple #4
0
def do_performance_metric_get_cpu(cs, args):
    """Gets cpu information."""
    if not args.server_id:
        raise exceptions.CommandError("you need specify a server_id")
    performance_metric = cs.performance_metrics.get_cpu(args.server_id)
    if isinstance(performance_metric, dict):
        utils.print_dict(performance_metric)
    else:
        utils.print_dict(performance_metric._info)
Exemple #5
0
def find_resource(manager, name_or_id):
    """Helper for the _find_* methods."""
    # first try to get entity as integer id
    try:
        if isinstance(name_or_id, int) or name_or_id.isdigit():
            return manager.get(int(name_or_id))
    except exceptions.NotFound:
        pass

    # now try to get entity as uuid
    try:
        uuid.UUID(strutils.safe_decode(name_or_id))
        return manager.get(name_or_id)
    except (ValueError, exceptions.NotFound):
        pass

    try:
        try:
            return manager.find(human_id=name_or_id)
        except exceptions.NotFound:
            pass

        # finally try to find entity by name
        try:
            return manager.find(name=name_or_id)
        except exceptions.NotFound:
            try:
                return manager.find(display_name=name_or_id)
            except (UnicodeDecodeError, exceptions.NotFound):
                try:
                    # Volumes does not have name, but display_name
                    return manager.find(display_name=name_or_id)
                except exceptions.NotFound:
                    msg = "No %s with a name or ID of '%s' exists." % \
                        (manager.resource_class.__name__.lower(), name_or_id)
                    raise exceptions.CommandError(msg)
    except exceptions.NoUniqueMatch:
        msg = ("Multiple %s matches found for '%s', use an ID to be more"
               " specific." %
               (manager.resource_class.__name__.lower(), name_or_id))
        raise exceptions.CommandError(msg)
Exemple #6
0
 def do_help(self, args):
     """
     Display help about this program or one of its subcommands.
     """
     if args.command:
         if args.command in self.subcommands:
             self.subcommands[args.command].print_help()
         else:
             raise exc.CommandError("'%s' is not a valid subcommand" %
                                    args.command)
     else:
         self.parser.print_help()
Exemple #7
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_hsm_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        subcommand_parser = self.get_subcommand_parser(
            options.os_hsm_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_tenant_id, endpoint_type, insecure,
         service_type, service_name, hsm_service_name,
         username, apikey, projectid, url, region_name, cacert) = (
             args.os_username, args.os_password,
             args.os_tenant_name, args.os_auth_url,
             args.os_region_name, args.os_tenant_id,
             args.endpoint_type, args.insecure,
             args.service_type, args.service_name,
             args.hsm_service_name, args.username,
             args.apikey, args.projectid,
             args.url, args.region_name, args.os_cacert)

        if not endpoint_type:
            endpoint_type = DEFAULT_HSM_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_HSM_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 or os_tenant_id):
                if not projectid:
                    raise exc.CommandError("You must provide a tenant_id "
                                           "via either --os-tenant-id or "
                                           "env[OS_TENANT_ID]")
                else:
                    os_tenant_name = projectid

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

            if not os_region_name and region_name:
                os_region_name = region_name

        if not (os_tenant_name or os_tenant_id):
            raise exc.CommandError(
                "You must provide a tenant_id "
                "via either --os-tenant-id or env[OS_TENANT_ID]")

        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_hsm_api_version, os_username,
                                os_password, os_tenant_name, os_auth_url,
                                insecure, region_name=os_region_name,
                                tenant_id=os_tenant_id,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                hsm_service_name=hsm_service_name,
                                retries=options.retries,
                                http_log_debug=args.debug,
                                cacert=cacert)

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