"""/components: the installable components of the server, which the Manager's System tab switches.""" import linuxmanager import linuxmanager_net from .common import settings_change, unit_state def router_choices(cfg): """The interfaces the router may take as its internal one: the NICs but the external interface.""" external = cfg['interfaces']['external'].strip() if not external and (cfg['general']['mode'] or 'manual') == 'auto': try: external, _ = linuxmanager_net.detect_interfaces(internal=False) except linuxmanager_net.ConfigError: external = '' return [name for name in linuxmanager_net.nics() if name != external] def listing(cfg): """Each component of linuxmanager.COMPONENTS: {name, enabled, state}, state being installing or failed while its installation unit is, else installed or not installed; the router also has available, whether the server has an interface for its internal side or the router is on, the interfaces to choose from, general.mode and its current internal_interface, int_addr and int_gateway_ip.""" found = [] for name, (_, packages) in linuxmanager.COMPONENTS.items(): unit = unit_state(f'linux-manager-install@{name}.service') state = {'activating': 'installing', 'active': 'installing', 'failed': 'failed'}.get(unit) or \ ('installed' if linuxmanager.packages_installed(packages) else 'not installed') entry = {'name': name, 'enabled': linuxmanager.component_on(cfg, name), 'state': state} if name == 'router': choices = router_choices(cfg) entry.update({'available': entry['enabled'] or bool(choices), 'interfaces': choices, 'mode': cfg['general']['mode'] or 'manual', 'internal_interface': cfg['interfaces']['internal'].strip(), 'int_addr': cfg['local']['int_addr'].strip(), 'int_gateway_ip': cfg['local']['int_gateway_ip'].strip()}) found.append(entry) return found def components(method, path, body): """Answer a /components request: the HTTP status and the JSON document. GET /components {"components": [...]} (listing) PUT /components/ switch a component: {"enabled"}, for the router switched on also "internal_interface", "int_addr" and "int_gateway_ip" as general.mode needs them; {"components": [...]} A switched-on component's packages are installed by linux-manager-setup (linux-manager-install@), which a change starts without waiting for it; switching a component off removes no package. The configuration's validation refuses a change it does not allow, such as the VPN off while IKEv2, OpenVPN or WireGuard is on. """ if body == 'invalid' or (body is not None and not isinstance(body, dict)): return 400, {'error': 'the body must be a JSON object'} body = body or {} parts = path.strip('/').split('/') cfg = linuxmanager.load_config() if method in ('GET', 'HEAD') and parts == ['components']: return 200, {'components': listing(cfg)} if method != 'PUT' or len(parts) != 2 or parts[1] not in linuxmanager.COMPONENTS: return 404, {'error': f'no route {method} {path}'} name, enabled = parts[1], body.get('enabled') if not isinstance(enabled, bool): return 400, {'error': 'enabled must be true or false'} (section, setting), _ = linuxmanager.COMPONENTS[name] changes = {section: {setting: enabled}} if name == 'router' and enabled and not cfg['router']['enabled']: choices = router_choices(cfg) if not choices: return 409, {'error': 'the router needs a second network interface; the server has only one'} values = {key: body.get(key, '') for key in ('internal_interface', 'int_addr', 'int_gateway_ip')} if any(not isinstance(value, str) for value in values.values()): return 400, {'error': 'internal_interface, int_addr and int_gateway_ip must be strings'} if values['internal_interface'] and values['internal_interface'] not in choices: return 400, {'error': f'internal_interface must be one of {", ".join(choices)}'} if values['internal_interface']: changes['interfaces'] = {'internal': values['internal_interface']} if values['int_addr'] or values['int_gateway_ip']: changes['local'] = {'int_addr': values['int_addr'].strip(), 'int_gateway_ip': values['int_gateway_ip'].strip()} changed = settings_change(changes) if isinstance(changed, tuple): return changed return 200, {'components': listing(changed)}