feat/muzan
Evan 2021-11-13 12:59:08 +08:00
parent 2ecac19807
commit f039033077
6 changed files with 196 additions and 59 deletions

View File

@ -77,8 +77,7 @@ type ConfigUI struct {
LogLevel klog.Llevel `json:"log_level"` LogLevel klog.Llevel `json:"log_level"`
// Running commands // Running commands
Onitachi []*Oni `json:"onitachi"` Onitachi map[string]*Oni `json:"onitachi"`
oniIndex map[string]int `json:"-"`
TmplFS embed.FS `json:"-"` TmplFS embed.FS `json:"-"`
tmpl *engine.Engine tmpl *engine.Engine
@ -149,7 +148,7 @@ func New() *ConfigUI {
cmdTimeout: time.Second * 10, cmdTimeout: time.Second * 10,
LogLevel: klog.Lerror | klog.Linfo, LogLevel: klog.Lerror | klog.Linfo,
log: klog.Sub("ConfigUI"), log: klog.Sub("ConfigUI"),
oniIndex: make(map[string]int), Onitachi: make(map[string]*Oni),
} }
} }
@ -170,6 +169,8 @@ func (cui *ConfigUI) File(file_name string) (*File, error) {
} }
func (cui *ConfigUI) LoadConfig(confstr string) error { func (cui *ConfigUI) LoadConfig(confstr string) error {
cui.configLock.Lock()
defer cui.configLock.Unlock()
tmpConf := &ConfigUI{} tmpConf := &ConfigUI{}
err := json.Unmarshal([]byte(confstr), tmpConf) err := json.Unmarshal([]byte(confstr), tmpConf)
if err != nil { if err != nil {
@ -192,31 +193,15 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
} }
} }
// construct oni // del oni dry run
for _, o := range tmpConf.Onitachi { for k, v := range cui.Onitachi {
if o.Cmd == "" { _, ok := tmpConf.Onitachi[k]
continue if !ok && v.State == STARTED {
} return ErrorNoDeleteRunning.New(k)
if o.Name == "" {
o.Name = o.Cmd
}
// update if exist
j, ok := cui.oniIndex[o.Name]
if ok {
cui.Onitachi[j].Cmd = o.Cmd
cui.Onitachi[j].Args = o.Args
cui.Onitachi[j].Envs = o.Envs
cui.Onitachi[j].Dir = o.Dir
cui.Onitachi[j].Policy = o.Policy
} else {
cui.oniIndex[o.Name] = len(cui.Onitachi)
cui.Onitachi = append(cui.Onitachi, o)
} }
} }
// copy // copy
cui.configLock.Lock()
defer cui.configLock.Unlock()
cui.fileIndex = tmpIndex cui.fileIndex = tmpIndex
cui.Files = tmpConf.Files cui.Files = tmpConf.Files
cui.AllowIP = tmpConf.AllowIP cui.AllowIP = tmpConf.AllowIP
@ -228,6 +213,39 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
cui.ResultBellow = tmpConf.ResultBellow cui.ResultBellow = tmpConf.ResultBellow
cui.SHELL = tmpConf.SHELL cui.SHELL = tmpConf.SHELL
cui.log = klog.Sub(cui.AppName)
cui.LogLevel = tmpConf.LogLevel
if cui.LogLevel == 0 {
cui.LogLevel = klog.Lerror | klog.Linfo
}
klog.LEVEL = cui.LogLevel
cui.LogPath = tmpConf.LogPath
cui.setLog()
// construct oni - update existing, del
for k, v := range cui.Onitachi {
n, ok := tmpConf.Onitachi[k]
if !ok {
delete(cui.Onitachi, k)
} else {
v.Name = k
v.Cmd = n.Cmd
v.Args = n.Args
v.Envs = n.Envs
v.Dir = n.Dir
v.Policy = n.Policy
}
}
// construct oni - append new
for k, v := range tmpConf.Onitachi {
_, ok := cui.Onitachi[k]
if !ok {
v.Name = k
v.Init(cui)
cui.Onitachi[k] = v
}
}
cui.Actions = tmpConf.Actions cui.Actions = tmpConf.Actions
for i := range cui.Actions { for i := range cui.Actions {
if cui.Actions[i].Name == "" { if cui.Actions[i].Name == "" {
@ -254,15 +272,6 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
cui.cmdTimeout = ct cui.cmdTimeout = ct
} }
cui.log = klog.Sub(cui.AppName)
cui.LogLevel = tmpConf.LogLevel
if cui.LogLevel == 0 {
cui.LogLevel = klog.Lerror | klog.Linfo
}
klog.LEVEL = cui.LogLevel
cui.LogPath = tmpConf.LogPath
cui.setLog()
// fmt.Printf("%+v", cui) // fmt.Printf("%+v", cui)
return nil return nil
} }

View File

@ -6,6 +6,12 @@ import (
"kumoly.io/lib/ksrv" "kumoly.io/lib/ksrv"
) )
var ErrorServerReloading = ksrv.Error{
Code: http.StatusServiceUnavailable,
ID: "ErrorServerReloading",
Message: "server is reloading",
}
var ErrorOniHasNoPID = ksrv.Error{ var ErrorOniHasNoPID = ksrv.Error{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
ID: "ErrorOniHasNoPID", ID: "ErrorOniHasNoPID",
@ -21,7 +27,7 @@ var ErrorOniNotStarted = ksrv.Error{
var ErrorOniHasStarted = ksrv.Error{ var ErrorOniHasStarted = ksrv.Error{
Code: http.StatusConflict, Code: http.StatusConflict,
ID: "ErrorOniHasStarted", ID: "ErrorOniHasStarted",
Message: "oni has started", Tmpl: "%s has started",
} }
var ErrorOniNotFound = ksrv.Error{ var ErrorOniNotFound = ksrv.Error{
@ -35,3 +41,15 @@ var ErrorOniNotValid = ksrv.Error{
ID: "ErrorOniNotValid", ID: "ErrorOniNotValid",
Message: "oni no name or cmd", Message: "oni no name or cmd",
} }
var ErrorNoDeleteRunning = ksrv.Error{
Code: http.StatusBadRequest,
ID: "ErrorNoDeleteRunning",
Tmpl: "cannot delete running: %s",
}
var ErrorOniNoLog = ksrv.Error{
Code: http.StatusNotFound,
ID: "ErrorOniNoLog",
Tmpl: "%s has no logs",
}

View File

@ -266,26 +266,24 @@ func (cui *ConfigUI) GetOni(w http.ResponseWriter, r *http.Request) {
ksrv.JSON(w, cui.Onitachi) ksrv.JSON(w, cui.Onitachi)
return return
} }
i, ok := cui.oniIndex[name] oni, ok := cui.Onitachi[name]
if !ok { if !ok {
panic(ErrorOniNotFound.New(name)) panic(ErrorOniNotFound.New(name))
} }
oni := cui.Onitachi[i]
ksrv.JSON(w, oni) ksrv.JSON(w, oni)
} }
func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
i, ok := cui.oniIndex[name] oni, ok := cui.Onitachi[name]
if !ok { if !ok {
panic(ErrorOniNotFound.New(name)) panic(ErrorOniNotFound.New(name))
} }
oni := cui.Onitachi[i]
switch oni.State { switch oni.State {
case "": case "":
oni.Init(cui) oni.Init(cui)
case STARTED: case STARTED:
panic(ErrorOniHasStarted) panic(ErrorOniHasStarted.New(name))
} }
err := oni.Start() err := oni.Start()
if err != nil { if err != nil {
@ -296,17 +294,64 @@ func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) {
func (cui *ConfigUI) OniStop(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) OniStop(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
i, ok := cui.oniIndex[name] oni, ok := cui.Onitachi[name]
if !ok { if !ok {
panic(ErrorOniNotFound.New(name)) panic(ErrorOniNotFound.New(name))
} }
oni := cui.Onitachi[i]
switch oni.State { switch oni.State {
case STARTED: case STARTED:
oni.Stop() err := oni.Stop()
if err != nil {
panic(err)
}
default: default:
panic(ErrorOniNotStarted.New(name)) panic(ErrorOniNotStarted.New(name))
} }
oni.Start()
w.Write([]byte("ok")) w.Write([]byte("ok"))
} }
func (cui *ConfigUI) OniLog(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
oni, ok := cui.Onitachi[name]
if !ok {
panic(ErrorOniNotFound.New(name))
}
if oni.LogFile == "" {
panic(ErrorOniNoLog.New(name))
}
stat, err := os.Stat(oni.LogFile)
if err != nil {
panic(err)
}
data, err := os.ReadFile(oni.LogFile)
if err != nil {
panic(err)
}
ksrv.JSON(w, map[string]string{
"name": name,
"data": string(data),
"delta": strconv.Itoa(int(stat.Size())),
})
}
func (cui *ConfigUI) OniKill(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
oni, ok := cui.Onitachi[name]
if !ok {
panic(ErrorOniNotFound.New(name))
}
switch oni.State {
case STARTED:
err := oni.Kill()
if err != nil {
panic(err)
}
default:
panic(ErrorOniNotStarted.New(name))
}
w.Write([]byte("ok"))
}
func (cui *ConfigUI) OniAttach(w http.ResponseWriter, r *http.Request) {
}

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"syscall"
) )
type State string type State string
@ -29,7 +30,7 @@ const (
) )
type Oni struct { type Oni struct {
Name string `json:"name"` Name string `json:"-"`
Cmd string `json:"cmd"` Cmd string `json:"cmd"`
Dir string `json:"dir"` Dir string `json:"dir"`
Args []string `json:"args"` Args []string `json:"args"`
@ -51,21 +52,12 @@ type Oni struct {
listeners []io.Writer listeners []io.Writer
} }
func (oni *Oni) Init(cui *ConfigUI) error { func (oni *Oni) Init(cui *ConfigUI) {
var err error
oni.parent = cui oni.parent = cui
oni.running = make(chan struct{}, 1) oni.running = make(chan struct{}, 1)
oni.listeners = make([]io.Writer, 0) oni.listeners = make([]io.Writer, 0)
oni.StateChange(READY) oni.StateChange(READY)
if cui.LogPath != "" { oni.SetLog()
logpath := filepath.Join(filepath.Dir(cui.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 {
return err
}
}
return err
} }
func (oni *Oni) Start() error { func (oni *Oni) Start() error {
@ -83,7 +75,9 @@ func (oni *Oni) Start() error {
default: default:
return ErrorOniHasStarted return ErrorOniHasStarted
} }
var err error
cmd := exec.Command(oni.Cmd, oni.Args...) cmd := exec.Command(oni.Cmd, oni.Args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Env = oni.Envs cmd.Env = oni.Envs
cmd.Dir = oni.Dir cmd.Dir = oni.Dir
var out io.Writer var out io.Writer
@ -95,7 +89,7 @@ func (oni *Oni) Start() error {
cmd.Stderr = out cmd.Stderr = out
cmd.Stdout = out cmd.Stdout = out
oni.cmd = cmd oni.cmd = cmd
err := oni.cmd.Start() err = oni.cmd.Start()
if err != nil { if err != nil {
oni.end(err) oni.end(err)
return err return err
@ -114,7 +108,19 @@ func (oni *Oni) Stop() error {
if oni.cmd == nil { if oni.cmd == nil {
return nil return nil
} }
return oni.cmd.Process.Signal(syscall.SIGTERM)
}
func (oni *Oni) Kill() error {
if oni.cmd == nil {
return nil
}
pgid, err := syscall.Getpgid(oni.cmd.Process.Pid)
if err == nil {
return syscall.Kill(-pgid, 15) // note the minus sign
}
return oni.cmd.Process.Kill() return oni.cmd.Process.Kill()
// return oni.cmd.Process.Kill()
} }
func (oni *Oni) end(v interface{}) { func (oni *Oni) end(v interface{}) {
@ -124,6 +130,12 @@ func (oni *Oni) end(v interface{}) {
oni.Error = fmt.Sprint(v) oni.Error = fmt.Sprint(v)
oni.StateChange(ERROR) oni.StateChange(ERROR)
} }
if oni.log != nil {
file, ok := oni.log.(*os.File)
if ok {
file.Close()
}
}
if len(oni.running) == 1 { if len(oni.running) == 1 {
<-oni.running <-oni.running
} }
@ -150,3 +162,23 @@ func (oni *Oni) StateChange(state State) {
// } // }
// } // }
} }
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
}

View File

@ -31,6 +31,9 @@ func (cui *ConfigUI) middleware(next http.Handler) http.Handler {
panic("permission denied") panic("permission denied")
} }
} }
if MutexLocked(&cui.configLock) {
panic(ErrorServerReloading)
}
next.ServeHTTP(rw, r) next.ServeHTTP(rw, r)
}), }),
) )
@ -122,7 +125,28 @@ func (cui *ConfigUI) mux() *http.ServeMux {
}) })
cuiR.HandleFunc("/api/oni/stop", func(w http.ResponseWriter, r *http.Request) { cuiR.HandleFunc("/api/oni/stop", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
cui.OniStart(w, r) cui.OniStop(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/kill", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniKill(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/log", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
cui.OniLog(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/attach", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
cui.OniAttach(w, r)
} else { } else {
w.WriteHeader(404) w.WriteHeader(404)
} }

View File

@ -6,7 +6,9 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"sync"
) )
func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error { func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error {
@ -81,3 +83,10 @@ func mkdir(args ...interface{}) error {
} }
return os.MkdirAll(path, mode) return os.MkdirAll(path, mode)
} }
const mutexLocked = 1
func MutexLocked(m *sync.Mutex) bool {
state := reflect.ValueOf(m).Elem().FieldByName("state")
return state.Int()&mutexLocked == mutexLocked
}