Merge pull request #947 from wanyaoqi/feature/wyq/host-service-support-pid-file

host: support pid file
This commit is contained in:
yunion-ci-robot
2019-05-28 15:51:18 +08:00
committed by GitHub
3 changed files with 79 additions and 4 deletions
+5 -1
View File
@@ -15,6 +15,7 @@
package main
import (
"yunion.io/x/onecloud/pkg/cloudcommon/service"
"yunion.io/x/onecloud/pkg/hostman"
"yunion.io/x/onecloud/pkg/util/atexit"
)
@@ -22,6 +23,9 @@ import (
func main() {
defer atexit.Handle()
var srv = hostman.SHostService{}
var srv = &hostman.SHostService{}
srv.SServiceBase = &service.SServiceBase{
Service: srv,
}
srv.StartService()
}
+64 -1
View File
@@ -14,4 +14,67 @@
package service
type SServiceBase struct{}
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/util/fileutils2"
"yunion.io/x/onecloud/pkg/util/procutils"
)
type IService interface {
InitService()
RunService()
OnExitService()
}
type SServiceBase struct {
Service IService
O *options.BaseOptions
}
func (s *SServiceBase) StartService() {
defer s.Service.OnExitService()
defer s.RemovePid()
s.Service.InitService()
if err := s.CreatePid(); err != nil {
log.Fatalln(err)
}
s.Service.RunService()
}
func (s *SServiceBase) CreatePid() error {
if s.O == nil || len(s.O.PidFile) == 0 {
return nil
}
absPath, err := filepath.Abs(s.O.PidFile)
if err != nil {
return fmt.Errorf("Get pidfile %s absolute path failed: %s", s.O.PidFile, err)
}
s.O.PidFile = absPath
pidDir := filepath.Dir(s.O.PidFile)
if !fileutils2.Exists(pidDir) {
output, err := procutils.NewCommand("mkdir", "-p", pidDir).Run()
if err != nil {
return fmt.Errorf("Make pid dir %s failed: %s", pidDir, output)
}
}
err = fileutils2.FilePutContents(s.O.PidFile, strconv.Itoa(os.Getpid()), false)
if err != nil {
return fmt.Errorf("Write pidfile %s failed: %s", s.O.PidFile, err)
}
return nil
}
func (s *SServiceBase) RemovePid() {
if s.O != nil && len(s.O.PidFile) > 0 && fileutils2.Exists(s.O.PidFile) {
os.Remove(s.O.PidFile)
}
}
+10 -2
View File
@@ -39,10 +39,10 @@ import (
)
type SHostService struct {
service.SServiceBase
*service.SServiceBase
}
func (host *SHostService) StartService() {
func (host *SHostService) InitService() {
common_options.ParseOptions(&options.HostOptions, os.Args, "host.conf", "host")
isRoot := sysutils.IsRootPermission()
if !isRoot {
@@ -50,6 +50,14 @@ func (host *SHostService) StartService() {
}
options.HostOptions.EnableRbac = false // disable rbac
host.SServiceBase.O = &options.HostOptions.BaseOptions
}
func (host *SHostService) OnExitService() {
// TODO
}
func (host *SHostService) RunService() {
app := app_common.InitApp(&options.HostOptions.BaseOptions, false)
hostInstance := hostinfo.Instance()
if err := hostInstance.Init(); err != nil {