-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
127 lines (118 loc) · 2.74 KB
/
service.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
package main
import (
"fmt"
"time"
"github.com/kardianos/service"
"github.com/sirupsen/logrus"
)
// copy from https://github.com/jeessy2/ddns-go/blob/1576b7bfd8272bb1dda1352919d367bab2f4ce94/main.go#L327
const sysvScript = `#!/bin/sh /etc/rc.common
DESCRIPTION="{{.Description}}"
cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}"
name="ddns-go"
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
START=99
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && cat /proc/$(get_pid)/stat > /dev/null 2>&1
}
start() {
if is_running; then
echo "Already started"
else
echo "Starting $name"
{{if .WorkingDirectory}}cd '{{.WorkingDirectory}}'{{end}}
$cmd >> "$stdout_log" 2>> "$stderr_log" &
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
}
stop() {
if is_running; then
echo -n "Stopping $name.."
kill $(get_pid)
for i in $(seq 1 10)
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
}
restart() {
stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
start
}
`
func getService(duration time.Duration, urls []string) service.Service {
var depends []string
options := make(service.KeyValue)
switch service.ChosenSystem().String() {
case "unix-systemv":
options["SysvScript"] = sysvScript
case "windows-service":
options["DelayedAutoStart"] = true
default:
depends = append(depends, "Requires=network.target",
"After=network-online.target")
}
svcConfig := &service.Config{
Name: "hosts-go",
DisplayName: "hosts-go",
Description: "Fetch and merge hosts files from the internet",
Dependencies: depends,
Option: options,
Arguments: []string{"--duration", fmt.Sprintf("%s", duration)},
}
for _, u := range urls {
svcConfig.Arguments = append(svcConfig.Arguments, "-u", u)
}
s, err := service.New(nil, svcConfig)
if err != nil {
logrus.Fatal(err)
}
return s
}
func installService(duration time.Duration, urls []string) error {
s := getService(duration, urls)
err := s.Install()
if err != nil {
return fmt.Errorf("failed to install service: %v", err)
}
fmt.Println("Service installed successfully!")
return nil
}
func uninstallService(duration time.Duration, urls []string) error {
s := getService(duration, urls)
err := s.Uninstall()
if err != nil {
return fmt.Errorf("failed to install service: %v", err)
}
logrus.Info("Service uninstalled successfully!")
return nil
}