def __init__(self, client_key=None, client_cert=None, ca_cert=None, server_addr=None): self.client = FaucetConfRpcClient(client_key=client_key, client_cert=client_cert, ca_cert=ca_cert, server_addr=server_addr)
class FaucetRemoteConfGetSetter(FaucetConfGetSetter): def __init__(self, client_key=None, client_cert=None, ca_cert=None, server_addr=None): self.client = FaucetConfRpcClient(client_key=client_key, client_cert=client_cert, ca_cert=ca_cert, server_addr=server_addr) @staticmethod def config_file_path(config_file): if config_file: return os.path.basename(config_file) return config_file def read_faucet_conf(self, config_file): self.faucet_conf = self.client.get_config_file( config_filename=self.config_file_path(config_file)) if self.faucet_conf is None: logging.error('Faucet config is empty, exiting.') sys.exit(1) return self.faucet_conf def write_faucet_conf(self, config_file=None, faucet_conf=None, merge=False): if not config_file: config_file = self.DEFAULT_CONFIG_FILE if faucet_conf is None: faucet_conf = self.faucet_conf return self.client.set_config_file( self.faucet_conf, config_filename=self.config_file_path(config_file), merge=merge) def get_dps(self): self.read_faucet_conf(config_file=None) return self.faucet_conf.get('dps', {}) def set_port_conf(self, dp, port, port_conf): return self.client.set_dp_interfaces([(dp, { port: yaml.dump(port_conf) })]) def update_switch_conf(self, dp, switch_conf): return self.write_faucet_conf(faucet_conf={'dps': { dp: switch_conf }}, merge=True)
def main(): """Instantiate client and call it.""" yaml_sanity() parser = argparse.ArgumentParser() parser.add_argument('--key', help='client private key', action='store', default='client.key') parser.add_argument('--cert', help='client public cert', action='store', default='client.crt') parser.add_argument('--cacert', help='CA public cert', action='store', default='ca.crt') parser.add_argument('--port', help='port of rpc server', action='store', default=59999, type=int) parser.add_argument('--host', help='host address of rpc server', default='localhost') parser.add_argument('commands', type=str, nargs='+', help='rpc commands') args = parser.parse_args() server_addr = f'{args.host}:{args.port}' client = FaucetConfRpcClient(args.key, args.cert, args.cacert, server_addr) if args.commands[0] == 'list_rpcs': attributes = get_attributes(client) for attribute in attributes: if not attribute[0].startswith('_'): print(attribute[0]) return command = getattr(client, args.commands[0], None) if not command: raise ClientError(f'no such rpc: {args.commands[0]}') command_args = {} for args in args.commands[1:]: arg, val = args.split('=') if val.lower() == 'true': val = True elif val.lower() == 'false': val = False command_args[arg] = val print(command(**command_args))
def setUpClass(cls): cls.tmpdir = tempfile.mkdtemp() cls.default_test_yaml = yaml_load(cls.default_test_yaml_str) cls.default_config = 'test.yaml' with open(os.path.join(cls.tmpdir, cls.default_config), 'w') as test_yaml_file: test_yaml_file.write(cls.default_test_yaml_str) # pytype: disable=wrong-arg-types cls.host = 'localhost' cls.port = 59999 cls.prom_port = 59998 certstrap = shutil.which('certstrap') cls.assertTrue(certstrap, 'certstrap not found') for cmd in (['init', '--common-name', 'ca', '--passphrase', ''], [ 'request-cert', '--common-name', 'client', '--passphrase', '' ], ['sign', 'client', '--CA', 'ca' ], ['request-cert', '--common-name', cls.host, '--passphrase', ''], ['sign', cls.host, '--CA', 'ca']): subprocess.check_call([certstrap, '--depot-path', cls.tmpdir] + cmd) client_key = os.path.join(cls.tmpdir, 'client.key') client_cert = os.path.join(cls.tmpdir, 'client.crt') server_key = os.path.join(cls.tmpdir, '%s.key' % cls.host) server_cert = os.path.join(cls.tmpdir, '%s.crt' % cls.host) ca_cert = os.path.join(cls.tmpdir, 'ca.crt') cls.server = subprocess.Popen([ 'timeout', '10s', './faucetconfrpc/faucetconfrpc_server.py', '--config_dir=%s' % cls.tmpdir, '--default_config=%s' % cls.default_config, '--port=%u' % cls.port, '--prom_port=%u' % cls.prom_port, '--host=%s' % cls.host, '--key=%s' % server_key, '--cert=%s' % server_cert, '--cacert=%s' % ca_cert, ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) cls._wait_for_port(cls.host, cls.port) cls._wait_for_port(cls.host, cls.prom_port) server_addr = '%s:%u' % (cls.host, cls.port) cls.client = FaucetConfRpcClient(client_key, client_cert, ca_cert, server_addr)
class FaucetRemoteConfGetSetter: DEFAULT_CONFIG_FILE = '' def __init__(self, client_key=None, client_cert=None, ca_cert=None, server_addr=None): self.client = FaucetConfRpcClient(client_key=client_key, client_cert=client_cert, ca_cert=ca_cert, server_addr=server_addr) @staticmethod def config_file_path(config_file): if config_file: return os.path.basename(config_file) return config_file def read_faucet_conf(self, config_file): self.faucet_conf = self.client.get_config_file( config_filename=self.config_file_path(config_file)) if self.faucet_conf is None: logging.error('Faucet config is empty, exiting.') sys.exit(1) return self.faucet_conf def write_faucet_conf(self, config_file=None, faucet_conf=None, merge=False): if not config_file: config_file = self.DEFAULT_CONFIG_FILE if faucet_conf is None: faucet_conf = self.faucet_conf return self.client.set_config_file( self.faucet_conf, config_filename=self.config_file_path(config_file), merge=merge) def get_dps(self): self.read_faucet_conf(config_file=None) return self.faucet_conf.get('dps', {}) def set_acls(self, acls): self.read_faucet_conf(config_file=None) self.faucet_conf['acls'] = acls self.write_faucet_conf(config_file=None) def get_port_conf(self, dp, port): switch_conf = self.get_switch_conf(dp) if not switch_conf: return None return switch_conf['interfaces'].get(port, None) def get_switch_conf(self, dp): return self.get_dps().get(dp, None) def get_stack_root_switch(self): root_stack_switch = [ switch for switch, switch_conf in self.get_dps().items() if switch_conf.get('stack', {}).get('priority', None) ] if root_stack_switch: return root_stack_switch[0] return None def set_port_conf(self, dp, port, port_conf): return self.client.set_dp_interfaces([(dp, { port: yaml.dump(port_conf) })]) def update_switch_conf(self, dp, switch_conf): return self.write_faucet_conf(faucet_conf={'dps': { dp: switch_conf }}, merge=True) def mirror_port(self, dp, mirror_port, port): # pragma: no cover self.client.add_port_mirror(dp, port, mirror_port) def unmirror_port(self, dp, mirror_port, port): # pragma: no cover self.client.remove_port_mirror(dp, port, mirror_port) def clear_mirror_port(self, dp, mirror_port): # pragma: no cover self.client.clear_port_mirror(dp, mirror_port)
def get_faucetconfrpc(key, cert, ca, server, port): # TODO: FaucetConfRpcClient's use of the C YAML parser causes pytest-cov to fail to report any coverage. from faucetconfrpc.faucetconfrpc_client_lib import FaucetConfRpcClient return FaucetConfRpcClient(key, cert, ca, ':'.join((server, port)))