-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_tools.go
142 lines (106 loc) · 2.84 KB
/
service_tools.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
package mttools
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"text/template"
_ "embed"
)
//go:embed service_tools.service.template
var systemdServiceUnitFileTemplate string
const SystemdServiceDirPath = "/etc/systemd/system"
type ServiceData struct {
Name string //service name
User string //user
Group string //group
Executable string //path to executable file
WorkingDir string //full working directory path
Autostart bool //start service automatically
}
func IsSystemdAvailable() bool {
return IsDirExists(SystemdServiceDirPath)
}
func (unit *ServiceData) InstallSystemdService() error {
service_unit_filename := filepath.Join(SystemdServiceDirPath, unit.Name+".service")
if IsFileExists(service_unit_filename) {
return fmt.Errorf(
"File %s already exists. Use 'uninstall' command or remove file manually.\n", service_unit_filename,
)
}
t := template.New("service")
if _, err := t.Parse(systemdServiceUnitFileTemplate); err != nil {
return err
}
var err error
unit.Executable, err = os.Executable()
if err != nil {
return err
}
unit.WorkingDir, err = os.Getwd()
if err != nil {
return err
}
file, err := os.Create(service_unit_filename)
if err != nil {
return err
}
if err := t.Execute(file, unit); err != nil {
return err
}
if err := file.Chmod(0644); err != nil {
return err
}
file.Close()
log.Printf("File %s created.", service_unit_filename)
log.Println("Reloading systemctl daemon")
out, err := exec.Command("systemctl", "daemon-reload").Output()
if err != nil {
return err
}
log.Println(string(out[:]))
if unit.Autostart {
log.Printf("Enabling '%s' service autostart.\n", unit.Name)
out, err := exec.Command("systemctl", "enable", unit.Name).Output()
if err != nil {
return err
}
log.Println(string(out[:]))
}
return nil
}
func (unit *ServiceData) UninstallSystemdService() error {
service_unit_filename := filepath.Join(SystemdServiceDirPath, unit.Name+".service")
if !IsFileExists(service_unit_filename) {
return fmt.Errorf(
"File %s does not exists. Use 'install' command to create service unit file.\n", service_unit_filename,
)
}
var out []byte
var err error
log.Printf("Stopping '%s' service.\n", unit.Name)
out, err = exec.Command("service", unit.Name, "stop").Output()
if err != nil {
return err
}
log.Println(string(out[:]))
log.Printf("Disabling '%s' service autostart.\n", unit.Name)
out, err = exec.Command("systemctl", "disable", unit.Name).Output()
if err != nil {
return err
}
log.Println(string(out[:]))
err = os.Remove(service_unit_filename)
if err != nil {
return err
}
log.Printf("File %s removed.", service_unit_filename)
log.Println("Reloading systemctl daemon")
out, err = exec.Command("systemctl", "daemon-reload").Output()
if err != nil {
return err
}
log.Println(string(out[:]))
return nil
}