phone/scripts/bootstrap-services.ts
2025-11-20 18:18:47 -08:00

99 lines
2.5 KiB
TypeScript

import { $ } from "bun"
import { writeFileSync } from "fs"
const AP_SERVICE_FILE = "/etc/systemd/system/phone-ap.service"
const WEB_SERVICE_FILE = "/etc/systemd/system/phone-web.service"
const PHONE_SERVICE_FILE = "/etc/systemd/system/phone.service"
export const setupServices = async (installDir: string) => {
console.log("\nInstalling systemd services...")
// Find where bun is installed
const bunPath = await $`which bun`
.quiet()
.nothrow()
.text()
.then((p) => p.trim())
if (!bunPath) {
console.error("Error: bun not found in PATH. Please ensure bun is available system-wide.")
process.exit(1)
}
console.log(`Using bun at: ${bunPath}`)
// Create AP monitor service
const apServiceContent = `[Unit]
Description=Phone WiFi AP Monitor
After=network.target
Before=phone-web.service
[Service]
Type=simple
ExecStart=${bunPath} ${installDir}/src/services/ap-monitor.ts
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
`
writeFileSync(AP_SERVICE_FILE, apServiceContent, "utf8")
console.log("✓ Created phone-ap.service")
// Create web server service
const webServiceContent = `[Unit]
Description=Phone Web Server
After=network.target phone-ap.service
[Service]
Type=simple
ExecStart=${bunPath} ${installDir}/src/services/server/server.tsx
WorkingDirectory=${installDir}
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
`
writeFileSync(WEB_SERVICE_FILE, webServiceContent, "utf8")
console.log("✓ Created phone-web.service")
// Create phone service
const phoneServiceContent = `[Unit]
Description=Phone Application
After=network.target sound.target
Requires=sound.target
[Service]
Type=simple
User=corey
ExecStart=${bunPath} ${installDir}/src/main.ts
WorkingDirectory=${installDir}
EnvironmentFile=${installDir}/.env
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
`
writeFileSync(PHONE_SERVICE_FILE, phoneServiceContent, "utf8")
console.log("✓ Created phone.service")
await $`systemctl daemon-reload`
await $`systemctl enable phone-ap.service`
await $`systemctl enable phone-web.service`
await $`systemctl enable phone.service`
console.log("✓ Services enabled")
console.log("\nStarting the services...")
await $`systemctl start phone-ap.service`
await $`systemctl start phone-web.service`
await $`systemctl start phone.service`
console.log("✓ Services started")
}