overflow_probes/manager/container/container.go
crusader 47d4a0b57d ing
2017-12-05 19:02:58 +09:00

107 lines
2.1 KiB
Go

package container
import (
"fmt"
"os"
"os/exec"
"time"
"git.loafle.net/commons_go/logging"
crc "git.loafle.net/commons_go/rpc/client"
oopmcc "git.loafle.net/overflow/overflow_probes/manager/container/client"
uuid "github.com/satori/go.uuid"
)
type ContainerManager interface {
GetClient(name string) crc.Client
}
type containerManager struct {
containerClients map[string]*containerState
}
type containerState struct {
socketName string
pid int
client crc.Client
}
func (cm *containerManager) GetClient(name string) crc.Client {
return nil
}
func (cm *containerManager) CheckClient(name string) bool {
cs, ok := cm.containerClients[name]
if !ok || nil == cs || nil == cs.client {
return false
}
stateOk := false
if err := cs.client.Call(&stateOk, "StateService.State"); nil != err {
logging.Logger().Error(fmt.Sprintf("Probe: Call[%s(%s)] err %v", name, "StateService.State", err))
return false
}
return stateOk
}
func (cm *containerManager) ConnectClient(name string) error {
return nil
}
func (cm *containerManager) CloseClient(name string) error {
return nil
}
func (cm *containerManager) runProcess(name string) error {
sockFile := uuid.NewV4().String()
sockArg := fmt.Sprintf("-sock=\"%s\"", sockFile)
cmd := exec.Command("", sockArg)
if err := cmd.Start(); nil != err {
logging.Logger().Error(fmt.Sprintf("Probe: To run container(%s) failed err %v", name, err))
return err
}
time.Sleep(time.Duration(time.Second * 2))
cs := &containerState{
socketName: sockFile,
pid: cmd.Process.Pid,
}
cs.client = oopmcc.New(sockFile, nil)
// write pid file
cm.containerClients[name] = cs
return nil
}
func (cm *containerManager) stopProcess(name string) error {
cs, ok := cm.containerClients[name]
if !ok || nil == cs || nil == cs.client {
return fmt.Errorf("Probe: Container[%s] is not exist", name)
}
p, err := os.FindProcess(cs.pid)
if nil != err {
return err
}
if err = p.Kill(); nil != err {
return err
}
// remove pid file
return nil
}
// func (cm *containerManager) {
// }
// func (cm *containerManager) {
// }
// func (cm *containerManager) {
// }