diff --git a/console/cli.go b/console/cli.go index 1598b38..1068172 100644 --- a/console/cli.go +++ b/console/cli.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "time" + "sort" ) var ( @@ -52,6 +53,26 @@ func KeyValueToMap(value string, sep string) (map[string]interface{}, error) { return m, nil } +type nodeHost struct { + name string + host string +} +type nodeHostSlice []nodeHost +func (a nodeHostSlice) Len() int { + return len(a) +} + +func (a nodeHostSlice) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +func (a nodeHostSlice) Less(i, j int) bool { + if strings.Compare(a[i].name, a[j].name) == -1 { + return true + } + return false +} + type CongoCli struct { baseUrl string cmd *cobra.Command @@ -99,9 +120,15 @@ func (c *CongoCli) list(cmd *cobra.Command, args []string) { fmt.Printf("Could not find any record.\n") os.Exit(0) } + tempNodes := make([]nodeHost, 0, len(nodes)) for _, v := range nodes { node := v.(map[string]interface{}) - fmt.Printf("%s (host: %s)\n", node["name"], node["host"]) + tempNodes = append(tempNodes, nodeHost{name:node["name"].(string), + host: node["host"].(string)}) + } + sort.Sort(nodeHostSlice(tempNodes)) + for _, v := range tempNodes { + fmt.Printf("%s (host: %s)\n", v.name, v.host) } } @@ -121,6 +148,10 @@ func (c *CongoCli) show(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "Usage: congo show \n") os.Exit(1) } + if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){ + fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n") + os.Exit(1) + } ret, err := congo.Show(args[0]) if err != nil { fmt.Fprintf(os.Stderr, "Could not get resources detail, %s\n", err.Error()) @@ -146,6 +177,10 @@ func (c *CongoCli) logging(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "Usage: congo logging on/off \n") os.Exit(1) } + if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){ + fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n") + os.Exit(1) + } if args[1] != "on" && args[1] != "off" { fmt.Fprintf(os.Stderr, "Usage: congo logging on/off \n") os.Exit(1) @@ -173,6 +208,10 @@ func (c *CongoCli) delete(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "Usage: congo delete \n") os.Exit(1) } + if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){ + fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n") + os.Exit(1) + } _, err := congo.Delete(args[0]) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err.Error()) @@ -200,6 +239,10 @@ func (c *CongoCli) create(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "Usage: congo create driver=ssh ondemand=true --param key=val,key=val\n") os.Exit(1) } + if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){ + fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n") + os.Exit(1) + } attribs, err := KeyValueArrayToMap(args[1:], "=") if err != nil { fmt.Fprintf(os.Stderr, "Parse failed, err=%s\n", err.Error()) @@ -234,6 +277,10 @@ func (c *CongoCli) console(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "Usage: congo console \n") os.Exit(1) } + if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){ + fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n") + os.Exit(1) + } retry := true common.NewTaskManager(100, 16) for retry { diff --git a/console/server.go b/console/server.go index 03aa01c..52ccfc1 100644 --- a/console/server.go +++ b/console/server.go @@ -512,11 +512,7 @@ func (m *NodeManager) ShowNode(name string) (*Node, int, string) { return nil, http.StatusBadRequest, fmt.Sprintf("The node %s is not exist.", name) } node = nodeManager.Nodes[name] - if err := node.RequireLock(true); err != nil { - return nil, http.StatusConflict, err.Error() - } node.State = STATUS_MAP[node.GetStatus()] - node.Release(true) } else { nodeWithHost := m.stor.ListNodeWithHost() if nodeWithHost == nil {