def create_power_control(power_config: Configuration, console: ConsoleBase) -> PowerBase: if not power_config: power_config = Configuration() POWER_SOFT = 'soft' POWER_IPPOWER9258 = 'ippower9258' POWER_UHUBCTL = 'uhubctl' POWER_LIST = [POWER_SOFT, POWER_IPPOWER9258, POWER_UHUBCTL] if len(power_config) > 1: raise TargetConfigError( 'Only one power control should be provided in the target configuration,' f' but two or more provided:{os.linesep}{power_config}') if power_config: control_type = power_config.first() elif console: control_type = POWER_SOFT else: return None if control_type not in POWER_LIST: raise TargetConfigError( f'Unsupported power control type "{control_type}". Supported types: {POWER_LIST}') power_config = power_config.pop(control_type) or Configuration() power = None if control_type == POWER_SOFT: if not console: raise TargetConfigError( 'No console available for soft power control') power = SoftPower(console) elif control_type == POWER_IPPOWER9258: host = power_config.pop('host') outlet = power_config.pop('outlet') if not host or not outlet: raise TargetConfigError( 'IP Power PDU "host" and/or "outlet" attributes are missing') port = power_config.pop('port') login = power_config.pop('login') password = power_config.pop('password') power = IPPowerPDU(outlet, host, netport=port, username=login, password=password) elif control_type == POWER_UHUBCTL: power = Uhubctl(location=power_config.pop('location'), port=power_config.pop('port')) else: raise Exception('Unreachable, unknown power controller') power_config.ensure_consumed() return power
def create_consoles(config: Optional[Configuration], system: SystemContext) -> Dict[str, ConsoleBase]: if not config: return {} consoles = {} serial = TargetFactory.create_serial( config.pop_optional(Configuration, 'serial'), system) if serial: consoles['serial'] = serial ssh = TargetFactory.create_ssh( config.pop_optional(Configuration, 'ssh'), system) if ssh: consoles['ssh'] = ssh while (config): console_name, console_dict = config.popitem() console_config = Configuration(console_dict) console_type = console_config.pop(str, 'type') if console_type == 'ssh': console = TargetFactory.create_ssh(console_config, system) elif console_type == 'serial': console = TargetFactory.create_serial(console_config, system) else: raise TargetConfigError( f'Unknown console type {console_type}. ' 'Console type must be "ssh" or "serial"') consoles[console_name] = console config.ensure_consumed() return consoles
def create_context(config: Configuration) -> PlumaContext: if not isinstance(config, Configuration): raise ValueError('Invalid configuration: The configuration passed ' 'should be a Configuration instance.') try: context = TargetConfig._create_context(config) except ConfigurationError as e: raise TargetConfigError(e) else: TargetConfig.print_context_settings(context) config.ensure_consumed() return context
def create_serial(serial_config: Configuration, system: SystemContext) -> ConsoleBase: if not serial_config: return None log.debug(f'Serial config = {serial_config}') port = serial_config.pop('port') if not port: raise TargetConfigError( 'Missing "port" attributes for serial console in the configuration file') serial = SerialConsole(port=port, baud=int(serial_config.pop('baud') or 115200), system=system) serial_config.ensure_consumed() return serial
def create_ssh(ssh_config: Configuration, system: SystemContext) -> ConsoleBase: if not ssh_config: return None log.debug('SSH config = ' + json.dumps(ssh_config.content())) target = ssh_config.pop('target') login = ssh_config.pop('login', system.credentials.login) if not target or not login: raise TargetConfigError( 'Missing "target" or "login" attributes for SSH console in the configuration file') password = ssh_config.pop('password', system.credentials.password) ssh_config.ensure_consumed() # Create a new system config to override default credentials ssh_system = deepcopy(system) ssh_system.credentials.login = login ssh_system.credentials.password = password return SSHConsole(target, system=ssh_system)
def create_power_control( power_config: Optional[Configuration], console: Optional[ConsoleBase]) -> Optional[PowerBase]: if not power_config: power_config = Configuration() POWER_SOFT = 'soft' POWER_IPPOWER9258 = 'ippower9258' POWER_UHUBCTL = 'uhubctl' POWER_LIST = [POWER_SOFT, POWER_IPPOWER9258, POWER_UHUBCTL] if len(power_config) > 1: raise TargetConfigError( 'Only one power control should be provided in the target configuration,' f' but two or more provided:{os.linesep}{power_config}') if power_config: control_type = power_config.first() elif console: control_type = POWER_SOFT else: return None if control_type not in POWER_LIST: raise TargetConfigError( f'Unsupported power control type "{control_type}". ' f'Supported types: {POWER_LIST}') power_config = power_config.pop_optional(Configuration, control_type, default=Configuration(), context='power control') reboot_delay = power_config.pop_optional(int, 'reboot_delay', context='power control') power: PowerBase if control_type == POWER_SOFT: if not console: raise TargetConfigError( 'No console available for soft power control') on_cmd = power_config.pop_optional(str, 'on_cmd', context='Soft power') off_cmd = power_config.pop_optional(str, 'off_cmd', context='Soft power') reboot_cmd = power_config.pop_optional(str, 'reboot_cmd', context='Soft power') power = SoftPower(console, on_cmd=on_cmd, off_cmd=off_cmd, reboot_cmd=reboot_cmd, reboot_delay=reboot_delay) elif control_type == POWER_IPPOWER9258: host = power_config.pop(str, 'host', context='IP Power PDU') outlet = power_config.pop(int, 'outlet', context='IP Power PDU') port = power_config.pop_optional(int, 'port', context='IP Power PDU') login = power_config.pop_optional(str, 'login', context='IP Power PDU') password = power_config.pop_optional(str, 'password', context='IP Power PDU') power = IPPowerPDU(port=outlet, host=host, netport=port, username=login, password=password, reboot_delay=reboot_delay) elif control_type == POWER_UHUBCTL: power = Uhubctl(location=power_config.pop(str, 'location', context='UHubCtl'), port=power_config.pop(int, 'port', context='UHubCtl'), reboot_delay=reboot_delay) else: raise Exception('Unreachable, unknown power controller') power_config.ensure_consumed() return power