diff --git a/bin/boot.sh b/bin/boot.sh index 6ace07b7..73bb408f 100755 --- a/bin/boot.sh +++ b/bin/boot.sh @@ -33,7 +33,7 @@ install() { } main() { while true; do - ${ctx_bin} "$@" 2>${ctx_log}/boot.log && break + ${ctx_bin} "$@" && break log "restarting..." && sleep 3 done } diff --git a/src/contexts/aaa/aaa.go b/src/contexts/aaa/aaa.go index 7031ea17..33457375 100644 --- a/src/contexts/aaa/aaa.go +++ b/src/contexts/aaa/aaa.go @@ -136,8 +136,6 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", "_init": &ctx.Command{Name: "_init", Help: "数字摘要", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Conf("runtime", "node.cert", m.Cmdx("nfs.load", os.Getenv("node_cert"))) m.Conf("runtime", "node.key", m.Cmdx("nfs.load", os.Getenv("node_key"))) - m.Conf("runtime", "user.cert", m.Cmdx("nfs.load", os.Getenv("user_cert"))) - m.Conf("runtime", "user.key", m.Cmdx("nfs.load", os.Getenv("user_key"))) return }}, "hash": &ctx.Command{Name: "hash [meta...]", Help: "数字摘要", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { @@ -794,8 +792,8 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", // 生成证书 template := x509.Certificate{ - SerialNumber: big.NewInt(1), - IsCA: true, + SerialNumber: big.NewInt(1), + IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign, Subject: pkix.Name{CommonName: kit.Format(common)}, diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 12c252f0..25ddb746 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -106,9 +106,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "ctx_box", "ctx_cas", "ctx_dev", "ctx_root", "ctx_home", "web_port", "ssh_port", - "HOSTNAME", "USER", "PWD", }, - "boot": map[string]interface{}{ + "boot": map[string]interface{}{ "web_port": ":9094", "ssh_port": ":9090", }, @@ -213,7 +212,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Conf("runtime", "boot.username", kit.Select(name, os.Getenv("USER"))) } if name, e := os.Getwd(); e == nil { - _, file := path.Split(name) + _, file := path.Split(kit.Select(name, os.Getenv("PWD"))) m.Conf("runtime", "boot.pathname", file) } return @@ -536,7 +535,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", p := m.Cmdx("nfs.path", m.Conf("compile", "bench")) if m.Cmdy("cli.system", env, "go", "build", "-o", path.Join(m.Conf("publish", "path"), name), p); m.Result(0) == "" { m.Append("bin", name) - m.Append("hash",kit.Hashs(p)) + m.Append("hash", kit.Hashs(p)) m.Table() } } @@ -583,12 +582,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } return }}, - "exit": &ctx.Command{Name: "exit", Help: "解析脚本, script: 脚本文件, stdio: 命令终端, snippet: 代码片段", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - m.Confm("daemon", func(key string, info map[string]interface{}) { - m.Cmd("cli.system", key, "stop") - }) - return - }}, "quit": &ctx.Command{Name: "quit code", Help: "停止服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Cmd("cli.source", m.Conf("system", "script.exit")) m.Append("directory", "") @@ -600,6 +593,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }) return }}, + "_exit": &ctx.Command{Name: "_exit", Help: "解析脚本, script: 脚本文件, stdio: 命令终端, snippet: 代码片段", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + m.Confm("daemon", func(key string, info map[string]interface{}) { + m.Cmd("cli.system", key, "stop") + }) + return + }}, "plugin": &ctx.Command{Name: "plugin [action] file", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { suffix, action, target := "go", "build", path.Join(m.Conf("runtime", "boot.ctx_home"), "src/examples/app/bench.go") diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index 0e0917bd..ab78373d 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -62,9 +62,6 @@ type Context struct { } func (c *Context) Register(s *Context, x Server, args ...interface{}) { - if c.contexts == nil { - c.contexts = make(map[string]*Context) - } force := false if len(args) > 0 { switch arg := args[0].(type) { @@ -72,6 +69,10 @@ func (c *Context) Register(s *Context, x Server, args ...interface{}) { force = arg } } + + if c.contexts == nil { + c.contexts = make(map[string]*Context) + } if x, ok := c.contexts[s.Name]; ok && !force { panic(errors.New(c.Name + "上下文中已存在模块:" + x.Name)) } @@ -114,12 +115,12 @@ func (c *Context) Begin(m *Message, arg ...string) *Context { c.exit = make(chan bool, 3) /* - m.Log("begin", "%d context %v %v", m.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"]) - for k, x := range c.Configs { - if x.Hand != nil { - m.Log("begin", "%s config %v", k, m.Conf(k, x.Value)) + m.Log("begin", "%d context %v %v", m.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"]) + for k, x := range c.Configs { + if x.Hand != nil { + m.Log("begin", "%s config %v", k, m.Conf(k, x.Value)) + } } - } */ if c.Server != nil { diff --git a/src/contexts/ctx/init.go b/src/contexts/ctx/init.go index 54114d1d..ca3b8a1c 100644 --- a/src/contexts/ctx/init.go +++ b/src/contexts/ctx/init.go @@ -3,7 +3,6 @@ package ctx import ( "encoding/json" "fmt" - "runtime" "strconv" "strings" @@ -22,18 +21,15 @@ func (ctx *CTX) Spawn(m *Message, c *Context, arg ...string) Server { return s } func (ctx *CTX) Begin(m *Message, arg ...string) Server { - m.Sess(m.target.Name, m) - m.target.root = m.target - m.root = m - m.Cap("begin_time", m.Time()) - m.Cap("goos", runtime.GOOS) m.Option("log.disable", true) m.Option("page.limit", 10) m.Option("page.offset", 0) m.Option("routine", 0) + + m.root = m + m.Sess(m.target.Name, m) for _, msg := range m.Search("") { - msg.target.root = m.target - if msg.target == m.target { + if msg.target.root = m.target; msg.target == m.target { continue } msg.target.Begin(msg, arg...) @@ -46,13 +42,17 @@ func (ctx *CTX) Start(m *Message, arg ...string) bool { if m.Optionv("ps_target", Index); len(arg) == 0 { Pulse.Option("log.disable", false) m.Cap("stream", "shy") + m.Cmd("log._init") + m.Cmd("gdb._init") + m.Cmd("yac._init") + return false m.Cmd("cli.source", m.Conf("system", "script.init")).Cmd("cli.source", "stdio").Cmd("cli.source", m.Conf("system", "script.exit")) } else { for _, v := range m.Spawn().Cmd(arg).Meta["result"] { fmt.Printf("%s", v) } } - m.Cmd("ctx.exit") + m.Cmd("ctx._exit") return true } func (ctx *CTX) Close(m *Message, arg ...string) bool { @@ -102,14 +102,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, }, Commands: map[string]*Command{ "_init": &Command{Name: "_init", Help: "启动", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { - for _, x := range []string{"cli", "yac", "nfs", "aaa", "log", "ssh", "web", "gdb"} { + for _, x := range []string{"cli", "nfs", "aaa", "ssh", "web"} { m.Cmd(x + "._init") } return }}, - "exit": &Command{Name: "exit", Help: "启动", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { + "_exit": &Command{Name: "_exit", Help: "启动", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { for _, x := range []string{"cli"} { - m.Cmd(x + ".exit") + m.Cmd(x + "._exit") } return }}, diff --git a/src/contexts/gdb/gdb.go b/src/contexts/gdb/gdb.go index 448ac643..cf976538 100644 --- a/src/contexts/gdb/gdb.go +++ b/src/contexts/gdb/gdb.go @@ -80,11 +80,12 @@ func (gdb *GDB) Begin(m *ctx.Message, arg ...string) ctx.Server { return gdb } func (gdb *GDB) Start(m *ctx.Message, arg ...string) bool { - gdb.goon = make(chan os.Signal, 10) + m.Cmd("nfs.save", m.Conf("logpid"), os.Getpid()) gdb.wait = make(chan interface{}, 10) + gdb.goon = make(chan os.Signal, 10) m.Confm("signal", func(sig string, action string) { - m.Log("error", "signal add %s %s", sig, action) + m.Log("signal", "add %s: %s", action, sig) signal.Notify(gdb.goon, syscall.Signal(kit.Int(sig))) }) @@ -92,7 +93,8 @@ func (gdb *GDB) Start(m *ctx.Message, arg ...string) bool { select { case sig := <-gdb.goon: action := m.Conf("signal", sig) - m.Log("signal", "signal %v %v", sig, action) + m.Log("signal", "%v: %v", action, sig) + break switch action { case "segv": case "quit": @@ -119,11 +121,43 @@ func (gdb *GDB) Close(m *ctx.Message, arg ...string) bool { var Index = &ctx.Context{Name: "gdb", Help: "调试中心", Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{ + "logpid": &ctx.Config{Name: "logpid", Value: "var/run/bench.pid", Help: ""}, "signal": &ctx.Config{Name: "signal", Value: map[string]interface{}{ - "3": "quit", - "10": "restart", - "11": "segv", - "12": "upgrade", + "1": "HUP", + "2": "INT", + "3": "QUIT", + "15": "TERM", + "28": "WINCH", + "30": "USR1", + "31": "USR2", + + // "9": "KILL", + // "10": "BUS", + // "11": "SEGV", + // "17": "STOP", + + "5": "TRAP", + "6": "ABRT", + "14": "ALRM", + "20": "CHLD", + "19": "CONT", + "18": "TSTP", + "21": "TTIN", + "22": "TTOUT", + + "13": "PIPE", + "16": "URG", + "23": "IO", + + "4": "ILL", + "7": "EMT", + "8": "FPE", + "12": "SYS", + "24": "XCPU", + "25": "XFSZ", + "26": "VTALRM", + "27": "PROF", + "29": "INFO", }, Help: "信号"}, "debug": &ctx.Config{Name: "debug", Value: map[string]interface{}{"value": map[string]interface{}{"enable": false}, "trace": map[string]interface{}{"value": map[string]interface{}{"enable": true}}, diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index 35289ab4..a901ec24 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -2,7 +2,6 @@ package log import ( "contexts/ctx" - "io/ioutil" "path" "toolkit" @@ -61,9 +60,6 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool { os.MkdirAll(m.Conf("logdir"), 0770) kit.Log("error", "make log dir %s", m.Conf("logdir")) - ioutil.WriteFile(m.Conf("logpid"), []byte(kit.Format(os.Getpid())), 0666) - kit.Log("error", "save log file %s", m.Conf("logpid")) - log.queue = make(chan map[string]interface{}, 1024) for _, v := range []string{"error", "bench", "debug"} { log.Log(m, v, "hello world\n") @@ -122,7 +118,6 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心", }, Configs: map[string]*ctx.Config{ "logdir": &ctx.Config{Name: "logdir", Value: "var/log", Help: ""}, - "logpid": &ctx.Config{Name: "logpid", Value: "var/run/bench.pid", Help: ""}, "output": &ctx.Config{Name: "output", Value: map[string]interface{}{ "error": map[string]interface{}{"value": map[string]interface{}{"file": "error.log", "meta": []interface{}{"time", "ship"}, "color_begin": "\033[31m", "color_end": "\033[0m"}}, "trace": map[string]interface{}{"value": map[string]interface{}{"file": "error.log", "meta": []interface{}{"time", "ship"}, "color_begin": "\033[32m", "color_end": "\033[0m"}}, diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index 3a3d2c77..2b7a606a 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -865,7 +865,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 && arg[0] == "scan" { // 终端用户 - m.Cmd("aaa.role", "root", "user", m.Option("username", m.Conf("runtime", "boot.USER"))) + m.Cmd("aaa.role", "root", "user", m.Option("username", m.Conf("runtime", "boot.username"))) // 创建会话 m.Option("sessid", m.Cmdx("aaa.user", "session", "select")) @@ -918,7 +918,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { msg := m.Backs(m.Spawn(m.Source()).Set( "detail", line).Set( "option", "file_pos", i).Set( - "option", "username", m.Conf("runtime", "boot.USER"))) + "option", "username", m.Conf("runtime", "boot.username"))) nfs.printf(m.Conf("prompt"), line) nfs.printf(msg.Meta["result"]) @@ -1369,16 +1369,21 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", if len(arg) == 1 && m.Has("data") { arg = append(arg, m.Option("data")) } - if p, f, e := open(m, kit.Format(arg[0]), os.O_WRONLY|os.O_CREATE|os.O_TRUNC); m.Assert(e) { - defer f.Close() - m.Append("directory", p) - m.Echo(p) + dir := path.Dir(arg[0]) + if _, e = os.Stat(dir); e != nil { + m.Assert(os.MkdirAll(dir, 0777)) + } + + if f, e := os.Create(arg[0]); m.Assert(e) { + defer f.Close() for _, v := range arg[1:] { n, e := fmt.Fprint(f, v) m.Assert(e) - m.Log("info", "save %s %d", p, n) + m.Log("info", "save %s %d", arg[0], n) } + m.Append("directory", arg[0]) + m.Echo(arg[0]) } return }}, diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index f0750da5..f005c351 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -204,7 +204,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", m.Conf("runtime", "node.name", strings.Replace(strings.TrimSuffix(m.Conf("runtime", "boot.hostname"), ".local"), ".", "_", -1)) } m.Conf("runtime", "node.route", m.Conf("runtime", "node.name")) - m.Conf("runtime", "user.name", m.Conf("runtime", "boot.USER")) + m.Conf("runtime", "user.name", m.Conf("runtime", "boot.username")) m.Cmd("aaa.role", "tech", "componet", "remote", "command", "tool") m.Cmd("aaa.role", "tech", "componet", "source", "command", "tool") diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 453a880b..88e9f694 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -486,7 +486,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } if arg[2] == "new" { - if uri, e := url.Parse(arg[3]); e == nil { + if uri, e := url.Parse(arg[3]); e == nil && arg[3] != "" { dir, file := path.Split(uri.EscapedPath()) m.Confv("spide", arg[0], map[string]interface{}{ "cookie": map[string]interface{}{}, @@ -1154,12 +1154,12 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", "/publish/": &ctx.Command{Name: "/publish/", Help: "下载文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { key = strings.TrimPrefix(key, "/publish/") if strings.HasSuffix(key, "bench") { - key = key+"."+m.Option("GOOS")+"."+m.Option("GOARCH") + key = key + "." + m.Option("GOOS") + "." + m.Option("GOARCH") } p := m.Cmdx("nfs.path", path.Join(m.Conf("publish", "path"), key)) if p == "" { - p = m.Cmdx("nfs.path", m.Conf("publish", []string{"list", strings.Replace(key, ".", "_", -1) })) + p = m.Cmdx("nfs.path", m.Conf("publish", []string{"list", strings.Replace(key, ".", "_", -1)})) } m.Log("info", "publish %s %s", kit.Hashs(p), p) diff --git a/src/toolkit/kit.go b/src/toolkit/kit.go index 34cec12f..568299d0 100644 --- a/src/toolkit/kit.go +++ b/src/toolkit/kit.go @@ -17,23 +17,22 @@ import ( ) var DisableLog = false -func Errorf(str string, args ...interface{}) { - if len(args) == 0 { - fmt.Fprintf(os.Stderr, "%s\n", str) - return - } - fmt.Fprintf(os.Stderr, str, args...) -} + func Log(action string, str string, args ...interface{}) { if DisableLog { return } - if len(args) == 0 { - fmt.Fprintf(os.Stderr, "%s", str) - } else { - fmt.Fprintf(os.Stderr, str, args...) + + if len(args) > 0 { + str = fmt.Sprintf(str, args...) } - fmt.Fprintln(os.Stderr) + fmt.Fprintf(os.Stderr, "%s: %s\n", action, str) +} +func Errorf(str string, args ...interface{}) { + Log("error", str, args...) +} +func Debugf(str string, args ...interface{}) { + Log("debug", str, args...) } func Env(key string) {