forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux.ts
More file actions
181 lines (161 loc) · 4.71 KB
/
linux.ts
File metadata and controls
181 lines (161 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* eslint-disable */
import { spawnSync } from "node:child_process"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import {
APP_DIR,
ERR_FILE,
LOG_FILE,
buildDirectCommand,
buildNpxCommand,
launcherPath,
} from "./shared"
import type { DaemonInstallArgs } from "./shared"
const SYSTEMD_UNIT = "xc-copilot-api.service"
function systemdUnitPath(): string {
return path.join(
os.homedir(),
".config",
"systemd",
"user",
SYSTEMD_UNIT,
)
}
export function installLinux(args: DaemonInstallArgs): void {
const unitPath = systemdUnitPath()
const launcher = launcherPath()
fs.mkdirSync(APP_DIR, { recursive: true })
fs.mkdirSync(path.dirname(unitPath), { recursive: true })
const execCmd = args.npx
? buildNpxCommand(args)
: buildDirectCommand(args)
// Launcher script sources login shell
fs.writeFileSync(
launcher,
[
"#!/bin/bash -l",
'[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"',
`exec ${execCmd}`,
"",
].join("\n"),
)
fs.chmodSync(launcher, 0o755)
const unitContent = `[Unit]
Description=xc-copilot-api - GitHub Copilot OpenAI Compatible API
After=network.target
[Service]
Type=simple
ExecStart=${launcher}
Restart=on-failure
RestartSec=10
StandardOutput=append:${LOG_FILE}
StandardError=append:${ERR_FILE}
[Install]
WantedBy=default.target
`
fs.writeFileSync(unitPath, unitContent)
spawnSync("systemctl", ["--user", "daemon-reload"], { encoding: "utf-8" })
spawnSync("systemctl", ["--user", "enable", SYSTEMD_UNIT], {
encoding: "utf-8",
})
console.log(`Installed systemd unit '${SYSTEMD_UNIT}'`)
console.log(` Unit: ${unitPath}`)
console.log(` Launcher: ${launcher}`)
console.log(` Log: ${LOG_FILE}`)
console.log(` Mode: ${args.npx ? "npx (auto-update)" : "direct"}`)
console.log(` Start: xc-copilot-api-daemon restart`)
}
export function uninstallLinux(): void {
spawnSync("systemctl", ["--user", "stop", SYSTEMD_UNIT], {
encoding: "utf-8",
})
spawnSync("systemctl", ["--user", "disable", SYSTEMD_UNIT], {
encoding: "utf-8",
})
const unitPath = systemdUnitPath()
if (fs.existsSync(unitPath)) {
fs.unlinkSync(unitPath)
spawnSync("systemctl", ["--user", "daemon-reload"], {
encoding: "utf-8",
})
}
const launcher = launcherPath()
if (fs.existsSync(launcher)) {
fs.unlinkSync(launcher)
}
console.log(`Uninstalled systemd unit '${SYSTEMD_UNIT}'`)
}
export function startLinux(): boolean {
const result = spawnSync(
"systemctl",
["--user", "start", SYSTEMD_UNIT],
{ encoding: "utf-8", timeout: 10000 },
)
if (result.status !== 0) {
console.error(
`systemd start failed: ${(result.stderr || result.stdout || "").trim()}`,
)
return false
}
console.log(`Started systemd unit '${SYSTEMD_UNIT}'`)
return true
}
export function stopLinux(): boolean {
const result = spawnSync(
"systemctl",
["--user", "stop", SYSTEMD_UNIT],
{ encoding: "utf-8", timeout: 15000 },
)
if (result.status !== 0) {
const detail = (result.stderr || result.stdout || "").trim()
if (!detail.includes("not loaded")) {
console.error(`systemd stop failed: ${detail}`)
return false
}
}
console.log(`Stopped systemd unit '${SYSTEMD_UNIT}'`)
return true
}
export function statusLinux(): void {
const unitPath = systemdUnitPath()
if (!fs.existsSync(unitPath)) {
console.log("Daemon: not installed")
return
}
console.log("Daemon: installed")
console.log(` Unit: ${unitPath}`)
const launcher = launcherPath()
if (fs.existsSync(launcher)) {
const content = fs.readFileSync(launcher, "utf-8")
const execLine = content
.split("\n")
.find((l) => l.startsWith("exec "))
if (execLine) {
console.log(` Command: ${execLine.replace("exec ", "")}`)
}
}
const result = spawnSync(
"systemctl",
["--user", "status", SYSTEMD_UNIT, "--no-pager"],
{ encoding: "utf-8", timeout: 5000 },
)
// systemctl status returns non-zero if daemon is not running, that's ok
console.log(result.stdout.trim())
}
export function findAllCopilotSystemdUnits(): string[] {
const result = spawnSync(
"systemctl",
["--user", "list-units", "--all", "--no-pager", "--plain"],
{ encoding: "utf-8", timeout: 5000 },
)
if (result.status !== 0) return []
const units: string[] = []
for (const line of result.stdout.split("\n")) {
if (line.toLowerCase().includes("copilot")) {
const unit = line.trim().split(/\s+/)[0]
if (unit) units.push(unit)
}
}
return units
}