This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon_linux_upstart.go
97 lines (83 loc) · 1.95 KB
/
daemon_linux_upstart.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
//go:build linux
// +build linux
package daemon
import (
"html/template"
"os"
"os/exec"
"strings"
)
type daemonUpstart struct {
config *Config
}
func (d *daemonUpstart) startRunLevels() []string {
return runLevels(d.config.StartRunLevels)
}
func (d *daemonUpstart) stopRunLevels() []string {
return runLevels(d.config.StopRunLevels)
}
func (d *daemonUpstart) path() string {
return "/etc/init/" + d.config.Name + ".conf"
}
func (d *daemonUpstart) installed() bool {
return checkInstalled(d.path())
}
func (d *daemonUpstart) running() (string, bool) {
return checkRunning(d.config.Name+" start/running", "process ([0-9]+)", "service", d.config.Name, "status")
}
func (d *daemonUpstart) Install(args ...string) error {
var (
srvPath = d.path()
file *os.File
execPath string
templ *template.Template
err error
)
file, err = os.Create(srvPath)
if err != nil {
return err
}
defer file.Close()
execPath, err = executablePath(d.config.Name)
if err != nil {
return err
}
templ, err = template.New("upstart").Parse(d.config.TemplateLinuxUpstart)
if err != nil {
return err
}
if err := templ.Execute(
file,
&struct {
Name, Description, Path, Args, StartRunLevels, StopRunLevels string
}{
d.config.Name,
d.config.Description,
execPath,
strings.Join(args, " "),
strings.Join(d.startRunLevels(), ""),
strings.Join(d.stopRunLevels(), ""),
},
); err != nil {
return err
}
if err = os.Chmod(srvPath, 0755); err != nil {
return err
}
return nil
}
func (d *daemonUpstart) Uninstall() error {
return os.Remove(d.path())
}
func (d *daemonUpstart) Restart() error {
return exec.Command("restart", d.config.Name).Run()
}
func (d *daemonUpstart) Start() error {
return exec.Command("start", d.config.Name).Run()
}
func (d *daemonUpstart) Stop() error {
return exec.Command("stop", d.config.Name).Run()
}
func (d *daemonUpstart) Reload() error {
return exec.Command("reload", d.config.Name, "reload").Run()
}