def foo(x):
     try:
         i = int(x)
         if i not in range(_min, _max):
             raise ArgumentError(
                 "{v} not in range [{min}-{max}].".format(v=i,
                                                          min=_min,
                                                          max=_max))
         return i
     except ValueError:
         pass
     if x not in _symbols:
         raise ArgumentError("'{v}' must be one of {s}.".format(
             v=x, s=_symbols))
     return x
    def parse_opts(cls, new_args, object_name):
        modify = {}
        deletes = []
        for arg in new_args:
            is_unset = arg.startswith(cls.unsetprefix)
            value = new_args[arg]
            prop_name = arg[len(cls.unsetprefix) + 1:] if is_unset else arg
            if prop_name.startswith(
                    "drbd-") and prop_name[5:] in cls.CLASH_OPTIONS:
                prop_name = prop_name[5:]
            option = cls.drbd_options[object_name][prop_name]

            key = option['key']
            if is_unset:
                deletes.append(key)
            else:
                if DrbdOptions._is_byte_unit(option):
                    unit = SizeCalc.UNIT_KiB if option.get(
                        'unit_prefix') == 'k' else SizeCalc.UNIT_B
                    value = SizeCalc.auto_convert(value, unit)
                    if option['min'] <= value <= option['max']:
                        value = str(value)
                    else:
                        raise ArgumentError(
                            prop_name +
                            " value {v}{u} is out of range [{mi}-{ma}]".format(
                                v=value,
                                u=SizeCalc.unit_to_str(unit),
                                mi=str(option['min']) +
                                option.get('unit_prefix', ''),
                                ma=str(option['max']) +
                                option.get('unit_prefix', '')))
                modify[key] = str(value)

        return modify, deletes
Exemple #3
0
    def create(self, args):
        async_flag = vars(args)["async"]
        current_state = self._state_service.get_state()

        if args.auto_place:
            if current_state.__class__ == ResourceCreateTransactionState:
                print(
                    "Error: --auto-place not allowed in state '{state.name}'".
                    format(state=current_state))
                return ExitCode.ILLEGAL_STATE

            place_count, additional_place_count, diskless_type = self.parse_place_count_args(
                args)

            # auto-place resource
            replies = self._linstor.resource_auto_place(
                args.resource_definition_name,
                place_count,
                args.storage_pool,
                args.do_not_place_with,
                args.do_not_place_with_regex,
                replicas_on_same=self.prepare_argparse_list(
                    args.replicas_on_same,
                    linstor.consts.NAMESPC_AUXILIARY + '/'),
                replicas_on_different=self.prepare_argparse_list(
                    args.replicas_on_different,
                    linstor.consts.NAMESPC_AUXILIARY + '/'),
                diskless_on_remaining=self.parse_diskless_on_remaining(args),
                async_msg=async_flag,
                layer_list=args.layer_list,
                provider_list=args.providers,
                additional_place_count=additional_place_count,
                diskless_type=diskless_type)

            return self.handle_replies(args, replies)

        else:
            # normal create resource
            # check that node is given
            if not args.node_name:
                raise ArgumentError(
                    "resource create: too few arguments: Node name missing.")

            rscs = [
                linstor.ResourceData(
                    node_name, args.resource_definition_name, args.diskless,
                    args.storage_pool[0] if args.storage_pool else None,
                    args.node_id, args.layer_list, args.drbd_diskless,
                    args.nvme_initiator, not args.inactive)
                for node_name in args.node_name
            ]

            if current_state.__class__ == ResourceCreateTransactionState:
                print("{} resource(s) added to transaction".format(len(rscs)))
                current_state.rscs.extend(rscs)
                return ExitCode.OK
            else:
                replies = self._linstor.resource_create(rscs, async_flag)
                return self.handle_replies(args, replies)
 def create(self, args):
     # construct correct driver name
     driver = 'LvmThin' if args.driver == 'lvmthin' else args.driver.title()
     try:
         replies = self._linstor.storage_pool_create(args.node_name, args.name, driver, args.driver_pool_name)
     except linstor.LinstorError as e:
         raise ArgumentError(e.message)
     return self.handle_replies(args, replies)
 def create(self, args):
     try:
         shrd_space = None if "shared_space" not in args else args.shared_space
         replies = self.get_linstorapi().storage_pool_create(
             args.node_name,
             args.name,
             args.driver,
             args.driver_pool_name,
             shared_space=shrd_space)
     except linstor.LinstorError as e:
         raise ArgumentError(e.message)
     return self.handle_replies(args, replies)
 def create(self, args):
     if not args.name and not args.external_name:
         raise ArgumentError(
             "ArgumentError: At least resource name or external name has to be specified."
         )
     replies = self._linstor.resource_dfn_create(
         args.name,
         args.port,
         external_name=args.external_name
         if not isinstance(args.external_name, bytes) else
         args.external_name.decode('utf-8'),  # py2-3
         layer_list=args.layer_list,
         resource_group=args.resource_group)
     return self.handle_replies(args, replies)
 def create_swordfish(self, args):
     prefix_key = linstor.consts.NAMESPC_STORAGE_DRIVER + '/'
     properties = {
         prefix_key + linstor.consts.KEY_STOR_POOL_SF_STOR_POOL:
         args.swordfish_storage_pool
     }
     try:
         replies = self.get_linstorapi().storage_pool_create(
             args.node_name,
             args.name,
             args.driver,
             None,
             shared_space=args.shared_space,
             property_dict=properties)
     except linstor.LinstorError as e:
         raise ArgumentError(e.message)
     return self.handle_replies(args, replies)
Exemple #8
0
 def create_exos(self, args):
     try:
         # no shared-space and no external locking. shared-space calculated by server, external locking not allowed
         props = {
             apiconsts.NAMESPC_EXOS + '/' + apiconsts.KEY_STOR_POOL_EXOS_ENCLOSURE:
             args.enclosure_name,
             apiconsts.NAMESPC_EXOS + '/' + apiconsts.KEY_STOR_POOL_EXOS_POOL_SN:
             args.pool_sn
         }
         replies = self.get_linstorapi().storage_pool_create(
             args.node_name,
             args.name,
             args.driver,
             args.enclosure_name + '_' + args.pool_sn,
             property_dict=props)
     except linstor.LinstorError as e:
         raise ArgumentError(e.message)
     return self.handle_replies(args, replies)
Exemple #9
0
    def create(self, args):
        all_replies = []
        if args.auto_place:
            # auto-place resource
            all_replies = self._linstor.resource_auto_place(
                args.resource_definition_name,
                args.auto_place,
                args.storage_pool,
                args.do_not_place_with,
                args.do_not_place_with_regex, [
                    linstor.consts.NAMESPC_AUXILIARY + '/' + x
                    for x in args.replicas_on_same
                ], [
                    linstor.consts.NAMESPC_AUXILIARY + '/' + x
                    for x in args.replicas_on_different
                ],
                diskless_on_remaining=args.diskless_on_remaining)

            if not self._linstor.all_api_responses_success(all_replies):
                return self.handle_replies(args, all_replies)

            if not args. async:

                def event_handler(event_header, event_data):
                    if event_header.event_name == apiconsts.EVENT_RESOURCE_DEFINITION_READY:
                        if event_header.event_action == apiconsts.EVENT_STREAM_CLOSE_REMOVED:
                            print((Output.color_str('ERROR:', Color.RED,
                                                    args.no_color)) +
                                  " Resource removed")
                            return ExitCode.API_ERROR

                        if event_data is not None:
                            if event_data.error_count > 0:
                                return ExitCode.API_ERROR

                            if event_data.ready_count == args.auto_place:
                                return ExitCode.OK

                    return None

                watch_result = self._linstor.watch_events(
                    self._linstor.return_if_failure, event_handler,
                    linstor.ObjectIdentifier(
                        resource_name=args.resource_definition_name))

                if isinstance(watch_result, list):
                    all_replies += watch_result
                    if not self._linstor.all_api_responses_success(
                            watch_result):
                        return self.handle_replies(args, all_replies)
                elif watch_result != ExitCode.OK:
                    return watch_result

        else:
            # normal create resource
            # check that node is given
            if not args.node_name:
                raise ArgumentError(
                    "resource create: too few arguments: Node name missing.")

            for node_name in args.node_name:
                all_replies += self._linstor.resource_create(
                    node_name, args.resource_definition_name, args.diskless,
                    args.storage_pool)

                if not self._linstor.all_api_responses_success(all_replies):
                    return self.handle_replies(args, all_replies)

            def event_handler(event_header, event_data):
                if event_header.node_name == node_name:
                    if event_header.event_name in [
                            apiconsts.EVENT_RESOURCE_STATE,
                            apiconsts.EVENT_RESOURCE_DEPLOYMENT_STATE
                    ]:
                        if event_header.event_action == apiconsts.EVENT_STREAM_CLOSE_NO_CONNECTION:
                            print(
                                Output.color_str('WARNING:', Color.YELLOW,
                                                 args.no_color) +
                                " Satellite connection lost")
                            return ExitCode.NO_SATELLITE_CONNECTION
                        if event_header.event_action == apiconsts.EVENT_STREAM_CLOSE_REMOVED:
                            print((Output.color_str('ERROR:', Color.RED,
                                                    args.no_color)) +
                                  " Resource removed")
                            return ExitCode.API_ERROR

                    if event_header.event_name == apiconsts.EVENT_RESOURCE_STATE and \
                            event_data is not None and event_data.ready:
                        return ExitCode.OK

                    return self.check_failure_events(event_header.event_name,
                                                     event_data)

                return None

            if not ResourceCommands._satellite_not_connected(
                    all_replies) and not args. async:
                for node_name in args.node_name:

                    watch_result = self._linstor.watch_events(
                        self._linstor.return_if_failure, event_handler,
                        linstor.ObjectIdentifier(
                            node_name=node_name,
                            resource_name=args.resource_definition_name))

                    if isinstance(watch_result, list):
                        all_replies += watch_result
                        if not self._linstor.all_api_responses_success(
                                watch_result):
                            return self.handle_replies(args, all_replies)
                    elif watch_result != ExitCode.OK:
                        return watch_result

        return self.handle_replies(args, all_replies)