1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

opt _init

This commit is contained in:
shaoying 2019-06-16 01:25:04 +08:00
parent 220c1e7aac
commit 607635523a
11 changed files with 101 additions and 70 deletions

View File

@ -33,7 +33,7 @@ install() {
} }
main() { main() {
while true; do while true; do
${ctx_bin} "$@" 2>${ctx_log}/boot.log && break ${ctx_bin} "$@" && break
log "restarting..." && sleep 3 log "restarting..." && sleep 3
done done
} }

View File

@ -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) { "_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.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", "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 return
}}, }},
"hash": &ctx.Command{Name: "hash [meta...]", Help: "数字摘要", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { "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{ template := x509.Certificate{
SerialNumber: big.NewInt(1), SerialNumber: big.NewInt(1),
IsCA: true, IsCA: true,
BasicConstraintsValid: true, BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageCertSign, KeyUsage: x509.KeyUsageCertSign,
Subject: pkix.Name{CommonName: kit.Format(common)}, Subject: pkix.Name{CommonName: kit.Format(common)},

View File

@ -106,9 +106,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"ctx_box", "ctx_cas", "ctx_dev", "ctx_box", "ctx_cas", "ctx_dev",
"ctx_root", "ctx_home", "ctx_root", "ctx_home",
"web_port", "ssh_port", "web_port", "ssh_port",
"HOSTNAME", "USER", "PWD",
}, },
"boot": map[string]interface{}{ "boot": map[string]interface{}{
"web_port": ":9094", "web_port": ":9094",
"ssh_port": ":9090", "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"))) m.Conf("runtime", "boot.username", kit.Select(name, os.Getenv("USER")))
} }
if name, e := os.Getwd(); e == nil { 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) m.Conf("runtime", "boot.pathname", file)
} }
return return
@ -536,7 +535,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
p := m.Cmdx("nfs.path", m.Conf("compile", "bench")) 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) == "" { 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("bin", name)
m.Append("hash",kit.Hashs(p)) m.Append("hash", kit.Hashs(p))
m.Table() m.Table()
} }
} }
@ -583,12 +582,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
return 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) { "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.Cmd("cli.source", m.Conf("system", "script.exit"))
m.Append("directory", "") m.Append("directory", "")
@ -600,6 +593,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
}) })
return 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) { "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") suffix, action, target := "go", "build", path.Join(m.Conf("runtime", "boot.ctx_home"), "src/examples/app/bench.go")

View File

@ -62,9 +62,6 @@ type Context struct {
} }
func (c *Context) Register(s *Context, x Server, args ...interface{}) { func (c *Context) Register(s *Context, x Server, args ...interface{}) {
if c.contexts == nil {
c.contexts = make(map[string]*Context)
}
force := false force := false
if len(args) > 0 { if len(args) > 0 {
switch arg := args[0].(type) { switch arg := args[0].(type) {
@ -72,6 +69,10 @@ func (c *Context) Register(s *Context, x Server, args ...interface{}) {
force = arg force = arg
} }
} }
if c.contexts == nil {
c.contexts = make(map[string]*Context)
}
if x, ok := c.contexts[s.Name]; ok && !force { if x, ok := c.contexts[s.Name]; ok && !force {
panic(errors.New(c.Name + "上下文中已存在模块:" + x.Name)) 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) c.exit = make(chan bool, 3)
/* /*
m.Log("begin", "%d context %v %v", m.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"]) m.Log("begin", "%d context %v %v", m.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"])
for k, x := range c.Configs { for k, x := range c.Configs {
if x.Hand != nil { if x.Hand != nil {
m.Log("begin", "%s config %v", k, m.Conf(k, x.Value)) m.Log("begin", "%s config %v", k, m.Conf(k, x.Value))
}
} }
}
*/ */
if c.Server != nil { if c.Server != nil {

View File

@ -3,7 +3,6 @@ package ctx
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"runtime"
"strconv" "strconv"
"strings" "strings"
@ -22,18 +21,15 @@ func (ctx *CTX) Spawn(m *Message, c *Context, arg ...string) Server {
return s return s
} }
func (ctx *CTX) Begin(m *Message, arg ...string) Server { 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("log.disable", true)
m.Option("page.limit", 10) m.Option("page.limit", 10)
m.Option("page.offset", 0) m.Option("page.offset", 0)
m.Option("routine", 0) m.Option("routine", 0)
m.root = m
m.Sess(m.target.Name, m)
for _, msg := range m.Search("") { for _, msg := range m.Search("") {
msg.target.root = m.target if msg.target.root = m.target; msg.target == m.target {
if msg.target == m.target {
continue continue
} }
msg.target.Begin(msg, arg...) 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 { if m.Optionv("ps_target", Index); len(arg) == 0 {
Pulse.Option("log.disable", false) Pulse.Option("log.disable", false)
m.Cap("stream", "shy") 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")) m.Cmd("cli.source", m.Conf("system", "script.init")).Cmd("cli.source", "stdio").Cmd("cli.source", m.Conf("system", "script.exit"))
} else { } else {
for _, v := range m.Spawn().Cmd(arg).Meta["result"] { for _, v := range m.Spawn().Cmd(arg).Meta["result"] {
fmt.Printf("%s", v) fmt.Printf("%s", v)
} }
} }
m.Cmd("ctx.exit") m.Cmd("ctx._exit")
return true return true
} }
func (ctx *CTX) Close(m *Message, arg ...string) bool { 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{ Commands: map[string]*Command{
"_init": &Command{Name: "_init", Help: "启动", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { "_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") m.Cmd(x + "._init")
} }
return 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"} { for _, x := range []string{"cli"} {
m.Cmd(x + ".exit") m.Cmd(x + "._exit")
} }
return return
}}, }},

View File

@ -80,11 +80,12 @@ func (gdb *GDB) Begin(m *ctx.Message, arg ...string) ctx.Server {
return gdb return gdb
} }
func (gdb *GDB) Start(m *ctx.Message, arg ...string) bool { 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.wait = make(chan interface{}, 10)
gdb.goon = make(chan os.Signal, 10)
m.Confm("signal", func(sig string, action string) { 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))) signal.Notify(gdb.goon, syscall.Signal(kit.Int(sig)))
}) })
@ -92,7 +93,8 @@ func (gdb *GDB) Start(m *ctx.Message, arg ...string) bool {
select { select {
case sig := <-gdb.goon: case sig := <-gdb.goon:
action := m.Conf("signal", sig) action := m.Conf("signal", sig)
m.Log("signal", "signal %v %v", sig, action) m.Log("signal", "%v: %v", action, sig)
break
switch action { switch action {
case "segv": case "segv":
case "quit": case "quit":
@ -119,11 +121,43 @@ func (gdb *GDB) Close(m *ctx.Message, arg ...string) bool {
var Index = &ctx.Context{Name: "gdb", Help: "调试中心", var Index = &ctx.Context{Name: "gdb", Help: "调试中心",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{ 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{}{ "signal": &ctx.Config{Name: "signal", Value: map[string]interface{}{
"3": "quit", "1": "HUP",
"10": "restart", "2": "INT",
"11": "segv", "3": "QUIT",
"12": "upgrade", "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: "信号"}, }, Help: "信号"},
"debug": &ctx.Config{Name: "debug", Value: map[string]interface{}{"value": map[string]interface{}{"enable": false}, "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}}, "trace": map[string]interface{}{"value": map[string]interface{}{"enable": true}},

View File

@ -2,7 +2,6 @@ package log
import ( import (
"contexts/ctx" "contexts/ctx"
"io/ioutil"
"path" "path"
"toolkit" "toolkit"
@ -61,9 +60,6 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
os.MkdirAll(m.Conf("logdir"), 0770) os.MkdirAll(m.Conf("logdir"), 0770)
kit.Log("error", "make log dir %s", m.Conf("logdir")) 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) log.queue = make(chan map[string]interface{}, 1024)
for _, v := range []string{"error", "bench", "debug"} { for _, v := range []string{"error", "bench", "debug"} {
log.Log(m, v, "hello world\n") log.Log(m, v, "hello world\n")
@ -122,7 +118,6 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
}, },
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"logdir": &ctx.Config{Name: "logdir", Value: "var/log", Help: ""}, "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{}{ "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"}}, "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"}}, "trace": map[string]interface{}{"value": map[string]interface{}{"file": "error.log", "meta": []interface{}{"time", "ship"}, "color_begin": "\033[32m", "color_end": "\033[0m"}},

View File

@ -865,7 +865,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
if len(arg) > 0 && arg[0] == "scan" { 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")) 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( msg := m.Backs(m.Spawn(m.Source()).Set(
"detail", line).Set( "detail", line).Set(
"option", "file_pos", i).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(m.Conf("prompt"), line)
nfs.printf(msg.Meta["result"]) nfs.printf(msg.Meta["result"])
@ -1369,16 +1369,21 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
if len(arg) == 1 && m.Has("data") { if len(arg) == 1 && m.Has("data") {
arg = append(arg, m.Option("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:] { for _, v := range arg[1:] {
n, e := fmt.Fprint(f, v) n, e := fmt.Fprint(f, v)
m.Assert(e) 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 return
}}, }},

View File

@ -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.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", "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", "remote", "command", "tool")
m.Cmd("aaa.role", "tech", "componet", "source", "command", "tool") m.Cmd("aaa.role", "tech", "componet", "source", "command", "tool")

View File

@ -486,7 +486,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
} }
if arg[2] == "new" { 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()) dir, file := path.Split(uri.EscapedPath())
m.Confv("spide", arg[0], map[string]interface{}{ m.Confv("spide", arg[0], map[string]interface{}{
"cookie": 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) { "/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/") key = strings.TrimPrefix(key, "/publish/")
if strings.HasSuffix(key, "bench") { 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)) p := m.Cmdx("nfs.path", path.Join(m.Conf("publish", "path"), key))
if p == "" { 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) m.Log("info", "publish %s %s", kit.Hashs(p), p)

View File

@ -17,23 +17,22 @@ import (
) )
var DisableLog = false 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{}) { func Log(action string, str string, args ...interface{}) {
if DisableLog { if DisableLog {
return return
} }
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "%s", str) if len(args) > 0 {
} else { str = fmt.Sprintf(str, args...)
fmt.Fprintf(os.Stderr, 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) { func Env(key string) {