def prepare_options_plain(cib, options, ticket, resource_id): options = options.copy() report = _validate_options_common(options) if not ticket: report.append(reports.required_options_are_missing(['ticket'])) options["ticket"] = ticket if not resource_id: report.append(reports.required_options_are_missing(['rsc'])) options["rsc"] = resource_id if "rsc-role" in options: if options["rsc-role"]: resource_role = options["rsc-role"].lower().capitalize() if resource_role not in ATTRIB_PLAIN["rsc-role"]: report.append( reports.invalid_option_value("rsc-role", options["rsc-role"], ATTRIB_PLAIN["rsc-role"])) options["rsc-role"] = resource_role else: del options["rsc-role"] if report: raise LibraryError(*report) return constraint.prepare_options( tuple(list(ATTRIB) + list(ATTRIB_PLAIN)), options, partial(_create_id, cib, options["ticket"], resource_id, options.get("rsc-role", "")), partial(tools.check_new_id_applicable, cib, DESCRIPTION))
def set_message(lib_env, device, node_name, message): """ Set message on device for node_name. lib_env -- LibrayEnvironment device -- string, absolute path to device node_name -- string message -- string, mesage type, should be one of settings.sbd_message_types """ report_item_list = [] missing_options = [] if not device: missing_options.append("device") if not node_name: missing_options.append("node") if missing_options: report_item_list.append( reports.required_options_are_missing(missing_options)) supported_messages = settings.sbd_message_types if message not in supported_messages: report_item_list.append( reports.invalid_option_value("message", message, supported_messages)) if lib_env.report_processor.report_list(report_item_list).has_errors: raise LibraryError() sbd.set_message(lib_env.cmd_runner(), device, node_name, message)
def initialize_block_devices(lib_env, device_list, option_dict): """ Initialize SBD devices in device_list with options_dict. lib_env -- LibraryEnvironment device_list -- list of strings option_dict -- dictionary """ report_item_list = [] if not device_list: report_item_list.append( reports.required_options_are_missing(["device"])) supported_options = sbd.DEVICE_INITIALIZATION_OPTIONS_MAPPING.keys() report_item_list += ( validate.NamesIn(supported_options).validate(option_dict)) report_item_list += validate.ValidatorAll([ validate.ValueNonnegativeInteger(key) for key in supported_options ]).validate(option_dict) if lib_env.report_processor.report_list(report_item_list).has_errors: raise LibraryError() sbd.initialize_block_devices(lib_env.report_processor, lib_env.cmd_runner(), device_list, option_dict)
def _validate_devices(resources_el, devices, force_device=False, allow_force=True) -> ReportItemList: report_list: ReportItemList = [] if not devices: report_list.append( reports.required_options_are_missing(["stonith devices"])) invalid_devices = [] for dev in devices: validate_id_report_list: ReportItemList = [] validate_id(dev, description="device id", reporter=validate_id_report_list) report_list.extend(validate_id_report_list) if has_errors(validate_id_report_list): continue # TODO use the new finding function if not is_stonith_resource(resources_el, dev): invalid_devices.append(dev) if invalid_devices: report_list.append( reports.stonith_resources_do_not_exist( invalid_devices, ReportItemSeverity.WARNING if force_device and allow_force else ReportItemSeverity.ERROR, None if force_device or not allow_force else report_codes.FORCE_STONITH_RESOURCE_DOES_NOT_EXIST)) return report_list
def create_alert( lib_env, alert_id, path, instance_attribute_dict, meta_attribute_dict, description=None ): """ Create new alert. Raises LibraryError if path is not specified, or any other failure. lib_env -- LibraryEnvironment alert_id -- id of alert to be created, if None it will be generated path -- path to script for alert instance_attribute_dict -- dictionary of instance attributes meta_attribute_dict -- dictionary of meta attributes description -- alert description description """ if not path: raise LibraryError(reports.required_options_are_missing(["path"])) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) id_provider = IdProvider(cib) alert_el = alert.create_alert(cib, alert_id, path, description) arrange_first_instance_attributes( alert_el, instance_attribute_dict, id_provider ) arrange_first_meta_attributes( alert_el, meta_attribute_dict, id_provider ) lib_env.push_cib()
def validate_parameters_update(self, current_parameters, new_parameters, force=False): # This is just a basic validation checking that required parameters are # set and all set parameters are known to an agent. Missing checks are: # 1. values checks - if a param is an integer, then "abc" is not valid # 2. warnings should be emitted when a deprecated param is set # 3. errors should be emitted when a deprecated parameter and a # parameter obsoleting it are set at the same time # 4. possibly some other checks # All of these have been missing in pcs since ever (ad 1. agents have # never provided enough info for us to do such validations, ad 2. and # 3. there were no deprecated parameters before). The checks should be # implemented in agents themselves, so I'm not adding them now either. report_items = [] # get resulting set of agent's parameters final_parameters = dict(current_parameters) for name, value in new_parameters.items(): if value: final_parameters[name] = value else: if name in final_parameters: del final_parameters[name] # report unknown parameters report_items.extend( validate.NamesIn( {param["name"] for param in self.get_parameters()}, option_type=self._agent_type_label, **validate.set_warning(report_codes.FORCE_OPTIONS, force)). validate( # Do not report unknown parameters already set in the CIB. They # have been reported already when the were added to the CIB. { name: value for name, value in new_parameters.items() if name not in current_parameters })) # report missing or removed required parameters missing_parameters = self._find_missing_required_parameters( final_parameters) if missing_parameters: forcible, severity = self._validate_report_forcible_severity(force) report_items.append( reports.required_options_are_missing( sorted(missing_parameters), self._agent_type_label, severity=severity, forceable=forcible, )) return report_items
def validate(self, option_dict): missing = set(self._option_name_list) - set(option_dict.keys()) if missing: return [ reports.required_options_are_missing( missing, self._option_type, ) ] return []
def validate_parameters_create( self, parameters, force=False, # TODO remove this argument, see pcs.lib.cib.commands.remote_node.create # for details do_not_report_instance_attribute_server_exists=False): # This is just a basic validation checking that required parameters are # set and all set parameters are known to an agent. Missing checks are: # 1. values checks - if a param is an integer, then "abc" is not valid # 2. warnings should be emitted when a deprecated param is set # 3. errors should be emitted when a deprecated parameter and a # parameter obsoleting it are set at the same time # 4. possibly some other checks # All of these have been missing in pcs since ever (ad 1. agents have # never provided enough info for us to do such validations, ad 2. and # 3. there were no deprecated parameters before). The checks should be # implemented in agents themselves, so I'm not adding them now either. report_items = [] # report unknown parameters report_items.extend( validate.NamesIn( {param["name"] for param in self.get_parameters()}, option_type=self._agent_type_label, **validate.set_warning(report_codes.FORCE_OPTIONS, force)).validate(parameters)) # TODO remove this "if", see pcs.lib.cib.commands.remote_node.create # for details if do_not_report_instance_attribute_server_exists: for report in report_items: if report.code == report_codes.INVALID_OPTIONS: report.info["allowed"] = [ value for value in report.info["allowed"] if value != "server" ] # report missing required parameters missing_parameters = self._find_missing_required_parameters(parameters) if missing_parameters: forcible, severity = self._validate_report_forcible_severity(force) report_items.append( reports.required_options_are_missing( sorted(missing_parameters), self._agent_type_label, severity=severity, forceable=forcible, )) return report_items
def prepare_options_with_set(cib, options, resource_set_list): options = constraint.prepare_options( tuple(ATTRIB.keys()), options, create_id_fn=partial(constraint.create_id, cib, TAG_NAME, resource_set_list), validate_id=partial(tools.check_new_id_applicable, cib, DESCRIPTION), ) report = _validate_options_common(options) if "ticket" not in options or not options["ticket"].strip(): report.append(reports.required_options_are_missing(['ticket'])) if report: raise LibraryError(*report) return options
def add_recipient( lib_env, alert_id, recipient_value, instance_attribute_dict, meta_attribute_dict, recipient_id=None, description=None, allow_same_value=False ): """ Add new recipient to alert witch id alert_id. lib_env -- LibraryEnvironment alert_id -- id of alert to which new recipient should be added recipient_value -- value of new recipient instance_attribute_dict -- dictionary of instance attributes to update meta_attribute_dict -- dictionary of meta attributes to update recipient_id -- id of new recipient, if None it will be generated description -- recipient description allow_same_value -- if True unique recipient value is not required """ if not recipient_value: raise LibraryError( reports.required_options_are_missing(["value"]) ) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) id_provider = IdProvider(cib) recipient = alert.add_recipient( lib_env.report_processor, cib, alert_id, recipient_value, recipient_id=recipient_id, description=description, allow_same_value=allow_same_value ) arrange_first_instance_attributes( recipient, instance_attribute_dict, id_provider ) arrange_first_meta_attributes( recipient, meta_attribute_dict, id_provider ) lib_env.push_cib()