#!/usr/bin/env python3 """Run the manager and the Vite development server on this workstation: dev.py CONFIG [CONNECTION]. CONFIG is the config.json of a platform. The server's address comes from the outputs of the platform's root module, module_root of CONFIG, relative to its directory, read with its iac_tool (terraform by default, or tofu). The sessions are kept in the database of the PostgreSQL connection string CONNECTION, or without one in PGDATABASE (else the login name), with libpq's PG* variables for the rest. The manager reads the generated dev.json, whose bind is 127.0.0.1, and listens on 127.0.0.1:7080; its Python packages are installed in .venv. The application is served at http://localhost:5180 and passes /api to the manager. A wrong argument is a message on standard error and the exit status 2. """ import getpass import json import os import pathlib import subprocess import sys ROOT = pathlib.Path(__file__).resolve().parent SETTINGS = ROOT / 'dev.json' VENV = ROOT / '.venv' WEB = ROOT / 'web' MANAGER = ROOT / 'debian-package' / 'src' / 'usr' / 'lib' / 'linux-manager' / 'manager' def main(): if not 2 <= len(sys.argv) <= 3: print(f'usage: {sys.argv[0]} [PostgreSQL connection string]', file=sys.stderr) return 2 config = pathlib.Path(sys.argv[1]).resolve() if not config.is_file(): print(f'dev.py: {sys.argv[1]} does not exist', file=sys.stderr) return 2 database = sys.argv[2] if len(sys.argv) == 3 else f'dbname={os.environ.get("PGDATABASE") or getpass.getuser()}' platform = json.loads(config.read_text(encoding='utf-8')) root = platform.get('module_root') if not isinstance(root, str) or not root.strip() or os.path.isabs(root): print(f'dev.py: module_root in {config} must be a directory relative to {config.parent}', file=sys.stderr) return 2 module_root = pathlib.Path(os.path.normpath(config.parent / root)) if not module_root.is_dir(): print(f'dev.py: module_root {module_root} is not a directory', file=sys.stderr) return 2 SETTINGS.write_text(json.dumps({'terraform_dir': str(module_root), 'database': database, 'iac_tool': platform.get('iac_tool') or 'terraform', 'bind': '127.0.0.1'}) + '\n', encoding='utf-8') # The manager's packages in .venv, and the application's in node_modules, installed when missing or older. python = VENV / 'bin' / 'python' if not os.access(python, os.X_OK): subprocess.run([sys.executable, '-m', 'venv', str(VENV)], check=True) if subprocess.run([str(python), '-c', 'import psycopg, requests'], stderr=subprocess.DEVNULL).returncode != 0: subprocess.run([str(VENV / 'bin' / 'pip'), 'install', '--quiet', 'psycopg[binary]', 'requests'], check=True) modules, lock = WEB / 'node_modules', WEB / 'package-lock.json' if not modules.is_dir() or lock.stat().st_mtime > modules.stat().st_mtime: subprocess.run(['npm', 'install'], cwd=WEB, check=True) manager = subprocess.Popen([str(python), str(MANAGER), '--config', str(SETTINGS)]) try: print(f'Platform module: {module_root}', flush=True) return subprocess.run(['npm', 'run', 'dev'], cwd=WEB, env={**os.environ, 'VITE_MANAGER': 'http://127.0.0.1:7080'}).returncode except KeyboardInterrupt: return 130 finally: manager.terminate() manager.wait() if __name__ == '__main__': sys.exit(main())