diff --git a/bin/boot.sh b/bin/boot.sh index 93d54837..6ace07b7 100755 --- a/bin/boot.sh +++ b/bin/boot.sh @@ -1,23 +1,24 @@ #! /bin/bash -export ctx_dev=${ctx_dev:="https://shylinux.com"} -export ctx_root="/usr/local/context" -export ctx_home=~/context - -ctx_bin="bench" && [ -e bin/bench ] && ctx_bin=bin/bench -export ctx_bin +ctx_log=${ctx_log:="var/log"} +ctx_app=${ctx_app:="bench"} +ctx_bin=${ctx_app} && [ -f bin/${ctx_app} ] && ctx_bin=$(pwd)/bin/${ctx_app} +# ctx_box= +ctx_dev=${ctx_dev:="https://shylinux.com"} +# ctx_cas= +ctx_root=${ctx_root:=/usr/local/context} +ctx_home=${ctx_home:=~/context} +# web_port= +# ssh_port= +# HOSTNAME= +# USER= +# PWD= +export ctx_log ctx_app ctx_bin ctx_dev ctx_root ctx_home log() { echo -e $* } - -prepare() { - mkdir -p bin etc usr - mkdir -p var/log var/tmp var/run -} - install() { - [ -n "$1" ] && ctx_dev=$1 && shift case `uname -s` in "Darwin") GOOS=darwin GOARCH=amd64;; *) GOOS=linux GOARCH=386;; @@ -26,51 +27,32 @@ install() { "x86_64") GOARCH=amd64;; "armv7l") GOARCH=arm;; esac - log "ctx_dev: $ctx_dev\nGOOS: $GOOS\nGOARCH: $GOARCH" - - ctx_dev=$ctx_dev/code/upgrade - wget -O etc/exit.shy $ctx_dev/exit_shy - wget -O etc/init.shy $ctx_dev/init_shy - wget -O etc/common.shy $ctx_dev/common_shy - wget -O bin/bench.new "$ctx_dev/bench?GOOS=$GOOS&GOARCH=$GOARCH" && chmod a+x bin/bench.new && mv bin/bench.new bin/bench - wget -O bin/boot.sh $ctx_dev/boot_sh && chmod a+x bin/boot.sh - wget -O bin/node.sh $ctx_dev/node_sh && chmod a+x bin/node.sh + wget -O ${ctx_app} "$ctx_dev/publish/${ctx_app}?GOOS=$GOOS&GOARCH=$GOARCH" && chmod a+x ${ctx_app} \ + && ./${ctx_app} upgrade system && md5sum ${ctx_app} \ + && mv ${ctx_app} bin/${ctx_app} } - -state() { - md=md5sum && [ `uname -s` = "Darwin" ] && md=md5 - for file in bin/node.sh bin/boot.sh bin/bench etc/init.shy etc/common.shy etc/exit.shy; do - echo `$md $file` - done -} - -action() { - log "kill" $1 && kill -$1 $pid -} - main() { while true; do - cp -r var/log var/log_$(date +%Y%m%d_%H%M%S) - $ctx_bin "$@" 2>var/log/boot.log && break + ${ctx_bin} "$@" 2>${ctx_log}/boot.log && break log "restarting..." && sleep 3 done } +action() { + pid=$(cat var/run/bench.log) + log "kill" $1 && kill -$1 ${pid} +} -dir=$ctx_root -dir=./ -[ -d "$1" ] && dir=$1 && shift -[ -d "$dir" ] && cd $dir -[ -f bin/bench ] && ctx_bin=bin/bench -pid=`cat var/run/bench.pid` -log "dev:$ctx_dev\ndir: $dir\nbench: $ctx_bin\npid: $pid" + +dir=./ && [ -d "$1" ] && dir=$1 && shift +[ -d "${dir}" ] && cd ${dir} +log "dev:$ctx_dev\ndir: $dir\nbin: $ctx_bin\n" case $1 in - install) shift; prepare && install "$@";; - create) mkdir -p $2; cd $2 && shift && shift && prepare && main "$@";; - start|"") shift; prepare && main "$@";; - state) state;; - stop) action QUIT;; - restart) action USR1;; + install) install "$@";; + start|"") main "$@";; + create) mkdir -p $2; cd $2 && shift && shift && main "$@";; upgrade) action USR2;; + restart) action USR1;; + stop) action QUIT;; esac diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 30c5fe07..12c252f0 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -5,13 +5,13 @@ import ( "contexts/ctx" "encoding/csv" "encoding/json" + "os/exec" "os/user" "path" "toolkit" "fmt" "os" - "os/exec" "plugin" "regexp" "runtime" @@ -100,35 +100,19 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "nshell": &ctx.Cache{Name: "nshell", Value: "0", Help: "终端数量"}, }, Configs: map[string]*ctx.Config{ - "compile": &ctx.Config{Name: "compile", Value: map[string]interface{}{ - "bench": "src/examples/app/bench.go", - "env": []interface{}{"GOPATH", "PATH"}, - }, Help: "运行环境"}, - "publish": &ctx.Config{Name: "publish", Value: map[string]interface{}{ - "list": map[string]interface{}{ - "boot_sh": "bin/boot.sh", - "node_sh": "bin/node.sh", - "init_shy": "etc/init.shy", - "exit_shy": "etc/exit.shy", - "common_shy": "etc/common.shy", - }, - "path": "usr/publish", - }, Help: "运行环境"}, "runtime": &ctx.Config{Name: "runtime", Value: map[string]interface{}{ - "init_env": []interface{}{"ctx_cas", "ctx_dev", "ctx_box", "ctx_root", "ctx_home", "web_port", "ssh_port", "USER"}, - "boot": map[string]interface{}{"web_port": ":9094", "ssh_port": ":9090"}, + "init_env": []interface{}{ + "ctx_log", "ctx_app", "ctx_bin", + "ctx_box", "ctx_cas", "ctx_dev", + "ctx_root", "ctx_home", + "web_port", "ssh_port", + "HOSTNAME", "USER", "PWD", + }, + "boot": map[string]interface{}{ + "web_port": ":9094", + "ssh_port": ":9090", + }, }, Help: "运行环境"}, - - "plugin": &ctx.Config{Name: "plugin", Value: map[string]interface{}{ - "go": map[string]interface{}{ - "build": []interface{}{"go", "build", "-buildmode=plugin"}, - "next": []interface{}{"so", "load"}, - }, - "so": map[string]interface{}{ - "load": []interface{}{"load"}, - }, - }, Help: "免密登录"}, - "system": &ctx.Config{Name: "system", Value: map[string]interface{}{ "timeout": "60s", "env": map[string]interface{}{}, @@ -144,7 +128,48 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "init": "etc/init.shy", "exit": "etc/exit.shy", }, }, Help: "系统环境, shell: path, cmd, arg, dir, env, active, daemon; "}, - "daemon": &ctx.Config{Name: "daemon", Value: map[string]interface{}{}, Help: "系统命令超时"}, + "daemon": &ctx.Config{Name: "daemon", Value: map[string]interface{}{}, Help: "守护任务"}, + "action": &ctx.Config{Name: "action", Value: map[string]interface{}{}, Help: "交互任务"}, + + "compile": &ctx.Config{Name: "compile", Value: map[string]interface{}{ + "bench": "src/examples/app/bench.go", + "env": []interface{}{"GOPATH", "PATH"}, + }, Help: "运行环境"}, + "publish": &ctx.Config{Name: "publish", Value: map[string]interface{}{ + "path": "usr/publish", "list": map[string]interface{}{ + "boot_sh": "bin/boot.sh", + "node_sh": "bin/node.sh", + "init_shy": "etc/init.shy", + "common_shy": "etc/common.shy", + "exit_shy": "etc/exit.shy", + }, + }, Help: "运行环境"}, + "upgrade": &ctx.Config{Name: "upgrade", Value: map[string]interface{}{ + "system": []interface{}{"boot_sh", "node_sh", "init_shy", "common_shy", "exit_shy"}, + "portal": []interface{}{"code_tmpl", "code_js", "context_js"}, + "list": map[string]interface{}{ + "bench": "bin/bench.new", + "boot_sh": "bin/boot.sh", + "node_sh": "bin/node.sh", + "init_shy": "etc/init.shy", + "common_shy": "etc/common.shy", + "exit_shy": "etc/exit.shy", + + "code_tmpl": "usr/template/code/code.tmpl", + "code_js": "usr/librarys/code.js", + "context_js": "usr/librarys/context.js", + }, + }, Help: "日志地址"}, + + "plugin": &ctx.Config{Name: "plugin", Value: map[string]interface{}{ + "go": map[string]interface{}{ + "build": []interface{}{"go", "build", "-buildmode=plugin"}, + "next": []interface{}{"so", "load"}, + }, + "so": map[string]interface{}{ + "load": []interface{}{"load"}, + }, + }, Help: "免密登录"}, "timer": &ctx.Config{Name: "timer", Value: map[string]interface{}{}, Help: "定时器"}, "timer_next": &ctx.Config{Name: "timer_next", Value: "", Help: "定时器"}, @@ -167,83 +192,33 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }, Help: "启动脚本"}, }, Commands: map[string]*ctx.Command{ - "_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", "host.GOARCH", runtime.GOARCH) m.Conf("runtime", "host.GOOS", runtime.GOOS) m.Conf("runtime", "host.pid", os.Getpid()) runtime.GOMAXPROCS(1) - if name, e := os.Hostname(); e == nil { - m.Conf("runtime", "boot.hostname", kit.Select(name, os.Getenv("HOSTNAME"))) - } - if name, e := os.Getwd(); e == nil { - _, file := path.Split(name) - m.Conf("runtime", "boot.pathname", file) - } - if user, e := user.Current(); e == nil { - ns := strings.Split(user.Username, "\\") - m.Conf("runtime", "boot.USER", ns[len(ns)-1]) - m.Conf("runtime", "boot.username", ns[len(ns)-1]) - } m.Confm("runtime", "init_env", func(index int, key string) { if value := os.Getenv(key); value != "" { m.Conf("runtime", "boot."+key, kit.Select("", value, value != "-")) } }) - return - }}, - "compile": &ctx.Command{Name: "compile [OS [ARCH]]", Help: "解析脚本, script: 脚本文件, stdio: 命令终端, snippet: 代码片段", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) > 0 && arg[0] == "self" { - if m.Cmdy("cli.system", "go", "install", m.Cmdx("nfs.path", m.Conf("compile", "bench"))); m.Result(0) == "" { - m.Cmdy("cli.quit", 1) - } - return + + if name, e := os.Hostname(); e == nil { + m.Conf("runtime", "boot.hostname", kit.Select(name, os.Getenv("HOSTNAME"))) } - - if len(arg) > 0 && arg[0] == "all" { - m.Cmd("cli.compile", "linux", "386") - m.Cmd("cli.compile", "linux", "amd64") - m.Cmd("cli.compile", "linux", "arm") - m.Cmd("cli.compile", "windows", "386") - m.Cmd("cli.compile", "windows", "amd64") - m.Cmd("cli.compile", "darwin", "amd64") - return + if user, e := user.Current(); e == nil { + ns := strings.Split(user.Username, "\\") + name := ns[len(ns)-1] + m.Conf("runtime", "boot.username", kit.Select(name, os.Getenv("USER"))) } - - if len(arg) > 0 { - goos := kit.Select(m.Conf("runtime", "host.GOOS"), arg, 0) - arch := kit.Select(m.Conf("runtime", "host.GOARCH"), arg, 1) - name := strings.Join([]string{"bench", goos, arch}, ".") - - env := []string{"cmd_env", "GOOS", goos, "cmd_env", "GOARCH", arch} - m.Confm("compile", "env", func(index int, key string) { - env = append(env, "cmd_env", key, kit.Select(os.Getenv(key), m.Option(key))) - }) - - if m.Cmdy("cli.system", env, "go", "build", "-o", path.Join(m.Conf("publish", "path"), name), - m.Cmdx("nfs.path", m.Conf("compile", "bench"))); m.Result(0) == "" { - m.Echo("bin: %s", name) - } + if name, e := os.Getwd(); e == nil { + _, file := path.Split(name) + m.Conf("runtime", "boot.pathname", file) } return }}, - "publish": &ctx.Command{Name: "publish", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - p := m.Conf("publish", "path") - m.Assert(os.MkdirAll(p, 0777)) - - m.Confm("publish", "list", func(key string, value string) { - m.Cmd("nfs.copy", path.Join(p, key), m.Cmdx("nfs.path", value)) - }) - m.Cmd("cli.system", "tar", "-zcvf", path.Join(p, "template.tar.gz"), "usr/template") - m.Cmd("cli.system", "tar", "-zcvf", path.Join(p, "librarys.tar.gz"), "usr/librarys") - return - }}, - "pushhub": &ctx.Command{Name: "pushhub", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - m.Cmd("cli.system", "git", "commit", "-am", arg[0]) - m.Cmd("cli.system", "git", "push") - return - }}, - "runtime": &ctx.Command{Name: "runtime", Help: "runtime", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + "runtime": &ctx.Command{Name: "runtime [host|boot|node|user|work [name [value]]]", Help: "运行环境", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { m.Cmdy("ctx.config", "runtime") return @@ -281,67 +256,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", "") - m.Echo("1s restarting...") - - m.GoFunc(m, func(m *ctx.Message) { - time.Sleep(time.Second * 1) - os.Exit(kit.Int(kit.Select("0", arg, 0))) - }) - 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") - if len(arg) == 0 { - arg = append(arg, target) - } - if cs := strings.Split(arg[0], "."); len(cs) > 1 { - suffix = cs[len(cs)-1] - } else if cs := strings.Split(arg[1], "."); len(cs) > 1 { - suffix, action, arg = cs[len(cs)-1], arg[0], arg[1:] - } - - if target = m.Cmdx("nfs.path", arg[0]); target == "" { - target = m.Cmdx("nfs.path", path.Join("src/plugin/", arg[0])) - } - - for suffix != "" && action != "" { - m.Log("info", "%v %v %v", suffix, action, target) - cook := m.Confv("plugin", suffix) - next := strings.Replace(target, "."+suffix, "."+kit.Chains(cook, "next.0"), -1) - - args := []string{} - if suffix == "so" { - if p, e := plugin.Open(target); m.Assert(e) { - s, e := p.Lookup("Index") - m.Assert(e) - w := *(s.(**ctx.Context)) - w.Name = kit.Select(w.Name, arg, 1) - c.Register(w, nil) - m.Spawn(w).Cmd("_init", arg[1:]) - } - } else { - if suffix == "go" { - args = append(args, "-o", next) - } - m.Assert(m.Cmd("cli.system", kit.Chain(cook, action), args, target)) - } - - suffix = kit.Chains(cook, "next.0") - action = kit.Chains(cook, "next.1") - target = next - } - return - }}, "system": &ctx.Command{Name: "system word...", Help: []string{"调用系统命令, word: 命令", "cmd_timeout: 命令超时", "cmd_active(true/false): 是否交互", @@ -585,6 +499,151 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } return }}, + + "compile": &ctx.Command{Name: "compile [OS [ARCH]]", Help: "解析脚本, script: 脚本文件, stdio: 命令终端, snippet: 代码片段", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) > 0 && arg[0] == "self" { + if m.Cmdy("cli.system", "go", "install", m.Cmdx("nfs.path", m.Conf("compile", "bench"))); m.Result(0) == "" { + m.Cmdy("cli.quit", 1) + } + return + } + + if len(arg) > 0 && arg[0] == "all" { + m.Cmdy("cli.compile", "linux", "386") + m.Cmdy("cli.compile", "linux", "amd64") + m.Cmdy("cli.compile", "linux", "arm") + m.Cmdy("cli.compile", "windows", "386") + m.Cmdy("cli.compile", "windows", "amd64") + m.Cmdy("cli.compile", "darwin", "amd64") + m.Set("result").Table() + return + } + + if len(arg) > 0 { + goos := kit.Select(m.Conf("runtime", "host.GOOS"), arg, 0) + arch := kit.Select(m.Conf("runtime", "host.GOARCH"), arg, 1) + name := strings.Join([]string{"bench", goos, arch}, ".") + + wd, _ := os.Getwd() + env := []string{"cmd_env", "GOOS", goos, "cmd_env", "GOARCH", arch, "cmd_env", + "cmd_env", "GOTMPDIR", path.Join(wd, "var/tmp"), + "cmd_env", "GOCACHE", path.Join(wd, "var/tmp"), + } + m.Confm("compile", "env", func(index int, key string) { + env = append(env, "cmd_env", key, kit.Select(os.Getenv(key), m.Option(key))) + }) + + 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.Table() + } + } + return + }}, + "publish": &ctx.Command{Name: "publish", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + p := m.Conf("publish", "path") + m.Assert(os.MkdirAll(p, 0777)) + + m.Confm("publish", "list", func(key string, value string) { + m.Cmd("nfs.copy", path.Join(p, key), m.Cmdx("nfs.path", value)) + }) + m.Cmd("cli.system", "tar", "-zcvf", path.Join(p, "template.tar.gz"), "usr/template") + m.Cmd("cli.system", "tar", "-zcvf", path.Join(p, "librarys.tar.gz"), "usr/librarys") + return + }}, + "upgrade": &ctx.Command{Name: "upgrade bench|system|extend|plugin|portal|client|script", Help: "服务升级", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { + m.Cmdy("ctx.config", "upgrade") + return + } + + restart := false + for _, link := range kit.View([]string{arg[0]}, m.Confm("upgrade")) { + file := kit.Select(link, m.Conf("upgrade", []string{"list", strings.Replace(link, ".", "_", -1)})) + + if m.Cmd("web.get", "dev", fmt.Sprintf("publish/%s", link), + "GOOS", m.Conf("runtime", "host.GOOS"), "GOARCH", m.Conf("runtime", "host.GOARCH"), + "save", file); strings.HasPrefix(file, "bin/") { + if m.Cmd("cli.system", "chmod", "a+x", file); link == "bench" { + m.Cmd("cli.system", "mv", "bin/bench", fmt.Sprintf("bin/bench_%s", m.Time("20060102_150405"))) + m.Cmd("cli.system", "mv", "bin/bench.new", "bin/bench") + file = "bin/bench" + } + restart = true + } + m.Add("append", "hash", kit.Hashs(file)) + m.Add("append", "file", file) + } + m.Table() + + if restart { + m.Cmd("cli.quit", 1) + } + 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", "") + m.Echo("wait 1s, restarting...") + + m.GoFunc(m, func(m *ctx.Message) { + time.Sleep(time.Second * 1) + os.Exit(kit.Int(kit.Select("0", arg, 0))) + }) + 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") + if len(arg) == 0 { + arg = append(arg, target) + } + if cs := strings.Split(arg[0], "."); len(cs) > 1 { + suffix = cs[len(cs)-1] + } else if cs := strings.Split(arg[1], "."); len(cs) > 1 { + suffix, action, arg = cs[len(cs)-1], arg[0], arg[1:] + } + + if target = m.Cmdx("nfs.path", arg[0]); target == "" { + target = m.Cmdx("nfs.path", path.Join("src/plugin/", arg[0])) + } + + for suffix != "" && action != "" { + m.Log("info", "%v %v %v", suffix, action, target) + cook := m.Confv("plugin", suffix) + next := strings.Replace(target, "."+suffix, "."+kit.Chains(cook, "next.0"), -1) + + args := []string{} + if suffix == "so" { + if p, e := plugin.Open(target); m.Assert(e) { + s, e := p.Lookup("Index") + m.Assert(e) + w := *(s.(**ctx.Context)) + w.Name = kit.Select(w.Name, arg, 1) + c.Register(w, nil) + m.Spawn(w).Cmd("_init", arg[1:]) + } + } else { + if suffix == "go" { + args = append(args, "-o", next) + } + m.Assert(m.Cmd("cli.system", kit.Chain(cook, action), args, target)) + } + + suffix = kit.Chains(cook, "next.0") + action = kit.Chains(cook, "next.1") + target = next + } + return + }}, "source": &ctx.Command{Name: "source [script|stdio|snippet]", Help: "解析脚本, script: 脚本文件, stdio: 命令终端, snippet: 代码片段", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { m.Cmdy("dir", "", "dir_deep", "dir_reg", ".*\\.(sh|shy|py)$") @@ -618,6 +677,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", if !strings.HasPrefix(text, "sess") && m.Options("remote") { text = m.Current(text) } + kit.Log("fuck", "what %v", text) // 解析代码片段 m.Sess("yac").Call(func(msg *ctx.Message) *ctx.Message { diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index 96968f94..0e0917bd 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -113,17 +113,18 @@ func (c *Context) Begin(m *Message, arg ...string) *Context { m.source.sessions = append(m.source.sessions, m) 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)) } } + */ if c.Server != nil { c.Server.Begin(m, m.Meta["detail"]...) } - return c } func (c *Context) Start(m *Message, arg ...string) bool { @@ -1201,6 +1202,9 @@ func (m *Message) Gdb(arg ...interface{}) interface{} { return nil } func (m *Message) Log(action string, str string, arg ...interface{}) *Message { + if m.Options("log.disable") { + return m + } if l := m.Sess("log", false); l != nil { if log, ok := l.target.Server.(LOGGER); ok { diff --git a/src/contexts/ctx/init.go b/src/contexts/ctx/init.go index a1081fdf..54114d1d 100644 --- a/src/contexts/ctx/init.go +++ b/src/contexts/ctx/init.go @@ -27,6 +27,10 @@ func (ctx *CTX) Begin(m *Message, arg ...string) Server { 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) for _, msg := range m.Search("") { msg.target.root = m.target if msg.target == m.target { @@ -40,10 +44,13 @@ func (ctx *CTX) Begin(m *Message, arg ...string) Server { func (ctx *CTX) Start(m *Message, arg ...string) bool { m.Cmd("ctx._init") if m.Optionv("ps_target", Index); len(arg) == 0 { + Pulse.Option("log.disable", false) m.Cap("stream", "shy") m.Cmd("cli.source", m.Conf("system", "script.init")).Cmd("cli.source", "stdio").Cmd("cli.source", m.Conf("system", "script.exit")) } else { - m.Cmd("cli.source", arg) + for _, v := range m.Spawn().Cmd(arg).Meta["result"] { + fmt.Printf("%s", v) + } } m.Cmd("ctx.exit") return true @@ -203,11 +210,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, case string: m.Echo("%s: %s\n%s\n", k, v.Name, v.Help) } - for k, v := range v.Form { - m.Add("append", "arg", k) - m.Add("append", "len", v) - m.Echo(" option: %s(%d)\n", k, v) - } return } keys = append(keys, k) @@ -1411,17 +1413,15 @@ func Start(args ...string) bool { if len(args) == 0 { args = append(args, os.Args[1:]...) } + + kit.DisableLog = true if len(args) > 0 && args[0] == "daemon" { Pulse.Options("daemon", true) args = args[1:] } - Pulse.Option("routine", 0) - Pulse.Option("page.limit", 10) - Pulse.Option("page.offset", 0) if Index.Begin(Pulse, args...); Index.Start(Pulse, args...) { return Index.Close(Pulse, args...) } - return Index.message.Wait() } diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index badddaf5..35289ab4 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -82,15 +82,18 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool { for _, v := range []string{kit.Format(l["action"]), "bench"} { for i := len(args); i >= 0; i-- { if value := log.Value(m, append([]string{v}, args[:i]...)); kit.Right(value) && kit.Right(value["file"]) { - name := path.Join(m.Conf("logdir"), kit.Format(value["file"])) - file, ok := log.file[name] - if !ok { - if f, e := os.Create(name); e == nil { - file, log.file[name] = f, f - kit.Log("error", "%s log file %s", "open", name) - } else { - kit.Log("error", "%s log file %s %s", "open", name, e) - continue + file, ok := os.Stdout, true + if kit.Format(value["file"]) != "stdout" { + name := path.Join(m.Conf("logdir"), kit.Format(value["file"])) + file, ok = log.file[name] + if !ok { + if f, e := os.Create(name); e == nil { + file, log.file[name] = f, f + kit.Log("error", "%s log file %s", "open", name) + } else { + kit.Log("error", "%s log file %s %s", "open", name, e) + continue + } } } diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index 76e33836..3a3d2c77 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -1163,8 +1163,8 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", } return }}, - "dir": &ctx.Command{Name: "dir path [dir_deep] [dir_type both|file|dir] [dir_reg reg] [dir_sort field order] fields...", - Help: "查看目录, path: 路径, dir_deep: 递归查询, dir_type: 文件类型, dir_reg: 正则表达式, dir_sort: 排序, fields: 查询字段", + "dir": &ctx.Command{Name: "dir [path [fields...]]", Help: []string{"查看目录, path: 路径, fields...: 查询字段, time|type|full|path|tree|filename|size|line|hash", + "dir_deep: 递归查询", "dir_type both|file|dir|all: 文件类型", "dir_reg reg: 正则表达式", "dir_sort field order: 排序"}, Form: map[string]int{"dir_deep": 0, "dir_type": 1, "dir_reg": 1, "dir_sort": 2}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index efbca993..f0750da5 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -161,7 +161,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", map[string]interface{}{"componet_name": "post", "componet_help": "post", "componet_tmpl": "componet", "componet_view": "Context", "componet_init": "", "componet_type": "private", "componet_ctx": "web", "componet_cmd": "post", - "componet_args": []interface{}{"content_type", "application/json"}, "inputs": []interface{}{ + "componet_args": []interface{}{"content_type", "application/json", "parse", "json"}, "inputs": []interface{}{ map[string]interface{}{"type": "text", "name": "spide", "imports": "plugin_site"}, map[string]interface{}{"type": "text", "name": "url"}, map[string]interface{}{"type": "button", "value": "执行"}, @@ -185,7 +185,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", "componet_args": []interface{}{}, "inputs": []interface{}{ map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"}, map[string]interface{}{"type": "text", "name": "cmd", "value": "gg"}, - map[string]interface{}{"type": "select", "name": "sub", "values": []interface{}{"deploygo"}}, + map[string]interface{}{"type": "select", "name": "sub", "values": []interface{}{"deploygo", "deploy"}}, map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_dir"}, map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_branch"}, map[string]interface{}{"type": "button", "value": "执行"}, diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 43460bae..453a880b 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -743,6 +743,11 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", */ default: if m.Options("save") { + dir := path.Dir(m.Option("save")) + if _, e = os.Stat(dir); e != nil { + m.Assert(os.MkdirAll(dir, 0777)) + } + f, e := os.Create(m.Option("save")) m.Assert(e) defer f.Close() @@ -1145,6 +1150,22 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } return }}, + + "/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") + } + + 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) })) + } + + m.Log("info", "publish %s %s", kit.Hashs(p), p) + http.ServeFile(m.Optionv("response").(http.ResponseWriter), m.Optionv("request").(*http.Request), p) + return + }}, }, } diff --git a/src/examples/code/code.go b/src/examples/code/code.go index deae1527..63c11dd6 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -4,8 +4,6 @@ import ( "contexts/ctx" "contexts/web" "fmt" - "net/http" - "os" "path" "plugin" "strconv" @@ -271,22 +269,6 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "maps": map[string]interface{}{"baidu": "%s"}, }, Help: "闪存"}, - "upgrade": &ctx.Config{Name: "upgrade", Value: map[string]interface{}{ - "system": []interface{}{"exit_shy", "common_shy", "init_shy", "bench", "boot_sh"}, - "portal": []interface{}{"code_tmpl", "code_js", "context_js"}, - "file": map[string]interface{}{ - "node_sh": "bin/node.sh", - "boot_sh": "bin/boot.sh", - "bench": "bin/bench.new", - "init_shy": "etc/init.shy", - "common_shy": "etc/common.shy", - "exit_shy": "etc/exit.shy", - - "code_tmpl": "usr/template/code/code.tmpl", - "code_js": "usr/librarys/code.js", - "context_js": "usr/librarys/context.js", - }, - }, Help: "日志地址"}, "counter": &ctx.Config{Name: "counter", Value: map[string]interface{}{ "nopen": "0", "nsave": "0", }, Help: "counter"}, @@ -517,67 +499,6 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", return }}, - "update": &ctx.Command{Name: "update", Help: "更新代码", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - return - }}, - "/upgrade/": &ctx.Command{Name: "/upgrade/", Help: "下载文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - p := m.Cmdx("nfs.path", key) - if strings.HasSuffix(key, "/bench") { - bench := m.Cmdx("nfs.path", key+"."+m.Option("GOOS")+"."+m.Option("GOARCH")) - if _, e := os.Stat(bench); e == nil { - p = bench - } - } - - if _, e = os.Stat(p); e != nil { - list := strings.Split(key, "/") - p = m.Cmdx("nfs.path", m.Conf("upgrade", []string{"file", list[len(list)-1]})) - } - - m.Log("info", "upgrade %s %s", p, m.Cmdx("aaa.hash", "file", p)) - http.ServeFile(m.Optionv("response").(http.ResponseWriter), m.Optionv("request").(*http.Request), p) - return - }}, - "upgrade": &ctx.Command{Name: "upgrade system|portal|script", Help: "服务升级", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) == 0 { - m.Cmdy("ctx.config", "upgrade") - return - } - - if m.Confs("upgrade", arg[0]) { - key, arg = arg[0], arg[1:] - m.Confm("upgrade", key, func(index int, value string) { - arg = append(arg, value) - }) - } - - restart := false - for _, link := range arg { - if file := m.Conf("upgrade", []string{"file", link}); file != "" { - dir := path.Dir(file) - if _, e = os.Stat(dir); e != nil { - e = os.Mkdir(dir, 0777) - m.Assert(e) - } - if m.Cmd("web.get", "dev", fmt.Sprintf("code/upgrade/%s", link), - "GOOS", m.Conf("runtime", "host.GOOS"), "GOARCH", m.Conf("runtime", "host.GOARCH"), - "save", file); strings.HasPrefix(file, "bin/") { - if m.Cmd("cli.system", "chmod", "a+x", file); link == "bench" { - m.Cmd("cli.system", "mv", "bin/bench", fmt.Sprintf("bin/bench_%s", m.Time("20060102_150405"))) - m.Cmd("cli.system", "mv", "bin/bench.new", "bin/bench") - } - } - restart = true - } else { - m.Cmdy("web.get", "dev", fmt.Sprintf("code/upgrade/script/%s", link), "save", fmt.Sprintf("usr/script/%s", link)) - } - } - - if restart { - m.Cmd("cli.quit", 1) - } - return - }}, "/counter": &ctx.Command{Name: "/counter", Help: "/counter", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) > 0 { m.Option("name", arg[0]) diff --git a/src/toolkit/kit.go b/src/toolkit/kit.go index 73639f73..34cec12f 100644 --- a/src/toolkit/kit.go +++ b/src/toolkit/kit.go @@ -16,6 +16,7 @@ import ( "time" ) +var DisableLog = false func Errorf(str string, args ...interface{}) { if len(args) == 0 { fmt.Fprintf(os.Stderr, "%s\n", str) @@ -24,6 +25,9 @@ func Errorf(str string, args ...interface{}) { 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 { @@ -695,7 +699,7 @@ func Hash(arg ...interface{}) (string, []string) { io.Copy(m, f) h := m.Sum(nil) args = append(args, hex.EncodeToString(h[:])) - break + return hex.EncodeToString(h[:]), args } } args = append(args, v)