mirror of
https://shylinux.com/x/ContextOS
synced 2025-06-26 18:07:30 +08:00
mac add ssh 添加了集群中心
This commit is contained in:
parent
debceb6c0f
commit
8f4d2302d6
@ -39,6 +39,9 @@ snippet c
|
||||
}
|
||||
|
||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
if `Filename()`.Context == Index {
|
||||
Pulse = m
|
||||
}
|
||||
return `Filename()`
|
||||
}
|
||||
|
||||
@ -46,11 +49,20 @@ snippet c
|
||||
return false
|
||||
}
|
||||
|
||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Exit(m *ctx.Message, arg ...string) bool {
|
||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch `Filename()`.Context {
|
||||
case m.Target:
|
||||
if `Filename()`.Context == Index {
|
||||
return false
|
||||
}
|
||||
case m.Source:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}",
|
||||
Caches: map[string]*ctx.Cache{},
|
||||
Configs: map[string]*ctx.Config{},
|
||||
@ -67,6 +79,7 @@ snippet cmd
|
||||
${4}
|
||||
return ""
|
||||
}},
|
||||
|
||||
snippet conf
|
||||
"${1}": &ctx.Config{Name: "${2}", Value: "${3}", Help: "${4}", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 {
|
||||
|
20
etc/init.sh
20
etc/init.sh
@ -1,13 +1,19 @@
|
||||
# ~lex source etc/lex.sh
|
||||
~cli @lex lex
|
||||
~root aaa login root root
|
||||
# ~tcp listen ":9393"
|
||||
~cli
|
||||
@lex lex
|
||||
|
||||
~aaa
|
||||
login root root
|
||||
|
||||
~tcp
|
||||
listen :9393
|
||||
|
||||
# ~tcp dial ":9393"
|
||||
~cli remote slaver listen ":9393" tcp
|
||||
# ~cli remote slaver listen ":9393" tcp
|
||||
# @debug on
|
||||
|
||||
~mdb open chat chat "chat:chat@/chat" mysql
|
||||
~web listen
|
||||
# ~mdb open chat chat "chat:chat@/chat" mysql
|
||||
# ~web listen
|
||||
# ~ssh listen :9898
|
||||
return
|
||||
|
||||
|
||||
|
@ -89,12 +89,12 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
|
||||
// }}}
|
||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||
switch aaa.Context {
|
||||
case m.Target:
|
||||
if aaa.Context == Index {
|
||||
return false
|
||||
}
|
||||
|
||||
switch aaa.Context {
|
||||
case m.Target:
|
||||
if len(aaa.Context.Requests) == 0 {
|
||||
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||
}
|
||||
|
@ -355,12 +355,12 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||
switch cli.Context {
|
||||
case m.Target:
|
||||
if cli.Context == Index {
|
||||
return false
|
||||
}
|
||||
|
||||
switch cli.Context {
|
||||
case m.Target:
|
||||
if len(cli.Context.Requests) == 0 {
|
||||
m.Log("info", nil, "%s close %v", Pulse.Cap("nterm"), arg)
|
||||
}
|
||||
|
@ -55,17 +55,17 @@ type Context struct {
|
||||
Configs map[string]*Config
|
||||
Commands map[string]*Command
|
||||
|
||||
contexts map[string]*Context
|
||||
context *Context
|
||||
root *Context
|
||||
context *Context
|
||||
contexts map[string]*Context
|
||||
|
||||
Sessions map[string]*Message
|
||||
Historys []*Message
|
||||
Requests []*Message
|
||||
|
||||
messages chan *Message
|
||||
message *Message
|
||||
Master *Context
|
||||
messages chan *Message
|
||||
|
||||
Pulse *Message
|
||||
Requests []*Message
|
||||
Historys []*Message
|
||||
Sessions map[string]*Message
|
||||
|
||||
Index map[string]*Context
|
||||
Groups map[string]*Context
|
||||
@ -86,24 +86,20 @@ func (c *Context) Register(s *Context, x Server) *Context { // {{{
|
||||
c.contexts[s.Name] = s
|
||||
s.context = c
|
||||
s.Server = x
|
||||
|
||||
if c.root != nil {
|
||||
s.root = c.root
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (c *Context) Spawn(m *Message, name string, help string) *Context { // {{{
|
||||
s := &Context{Name: name, Help: help, context: c}
|
||||
s := &Context{Name: name, Help: help, root: c.root, context: c}
|
||||
|
||||
if c.Server != nil {
|
||||
if m.Target = s; c.Server != nil {
|
||||
c.Register(s, c.Server.Spawn(m, s, m.Meta["detail"]...))
|
||||
} else {
|
||||
c.Register(s, nil)
|
||||
}
|
||||
|
||||
if m.Target = s; m.Template != nil {
|
||||
if m.Template != nil {
|
||||
m.Template.Source = s
|
||||
}
|
||||
|
||||
@ -115,21 +111,22 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
c.Caches["status"] = &Cache{Name: "服务状态(begin/start/close)", Value: "begin", Help: "服务状态,begin:初始完成,start:正在运行,close:未在运行"}
|
||||
c.Caches["stream"] = &Cache{Name: "服务数据", Value: "", Help: "服务数据"}
|
||||
|
||||
m.Index = 1
|
||||
c.Pulse = m
|
||||
c.Requests = []*Message{m}
|
||||
c.Historys = []*Message{m}
|
||||
|
||||
c.Master = m.Master.Master
|
||||
c.Owner = m.Master.Owner
|
||||
c.Group = m.Master.Group
|
||||
|
||||
m.Log("begin", nil, "%d %v", m.Capi("ncontext", 1), m.Meta["detail"])
|
||||
m.Log("begin", nil, "%d context %v", m.Capi("ncontext", 1), m.Meta["detail"])
|
||||
for k, x := range c.Configs {
|
||||
if x.Hand != nil {
|
||||
m.Conf(k, x.Value)
|
||||
}
|
||||
}
|
||||
|
||||
m.Index = 1
|
||||
c.Historys = []*Message{m}
|
||||
c.Requests = []*Message{m}
|
||||
|
||||
if c.Server != nil {
|
||||
c.Server.Begin(m, m.Meta["detail"]...)
|
||||
}
|
||||
@ -139,42 +136,35 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
|
||||
// }}}
|
||||
func (c *Context) Start(m *Message) bool { // {{{
|
||||
if m.Cap("status") != "start" && c.Server != nil {
|
||||
if c.Requests = append(c.Requests, m); m.Cap("status") != "start" {
|
||||
running := make(chan bool)
|
||||
go m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log(m.Cap("status", "start"), c, "%d %v", m.Capi("nserver", 1), m.Meta["detail"])
|
||||
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.Capi("nserver", 1), m.Meta["detail"])
|
||||
|
||||
if m != c.Requests[0] {
|
||||
c.Requests = append(c.Requests, m)
|
||||
}
|
||||
|
||||
if running <- true; c.Server.Start(m, m.Meta["detail"]...) {
|
||||
if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) {
|
||||
c.Close(m, m.Meta["detail"]...)
|
||||
}
|
||||
})
|
||||
<-running
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
m.Log("close", c, "close %v", arg)
|
||||
m.Log("close", c, "%v", arg)
|
||||
|
||||
if m.Target == c {
|
||||
switch {
|
||||
case m.Index == 0:
|
||||
if m.Index == 0 {
|
||||
for i := len(c.Requests) - 1; i >= 0; i-- {
|
||||
v := c.Requests[i]
|
||||
v.Index = -1
|
||||
if v.Source != c && !v.Source.Close(v, arg...) {
|
||||
if v.Index = -1; v.Source != c && !v.Source.Close(v, arg...) {
|
||||
v.Index = i
|
||||
return false
|
||||
}
|
||||
c.Requests = c.Requests[:i]
|
||||
}
|
||||
case m.Index > 0:
|
||||
} else if m.Index > 0 {
|
||||
for i := m.Index - 1; i < len(c.Requests)-1; i++ {
|
||||
c.Requests[i] = c.Requests[i+1]
|
||||
}
|
||||
@ -193,22 +183,23 @@ func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
return true
|
||||
}
|
||||
|
||||
if len(c.Requests) > 0 {
|
||||
if len(c.Requests) > 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
m.Log(m.Cap("status", "close"), c, "%d server %v", m.root.Capi("nserver", -1)+1, arg)
|
||||
if c.context != nil && len(c.contexts) == 0 {
|
||||
m.Log("close", c, "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
||||
delete(c.context.contexts, c.Name)
|
||||
}
|
||||
|
||||
if m.Cap("status") == "start" {
|
||||
m.Log(m.Cap("status", "close"), nil, "%d server %v", m.root.Capi("nserver", -1)+1, arg)
|
||||
for _, v := range c.Sessions {
|
||||
if v.Target != c {
|
||||
v.Target.Close(v, arg...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if m.Index == 0 && c.context != nil && len(c.contexts) == 0 {
|
||||
m.Log("close", nil, "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
||||
delete(c.context.contexts, c.Name)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@ -392,6 +383,7 @@ type Message struct {
|
||||
code int
|
||||
time time.Time
|
||||
|
||||
Recv chan bool
|
||||
Wait chan bool
|
||||
Meta map[string][]string
|
||||
Data map[string]interface{}
|
||||
@ -538,7 +530,7 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
||||
e = errors.New("error")
|
||||
}
|
||||
|
||||
m.Set("result", "error: ", fmt.Sprintln(e))
|
||||
m.Set("result", "error: ", fmt.Sprintln(e), "\n")
|
||||
panic(e)
|
||||
}
|
||||
|
||||
@ -832,7 +824,7 @@ func (m *Message) End(s bool) { // {{{
|
||||
|
||||
func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
|
||||
for _, c := range []*Context{m.Target, m.Target.Master, m.Source, m.Source.Master} {
|
||||
for _, c := range []*Context{m.Target, m.Target.Master, m.Target.Owner, m.Source, m.Source.Master, m.Source.Owner} {
|
||||
for s := c; s != nil; s = s.context {
|
||||
|
||||
m.Master = m.Source
|
||||
|
@ -1,39 +1,153 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
package ssh // {{{
|
||||
// }}}
|
||||
import ( // {{{
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// }}}
|
||||
|
||||
type SSH struct {
|
||||
send map[string]*ctx.Message
|
||||
*bufio.Reader
|
||||
net.Conn
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
c.Caches = map[string]*ctx.Cache{}
|
||||
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"nsend": &ctx.Cache{Name: "nsend", Value: "0", Help: "消息发送数量"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
c.Commands = map[string]*ctx.Command{}
|
||||
|
||||
s := new(SSH)
|
||||
s.Context = c
|
||||
|
||||
s.send = make(map[string]*ctx.Message)
|
||||
return s
|
||||
}
|
||||
|
||||
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
// }}}
|
||||
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
if ssh.Context == Index {
|
||||
Pulse = m
|
||||
}
|
||||
return ssh
|
||||
}
|
||||
|
||||
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
// }}}
|
||||
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
ssh.Owner = nil
|
||||
ssh.Conn = m.Data["io"].(net.Conn)
|
||||
ssh.Reader = bufio.NewReader(ssh.Conn)
|
||||
m.Log("info", nil, "%d remote %v", 0, ssh.Conn.RemoteAddr())
|
||||
|
||||
target := m.Target
|
||||
msg := m.Spawn(target)
|
||||
|
||||
for {
|
||||
line, e := ssh.Reader.ReadString('\n')
|
||||
m.Assert(e)
|
||||
|
||||
if line = strings.TrimSpace(line); len(line) == 0 {
|
||||
if msg.Has("detail") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["option"])
|
||||
msg.Cmd(msg.Meta["detail"]...)
|
||||
target = msg.Target
|
||||
|
||||
fmt.Fprintf(ssh.Conn, "result: ")
|
||||
for _, v := range msg.Meta["result"] {
|
||||
fmt.Fprintf(ssh.Conn, "%s", url.QueryEscape(v))
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
|
||||
msg.Meta["append"] = append(msg.Meta["append"], "nsend")
|
||||
msg.Add("append", "nsend", msg.Get("nsend"))
|
||||
for _, k := range msg.Meta["append"] {
|
||||
for _, v := range msg.Meta[k] {
|
||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
} else if msg.Has("result") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
||||
send := ssh.send[msg.Get("nsend")]
|
||||
send.Meta = msg.Meta
|
||||
send.Recv <- true
|
||||
}
|
||||
msg = m.Spawn(target)
|
||||
continue
|
||||
}
|
||||
|
||||
ls := strings.SplitN(line, ":", 2)
|
||||
ls[0] = strings.TrimSpace(ls[0])
|
||||
ls[1], e = url.QueryUnescape(strings.TrimSpace(ls[1]))
|
||||
m.Assert(e)
|
||||
msg.Add("option", ls[0], ls[1])
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
||||
// }}}
|
||||
func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||
return false
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||
Caches: map[string]*ctx.Cache{},
|
||||
Caches: map[string]*ctx.Cache{
|
||||
"nhost": &ctx.Cache{Name: "nhost", Value: "0", Help: "主机数量"},
|
||||
},
|
||||
Configs: map[string]*ctx.Config{},
|
||||
Commands: map[string]*ctx.Command{},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||
tcp := m.Find("tcp", true) // {{{
|
||||
tcp.Cmd(m.Meta["detail"]...)
|
||||
return ""
|
||||
// }}}
|
||||
}},
|
||||
"dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||
tcp := m.Find("tcp", true) // {{{
|
||||
tcp.Cmd(m.Meta["detail"]...)
|
||||
return ""
|
||||
// }}}
|
||||
}},
|
||||
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||
m.Start("host"+Pulse.Cap("nhost"), "主机连接") // {{{
|
||||
return ""
|
||||
// }}}
|
||||
}},
|
||||
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||
ssh, ok := m.Target.Server.(*SSH) // {{{
|
||||
m.Assert(ok)
|
||||
m.Capi("nsend", 1)
|
||||
m.Recv = make(chan bool)
|
||||
m.Add("option", "nsend", m.Cap("nsend"))
|
||||
ssh.send[m.Cap("nsend")] = m
|
||||
|
||||
for _, v := range arg {
|
||||
fmt.Fprintf(ssh.Conn, "detail: %v\n", v)
|
||||
}
|
||||
for _, k := range m.Meta["option"] {
|
||||
for _, v := range m.Meta[k] {
|
||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
<-m.Recv
|
||||
return ""
|
||||
// }}}
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user