-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceCommands.go
158 lines (148 loc) · 3.54 KB
/
serviceCommands.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
)
func runStart(name string) {
var runner string
if runtime.GOOS != "windows" {
runner = fmt.Sprintf("%s/run.sh", name)
} else {
runner = fmt.Sprintf(".\\%s\\run.cmd", name)
}
runcmd := exec.Command(runner)
runcmd.Stdout = os.Stdout
runcmd.Stderr = os.Stderr
err := runcmd.Start()
if err != nil {
log.Fatal(err)
}
}
func runStop(name string) {
if runtime.GOOS != "windows" {
//kill the 3 created processes
runnerprocs := fmt.Sprintf("%[1]s/run.sh|%[1]s/bin/Runner.Listener|%[1]s/run-helper.sh", name)
//Find the pid of the runner
c1 := exec.Command("pgrep", "-f", runnerprocs)
//kill the processes
c2 := exec.Command("xargs", "kill")
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
err := c1.Run()
err2 := c2.Wait()
if err != nil {
log.Fatal(err)
}
if err2 != nil {
log.Fatal(err)
}
} else {
//TODO: Implement a better stop for windows
c1 := exec.Command("Stop-Process", "-Name", "Runner.Listener")
//kill the processes
c1.Stdout = os.Stdout
c1.Stderr = os.Stderr
err := c1.Run()
if err != nil {
log.Fatal(err)
}
}
}
func createService(name string) {
err := os.Chdir(name)
if err != nil {
log.Fatal(err)
}
//Create a service to run the runner
var runcmd *exec.Cmd
if runtime.GOOS == "darwin" {
runcmd = exec.Command("./svc.sh", "install")
} else if runtime.GOOS == "linux" {
runcmd = exec.Command("sudo", "./svc.sh", "install")
} else if runtime.GOOS == "windows" {
log.Fatal("On Windows, you must configure the service when creating the runner. Remove the runner and re-create it with the --windows-service flag")
} else {
log.Fatal("Unsupported OS")
}
runcmd.Stdout = os.Stdout
runcmd.Stderr = os.Stderr
err = runcmd.Run()
if err != nil {
log.Fatal(err)
}
os.Chdir("..")
runService(name)
}
func runService(name string) {
err := os.Chdir(name)
if err != nil {
log.Fatal(err)
}
//run the configured service
var runcmd *exec.Cmd
if runtime.GOOS == "darwin" {
runcmd = exec.Command("./svc.sh", "start")
} else if runtime.GOOS == "linux" {
runcmd = exec.Command("sudo", "./svc.sh", "start")
} else if runtime.GOOS == "windows" {
runcmd = exec.Command("powershell", "Start-Service", "actions.runner.*")
} else {
log.Fatal("Unsupported OS")
}
runcmd.Stdout = os.Stdout
runcmd.Stderr = os.Stderr
err = runcmd.Run()
if err != nil {
log.Fatal(err)
}
}
func stopService(name string) {
err := os.Chdir(name)
if err != nil {
log.Fatal(err)
}
//Stop the running runner service
var runcmd *exec.Cmd
if runtime.GOOS == "darwin" {
runcmd = exec.Command("./svc.sh", "stop")
} else if runtime.GOOS == "linux" {
runcmd = exec.Command("sudo", "./svc.sh", "stop")
} else if runtime.GOOS == "windows" {
runcmd = exec.Command("powershell", "Stop-Service", "actions.runner.*")
} else {
log.Fatal("Unsupported OS")
}
runcmd.Stdout = os.Stdout
runcmd.Stderr = os.Stderr
err = runcmd.Run()
if err != nil {
log.Fatal(err)
}
}
func removeService(name string) {
err := os.Chdir(name)
if err != nil {
log.Fatal(err)
}
//Remove the service
var runcmd *exec.Cmd
if runtime.GOOS == "darwin" {
runcmd = exec.Command("./svc.sh", "uninstall")
} else if runtime.GOOS == "linux" {
runcmd = exec.Command("sudo", "./svc.sh", "uninstall")
} else if runtime.GOOS == "windows" {
log.Fatal("On Windows, remove the runner to remove the service.")
} else {
log.Fatal("Unsupported OS")
}
runcmd.Stdout = os.Stdout
runcmd.Stderr = os.Stderr
err = runcmd.Run()
if err != nil {
log.Fatal(err)
}
}