1
0
forked from x/ContextOS

tce add cli.runtime

This commit is contained in:
shaoying 2018-11-08 22:32:22 +08:00
parent 2ff1d9fa4c
commit 911f679a40
7 changed files with 168 additions and 90 deletions

View File

@ -2,10 +2,15 @@ package cli
import (
"contexts/ctx"
"io/ioutil"
"syscall"
"toolkit"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
@ -137,10 +142,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"time_close": &ctx.Config{Name: "time_close(open/close)", Value: "open", Help: "时间区间"},
"cmd_script": &ctx.Config{Name: "cmd_script", Value: map[string]interface{}{
".sh": "bash",
".py": "python",
".shy": "source",
"sh": "bash",
"py": "python",
"shy": "source",
}, Help: "系统命令超时"},
"cmd_timeout": &ctx.Config{Name: "cmd_timeout", Value: "60s", Help: "系统命令超时"},
"cmd_combine": &ctx.Config{Name: "cmd_combine", Value: map[string]interface{}{
"vi": map[string]interface{}{"active": true},
@ -216,7 +222,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
msg := m
for k, v := range m.Confv("cmd_script").(map[string]interface{}) {
if strings.HasSuffix(detail[0], k) {
if strings.HasSuffix(detail[0], "."+k) {
detail = append([]string{v.(string)}, detail...)
msg = m.Spawn(cli.target)
break
@ -307,7 +313,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
for _, v := range exports {
m.Log("info", "export %v", v)
if v["file"] != "" {
m.Spawn().Copy(msg, "option").Copy(msg, "append").Copy(msg, "result").Cmd("export", v["file"])
m.Sess("nfs").Copy(msg, "option").Copy(msg, "append").Copy(msg, "result").Cmd("export", v["file"])
}
if v["cache"] != "" {
if v["index"] == "result" {
@ -327,7 +333,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
if len(rest) > 0 {
pipe := m.Spawn().Copy(msg, "option").Copy(msg, "append").Cmd("cmd", rest)
msg.Set("result").Set("append").Copy(pipe, "result").Copy(pipe, "append")
msg.Set("result").Set("append")
m.Log("fuck", "what %v", msg.Meta)
msg.Copy(pipe, "result").Copy(pipe, "append")
m.Log("fuck", "what %v", msg.Meta)
}
m.Target().Message().Set("result").Set("append").Copy(msg, "result").Copy(msg, "append")
@ -633,7 +643,13 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
m.Copy(msg, "target")
}
m.Start(fmt.Sprintf("shell%d", m.Capi("nshell", 1)), "shell", key, arg[0])
name := fmt.Sprintf("shell%d", m.Capi("nshell", 1))
if arg[0] == "stdio" {
name = "shy"
}
m.Start(name, "shell", key, arg[0])
if arg[0] == "stdio" {
if m.Sess("nfs").Cmd("path", m.Confx("init.shy", arg, 1)).Results(0) {
m.Spawn().Add("option", "scan_end", "false").Cmd("source", m.Conf("init.shy"))
@ -846,6 +862,64 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
}
}
}},
"sysinfo": &ctx.Command{Name: "sysinfo", Help: "sysinfo", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
sys := &syscall.Sysinfo_t{}
syscall.Sysinfo(sys)
d, e := time.ParseDuration(fmt.Sprintf("%ds", sys.Uptime))
m.Assert(e)
m.Append("NumCPU", runtime.NumCPU())
m.Append("uptime", d)
m.Append("procs", sys.Procs)
m.Append("total", kit.FmtSize(sys.Totalram))
m.Append("free", kit.FmtSize(sys.Freeram))
m.Append("mper", fmt.Sprintf("%d%%", sys.Freeram*100/sys.Totalram))
fs := &syscall.Statfs_t{}
syscall.Statfs("./", fs)
m.Append("blocks", kit.FmtSize(fs.Blocks*uint64(fs.Bsize)))
m.Append("bavail", kit.FmtSize(fs.Bavail*uint64(fs.Bsize)))
m.Append("bper", fmt.Sprintf("%d%%", fs.Bavail*100/fs.Blocks))
m.Append("files", fs.Files)
m.Append("ffree", fs.Ffree)
m.Append("fper", fmt.Sprintf("%d%%", fs.Ffree*100/fs.Files))
m.Table()
}},
"runtime": &ctx.Command{Name: "runtime", Help: "runtime", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
mem := &runtime.MemStats{}
runtime.ReadMemStats(mem)
m.Append("NumGoroutine", runtime.NumGoroutine())
m.Append("NumGC", mem.NumGC)
m.Append("other", kit.FmtSize(mem.OtherSys))
m.Append("stack", kit.FmtSize(mem.StackSys))
m.Append("heapsys", kit.FmtSize(mem.HeapSys))
m.Append("heapinuse", kit.FmtSize(mem.HeapInuse))
m.Append("heapidle", kit.FmtSize(mem.HeapIdle))
m.Append("heapalloc", kit.FmtSize(mem.HeapAlloc))
m.Append("lookups", mem.Lookups)
m.Append("objects", mem.HeapObjects)
m.Table()
}},
"develop": &ctx.Command{Name: "develop", Help: "develop", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Append("nclient", strings.Count(m.Spawn().Cmd("system", "tmux", "list-clients").Result(0), "\n"))
m.Append("nsesion", strings.Count(m.Spawn().Cmd("system", "tmux", "list-sessions").Result(0), "\n"))
m.Append("nwindow", strings.Count(m.Spawn().Cmd("system", "tmux", "list-windows", "-a").Result(0), "\n"))
m.Append("npane", strings.Count(m.Spawn().Cmd("system", "tmux", "list-panes", "-a").Result(0), "\n"))
m.Append("ncommand", strings.Count(m.Spawn().Cmd("system", "tmux", "list-commands").Result(0), "\n"))
m.Append("nkey", strings.Count(m.Spawn().Cmd("system", "tmux", "list-keys").Result(0), "\n"))
m.Append("nbuffer", strings.Count(m.Spawn().Cmd("system", "tmux", "list-buffers").Result(0), "\n"))
nw, _ := ioutil.ReadFile("var/.nwrite")
m.Append("nwrite", string(nw))
nr, _ := ioutil.ReadFile("var/.nread")
m.Append("nread", string(nr))
m.Table()
}},
"label": &ctx.Command{Name: "label name", Help: "记录当前脚本的位置, name: 位置名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) {

View File

@ -3205,6 +3205,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
if m.Meta["parse"][1] != "" {
value = Chain(m, value, m.Meta["parse"][1])
}
m.Log("fuck", "what %v", value)
switch val := value.(type) {
case map[string]interface{}:
@ -3281,7 +3282,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
return
}
m.Table()
m.Set("result").Table()
}},
"import": &Command{Name: "import filename", Help: "导入数据", Hand: func(m *Message, c *Context, key string, arg ...string) {
f, e := os.Open(arg[0])
@ -3324,51 +3325,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
}
m.Table()
}},
"export": &Command{Name: "export filename", Help: "导出数据", Hand: func(m *Message, c *Context, key string, arg ...string) {
f, e := os.Create(arg[0])
m.Assert(e)
defer f.Close()
switch {
case strings.HasSuffix(arg[0], ".json"):
data := []interface{}{}
nrow := len(m.Meta[m.Meta["append"][0]])
for i := 0; i < nrow; i++ {
line := map[string]interface{}{}
for _, k := range m.Meta["append"] {
line[k] = m.Meta[k][i]
}
data = append(data, line)
}
en := json.NewEncoder(f)
en.SetIndent("", " ")
en.Encode(data)
case strings.HasSuffix(arg[0], ".csv"):
w := csv.NewWriter(f)
line := []string{}
for _, v := range m.Meta["append"] {
line = append(line, v)
}
w.Write(line)
nrow := len(m.Meta[m.Meta["append"][0]])
for i := 0; i < nrow; i++ {
line := []string{}
for _, k := range m.Meta["append"] {
line = append(line, m.Meta[k][i])
}
w.Write(line)
}
w.Flush()
default:
for _, v := range m.Meta["result"] {
f.WriteString(v)
}
}
}},
},
}

View File

@ -3,6 +3,7 @@ package nfs
import (
"bufio"
"contexts/ctx"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
@ -47,6 +48,10 @@ func open(m *ctx.Message, name string, arg ...int) (string, *os.File, error) {
paths := m.Confv("paths").([]interface{})
for _, v := range paths {
p := path.Join(v.(string), name)
if len(arg) > 0 {
name = p
break
}
if s, e := os.Stat(p); e == nil && !s.IsDir() {
name = p
break
@ -1029,6 +1034,51 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}
}
}},
"export": &ctx.Command{Name: "export filename", Help: "导出数据", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
_, f, e := open(m, arg[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
m.Assert(e)
defer f.Close()
switch {
case strings.HasSuffix(arg[0], ".json"):
data := []interface{}{}
nrow := len(m.Meta[m.Meta["append"][0]])
for i := 0; i < nrow; i++ {
line := map[string]interface{}{}
for _, k := range m.Meta["append"] {
line[k] = m.Meta[k][i]
}
data = append(data, line)
}
en := json.NewEncoder(f)
en.SetIndent("", " ")
en.Encode(data)
case strings.HasSuffix(arg[0], ".csv"):
w := csv.NewWriter(f)
line := []string{}
for _, v := range m.Meta["append"] {
line = append(line, v)
}
w.Write(line)
nrow := len(m.Meta[m.Meta["append"][0]])
for i := 0; i < nrow; i++ {
line := []string{}
for _, k := range m.Meta["append"] {
line = append(line, m.Meta[k][i])
}
w.Write(line)
}
w.Flush()
default:
for _, v := range m.Meta["result"] {
f.WriteString(v)
}
}
}},
"pwd": &ctx.Command{Name: "pwd [all] | [[index] path] ", Help: "工作目录all: 查看所有, index path: 设置路径, path: 设置当前路径", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if len(arg) > 0 && arg[0] == "all" {

View File

@ -33,6 +33,30 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
},
"index": []interface{}{
map[string]interface{}{"name": "head", "template": "head"},
map[string]interface{}{"name": "sysinfo", "help": "sysinfo", "template": "componet",
"context": "cli", "command": "sysinfo",
"inputs": []interface{}{
map[string]interface{}{"type": "button", "label": "refresh"},
},
"pre_run": true,
"display_result": "",
},
map[string]interface{}{"name": "runtime", "help": "runtime", "template": "componet",
"context": "cli", "command": "runtime",
"inputs": []interface{}{
map[string]interface{}{"type": "button", "label": "refresh"},
},
"pre_run": true,
"display_result": "",
},
map[string]interface{}{"name": "develop", "help": "develop", "template": "componet",
"context": "cli", "command": "develop",
"inputs": []interface{}{
map[string]interface{}{"type": "button", "label": "refresh"},
},
"pre_run": true,
"display_result": "",
},
map[string]interface{}{"name": "clipbaord", "help": "clipbaord", "template": "clipboard"},
map[string]interface{}{"name": "buffer", "help": "buffer", "template": "componet",
"context": "cli", "command": "tmux", "arguments": []interface{}{"buffer"}, "inputs": []interface{}{
@ -43,7 +67,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
"pre_run": true,
},
map[string]interface{}{"name": "command", "help": "command", "template": "componet",
"context": "cli.shell1", "command": "source", "arguments": []interface{}{"@cmd"},
"context": "cli.shy", "command": "source", "arguments": []interface{}{"@cmd"},
"inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "cmd", "value": "",
"class": "cmd", "clipstack": "clistack",
@ -68,7 +92,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
},
},
map[string]interface{}{"name": "upload", "help": "upload", "template": "componet",
"form_type": "upload",
"context": "web", "command": "upload", "form_type": "upload",
"inputs": []interface{}{
map[string]interface{}{"type": "file", "name": "upload"},
map[string]interface{}{"type": "submit", "value": "submit"},
@ -111,32 +135,6 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心",
map[string]interface{}{"type": "text", "name": "dir", "label": "dir"},
},
},
map[string]interface{}{"name": "web_site", "help": "web_site", "template": "componet",
"context": "web", "command": "config", "arguments": []interface{}{
"web_site", "format_field", "site", "<a href='%s'>%s<a>",
},
"display_result": "",
},
map[string]interface{}{"name": "prompt", "help": "prompt", "template": "componet",
"context": "nfs.stdio", "command": "prompt", "arguments": []interface{}{"@string"},
"inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "string", "label": "string"},
map[string]interface{}{"type": "button", "label": "refresh"},
},
},
map[string]interface{}{"name": "exec", "help": "exec", "template": "componet",
"context": "nfs.stdio", "command": "exec", "arguments": []interface{}{"@string"},
"inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "string"},
},
},
map[string]interface{}{"name": "show", "help": "show", "template": "componet",
"context": "nfs.stdio", "command": "show", "arguments": []interface{}{"\n", "@string", "\n"},
"inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "string", "label": "string"},
map[string]interface{}{"type": "button", "label": "refresh"},
},
},
map[string]interface{}{"name": "tail", "template": "tail"},
},
}, Help: "组件列表"},

View File

@ -8,17 +8,17 @@ import (
"path"
)
func FmtSize(size int64) string {
if size > 1000000000 {
return fmt.Sprintf("%d.%dG", size/1000000000, size/100000000%100)
func FmtSize(size uint64) string {
if size > 1<<30 {
return fmt.Sprintf("%d.%dG", size>>30, (size>>20)%1024*100/1024)
}
if size > 1000000 {
return fmt.Sprintf("%d.%dM", size/100000, size/100000%100)
if size > 1<<20 {
return fmt.Sprintf("%d.%dM", size>>20, (size>>10)%1024*100/1024)
}
if size > 1000 {
return fmt.Sprintf("%d.%dK", size/1000, size/100%100)
if size > 1<<10 {
return fmt.Sprintf("%d.%dK", size>>10, size%1024*100/1024)
}
return fmt.Sprintf("%dB", size)

View File

@ -56,7 +56,7 @@ context = {
return "";
}
document.cookie = key+"="+value;
document.cookie = key+"="+value+";path=/";
return this.Cookie(key);
},
Cache: function(key, cb, sync) {

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=0.7">
<title>{{option .Meta "page_title"}}</title>
<title>code</title>
<style>
html, body {
height:100%;