def _parse_args(args_list, watcher_plugin_class, health_plugin_class): """ Parse command line arguments and return relevant values in a dict. Also perform basic sanity checking on some arguments. If plugin classes have been provided then a callback into those classes is used to extend the arguments with plugin-specific options. Likewise, the sanity checking will then also invoke a callback into the plugins, in order to perform a sanity check on the plugin options. """ conf = {} # Setting up the command line argument parser. Note that we pass the # complete list of all plugins, so that their parameter can be added to the # official parameter handling, the help screen, etc. Some plugins may even # add further plugins themselves, but will handle this themselves. parser, arglist = _setup_arg_parser(args_list, watcher_plugin_class, health_plugin_class) args = parser.parse_args(args_list) # Transcribe argument values into our own dict for argname in arglist: conf[argname] = getattr(args, argname) # Sanity checking of arguments. Let the watcher and health-monitor plugin # class check their own arguments. for plugin_class in [watcher_plugin_class, health_plugin_class]: if plugin_class: try: plugin_class.check_arguments(conf) except ArgsError as e: parser.print_help() raise e # Sanity checking of other args if conf['route_recheck_interval'] < 5 and \ conf['route_recheck_interval'] != 0: raise ArgsError("route_recheck_interval argument must be either 0 " "or at least 5") if not 0 < conf['port'] < 65535: raise ArgsError("Invalid listen port '%d' for built-in http server." % conf['port']) if not conf['addr'] == "localhost": # Check if a proper address was specified (already raises a suitable # ArgsError if not) utils.ip_check(conf['addr']) # Store a reference to the config dict in the current state CURRENT_STATE.conf = conf return conf
def check_arguments(cls, conf): """ Callback to perform sanity checking for the plugin's specific parameters. """ # Perform sanity checking on CIDR utils.ip_check(conf['fixed_cidr'], netmask_expected=True) # Perform sanity checking on host list for host in conf['fixed_hosts'].split(":"): utils.ip_check(host)
def test_correct(self): # # Specific test for the ip_check function, using correct values. # ip_check("192.168.1.2") ip_check("192.168.0.0/16", netmask_expected=True) ip_check("192.168.0.0/1", netmask_expected=True) ip_check("192.168.0.0/32", netmask_expected=True)
def parse_route_spec_config(data): """ Parse and sanity check the route spec config. The config data is a blob of JSON that needs to be in this format: { "<CIDR-1>" : [ "host-1-ip", "host-2-ip", "host-3-ip" ], "<CIDR-2>" : [ "host-4-ip", "host-5-ip" ], "<CIDR-3>" : [ "host-6-ip", "host-7-ip", "host-8-ip", "host-9-ip" ] } Returns the validated route config. This validation is performed on any route-spec pushed out by the config watcher plugin. Duplicate hosts in the host lists are removed. Raises ValueError exception in case of problems. """ # Sanity checking on the data object if type(data) is not dict: raise ValueError("Expected dictionary at top level") try: for k, v in data.items(): utils.ip_check(k, netmask_expected=True) if type(v) is not list: raise ValueError("Expect list of IPs as values in dict") hosts = set(v) # remove duplicates for ip in hosts: utils.ip_check(ip) clean_host_list = sorted(list(hosts)) data[k] = clean_host_list except ArgsError as e: raise ValueError(e.message) return data