2021-11-12 09:25:44 +00:00
|
|
|
package configui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-11-13 04:59:08 +00:00
|
|
|
"syscall"
|
2021-11-12 09:25:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type State string
|
|
|
|
|
|
|
|
const (
|
|
|
|
READY State = "ready"
|
|
|
|
ERROR State = "error"
|
|
|
|
STARTED State = "started"
|
|
|
|
ENDED State = "ended"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Policy string
|
|
|
|
|
|
|
|
const (
|
|
|
|
NO Policy = "no"
|
|
|
|
ONFAIL Policy = "on-failure"
|
|
|
|
ALWAYS Policy = "always"
|
|
|
|
UNLESSSTOP Policy = "unless-stopped"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Oni struct {
|
2021-11-13 04:59:08 +00:00
|
|
|
Name string `json:"-"`
|
2021-11-12 09:25:44 +00:00
|
|
|
Cmd string `json:"cmd"`
|
|
|
|
Dir string `json:"dir"`
|
|
|
|
Args []string `json:"args"`
|
|
|
|
Envs []string `json:"envs"`
|
|
|
|
Policy Policy `json:"policy"`
|
|
|
|
|
|
|
|
PID int `json:"pid"`
|
|
|
|
LogFile string `json:"log_file"`
|
|
|
|
State State `json:"state"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
ManStop bool `json:"manual_stop"`
|
|
|
|
|
2021-11-12 15:09:32 +00:00
|
|
|
parent *ConfigUI
|
|
|
|
cmd *exec.Cmd
|
|
|
|
running chan struct{}
|
|
|
|
buff bytes.Buffer
|
|
|
|
log io.Writer
|
2021-11-12 09:25:44 +00:00
|
|
|
|
|
|
|
listeners []io.Writer
|
|
|
|
}
|
|
|
|
|
2021-11-13 04:59:08 +00:00
|
|
|
func (oni *Oni) Init(cui *ConfigUI) {
|
2021-11-12 15:09:32 +00:00
|
|
|
oni.parent = cui
|
|
|
|
oni.running = make(chan struct{}, 1)
|
|
|
|
oni.listeners = make([]io.Writer, 0)
|
|
|
|
oni.StateChange(READY)
|
2021-11-13 04:59:08 +00:00
|
|
|
oni.SetLog()
|
2021-11-12 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (oni *Oni) Start() error {
|
|
|
|
if oni.Cmd == "" {
|
|
|
|
return ErrorOniNotValid
|
|
|
|
}
|
|
|
|
select {
|
2021-11-12 15:09:32 +00:00
|
|
|
case oni.running <- struct{}{}:
|
2021-11-12 09:25:44 +00:00
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err != nil {
|
|
|
|
oni.end(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
return ErrorOniHasStarted
|
|
|
|
}
|
2021-11-13 04:59:08 +00:00
|
|
|
var err error
|
2021-11-12 09:25:44 +00:00
|
|
|
cmd := exec.Command(oni.Cmd, oni.Args...)
|
2021-11-15 01:59:10 +00:00
|
|
|
setgpid(cmd)
|
2021-11-12 09:25:44 +00:00
|
|
|
cmd.Env = oni.Envs
|
|
|
|
cmd.Dir = oni.Dir
|
|
|
|
var out io.Writer
|
|
|
|
if oni.LogFile != "" {
|
|
|
|
out = io.MultiWriter(oni.log, &oni.buff)
|
|
|
|
} else {
|
|
|
|
out = &oni.buff
|
|
|
|
}
|
|
|
|
cmd.Stderr = out
|
|
|
|
cmd.Stdout = out
|
|
|
|
oni.cmd = cmd
|
2021-11-13 04:59:08 +00:00
|
|
|
err = oni.cmd.Start()
|
2021-11-12 09:25:44 +00:00
|
|
|
if err != nil {
|
|
|
|
oni.end(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go oni.read()
|
|
|
|
oni.PID = cmd.Process.Pid
|
2021-11-12 15:09:32 +00:00
|
|
|
oni.StateChange(STARTED)
|
2021-11-12 09:25:44 +00:00
|
|
|
go func() {
|
|
|
|
oni.end(cmd.Wait())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oni *Oni) Stop() error {
|
|
|
|
if oni.cmd == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-13 04:59:08 +00:00
|
|
|
return oni.cmd.Process.Signal(syscall.SIGTERM)
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:25:44 +00:00
|
|
|
func (oni *Oni) end(v interface{}) {
|
|
|
|
if v == nil {
|
2021-11-12 15:09:32 +00:00
|
|
|
oni.StateChange(ENDED)
|
2021-11-12 09:25:44 +00:00
|
|
|
} else {
|
|
|
|
oni.Error = fmt.Sprint(v)
|
2021-11-12 15:09:32 +00:00
|
|
|
oni.StateChange(ERROR)
|
2021-11-12 09:25:44 +00:00
|
|
|
}
|
2021-11-13 04:59:08 +00:00
|
|
|
if oni.log != nil {
|
|
|
|
file, ok := oni.log.(*os.File)
|
|
|
|
if ok {
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 15:09:32 +00:00
|
|
|
if len(oni.running) == 1 {
|
|
|
|
<-oni.running
|
2021-11-12 09:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oni *Oni) read() {
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(&oni.buff)
|
|
|
|
for scanner.Scan() {
|
|
|
|
if len(oni.listeners) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
w := io.MultiWriter(oni.listeners...)
|
|
|
|
w.Write([]byte(scanner.Text()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-11-12 15:09:32 +00:00
|
|
|
|
|
|
|
func (oni *Oni) StateChange(state State) {
|
|
|
|
oni.State = state
|
|
|
|
// for i := range oni.parent.Actions {
|
|
|
|
// if oni.parent.Actions[i].Name == oni.Name {
|
|
|
|
// oni.parent.Actions[i].State = state
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
2021-11-13 04:59:08 +00:00
|
|
|
|
|
|
|
func (oni *Oni) SetLog() error {
|
|
|
|
var err error
|
|
|
|
if oni.log != nil {
|
|
|
|
file, ok := oni.log.(*os.File)
|
|
|
|
if ok {
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oni.parent.LogPath != "" {
|
|
|
|
logpath := filepath.Join(filepath.Dir(oni.parent.LogPath), oni.Name+".log")
|
|
|
|
oni.LogFile = logpath
|
|
|
|
oni.log, err = os.OpenFile(logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
oni.end(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|