From a1141a405ac2d281c08cf4d126ed85ecd772d780 Mon Sep 17 00:00:00 2001 From: "meilin.huang" <954537473@qq.com> Date: Wed, 24 Mar 2021 17:22:38 +0800 Subject: [PATCH] =?UTF-8?q?del:=20=E7=A7=BB=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock-server/controllers/machine.go | 127 ----------- mock-server/controllers/mock.go | 2 +- mock-server/machine/machine.go | 192 ---------------- mock-server/machine/machine_test.go | 142 ------------ mock-server/machine/shell.go | 34 --- .../machine/shell/get_process_by_name.sh | 23 -- mock-server/machine/shell/monitor.sh | 13 -- mock-server/machine/shell/sys_info.sh | 192 ---------------- mock-server/machine/shell/system_info.sh | 41 ---- mock-server/machine/status.go | 105 --------- mock-server/machine/ws_shell_session.go | 212 ------------------ mock-server/models/machine.go | 37 --- 12 files changed, 1 insertion(+), 1119 deletions(-) delete mode 100644 mock-server/controllers/machine.go delete mode 100644 mock-server/machine/machine.go delete mode 100644 mock-server/machine/machine_test.go delete mode 100644 mock-server/machine/shell.go delete mode 100644 mock-server/machine/shell/get_process_by_name.sh delete mode 100644 mock-server/machine/shell/monitor.sh delete mode 100644 mock-server/machine/shell/sys_info.sh delete mode 100644 mock-server/machine/shell/system_info.sh delete mode 100644 mock-server/machine/status.go delete mode 100644 mock-server/machine/ws_shell_session.go delete mode 100644 mock-server/models/machine.go diff --git a/mock-server/controllers/machine.go b/mock-server/controllers/machine.go deleted file mode 100644 index 3cb4fc9a..00000000 --- a/mock-server/controllers/machine.go +++ /dev/null @@ -1,127 +0,0 @@ -package controllers - -import ( - "mayfly-go/base" - "mayfly-go/base/biz" - "mayfly-go/base/ctx" - "mayfly-go/base/rediscli" - "mayfly-go/base/utils" - "mayfly-go/mock-server/machine" - "mayfly-go/mock-server/models" - "net/http" - - "github.com/gorilla/websocket" -) - -type MachineController struct { - base.Controller -} - -const machineKey = "ccbscf:machines" - -var upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024 * 1024 * 10, - CheckOrigin: func(r *http.Request) bool { - return true - }, -} - -func (c *MachineController) Machines() { - c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} { - return rediscli.HGetAll(machineKey) - }) -} - -// 创建机器信息 -func (c *MachineController) CreateMachine() { - c.Operation(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) { - machine := &models.Machine{} - c.UnmarshalBodyAndValid(machine) - machine.CreateMachine() - }) -} - -// @router /api/mock-datas/:method [delete] -func (c *MockController) DeleteMachine() { - c.Operation(ctx.NewReqCtx(false, "删除mock数据"), func(account *ctx.LoginAccount) { - models.DeleteMachine(c.Ctx.Input.Param(":ip")) - }) -} - -func (c *MachineController) Run() { - rc := ctx.NewReqCtx(true, "执行机器命令") - c.ReturnData(rc, func(account *ctx.LoginAccount) interface{} { - cmd := c.GetString("cmd") - biz.NotEmpty(cmd, "cmd不能为空") - - rc.ReqParam = cmd - - res, err := c.getCli().Run(cmd) - biz.BizErrIsNil(err, "执行命令失败") - return res - }) -} - -// 系统基本信息 -func (c *MachineController) SysInfo() { - c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} { - res, err := c.getCli().GetSystemInfo() - biz.BizErrIsNil(err, "获取系统基本信息失败") - return res - }) -} - -// top命令信息 -func (c *MachineController) Top() { - c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} { - return c.getCli().GetTop() - }) -} - -func (c *MachineController) GetProcessByName() { - c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} { - name := c.GetString("name") - biz.NotEmpty(name, "name不能为空") - res, err := c.getCli().GetProcessByName(name) - biz.BizErrIsNil(err, "获取失败") - return res - }) -} - -func (c *MachineController) WsSSH() { - wsConn, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil) - if err != nil { - panic(biz.NewBizErr("获取requst responsewirte错误")) - } - - cols, _ := c.GetInt("cols", 80) - rows, _ := c.GetInt("rows", 40) - - sws, err := machine.NewLogicSshWsSession(cols, rows, c.getCli(), wsConn) - if sws == nil { - panic(biz.NewBizErr("连接失败")) - } - //if wshandleError(wsConn, err) { - // return - //} - defer sws.Close() - - quitChan := make(chan bool, 3) - sws.Start(quitChan) - go sws.Wait(quitChan) - - <-quitChan -} - -func (c *MachineController) GetMachineIp() string { - machineIp := c.Ctx.Input.Param(":ip") - biz.IsTrue(utils.StrLen(machineIp) > 0, "ip错误") - return machineIp -} - -func (c *MachineController) getCli() *machine.Cli { - cli, err := machine.GetCli(c.GetMachineIp()) - biz.BizErrIsNil(err, "获取客户端错误") - return cli -} diff --git a/mock-server/controllers/mock.go b/mock-server/controllers/mock.go index eb4ee964..2b0bb40e 100644 --- a/mock-server/controllers/mock.go +++ b/mock-server/controllers/mock.go @@ -10,7 +10,7 @@ import ( "mayfly-go/mock-server/controllers/form" ) -const key = "ccbscf:mock:data" +const key = "mock:data" type MockController struct { base.Controller diff --git a/mock-server/machine/machine.go b/mock-server/machine/machine.go deleted file mode 100644 index 4dc8b9f8..00000000 --- a/mock-server/machine/machine.go +++ /dev/null @@ -1,192 +0,0 @@ -package machine - -import ( - "errors" - "fmt" - "io" - "mayfly-go/base/biz" - "mayfly-go/base/utils" - "mayfly-go/mock-server/models" - "net" - "os" - "sync" - "time" - - "github.com/pkg/sftp" - "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/terminal" -) - -// 客户端信息 -type Cli struct { - machine *models.Machine - // ssh客户端 - client *ssh.Client -} - -// 客户端缓存 -var clientCache sync.Map -var mutex sync.Mutex - -// 从缓存中获取客户端信息,不存在则查库,并新建 -func GetCli(machineIp string) (*Cli, error) { - mutex.Lock() - defer mutex.Unlock() - load, ok := clientCache.Load(machineIp) - if ok { - return load.(*Cli), nil - } - - cli, err := newClient(models.GetMachineByIp(machineIp)) - if err != nil { - return nil, err - } - clientCache.LoadOrStore(machineIp, cli) - return cli, nil -} - -//根据机器信息创建客户端对象 -func newClient(machine *models.Machine) (*Cli, error) { - if machine == nil { - return nil, errors.New("机器不存在") - } - - cli := new(Cli) - cli.machine = machine - err := cli.connect() - if err != nil { - return nil, err - } - return cli, nil -} - -//连接 -func (c *Cli) connect() error { - // 如果已经有client则直接返回 - if c.client != nil { - return nil - } - m := c.machine - config := ssh.ClientConfig{ - User: m.Username, - Auth: []ssh.AuthMethod{ssh.Password(m.Password)}, - HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { - return nil - }, - Timeout: 5 * time.Second, - } - addr := fmt.Sprintf("%s:%d", m.Ip, m.Port) - sshClient, err := ssh.Dial("tcp", addr, &config) - if err != nil { - return err - } - c.client = sshClient - return nil -} - -// 测试连接 -func TestConn(m *models.Machine) (*ssh.Client, error) { - config := ssh.ClientConfig{ - User: m.Username, - Auth: []ssh.AuthMethod{ssh.Password(m.Password)}, - HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { - return nil - }, - Timeout: 5 * time.Second, - } - addr := fmt.Sprintf("%s:%d", m.Ip, m.Port) - sshClient, err := ssh.Dial("tcp", addr, &config) - if err != nil { - return nil, err - } - return sshClient, nil -} - -// 关闭client和并从缓存中移除 -func (c *Cli) Close() { - if c.client != nil { - c.client.Close() - } - if utils.StrLen(c.machine.Ip) > 0 { - clientCache.Delete(c.machine.Ip) - } -} - -// 获取sftp client -func (c *Cli) GetSftpCli() *sftp.Client { - if c.client == nil { - if err := c.connect(); err != nil { - panic(biz.NewBizErr("连接ssh失败:" + err.Error())) - } - } - client, serr := sftp.NewClient(c.client, sftp.MaxPacket(1<<15)) - if serr != nil { - panic(biz.NewBizErr("获取sftp client失败:" + serr.Error())) - } - return client -} - -// 获取session -func (c *Cli) GetSession() (*ssh.Session, error) { - if c.client == nil { - if err := c.connect(); err != nil { - return nil, err - } - } - return c.client.NewSession() -} - -//执行shell -//@param shell shell脚本命令 -func (c *Cli) Run(shell string) (*string, error) { - session, err := c.GetSession() - if err != nil { - c.Close() - return nil, err - } - defer session.Close() - buf, rerr := session.CombinedOutput(shell) - if rerr != nil { - return nil, rerr - } - res := string(buf) - return &res, nil -} - -//执行带交互的命令 -func (c *Cli) RunTerminal(shell string, stdout, stderr io.Writer) error { - session, err := c.GetSession() - if err != nil { - return err - } - //defer session.Close() - - fd := int(os.Stdin.Fd()) - oldState, err := terminal.MakeRaw(fd) - if err != nil { - panic(err) - } - defer terminal.Restore(fd, oldState) - - session.Stdout = stdout - session.Stderr = stderr - session.Stdin = os.Stdin - - termWidth, termHeight, err := terminal.GetSize(fd) - if err != nil { - panic(err) - } - // Set up terminal modes - modes := ssh.TerminalModes{ - ssh.ECHO: 1, // enable echoing - ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud - ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud - } - - // Request pseudo terminal - if err := session.RequestPty("xterm-256color", termHeight, termWidth, modes); err != nil { - return err - } - - return session.Run(shell) -} diff --git a/mock-server/machine/machine_test.go b/mock-server/machine/machine_test.go deleted file mode 100644 index 8fe618f2..00000000 --- a/mock-server/machine/machine_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package machine - -import ( - "fmt" - "mayfly-go/base/utils" - "strings" - "testing" -) - -func TestSSH(t *testing.T) { - //ssh.ListenAndServe("148.70.36.197") - //cli := New("148.70.36.197", "root", "gea&630_..91mn#", 22) - ////output, err := cli.Run("free -h") - ////fmt.Printf("%v\n%v", output, err) - //err := cli.RunTerminal("tail -f /usr/local/java/logs/eatlife-info.log", os.Stdout, os.Stdin) - //fmt.Println(err) - - res := "top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05\nTasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie\n%Cpu(s): 6.2 us, 0.0 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st\nKiB Mem : 1882012 total, 73892 free, 770360 used, 1037760 buff/cache\nKiB Swap: 0 total, 0 free, 0 used. 933492 avail Mem" - split := strings.Split(res, "\n") - //var firstLine string - //for i := 0; i < len(split); i++ { - // if i == 0 { - // val := strings.Split(split[i], "top -")[1] - // vals := strings.Split(val, ",") - // - // } - //} - firstLine := strings.Split(strings.Split(split[0], "top -")[1], ",") - // 17:14:07 up 5 days - up := strings.Trim(strings.Split(firstLine[0], "up")[1], " ") + firstLine[1] - // 2 users - users := strings.Split(strings.Trim(firstLine[2], " "), " ")[0] - // load average: 0.03 - oneMinLa := strings.Trim(strings.Split(strings.Trim(firstLine[3], " "), ":")[1], " ") - fiveMinLa := strings.Trim(firstLine[4], " ") - fietMinLa := strings.Trim(firstLine[5], " ") - fmt.Println(firstLine, up, users, oneMinLa, fiveMinLa, fietMinLa) - tasks := Parse(strings.Split(split[1], "Tasks:")[1]) - cpu := Parse(strings.Split(split[2], "%Cpu(s):")[1]) - mem := Parse(strings.Split(split[3], "KiB Mem :")[1]) - fmt.Println(tasks, cpu, mem) -} - -func Parse(val string) map[string]string { - res := make(map[string]string) - vals := strings.Split(val, ",") - for i := 0; i < len(vals); i++ { - trimData := strings.Trim(vals[i], " ") - keyValue := strings.Split(trimData, " ") - res[keyValue[1]] = keyValue[0] - } - return res -} - -func TestTemplateRev(t *testing.T) { - temp := "hello my name is {name} hahahaha lihaiba {age} years old {public}" - str := "hello my name is hmlhmlhm 慌慌信息 hahahaha lihaiba 15 years old private protected" - - //temp1 := " top - {up}, {users} users, load average: {loadavg}" - //str1 := " top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05" - - //taskTemp := "Tasks: {total} total, {running} running, {sleeping} sleeping, {stopped} stopped, {zombie} zombie" - //taskVal := "Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie" - - //nameRunne := []rune(str) - //index := strings.Index(temp, "{") - //ei := strings.Index(temp, "}") + 1 - //next := temp[ei:] - //key := temp[index+1 : ei-1] - //value := SubString(str, index, UnicodeIndex(str, next)) - res := make(map[string]interface{}) - utils.ReverStrTemplate(temp, str, res) - fmt.Println(res) -} - -//func ReverStrTemplate(temp, str string, res map[string]string) { -// index := UnicodeIndex(temp, "{") -// ei := UnicodeIndex(temp, "}") + 1 -// next := temp[ei:] -// nextContain := UnicodeIndex(next, "{") -// nextIndexValue := next -// if nextContain != -1 { -// nextIndexValue = SubString(next, 0, nextContain) -// } -// key := temp[index+1 : ei-1] -// // 如果后面没有内容了,则取字符串的长度即可 -// var valueLastIndex int -// if nextIndexValue == "" { -// valueLastIndex = StrLen(str) -// } else { -// valueLastIndex = UnicodeIndex(str, nextIndexValue) -// } -// value := SubString(str, index, valueLastIndex) -// res[key] = value -// -// if nextContain != -1 { -// ReverStrTemplate(next, SubString(str, UnicodeIndex(str, value)+StrLen(value), StrLen(str)), res) -// } -//} -// -//func StrLen(str string) int { -// return len([]rune(str)) -//} -// -//func SubString(str string, begin, end int) (substr string) { -// // 将字符串的转换成[]rune -// rs := []rune(str) -// lth := len(rs) -// -// // 简单的越界判断 -// if begin < 0 { -// begin = 0 -// } -// if begin >= lth { -// begin = lth -// } -// if end > lth { -// end = lth -// } -// -// // 返回子串 -// return string(rs[begin:end]) -//} -// -//func UnicodeIndex(str, substr string) int { -// // 子串在字符串的字节位置 -// result := strings.Index(str, substr) -// if result >= 0 { -// // 获得子串之前的字符串并转换成[]byte -// prefix := []byte(str)[0:result] -// // 将子串之前的字符串转换成[]rune -// rs := []rune(string(prefix)) -// // 获得子串之前的字符串的长度,便是子串在字符串的字符位置 -// result = len(rs) -// } -// -// return result -//} - -func TestRunShellFile(t *testing.T) { - -} diff --git a/mock-server/machine/shell.go b/mock-server/machine/shell.go deleted file mode 100644 index f7771f45..00000000 --- a/mock-server/machine/shell.go +++ /dev/null @@ -1,34 +0,0 @@ -package machine - -import ( - "io/ioutil" - "mayfly-go/base/biz" -) - -const BasePath = "./machine/shell/" - -const MonitorTemp = "cpuRate:{cpuRate}%,memRate:{memRate}%,sysLoad:{sysLoad}\n" - -// shell文件内容缓存,避免每次读取文件 -var shellCache = make(map[string]string) - -func (c *Cli) GetProcessByName(name string) (*string, error) { - return c.Run(getShellContent("sys_info")) -} - -func (c *Cli) GetSystemInfo() (*string, error) { - return c.Run(getShellContent("system_info")) -} - -// 获取shell内容 -func getShellContent(name string) string { - cacheShell := shellCache[name] - if cacheShell != "" { - return cacheShell - } - bytes, err := ioutil.ReadFile(BasePath + name + ".sh") - biz.ErrIsNil(err, "获取shell文件失败") - shellStr := string(bytes) - shellCache[name] = shellStr - return shellStr -} diff --git a/mock-server/machine/shell/get_process_by_name.sh b/mock-server/machine/shell/get_process_by_name.sh deleted file mode 100644 index 058706ef..00000000 --- a/mock-server/machine/shell/get_process_by_name.sh +++ /dev/null @@ -1,23 +0,0 @@ -#! /bin/bash -# Function: 根据输入的程序的名字过滤出所对应的PID,并显示出详细信息,如果有几个PID,则全部显示 -NAME=%s -N=`ps -aux | grep $NAME | grep -v grep | wc -l` ##统计进程总数 -if [ $N -le 0 ];then - echo "该进程名没有运行!" -fi -i=1 -while [ $N -gt 0 ] -do - echo "进程PID: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $2}'`" - echo "进程命令:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`" - echo "进程所属用户: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $1}'`" - echo "CPU占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $3}'`%" - echo "内存占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $4}'`%" - echo "进程开始运行的时刻:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $9}'`" - echo "进程运行的时间:` ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`" - echo "进程状态:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $8}'`" - echo "进程虚拟内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $5}'`" - echo "进程共享内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $6}'`" - echo "***************************************************************" - let N-- i++ -done \ No newline at end of file diff --git a/mock-server/machine/shell/monitor.sh b/mock-server/machine/shell/monitor.sh deleted file mode 100644 index 7d0c5f7a..00000000 --- a/mock-server/machine/shell/monitor.sh +++ /dev/null @@ -1,13 +0,0 @@ - - -# 获取监控信息 -function get_monitor_info() { - cpu_rate=$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1) - mem_rate=$(free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}') - sys_load=$(uptime | cut -d: -f5) - cat <&1) - pub_ipadd=$(curl -s http://ddns.oray.com/checkip | awk -F ":" '{print $2}' | awk -F "<" '{print $1}' | awk '{print $1}') - gateway=$(ip route | grep default | awk '{print $3}') - mac_info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}') - dns_config=$(egrep 'nameserver' /etc/resolv.conf) - route_info=$(route -n) - echo -e ' IP信息:' - cat </dev/null) - if [ ${sysversion} -gt 6 ]; then - service_config=$(systemctl list-unit-files --type=service --state=enabled | grep "enabled") - run_service=$(systemctl list-units --type=service --state=running | grep ".service") - else - service_config=$(/sbin/chkconfig | grep -E ":on|:启用" | column -t) - run_service=$(/sbin/service --status-all | grep -E "running") - fi - echo -e ' 服务启动配置:' - cat </dev/null | cut -d/ -f5 - egrep -v "^$|^#" ${cronuser} 2>/dev/null - echo "" - done) - echo -e ' 系统登录用户:' - cat < 0 && msgObj.Rows > 0 { - if err := sws.session.WindowChange(msgObj.Rows, msgObj.Cols); err != nil { - logs.Error("ssh pty change windows size failed") - } - } - case wsMsgCmd: - //handle xterm.js stdin - //decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd) - //if err != nil { - // logs.Error("websock cmd string base64 decoding failed") - // //panic(base.NewBizErr("websock cmd string base64 decoding failed")) - //} - sws.sendWebsocketInputCommandToSshSessionStdinPipe([]byte(msgObj.Msg)) - } - } - } -} - -//sendWebsocketInputCommandToSshSessionStdinPipe -func (sws *LogicSshWsSession) sendWebsocketInputCommandToSshSessionStdinPipe(cmdBytes []byte) { - if _, err := sws.stdinPipe.Write(cmdBytes); err != nil { - logs.Error("ws cmd bytes write to ssh.stdin pipe failed") - } -} - -func (sws *LogicSshWsSession) sendComboOutput(exitCh chan bool) { - wsConn := sws.wsConn - //todo 优化成一个方法 - //tells other go routine quit - defer setQuit(exitCh) - - //every 120ms write combine output bytes into websocket response - tick := time.NewTicker(time.Millisecond * time.Duration(60)) - //for range time.Tick(120 * time.Millisecond){} - defer tick.Stop() - for { - select { - case <-tick.C: - if sws.comboOutput == nil { - return - } - bs := sws.comboOutput.Bytes() - if len(bs) > 0 { - err := wsConn.WriteMessage(websocket.TextMessage, bs) - if err != nil { - logs.Error("ssh sending combo output to webSocket failed") - } - _, err = sws.logBuff.Write(bs) - if err != nil { - logs.Error("combo output to log buffer failed") - } - sws.comboOutput.buffer.Reset() - } - - case <-exitCh: - return - } - } -} - -func (sws *LogicSshWsSession) Wait(quitChan chan bool) { - if err := sws.session.Wait(); err != nil { - setQuit(quitChan) - } -} - -func (sws *LogicSshWsSession) LogString() string { - return sws.logBuff.buffer.String() -} - -func setQuit(ch chan bool) { - ch <- true -} diff --git a/mock-server/models/machine.go b/mock-server/models/machine.go deleted file mode 100644 index 3330573a..00000000 --- a/mock-server/models/machine.go +++ /dev/null @@ -1,37 +0,0 @@ -package models - -import ( - "encoding/json" - "mayfly-go/base/biz" - "mayfly-go/base/rediscli" -) - -const machineKey = "ccbscf:machines" - -type Machine struct { - Name string `json:"name"` - Ip string `json:"ip"` // IP地址 - Username string `json:"username"` // 用户名 - Password string `json:"-"` - Port int `json:"port"` // 端口号 -} - -func (c *Machine) CreateMachine() { - biz.IsTrue(!rediscli.HExist(machineKey, c.Ip), "该机器已存在") - val, _ := json.Marshal(c) - rediscli.HSet(machineKey, c.Ip, val) -} - -func DeleteMachine(ip string) { - rediscli.HDel(machineKey, ip) -} - -func GetMachineByIp(ip string) *Machine { - machine := &Machine{} - json.Unmarshal([]byte(rediscli.HGet(machineKey, ip)), machine) - return machine -} - -func GetMachineList() map[string]string { - return rediscli.HGetAll(machineKey) -}