/
controller_systemd.go
176 lines (147 loc) · 4.77 KB
/
controller_systemd.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/node/nodem/service"
"io/ioutil"
"os"
"os/exec"
"regexp"
)
type ControllerSystemd struct {
SysConfigDir string
NodeType string
StartType string
conf *option.Conf
cluster client.ClusterClient
regBlock *regexp.Regexp
ServiceCli string
}
// At the stage you want to load the configurations of all rainbond components
func NewControllerSystemd(conf *option.Conf, cluster client.ClusterClient) *ControllerSystemd {
cli, err := exec.LookPath("systemctl")
if err != nil {
panic(err)
}
return &ControllerSystemd{
conf: conf,
cluster: cluster,
ServiceCli: cli,
SysConfigDir: "/etc/systemd/system",
}
}
func (m *ControllerSystemd) CheckBeforeStart() bool {
logrus.Info("Checking environments.")
return true
}
func (m *ControllerSystemd) StartService(serviceName string) error {
err := exec.Command(m.ServiceCli, "start", serviceName).Run()
if err != nil {
logrus.Errorf("Start service %s: %v", serviceName, err)
return err
}
return nil
}
func (m *ControllerSystemd) StopService(serviceName string) error {
err := exec.Command(m.ServiceCli, "stop", serviceName).Run()
if err != nil {
logrus.Errorf("Stop service %s: %v", serviceName, err)
return err
}
return nil
}
func (m *ControllerSystemd) RestartService(serviceName string) error {
err := exec.Command(m.ServiceCli, "restart", serviceName).Run()
if err != nil {
logrus.Errorf("Restart service %s: %v", serviceName, err)
return err
}
return nil
}
func (m *ControllerSystemd) StartList(list []*service.Service) error {
logrus.Info("Starting all services.")
err := exec.Command(m.ServiceCli, "start", "multi-user.target").Run()
if err != nil {
logrus.Errorf("Start target multi-user: %v", err)
return err
}
return nil
}
func (m *ControllerSystemd) StopList(list []*service.Service) error {
logrus.Info("Stop all services.")
for _, s := range list {
err := exec.Command(m.ServiceCli, "stop", s.Name).Run()
if err != nil {
logrus.Errorf("Enable service %s: %v", s.Name, err)
}
}
return nil
}
func (m *ControllerSystemd) EnableService(name string) error {
logrus.Info("Enable service config by systemd.")
err := exec.Command(m.ServiceCli, "enable", name).Run()
if err != nil {
logrus.Errorf("Enable service %s: %v", name, err)
}
return nil
}
func (m *ControllerSystemd) DisableService(name string) error {
logrus.Info("Disable service config by systemd.")
err := exec.Command(m.ServiceCli, "disable", name).Run()
if err != nil {
logrus.Errorf("Disable service %s: %v", name, err)
}
return nil
}
func (m *ControllerSystemd) WriteConfig(s *service.Service) error {
fileName := fmt.Sprintf("%s/%s.service", m.SysConfigDir, s.Name)
content := service.ToConfig(s)
content = service.InjectConfig(content, m.cluster)
if content == "" {
err := fmt.Errorf("can not generate config for service %s", s.Name)
logrus.Error(err)
return err
}
if err := ioutil.WriteFile(fileName, []byte(content), 0644); err != nil {
logrus.Errorf("Generate config file %s: %v", fileName, err)
return err
}
logrus.Info("Reload config for systemd by daemon-reload.")
err := exec.Command(m.ServiceCli, "daemon-reload").Run()
if err != nil {
logrus.Errorf("Reload config by systemd daemon-reload for %s: %v ", s.Name, err)
return err
}
return nil
}
func (m *ControllerSystemd) RemoveConfig(name string) error {
logrus.Info("Remote service config by systemd.")
fileName := fmt.Sprintf("%s/%s.service", m.SysConfigDir, name)
_, err := os.Stat(fileName)
if err == nil {
os.Remove(fileName)
}
logrus.Info("Reload config for systemd by daemon-reload.")
err = exec.Command(m.ServiceCli, "daemon-reload").Run()
if err != nil {
logrus.Errorf("Reload config by systemd daemon-reload for %s: %v ", name, err)
return err
}
return nil
}