[FIX] change write system config code

This commit is contained in:
barnett 2018-12-01 22:28:26 +08:00
parent 2fbf46959a
commit 8644b1915d
6 changed files with 35 additions and 34 deletions

View File

@ -16,9 +16,6 @@ build_items="api chaos entrance monitor mq node webcli worker eventlog"
ifeq ($(origin WHAT), undefined)
WHAT = all
endif
ifeq ($(origin GOOS), undefined)
GOOS = darwin
endif
ifeq ($(origin STATIC), undefined)
STATIC = false
else

View File

@ -20,12 +20,11 @@ package controller
import (
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/node/nodem/service"
)
//NewController At the stage you want to load the configurations of all rainbond components
func NewController(conf *option.Conf, cluster client.ClusterClient) Controller {
func NewController(conf *option.Conf, manager *ManagerService) Controller {
return &testController{}
}

View File

@ -20,21 +20,20 @@ package controller
import (
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/node/nodem/service"
)
//NewController At the stage you want to load the configurations of all rainbond components
func NewController(conf *option.Conf, cluster client.ClusterClient) Controller {
func NewController(conf *option.Conf, manager *ManagerService) Controller {
return &windowsServiceController{
conf: conf,
cluster: cluster,
manager: manager,
}
}
type windowsServiceController struct {
conf *option.Conf
cluster client.ClusterClient
manager *ManagerService
}
func (w *windowsServiceController) InitStart(services []*service.Service) error {

View File

@ -19,6 +19,7 @@
package controller
import (
"bytes"
"fmt"
"io/ioutil"
"os"
@ -139,8 +140,8 @@ func (m *ControllerSystemd) DisableService(serviceName string) error {
//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.manager)
content := ToConfig(s)
content = m.manager.InjectConfig(content)
if content == "" {
err := fmt.Errorf("can not generate config for service %s", s.Name)
logrus.Error(err)
@ -196,7 +197,7 @@ func (m *ControllerSystemd) InitStart(services []*service.Service) error {
for _, s := range services {
if s.Name == "etcd" {
fileName := fmt.Sprintf("/etc/systemd/system/%s.service", s.Name)
content := service.ToConfig(s)
content := ToConfig(s)
if content == "" {
err := fmt.Errorf("can not generate config for service %s", s.Name)
fmt.Println(err)
@ -215,13 +216,14 @@ func (m *ControllerSystemd) InitStart(services []*service.Service) error {
return nil
}
func ToConfig(svc *Service) string {
func ToConfig(svc *service.Service) string {
if svc.Start == "" {
logrus.Error("service start command is empty.")
return ""
}
s := service.Lines{"[Unit]"}
s := ConfigWriter{writer: bytes.NewBuffer(nil)}
s.AddTitle("[Unit]")
s.Add("Description", svc.Name)
for _, d := range svc.After {
dpd := d
@ -257,3 +259,26 @@ func ToConfig(svc *Service) string {
return s.Get()
}
type ConfigWriter struct {
writer *bytes.Buffer
}
func (l *ConfigWriter) AddTitle(line string) {
l.writer.WriteString("\n")
l.writer.WriteString(line)
}
func (l *ConfigWriter) Add(k, v string) {
if v == "" {
return
}
l.writer.WriteString("\n")
l.writer.WriteString(k)
l.writer.WriteString("=")
l.writer.WriteString(v)
}
func (l *ConfigWriter) Get() string {
return l.writer.String()
}

View File

@ -441,11 +441,11 @@ func NewManagerService(conf *option.Conf, healthyManager healthy.Manager, cluste
cancel: cancel,
conf: conf,
cluster: cluster,
ctr: NewController(conf, cluster),
healthyManager: healthyManager,
etcdcli: conf.EtcdCli,
services: new([]*service.Service),
allservice: new([]*service.Service),
}
manager.ctr = NewController(conf, manager)
return manager
}

View File

@ -93,22 +93,3 @@ func loadServicesFromFile(serviceListFile string) ([]*Service, error) {
}
return defaultConfigs.Services, nil
}
type Lines struct {
str string
}
func (l *Lines) AddTitle(line string) {
l.str = fmt.Sprintf("%s\n\n%s", l.str, line)
}
func (l *Lines) Add(k, v string) {
if v == "" {
return
}
l.str = fmt.Sprintf("%s\n%s=%s", l.str, k, v)
}
func (l *Lines) Get() string {
return l.str
}