1
0
forked from x/ContextOS

vps add ssh.route 添加主机动态域名

This commit is contained in:
shylinux 2018-04-18 09:10:07 +08:00
parent a27b443df3
commit bdb86ead00
4 changed files with 56 additions and 44 deletions

View File

@ -228,7 +228,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
if cli.nfs = m.Find("nfs"); m.Has("stdio") { if cli.nfs = m.Find("nfs"); m.Has("stdio") {
cli.nfs.Cmd("scan", m.Cap("stream", "stdio"), m.Cmd("source", m.Cap("init.shy")).Get("result")) cli.nfs.Cmd("scan", m.Cap("stream", "stdio"), m.Spawn(m.Target()).Cmd("source", m.Cap("init.shy")).Get("result"))
} else { } else {
if _, e := os.Stat(m.Cap("init.shy")); e == nil { if _, e := os.Stat(m.Cap("init.shy")); e == nil {
cli.nfs.Cmd("scan", m.Cap("stream", m.Cap("init.shy"))) cli.nfs.Cmd("scan", m.Cap("stream", m.Cap("init.shy")))
@ -261,6 +261,9 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
switch cli.Context { switch cli.Context {
case m.Target(): case m.Target():
m.Echo(cli.nfs.Cap("return")) m.Echo(cli.nfs.Cap("return"))
if p, ok := m.Source().Server.(*CLI); ok {
p.target = cli.target
}
case m.Source(): case m.Source():
if m.Name == "aaa" { if m.Name == "aaa" {
if !cli.Context.Close(m.Spawn(cli.Context), arg...) { if !cli.Context.Close(m.Spawn(cli.Context), arg...) {

View File

@ -6,7 +6,6 @@ import ( // {{{
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@ -355,7 +354,7 @@ func (m *Message) Log(action string, ctx *Context, str string, arg ...interface{
l.Options("log", false) l.Options("log", false)
l.Cmd("log", action, fmt.Sprintf(str, arg...)) l.Cmd("log", action, fmt.Sprintf(str, arg...))
} else { } else {
log.Printf(str, arg...) // log.Printf(str, arg...)
return return
} }
} }
@ -1330,19 +1329,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
"start": &Config{Name: "启动模块", Value: "cli", Help: "启动时自动运行的模块"}, "start": &Config{Name: "启动模块", Value: "cli", Help: "启动时自动运行的模块"},
"init.shy": &Config{Name: "启动脚本", Value: "etc/init.shy", Help: "模块启动时自动运行的脚本"}, "init.shy": &Config{Name: "启动脚本", Value: "etc/init.shy", Help: "模块启动时自动运行的脚本"},
"bench.log": &Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件", Hand: func(m *Message, x *Config, arg ...string) string { "bench.log": &Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件"},
if len(arg) > 0 { // {{{
// if e := os.MkdirAll(path.Dir(arg[0]), os.ModePerm); e == nil {
if l, e := os.Create(x.Value); e == nil {
log.SetOutput(l)
return arg[0]
}
return ""
// }
}
return x.Value
// }}}
}},
"root": &Config{Name: "工作目录", Value: ".", Help: "所有模块的当前目录", Hand: func(m *Message, x *Config, arg ...string) string { "root": &Config{Name: "工作目录", Value: ".", Help: "所有模块的当前目录", Hand: func(m *Message, x *Config, arg ...string) string {
if len(arg) > 0 { // {{{ if len(arg) > 0 { // {{{
if !path.IsAbs(x.Value) { if !path.IsAbs(x.Value) {
@ -1935,23 +1922,18 @@ func init() {
func Start(args ...string) { func Start(args ...string) {
if len(args) > 0 { if len(args) > 0 {
Pulse.Conf("start", args[0]) Pulse.Conf("init.shy", args[0])
} }
if len(args) > 1 { if len(args) > 1 {
Pulse.Conf("init.shy", args[1]) Pulse.Conf("bench.log", args[1])
} }
if len(args) > 2 { if len(args) > 2 {
Pulse.Conf("bench.log", args[2]) Pulse.Conf("root", args[2])
} else {
Pulse.Conf("bench.log", Pulse.Conf("bench.log"))
} }
if len(args) > 3 { if len(args) > 3 {
Pulse.Conf("root", args[3]) Pulse.Conf("start", args[3])
} }
Pulse.Options("log", true)
// log.Println("\n\n")
Index.Group = "root" Index.Group = "root"
Index.Owner = Index.contexts["aaa"] Index.Owner = Index.contexts["aaa"]
Index.master = Index.contexts["cli"] Index.master = Index.contexts["cli"]
@ -1959,8 +1941,9 @@ func Start(args ...string) {
m.target.root = Index m.target.root = Index
m.target.Begin(m) m.target.Begin(m)
} }
// log.Println()
Pulse.Sess("log", "log").Conf("bench.log", "var/bench.log") Pulse.Options("log", true)
Pulse.Sess("log", "log").Conf("bench.log", Pulse.Conf("bench.log"))
for _, m := range Pulse.Search(Pulse.Conf("start")) { for _, m := range Pulse.Search(Pulse.Conf("start")) {
m.Set("detail", Pulse.Conf("init.shy")).Set("option", "stdio").target.Start(m) m.Set("detail", Pulse.Conf("init.shy")).Set("option", "stdio").target.Start(m)

View File

@ -1,14 +1,17 @@
package log package log // {{{
// }}}
import ( import ( // {{{
"contexts" "contexts"
"fmt" "fmt"
Log "log" Log "log"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
// }}}
type LOG struct { type LOG struct {
module map[string]map[string]bool module map[string]map[string]bool
slient map[string]bool slient map[string]bool
@ -19,7 +22,7 @@ type LOG struct {
*ctx.Context *ctx.Context
} }
func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{} c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
@ -28,7 +31,8 @@ func (log *LOG) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s return s
} }
func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server { // }}}
func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
if log.Context == Index { if log.Context == Index {
Pulse = m Pulse = m
} }
@ -45,6 +49,7 @@ func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server {
log.Configs["bench.log"] = &ctx.Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string { log.Configs["bench.log"] = &ctx.Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
if len(arg) > 0 { if len(arg) > 0 {
if m.Sess("nfs") == nil { if m.Sess("nfs") == nil {
os.Create(arg[0])
m.Sess("nfs", "nfs").Cmd("open", arg[0]) m.Sess("nfs", "nfs").Cmd("open", arg[0])
} }
return arg[0] return arg[0]
@ -55,12 +60,14 @@ func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server {
return log return log
} }
func (log *LOG) Start(m *ctx.Message, arg ...string) bool { // }}}
func (log *LOG) Start(m *ctx.Message, arg ...string) bool { // {{{
log.Message = m log.Message = m
return false return false
} }
func (log *LOG) Close(m *ctx.Message, arg ...string) bool { // }}}
func (log *LOG) Close(m *ctx.Message, arg ...string) bool { // {{{
switch log.Context { switch log.Context {
case m.Target(): case m.Target():
case m.Source(): case m.Source():
@ -68,13 +75,15 @@ func (log *LOG) Close(m *ctx.Message, arg ...string) bool {
return true return true
} }
// }}}
var Pulse *ctx.Message var Pulse *ctx.Message
var Index = &ctx.Context{Name: "log", Help: "日志中心", var Index = &ctx.Context{Name: "log", Help: "日志中心",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{}, Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"slient": &ctx.Command{Name: "slient [[module] level state]", Help: "查看或设置日志开关, module: 模块名, level: 日志类型, state(true/false): 是否打印日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "slient": &ctx.Command{Name: "slient [[module] level state]", Help: "查看或设置日志开关, module: 模块名, level: 日志类型, state(true/false): 是否打印日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { // {{{
switch len(arg) { switch len(arg) {
case 2: case 2:
if len(arg) > 1 { if len(arg) > 1 {
@ -95,10 +104,10 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
m.Echo("%s(%s): %t\n", k, i, x) m.Echo("%s(%s): %t\n", k, i, x)
} }
} }
} } // }}}
}}, }},
"color": &ctx.Command{Name: "color [level color]", Help: "查看或设置日志颜色, level: 日志类型, color: 文字颜色", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "color": &ctx.Command{Name: "color [level color]", Help: "查看或设置日志颜色, level: 日志类型, color: 文字颜色", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { // {{{
if len(arg) > 1 { if len(arg) > 1 {
c, e := strconv.Atoi(arg[1]) c, e := strconv.Atoi(arg[1])
m.Assert(e) m.Assert(e)
@ -108,10 +117,10 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
for k, v := range log.color { for k, v := range log.color {
m.Echo("\033[%dm%s: %d\033[0m\n", v, k, v) m.Echo("\033[%dm%s: %d\033[0m\n", v, k, v)
} }
} } // }}}
}}, }},
"log": &ctx.Command{Name: "log level string...", Help: "输出日志, level: 日志类型, string: 日志内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "log": &ctx.Command{Name: "log level string...", Help: "输出日志, level: 日志类型, string: 日志内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { if log, ok := m.Target().Server.(*LOG); m.Assert(ok) { // {{{
if s, ok := log.slient[arg[0]]; ok && s == true { if s, ok := log.slient[arg[0]]; ok && s == true {
return return
} }
@ -167,7 +176,7 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
nfs.Cmd("write", fmt.Sprintf("%s%s%s %s\n", date, code, action, strings.Join(arg[1:], ""))) nfs.Cmd("write", fmt.Sprintf("%s%s%s %s\n", date, code, action, strings.Join(arg[1:], "")))
} }
} }
} } // }}}
}}, }},
}, },
} }

View File

@ -118,8 +118,11 @@ var Pulse *ctx.Message
var Index = &ctx.Context{Name: "ssh", Help: "集群中心", var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
Caches: map[string]*ctx.Cache{ Caches: map[string]*ctx.Cache{
"nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"}, "nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"},
"route": &ctx.Cache{Name: "route", Value: "", Help: "主机数量"},
},
Configs: map[string]*ctx.Config{
"route": &ctx.Config{Name: "route", Value: "com", Help: "主机数量"},
}, },
Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("file", "nfs") msg := m.Sess("file", "nfs")
@ -129,7 +132,10 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "打开文件", sub.Meta["detail"]...) sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "打开文件", sub.Meta["detail"]...)
sub.Cap("stream", msg.Target().Name) sub.Cap("stream", msg.Target().Name)
sub.Target().Sessions["file"] = msg sub.Target().Sessions["file"] = msg
sub.Target(msg.Target())
sub.Echo(sub.Target().Name) sub.Echo(sub.Target().Name)
sub.Spawn(sub.Target()).Cmd("send", "context", "ssh")
sub.Spawn(sub.Target()).Cmd("send", "route", sub.Target().Name, msg.Cap("route")+"."+msg.Conf("route"))
} }
return false, true return false, true
}, false).Cmd(m.Meta["detail"]) }, false).Cmd(m.Meta["detail"])
@ -151,7 +157,18 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
msg.Copy(m, "detail").Cmd() msg.Copy(m, "detail").Cmd()
m.Copy(msg, "result") m.Copy(msg, "result")
}}, }},
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "route": &ctx.Command{Name: "route", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Conf("route", arg[0])
m.Cap("route", arg[1]+"."+arg[0])
}},
"dispatch": &ctx.Command{Name: "dispatch cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Travel(m.Target(), func(m *ctx.Message) bool {
msg := m.Spawn(m.Target())
msg.Cmd("send", arg)
return true
})
}},
"register": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
ssh, ok := m.Target().Server.(*SSH) ssh, ok := m.Target().Server.(*SSH)
m.Assert(ok) m.Assert(ok)