Rainbond/node/nodem/controller/controller_systemd_linux.go

217 lines
5.9 KiB
Go

// 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"
"io/ioutil"
"os"
"os/exec"
"regexp"
"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"
)
type ControllerSystemd struct {
SysConfigDir string
NodeType string
StartType string
conf *option.Conf
cluster client.ClusterClient
regBlock *regexp.Regexp
ServiceCli string
}
//NewController At the stage you want to load the configurations of all rainbond components
func NewController(conf *option.Conf, cluster client.ClusterClient) Controller {
cli, err := exec.LookPath("systemctl")
if err != nil {
logrus.Errorf("current machine do not have systemctl utils")
}
return &ControllerSystemd{
conf: conf,
cluster: cluster,
ServiceCli: cli,
SysConfigDir: "/etc/systemd/system",
}
}
//CheckBeforeStart check before start
func (m *ControllerSystemd) CheckBeforeStart() bool {
logrus.Info("Checking environments.")
if m.ServiceCli == "" {
logrus.Errorf("current machine do not have systemctl utils")
return false
}
return true
}
//StartService start service
func (m *ControllerSystemd) StartService(serviceName string) error {
err := m.run("start", serviceName)
if err != nil {
logrus.Errorf("Start service %s: %v", serviceName, err)
return err
}
return nil
}
//StopService stop service
func (m *ControllerSystemd) StopService(serviceName string) error {
err := m.run("stop", serviceName)
if err != nil {
logrus.Errorf("Stop service %s: %v", serviceName, err)
return err
}
return nil
}
//RestartService restart service
func (m *ControllerSystemd) RestartService(serviceName string) error {
err := m.run("restart", serviceName)
if err != nil {
logrus.Errorf("Restart service %s: %v", serviceName, err)
return err
}
return nil
}
//StartList start some service
func (m *ControllerSystemd) StartList(list []*service.Service) error {
logrus.Infof("Starting %d all services.", len(list))
for _, s := range list {
m.StartService(s.Name)
}
return nil
}
//StopList stop some service
func (m *ControllerSystemd) StopList(list []*service.Service) error {
logrus.Info("Stop all services.")
for _, s := range list {
m.StopService(s.Name)
}
return nil
}
//EnableService enable service
func (m *ControllerSystemd) EnableService(serviceName string) error {
logrus.Info("Enable service config by systemd.")
err := m.run("enable", serviceName)
if err != nil {
logrus.Errorf("Enable service %s: %v", serviceName, err)
}
return nil
}
//DisableService disable service
func (m *ControllerSystemd) DisableService(serviceName string) error {
logrus.Info("Disable service config by systemd.")
err := m.run("disable", serviceName)
if err != nil {
logrus.Errorf("Disable service %s: %v", serviceName, err)
}
return nil
}
//WriteConfig write config
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 := m.run("daemon-reload")
if err != nil {
logrus.Errorf("Reload config by systemd daemon-reload for %s: %v ", s.Name, err)
return err
}
return nil
}
//RemoveConfig remove config
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 = m.run("daemon-reload")
if err != nil {
logrus.Errorf("Reload config by systemd daemon-reload for %s: %v ", name, err)
return err
}
return nil
}
func (m *ControllerSystemd) run(args ...string) error {
err := exec.Command(m.ServiceCli, args...).Run()
if err != nil {
logrus.Errorf("systemd run error: %v", err)
return err
}
return nil
}
//InitStart init start. will start some required service
func (m *ControllerSystemd) InitStart(services []*service.Service) error {
if err := m.run("start", "docker"); err != nil {
return fmt.Errorf("systemctl start docker error:%s", err.Error())
}
for _, s := range services {
if s.Name == "etcd" {
fileName := fmt.Sprintf("/etc/systemd/system/%s.service", s.Name)
content := service.ToConfig(s)
if content == "" {
err := fmt.Errorf("can not generate config for service %s", s.Name)
fmt.Println(err)
return err
}
if err := ioutil.WriteFile(fileName, []byte(content), 0644); err != nil {
fmt.Printf("Generate config file %s: %v", fileName, err)
return err
}
if err := m.run("start", s.Name); err != nil {
return fmt.Errorf("systemctl start %s error:%s", s.Name, err.Error())
}
}
}
return nil
}