Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

2.8 KB
Raw Blame
/**
 * Pure systemd unit rendering shared by the installer (`systemd-unit.mjs`) and
 * the runtime ownership proof (`lib/managed-apps`). Keeping the exact bytes in
 * one place means the producer and consumer cannot drift into a fake fixture.
 *
 * Validation belongs to the caller: `systemd-unit.mjs` rejects non-absolute,
 * dynamic, loopback, or malformed configuration before calling this.
 */

const CONTROL = /[\0\r\n]/

/** @param {string} value */
export function systemdQuote(value) {
  if (typeof value !== 'string' || value.length === 0 || CONTROL.test(value)) {
    throw new Error('unit value must be a non-empty string without control characters')
  }
  const escaped = value
    .replace(/%/g, '%%')
    .replace(/\$/g, () => '$$')
    .replace(/\\/g, '\\\\')
    .replace(/"/g, '\\"')
  return `"${escaped}"`
}

/**
 * Render an absolute path for a systemd path directive (`WorkingDirectory=`).
 * Quotes are WRONG here: systemd does not strip them for these directives, so
 * `WorkingDirectory="/home/x"` fails with `path is not absolute` and the unit
 * never starts. Only the specifier escape and whitespace escaping apply.
 * @param {string} value
 */
export function systemdPath(value) {
  if (typeof value !== 'string' || value.length === 0 || CONTROL.test(value)) {
    throw new Error('unit path must be a non-empty string without control characters')
  }
  if (!value.startsWith('/')) throw new Error(`unit path must be absolute, got: ${value}`)
  return value.replace(/%/g, '%%').replace(/ /g, '\\x20')
}

/**
 * @typedef {object} UnitRenderInput
 * @property {string} marker
 * @property {string} serviceName
 * @property {string} hostNode
 * @property {string} cliScript
 * @property {string} managedRoot
 * @property {number} port
 * @property {string} advertisedEndpoint
 */

/**
 * @param {UnitRenderInput} input
 */
export function renderUnitText({ marker, serviceName, hostNode, cliScript, managedRoot, port, advertisedEndpoint }) {
  const argv = [
    hostNode,
    cliScript,
    'run',
    '--managed-root',
    managedRoot,
    '--port',
    String(port),
    '--advertised-endpoint',
    advertisedEndpoint
  ]

  return `${marker}
# Generated by MSO's managed-app-orca adapter. Do not edit by hand; reinstall
# through MSO instead. This unit runs only the pinned, verified Orca release
# under the MSO-owned managed root. It contains no credential.
[Unit]
Description=Orca web workspace (MSO managed ${serviceName})
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=3

[Service]
Type=simple
WorkingDirectory=${systemdPath(managedRoot)}
ExecStart=${argv.map(systemdQuote).join(' ')}
Environment="NODE_ENV=production"
Restart=on-failure
RestartSec=5
TimeoutStartSec=90
TimeoutStopSec=20
KillMode=process
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=default.target
`
}