import fs from 'node:fs'; import path from 'node:path'; import { AlertsFileSchema, SettingsSchema, SnippetsFileSchema, SystemConfigSchema, type SystemConfig, } from '../../shared/schemas/index.js'; export const PRESET_SYSTEMS: SystemConfig[] = [ SystemConfigSchema.parse({ id: 'docker-compose', name: 'Docker Compose Stacks', description: 'Discover and manage Compose stacks across configured root directories.', cacheTtlSec: 45, backgroundCheckSec: 60, params: [ { name: 'rootDirs', type: 'string[]', default: ['~/Services'], required: true, description: 'Directories scanned for compose.yml / docker-compose.yml stacks.', }, ], discovery: { type: 'folder-scan', rootsParam: 'rootDirs', maxDepth: 1, matchFiles: ['compose.yml', 'compose.yaml', 'docker-compose.yml', 'docker-compose.yaml'], selectableFiles: ['*compose*.yml', '*compose*.yaml'], }, status: { command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES ps --all --format json', parser: 'json-lines', subItemField: 'Service', health: { field: 'State', healthyValues: ['running'], degradedValues: ['restarting', 'paused', 'created'], }, }, actions: [ { id: 'up', label: 'Run', scope: 'item', mode: 'oneshot', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES up -d', }, { id: 'down', label: 'Shut Down', scope: 'item', mode: 'oneshot', confirm: true, command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES down', }, { id: 'restart', label: 'Restart', scope: 'item', mode: 'oneshot', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES restart', }, { id: 'rebuild', label: 'Rebuild', scope: 'item', mode: 'stream', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES up -d --build --force-recreate', }, { id: 'update', label: 'Pull & Update', scope: 'item', mode: 'stream', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES pull && docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES up -d', }, { id: 'logs', label: 'Logs', scope: 'item', mode: 'stream', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES logs -f --tail 100', }, { id: 'debug-shell', label: 'Debug Shell', scope: 'subitem', mode: 'interactive', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES exec "$GCOORD_SUBITEM" sh -c \'command -v bash >/dev/null && exec bash || exec sh\'', }, { id: 'inspect', label: 'Inspect', scope: 'subitem', mode: 'oneshot', command: 'docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES ps "$GCOORD_SUBITEM" --format json && docker inspect "$(docker compose --project-directory "$GCOORD_ITEM_DIR" $GCOORD_COMPOSE_FILES ps -q "$GCOORD_SUBITEM")"', }, { id: 'prune', label: 'Prune Host', scope: 'system', mode: 'stream', confirm: true, command: 'docker system prune -f', }, ], editableFiles: ['*compose*.yml', '*compose*.yaml', '.env'], }), SystemConfigSchema.parse({ id: 'systemd', name: 'systemd Services', description: 'Monitor and control systemd units, overrides, and live journalctl streams.', cacheTtlSec: 30, backgroundCheckSec: 60, params: [ { name: 'unitFilter', type: 'string', default: '', required: false, description: 'Optional extended regex matching unit names to track.', }, ], discovery: { type: 'command', command: 'systemctl list-units --type=service --all --no-pager --no-legend --plain | awk \'{print $1}\' | { if [ -n "$GCOORD_PARAM_UNITFILTER" ]; then grep -E "$GCOORD_PARAM_UNITFILTER" || true; else cat; fi; }', parser: 'lines', }, status: { command: 'systemctl show "$GCOORD_ITEM_NAME" --property=Id,LoadState,ActiveState,SubState,MainPID --no-pager | awk -F= \'BEGIN{printf "{"} NR>1{printf ","} {gsub(/"/,"\\\\\"",$2); printf "\\"%s\\":\\"%s\\"",$1,$2} END{printf "}\\n"}\'', parser: 'json', health: { field: 'ActiveState', healthyValues: ['active'], degradedValues: ['activating', 'reloading', 'deactivating'], }, }, actions: [ { id: 'start', label: 'Start', scope: 'item', mode: 'oneshot', sudo: true, command: 'systemctl start "$GCOORD_ITEM_NAME"', }, { id: 'stop', label: 'Stop', scope: 'item', mode: 'oneshot', sudo: true, confirm: true, command: 'systemctl stop "$GCOORD_ITEM_NAME"', }, { id: 'restart', label: 'Restart', scope: 'item', mode: 'oneshot', sudo: true, command: 'systemctl restart "$GCOORD_ITEM_NAME"', }, { id: 'reload', label: 'Reload', scope: 'item', mode: 'oneshot', sudo: true, command: 'systemctl reload "$GCOORD_ITEM_NAME"', }, { id: 'enable', label: 'Enable', scope: 'item', mode: 'oneshot', sudo: true, command: 'systemctl enable "$GCOORD_ITEM_NAME"', }, { id: 'disable', label: 'Disable', scope: 'item', mode: 'oneshot', sudo: true, confirm: true, command: 'systemctl disable "$GCOORD_ITEM_NAME"', }, { id: 'logs', label: 'Journal Logs', scope: 'item', mode: 'stream', sudo: true, command: 'journalctl -u "$GCOORD_ITEM_NAME" -f -n 100 --no-pager', }, ], editableFiles: ['/etc/systemd/system/*.service', '/etc/systemd/system/*.d/*.conf'], }), SystemConfigSchema.parse({ id: 'os-packages', name: 'OS Package Updates', description: 'Cross-distro package update status and streamed upgrades (apt/dnf/apk/pacman).', cacheTtlSec: 300, params: [], discovery: { type: 'command', command: 'echo "system-packages"', parser: 'lines', }, status: { command: 'if command -v apt-get >/dev/null; then echo "manager=apt"; apt list --upgradable 2>/dev/null | tail -n +2 | head -n 50; elif command -v dnf >/dev/null; then echo "manager=dnf"; dnf check-update -q || true; elif command -v apk >/dev/null; then echo "manager=apk"; apk version -l "<" || true; elif command -v pacman >/dev/null; then echo "manager=pacman"; pacman -Qu || true; fi; [ -f /var/run/reboot-required ] && echo "REBOOT_REQUIRED=true" || echo "REBOOT_REQUIRED=false"', parser: 'lines', health: { healthyExitCodes: [0], }, }, actions: [ { id: 'refresh', label: 'Refresh Index', scope: 'system', mode: 'stream', sudo: true, command: 'if command -v apt-get >/dev/null; then apt-get update; elif command -v dnf >/dev/null; then dnf makecache; elif command -v apk >/dev/null; then apk update; elif command -v pacman >/dev/null; then pacman -Sy; fi', }, { id: 'upgrade', label: 'Upgrade Packages', scope: 'system', mode: 'stream', sudo: true, confirm: true, command: 'if command -v apt-get >/dev/null; then DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y; elif command -v dnf >/dev/null; then dnf upgrade -y; elif command -v apk >/dev/null; then apk upgrade; elif command -v pacman >/dev/null; then pacman -Syu --noconfirm; fi', }, ], editableFiles: [], }), ]; export const DEFAULT_SNIPPETS = SnippetsFileSchema.parse({ version: 1, snippets: [ { id: 'disk-usage', name: 'Disk Usage (Real FS)', description: 'Show mounted real filesystems with types and usage percentages.', command: 'df -hPT -x tmpfs -x devtmpfs', tags: ['diagnostics', 'disk'], inputs: [], sudo: false, confirm: false, }, { id: 'listening-ports', name: 'Listening Sockets', description: 'Inspect listening TCP/UDP sockets and owning processes.', command: 'ss -tulnp 2>/dev/null || netstat -tulnp', tags: ['network', 'diagnostics'], inputs: [], sudo: true, confirm: false, }, { id: 'failed-units', name: 'Failed systemd Units', description: 'List failed systemd services.', command: 'systemctl --failed --no-pager', tags: ['systemd'], inputs: [], sudo: false, confirm: false, }, { id: 'docker-stats-once', name: 'Docker Container Stats (Snapshot)', description: 'Print a single snapshot of running Docker container CPU/RAM usage.', command: 'docker stats --no-stream --format "table {{.Name}}\\t{{.CPUPerc}}\\t{{.MemUsage}}\\t{{.NetIO}}"', tags: ['docker'], inputs: [], sudo: false, confirm: false, }, ], }); export function seedDefaultConfigs(configDir: string): string[] { const created: string[] = []; for (const sub of ['servers', 'workflows', 'systems']) { fs.mkdirSync(path.join(configDir, sub), { recursive: true }); } const defaults: Array<[string, unknown]> = [ ['settings.json', SettingsSchema.parse({})], ['alerts.json', AlertsFileSchema.parse({})], ['snippets.json', DEFAULT_SNIPPETS], ...PRESET_SYSTEMS.map( (sys): [string, SystemConfig] => [`systems/${sys.id}.json`, sys], ), ]; for (const [relPath, obj] of defaults) { const absPath = path.join(configDir, relPath); if (!fs.existsSync(absPath)) { fs.writeFileSync(absPath, `${JSON.stringify(obj, null, 2)}\n`, 'utf8'); created.push(relPath); } } return created; }