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

opt boot.sh

This commit is contained in:
shaoying 2019-06-15 22:21:49 +08:00
parent a67f0eb73e
commit 220c1e7aac
10 changed files with 300 additions and 305 deletions

View File

@ -1,23 +1,24 @@
#! /bin/bash #! /bin/bash
export ctx_dev=${ctx_dev:="https://shylinux.com"} ctx_log=${ctx_log:="var/log"}
export ctx_root="/usr/local/context" ctx_app=${ctx_app:="bench"}
export ctx_home=~/context ctx_bin=${ctx_app} && [ -f bin/${ctx_app} ] && ctx_bin=$(pwd)/bin/${ctx_app}
# ctx_box=
ctx_bin="bench" && [ -e bin/bench ] && ctx_bin=bin/bench ctx_dev=${ctx_dev:="https://shylinux.com"}
export ctx_bin # 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() { log() {
echo -e $* echo -e $*
} }
prepare() {
mkdir -p bin etc usr
mkdir -p var/log var/tmp var/run
}
install() { install() {
[ -n "$1" ] && ctx_dev=$1 && shift
case `uname -s` in case `uname -s` in
"Darwin") GOOS=darwin GOARCH=amd64;; "Darwin") GOOS=darwin GOARCH=amd64;;
*) GOOS=linux GOARCH=386;; *) GOOS=linux GOARCH=386;;
@ -26,51 +27,32 @@ install() {
"x86_64") GOARCH=amd64;; "x86_64") GOARCH=amd64;;
"armv7l") GOARCH=arm;; "armv7l") GOARCH=arm;;
esac esac
log "ctx_dev: $ctx_dev\nGOOS: $GOOS\nGOARCH: $GOARCH" 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} \
ctx_dev=$ctx_dev/code/upgrade && mv ${ctx_app} bin/${ctx_app}
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
} }
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() { main() {
while true; do while true; do
cp -r var/log var/log_$(date +%Y%m%d_%H%M%S) ${ctx_bin} "$@" 2>${ctx_log}/boot.log && break
$ctx_bin "$@" 2>var/log/boot.log && break
log "restarting..." && sleep 3 log "restarting..." && sleep 3
done done
} }
action() {
pid=$(cat var/run/bench.log)
log "kill" $1 && kill -$1 ${pid}
}
dir=$ctx_root
dir=./ dir=./ && [ -d "$1" ] && dir=$1 && shift
[ -d "$1" ] && dir=$1 && shift [ -d "${dir}" ] && cd ${dir}
[ -d "$dir" ] && cd $dir log "dev:$ctx_dev\ndir: $dir\nbin: $ctx_bin\n"
[ -f bin/bench ] && ctx_bin=bin/bench
pid=`cat var/run/bench.pid`
log "dev:$ctx_dev\ndir: $dir\nbench: $ctx_bin\npid: $pid"
case $1 in case $1 in
install) shift; prepare && install "$@";; install) install "$@";;
create) mkdir -p $2; cd $2 && shift && shift && prepare && main "$@";; start|"") main "$@";;
start|"") shift; prepare && main "$@";; create) mkdir -p $2; cd $2 && shift && shift && main "$@";;
state) state;;
stop) action QUIT;;
restart) action USR1;;
upgrade) action USR2;; upgrade) action USR2;;
restart) action USR1;;
stop) action QUIT;;
esac esac

View File

@ -5,13 +5,13 @@ import (
"contexts/ctx" "contexts/ctx"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"os/exec"
"os/user" "os/user"
"path" "path"
"toolkit" "toolkit"
"fmt" "fmt"
"os" "os"
"os/exec"
"plugin" "plugin"
"regexp" "regexp"
"runtime" "runtime"
@ -100,35 +100,19 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"nshell": &ctx.Cache{Name: "nshell", Value: "0", Help: "终端数量"}, "nshell": &ctx.Cache{Name: "nshell", Value: "0", Help: "终端数量"},
}, },
Configs: map[string]*ctx.Config{ 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{}{ "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"}, "init_env": []interface{}{
"boot": map[string]interface{}{"web_port": ":9094", "ssh_port": ":9090"}, "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: "运行环境"}, }, 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{}{ "system": &ctx.Config{Name: "system", Value: map[string]interface{}{
"timeout": "60s", "timeout": "60s",
"env": map[string]interface{}{}, "env": map[string]interface{}{},
@ -144,7 +128,48 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"init": "etc/init.shy", "exit": "etc/exit.shy", "init": "etc/init.shy", "exit": "etc/exit.shy",
}, },
}, Help: "系统环境, shell: path, cmd, arg, dir, env, active, daemon; "}, }, 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": &ctx.Config{Name: "timer", Value: map[string]interface{}{}, Help: "定时器"},
"timer_next": &ctx.Config{Name: "timer_next", Value: "", Help: "定时器"}, "timer_next": &ctx.Config{Name: "timer_next", Value: "", Help: "定时器"},
@ -167,83 +192,33 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
}, Help: "启动脚本"}, }, Help: "启动脚本"},
}, },
Commands: map[string]*ctx.Command{ 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.GOARCH", runtime.GOARCH)
m.Conf("runtime", "host.GOOS", runtime.GOOS) m.Conf("runtime", "host.GOOS", runtime.GOOS)
m.Conf("runtime", "host.pid", os.Getpid()) m.Conf("runtime", "host.pid", os.Getpid())
runtime.GOMAXPROCS(1) 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) { m.Confm("runtime", "init_env", func(index int, key string) {
if value := os.Getenv(key); value != "" { if value := os.Getenv(key); value != "" {
m.Conf("runtime", "boot."+key, kit.Select("", value, value != "-")) m.Conf("runtime", "boot."+key, kit.Select("", value, value != "-"))
} }
}) })
return
}}, if name, e := os.Hostname(); e == nil {
"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) { m.Conf("runtime", "boot.hostname", kit.Select(name, os.Getenv("HOSTNAME")))
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 user, e := user.Current(); e == nil {
if len(arg) > 0 && arg[0] == "all" { ns := strings.Split(user.Username, "\\")
m.Cmd("cli.compile", "linux", "386") name := ns[len(ns)-1]
m.Cmd("cli.compile", "linux", "amd64") m.Conf("runtime", "boot.username", kit.Select(name, os.Getenv("USER")))
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 name, e := os.Getwd(); e == nil {
if len(arg) > 0 { _, file := path.Split(name)
goos := kit.Select(m.Conf("runtime", "host.GOOS"), arg, 0) m.Conf("runtime", "boot.pathname", file)
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)
}
} }
return return
}}, }},
"publish": &ctx.Command{Name: "publish", Help: "", 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) {
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) {
if len(arg) == 0 { if len(arg) == 0 {
m.Cmdy("ctx.config", "runtime") m.Cmdy("ctx.config", "runtime")
return return
@ -281,67 +256,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) {
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: 命令", "system": &ctx.Command{Name: "system word...", Help: []string{"调用系统命令, word: 命令",
"cmd_timeout: 命令超时", "cmd_timeout: 命令超时",
"cmd_active(true/false): 是否交互", "cmd_active(true/false): 是否交互",
@ -585,6 +499,151 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
return 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) { "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 { if len(arg) == 0 {
m.Cmdy("dir", "", "dir_deep", "dir_reg", ".*\\.(sh|shy|py)$") 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") { if !strings.HasPrefix(text, "sess") && m.Options("remote") {
text = m.Current(text) text = m.Current(text)
} }
kit.Log("fuck", "what %v", text)
// 解析代码片段 // 解析代码片段
m.Sess("yac").Call(func(msg *ctx.Message) *ctx.Message { m.Sess("yac").Call(func(msg *ctx.Message) *ctx.Message {

View File

@ -113,17 +113,18 @@ func (c *Context) Begin(m *Message, arg ...string) *Context {
m.source.sessions = append(m.source.sessions, m) m.source.sessions = append(m.source.sessions, m)
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 {
c.Server.Begin(m, m.Meta["detail"]...) c.Server.Begin(m, m.Meta["detail"]...)
} }
return c return c
} }
func (c *Context) Start(m *Message, arg ...string) bool { func (c *Context) Start(m *Message, arg ...string) bool {
@ -1201,6 +1202,9 @@ func (m *Message) Gdb(arg ...interface{}) interface{} {
return nil return nil
} }
func (m *Message) Log(action string, str string, arg ...interface{}) *Message { 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 l := m.Sess("log", false); l != nil {
if log, ok := l.target.Server.(LOGGER); ok { if log, ok := l.target.Server.(LOGGER); ok {

View File

@ -27,6 +27,10 @@ func (ctx *CTX) Begin(m *Message, arg ...string) Server {
m.root = m m.root = m
m.Cap("begin_time", m.Time()) m.Cap("begin_time", m.Time())
m.Cap("goos", runtime.GOOS) 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("") { for _, msg := range m.Search("") {
msg.target.root = m.target msg.target.root = m.target
if msg.target == 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 { func (ctx *CTX) Start(m *Message, arg ...string) bool {
m.Cmd("ctx._init") m.Cmd("ctx._init")
if m.Optionv("ps_target", Index); len(arg) == 0 { if m.Optionv("ps_target", Index); len(arg) == 0 {
Pulse.Option("log.disable", false)
m.Cap("stream", "shy") 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")) m.Cmd("cli.source", m.Conf("system", "script.init")).Cmd("cli.source", "stdio").Cmd("cli.source", m.Conf("system", "script.exit"))
} else { } else {
m.Cmd("cli.source", arg) for _, v := range m.Spawn().Cmd(arg).Meta["result"] {
fmt.Printf("%s", v)
}
} }
m.Cmd("ctx.exit") m.Cmd("ctx.exit")
return true return true
@ -203,11 +210,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{},
case string: case string:
m.Echo("%s: %s\n%s\n", k, v.Name, v.Help) 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 return
} }
keys = append(keys, k) keys = append(keys, k)
@ -1411,17 +1413,15 @@ func Start(args ...string) bool {
if len(args) == 0 { if len(args) == 0 {
args = append(args, os.Args[1:]...) args = append(args, os.Args[1:]...)
} }
kit.DisableLog = true
if len(args) > 0 && args[0] == "daemon" { if len(args) > 0 && args[0] == "daemon" {
Pulse.Options("daemon", true) Pulse.Options("daemon", true)
args = args[1:] 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...) { if Index.Begin(Pulse, args...); Index.Start(Pulse, args...) {
return Index.Close(Pulse, args...) return Index.Close(Pulse, args...)
} }
return Index.message.Wait() return Index.message.Wait()
} }

View File

@ -82,15 +82,18 @@ func (log *LOG) Start(m *ctx.Message, arg ...string) bool {
for _, v := range []string{kit.Format(l["action"]), "bench"} { for _, v := range []string{kit.Format(l["action"]), "bench"} {
for i := len(args); i >= 0; i-- { for i := len(args); i >= 0; i-- {
if value := log.Value(m, append([]string{v}, args[:i]...)); kit.Right(value) && kit.Right(value["file"]) { 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 := os.Stdout, true
file, ok := log.file[name] if kit.Format(value["file"]) != "stdout" {
if !ok { name := path.Join(m.Conf("logdir"), kit.Format(value["file"]))
if f, e := os.Create(name); e == nil { file, ok = log.file[name]
file, log.file[name] = f, f if !ok {
kit.Log("error", "%s log file %s", "open", name) if f, e := os.Create(name); e == nil {
} else { file, log.file[name] = f, f
kit.Log("error", "%s log file %s %s", "open", name, e) kit.Log("error", "%s log file %s", "open", name)
continue } else {
kit.Log("error", "%s log file %s %s", "open", name, e)
continue
}
} }
} }

View File

@ -1163,8 +1163,8 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} }
return return
}}, }},
"dir": &ctx.Command{Name: "dir path [dir_deep] [dir_type both|file|dir] [dir_reg reg] [dir_sort field order] fields...", "dir": &ctx.Command{Name: "dir [path [fields...]]", Help: []string{"查看目录, path: 路径, fields...: 查询字段, time|type|full|path|tree|filename|size|line|hash",
Help: "查看目录, path: 路径, dir_deep: 递归查询, dir_type: 文件类型, dir_reg: 正则表达式, dir_sort: 排序, fields: 查询字段", "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}, 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) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
if len(arg) == 0 { if len(arg) == 0 {

View File

@ -161,7 +161,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
map[string]interface{}{"componet_name": "post", "componet_help": "post", map[string]interface{}{"componet_name": "post", "componet_help": "post",
"componet_tmpl": "componet", "componet_view": "Context", "componet_init": "", "componet_tmpl": "componet", "componet_view": "Context", "componet_init": "",
"componet_type": "private", "componet_ctx": "web", "componet_cmd": "post", "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": "spide", "imports": "plugin_site"},
map[string]interface{}{"type": "text", "name": "url"}, map[string]interface{}{"type": "text", "name": "url"},
map[string]interface{}{"type": "button", "value": "执行"}, map[string]interface{}{"type": "button", "value": "执行"},
@ -185,7 +185,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"componet_args": []interface{}{}, "inputs": []interface{}{ "componet_args": []interface{}{}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"}, map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "text", "name": "cmd", "value": "gg"}, 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_dir"},
map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_branch"}, map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_branch"},
map[string]interface{}{"type": "button", "value": "执行"}, map[string]interface{}{"type": "button", "value": "执行"},

View File

@ -743,6 +743,11 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
*/ */
default: default:
if m.Options("save") { 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")) f, e := os.Create(m.Option("save"))
m.Assert(e) m.Assert(e)
defer f.Close() defer f.Close()
@ -1145,6 +1150,22 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
} }
return 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
}},
}, },
} }

View File

@ -4,8 +4,6 @@ import (
"contexts/ctx" "contexts/ctx"
"contexts/web" "contexts/web"
"fmt" "fmt"
"net/http"
"os"
"path" "path"
"plugin" "plugin"
"strconv" "strconv"
@ -271,22 +269,6 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
"maps": map[string]interface{}{"baidu": "<a href='baidumap://map/direction?region=&origin=&destination=%s'>%s</a>"}, "maps": map[string]interface{}{"baidu": "<a href='baidumap://map/direction?region=&origin=&destination=%s'>%s</a>"},
}, Help: "闪存"}, }, 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{}{ "counter": &ctx.Config{Name: "counter", Value: map[string]interface{}{
"nopen": "0", "nsave": "0", "nopen": "0", "nsave": "0",
}, Help: "counter"}, }, Help: "counter"},
@ -517,67 +499,6 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
return 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) { "/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 { if len(arg) > 0 {
m.Option("name", arg[0]) m.Option("name", arg[0])

View File

@ -16,6 +16,7 @@ import (
"time" "time"
) )
var DisableLog = false
func Errorf(str string, args ...interface{}) { func Errorf(str string, args ...interface{}) {
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintf(os.Stderr, "%s\n", str) fmt.Fprintf(os.Stderr, "%s\n", str)
@ -24,6 +25,9 @@ func Errorf(str string, args ...interface{}) {
fmt.Fprintf(os.Stderr, str, args...) fmt.Fprintf(os.Stderr, str, args...)
} }
func Log(action string, str string, args ...interface{}) { func Log(action string, str string, args ...interface{}) {
if DisableLog {
return
}
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintf(os.Stderr, "%s", str) fmt.Fprintf(os.Stderr, "%s", str)
} else { } else {
@ -695,7 +699,7 @@ func Hash(arg ...interface{}) (string, []string) {
io.Copy(m, f) io.Copy(m, f)
h := m.Sum(nil) h := m.Sum(nil)
args = append(args, hex.EncodeToString(h[:])) args = append(args, hex.EncodeToString(h[:]))
break return hex.EncodeToString(h[:]), args
} }
} }
args = append(args, v) args = append(args, v)