-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
360 lines (315 loc) · 8.71 KB
/
Copy pathexec.go
File metadata and controls
360 lines (315 loc) · 8.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
type ServiceKind string
const (
KindContainer ServiceKind = "container"
KindDotNet ServiceKind = "dotnet"
KindCLI ServiceKind = "cli"
)
type Service struct {
Name string
Kind ServiceKind
Section string
ComposeFile string // container
ContainerName string // container
CsprojPath string // dotnet
URL string // dotnet launch url / cli url
WorkDir string // cli
}
type ServiceStatus int
const (
StatusDown ServiceStatus = iota
StatusStarting // process started but not yet reachable (e.g. still building)
StatusUp // started by us and reachable
StatusExternal // port is open but not our PID
)
func (s Service) GetStatus(pidsFile string) ServiceStatus {
switch s.Kind {
case KindContainer:
if containerIsRunning(s.ContainerName) {
return StatusUp
}
return StatusDown
case KindDotNet, KindCLI:
processRunning := processIsRunning(pidsFile, s.Name)
if s.URL != "" {
portOpen := isPortOpen(s.URL)
if processRunning && portOpen {
return StatusUp
}
if processRunning && !portOpen {
return StatusStarting
}
if !processRunning && portOpen {
return StatusExternal
}
return StatusDown
}
if processRunning {
return StatusUp
}
return StatusDown
}
return StatusDown
}
func (s Service) IsUp(pidsFile string) bool {
st := s.GetStatus(pidsFile)
return st == StatusUp || st == StatusExternal
}
// WorkingDirectory returns the filesystem path to open in a file explorer for this service.
func (s Service) WorkingDirectory(rootDir string) string {
switch s.Kind {
case KindDotNet:
return filepath.Dir(s.CsprojPath)
case KindCLI:
return s.WorkDir
case KindContainer:
return filepath.Join(rootDir, "etc", "docker", "containers")
}
return rootDir
}
func (s Service) Start(rootDir, logDir, pidsFile, composeProject string) error {
switch s.Kind {
case KindContainer:
return startContainer(rootDir, s.ComposeFile, composeProject)
case KindDotNet:
return startDotNet(rootDir, logDir, pidsFile, s.Name, s.CsprojPath, s.URL)
case KindCLI:
return startCLI(logDir, pidsFile, s.Name, s.WorkDir)
}
return nil
}
func (s Service) Stop(rootDir, pidsFile, composeProject string) error {
switch s.Kind {
case KindContainer:
return stopContainer(rootDir, s.ComposeFile, composeProject)
case KindDotNet, KindCLI:
return stopProcess(pidsFile, s.Name)
}
return nil
}
func (s Service) LogContent(rootDir, logDir string, lines int) string {
switch s.Kind {
case KindContainer:
out, _ := exec.Command("docker", "logs", "--tail", strconv.Itoa(lines), s.ContainerName).CombinedOutput()
return string(out)
case KindDotNet, KindCLI:
logFile := filepath.Join(logDir, s.Name+".log")
return tailFile(logFile, lines)
}
return ""
}
// ── Container management ──────────────────────────────────────
func containerIsRunning(name string) bool {
out, err := exec.Command("docker", "inspect", "-f", "{{.State.Status}}", name).Output()
if err != nil {
return false
}
return strings.TrimSpace(string(out)) == "running"
}
func runWithOutput(cmd *exec.Cmd) error {
out, err := cmd.CombinedOutput()
if err != nil {
msg := strings.TrimSpace(string(out))
if msg != "" {
return fmt.Errorf("%s", lastMeaningfulLine(msg))
}
return err
}
return nil
}
func lastMeaningfulLine(s string) string {
lines := strings.Split(s, "\n")
for i := len(lines) - 1; i >= 0; i-- {
l := strings.TrimSpace(lines[i])
if l != "" {
return l
}
}
return s
}
func startContainer(rootDir, composeFile, project string) error {
dockerDir := filepath.Join(rootDir, "etc", "docker")
exec.Command("docker", "network", "create", "tcmb", "--label=tcmb").Run()
cmd := exec.Command("docker", "compose", "-p", project, "-f", filepath.Join("containers", composeFile), "up", "-d")
cmd.Dir = dockerDir
return runWithOutput(cmd)
}
func stopContainer(rootDir, composeFile, project string) error {
dockerDir := filepath.Join(rootDir, "etc", "docker")
cmd := exec.Command("docker", "compose", "-p", project, "-f", filepath.Join("containers", composeFile), "down")
cmd.Dir = dockerDir
return runWithOutput(cmd)
}
// ── Process management ────────────────────────────────────────
func loadPids(path string) map[string]int {
pids := make(map[string]int)
data, err := os.ReadFile(path)
if err != nil {
return pids
}
if err := json.Unmarshal(data, &pids); err != nil {
return pids
}
return pids
}
func savePids(path string, pids map[string]int) error {
data, err := json.MarshalIndent(pids, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func processIsRunning(pidsFile, name string) bool {
pids := loadPids(pidsFile)
pid, ok := pids[name]
if !ok {
return false
}
return isProcessAlive(pid)
}
func startDotNet(rootDir, logDir, pidsFile, name, csprojPath, url string) error {
logFile := filepath.Join(logDir, name+".log")
f, err := os.Create(logFile)
if err != nil {
return fmt.Errorf("create log file: %w", err)
}
cmd := exec.Command("dotnet", "run", "--project", csprojPath, "--no-launch-profile")
cmd.Env = append(os.Environ(),
"ASPNETCORE_ENVIRONMENT=Development",
"ASPNETCORE_URLS="+url,
)
cmd.Stdout = f
cmd.Stderr = f
setProcessGroup(cmd)
if err := cmd.Start(); err != nil {
f.Close()
return fmt.Errorf("start %s: %w", name, err)
}
go func() {
cmd.Wait()
f.Close()
}()
pids := loadPids(pidsFile)
pids[name] = cmd.Process.Pid
return savePids(pidsFile, pids)
}
func startCLI(logDir, pidsFile, name, workDir string) error {
logFile := filepath.Join(logDir, name+".log")
f, err := os.Create(logFile)
if err != nil {
return fmt.Errorf("create log file: %w", err)
}
cmd := exec.Command("yarn", "start")
cmd.Dir = workDir
cmd.Stdout = f
cmd.Stderr = f
setProcessGroup(cmd)
if err := cmd.Start(); err != nil {
f.Close()
return fmt.Errorf("start %s: %w", name, err)
}
go func() {
cmd.Wait()
f.Close()
}()
pids := loadPids(pidsFile)
pids[name] = cmd.Process.Pid
return savePids(pidsFile, pids)
}
func stopProcess(pidsFile, name string) error {
pids := loadPids(pidsFile)
pid, ok := pids[name]
if !ok {
return nil
}
if err := killProcessGroup(pid); err != nil {
return err
}
delete(pids, name)
return savePids(pidsFile, pids)
}
// ── Port utilities ────────────────────────────────────────────
func portFromURL(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return ""
}
port := u.Port()
if port == "" {
if u.Scheme == "https" {
return "443"
}
return "80"
}
return port
}
const portCheckTimeout = 500 * time.Millisecond
func isPortOpen(rawURL string) bool {
port := portFromURL(rawURL)
if port == "" {
return false
}
conn, err := net.DialTimeout("tcp", "localhost:"+port, portCheckTimeout)
if err != nil {
return false
}
conn.Close()
return true
}
func openBrowser(rawURL string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", rawURL)
case "linux":
cmd = exec.Command("xdg-open", rawURL)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", rawURL)
default:
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
return cmd.Start()
}
func openFileExplorer(dir string) error {
absDir, err := filepath.Abs(dir)
if err != nil {
return err
}
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", absDir)
case "linux":
cmd = exec.Command("xdg-open", absDir)
case "windows":
cmd = exec.Command("explorer", absDir)
default:
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
return cmd.Start()
}
// ── Log reading ───────────────────────────────────────────────
func tailFile(path string, n int) string {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Sprintf("(no log file: %s)", filepath.Base(path))
}
lines := strings.Split(string(data), "\n")
if len(lines) > n {
lines = lines[len(lines)-n:]
}
return strings.Join(lines, "\n")
}