func (m *Manager) deploy() { if !m.isClusterSet() { return } var err error w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) fmt.Fprintf(w, "NODE\tSERVICE\tSTATUS\n") services := cluster.ListServices(m.Cluster) servicesNames := make([]string, 0, len(services)) for name, _ := range services { servicesNames = append(servicesNames, name) } nodes := cluster.ListNodes(m.Cluster, false) nodesNames := make([]string, 0, len(nodes)) for name, _ := range nodes { nodesNames = append(nodesNames, name) } if len(nodes) < len(services) { fmt.Println("Cannot deploy: not enough nodes") return } for i := 0; i < len(services); i++ { service := servicesNames[i] node := nodesNames[i] address := nodes[node] status := "done" err = network.SendUpdateCommand(address, "node-base-services", []string{service}) if err != nil { fmt.Println("Error sending update command to ", address) status = "error" } else { err = network.SendStartServiceCommand(address, service) if err != nil { fmt.Println("Error sending start service command to node ", node) status = "error" } } fmt.Fprintf(w, "%s\t%s\t%s\n", node, service, status, ) } w.Flush() }
func (m *Manager) list(cmd string) { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) args := strings.Split(strings.TrimSuffix(strings.TrimSpace(cmd), ";"), " ") if len(args) != 2 { fmt.Printf("Not enough arguments %q.\n", cmd) return } var names map[string]string switch args[1] { case "clusters": names = cluster.ListClusters() fmt.Fprintf(w, "NAME\tUUID\n") case "nodes": if !m.isClusterSet() { return } names = cluster.ListNodes(m.Cluster, false) fmt.Fprintf(w, "NAME\tADDRESS\n") case "services": if !m.isClusterSet() { return } names = cluster.ListServices(m.Cluster) fmt.Fprintf(w, "NAME\tIMAGE\n") default: fmt.Println("Unrecognized identifier. Please specify clusters/nodes") return } for name, _ := range names { fmt.Fprintf(w, "%s\t%s\n", name, names[name]) } fmt.Fprintf(w, "-------------------\n") fmt.Fprintf(w, "TOTAL ACTIVE: %d\n", len(names)) w.Flush() }
func setService(clusterName string, who string, what string, to_what []string) { nodes := cluster.ListNodes(clusterName, false) names := []string{} for name, _ := range cluster.ListServices(clusterName) { names = append(names, name) } ok, _ := checkValidServices([]string{who}, names) if !ok { fmt.Println("Unrecognized service ", who) return } service := cluster.GetService(clusterName, who) switch what { case "TODO": cluster.UpdateService(clusterName, who, service) for _, address := range nodes { network.SendUpdateCommand(address, "TODO", who) } default: fmt.Println("Unrecognized parameter ", what) } }
func (m *Manager) undeploy() { if !m.isClusterSet() { return } var err error w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) fmt.Fprintf(w, "NODE\tSTATUS\n") services := cluster.ListServices(m.Cluster) nodes := cluster.ListNodes(m.Cluster, false) for node, address := range nodes { status := "done" err = network.SendUpdateCommand(address, "node-base-services", []string{}) if err != nil { fmt.Println("Error sending update command to ", address) status = "error" } for service, _ := range services { err = network.SendStopServiceCommand(address, service) if err != nil { fmt.Println("Error sending stop service command to node ", node) status = "error" } } fmt.Fprintf(w, "%s\t%s\n", node, status, ) } w.Flush() }
func setNode(clusterName string, who string, what string, to_what []string) { nodes := cluster.ListNodes(clusterName, false) dest := []string{} if who == "all" { for _, address := range nodes { dest = append(dest, address) } } else { if address, ok := nodes[who]; !ok { fmt.Println("Unrecognized node ", who) return } else { dest = append(dest, address) } } switch what { case "base-services": services := cluster.ListServices(clusterName) names := make([]string, 0, len(services)) for k, _ := range services { names = append(names, k) } ok, notValid := checkValidServices(to_what, names) if !ok { fmt.Println("Services are not valid:") for _, name := range notValid { fmt.Println(name) } return } for _, address := range dest { err := network.SendUpdateCommand(address, "node-base-services", to_what) if err != nil { fmt.Println("Error sending update command to ", address) } } case "cpumin": cpumin := to_what[0] if ok, value := checkValidCpuValue(cpumin); ok { for _, address := range dest { err := network.SendUpdateCommand(address, "node-cpumin", value) if err != nil { fmt.Println("Error sending update command to ", address) } } } else { fmt.Println("CPU value not valid: it should be a float between 0.0 and 1.0") } case "cpumax": cpumax := to_what[0] if ok, value := checkValidCpuValue(cpumax); ok { for _, address := range dest { err := network.SendUpdateCommand(address, "node-cpumax", value) if err != nil { fmt.Println("Error sending update command to ", address) } } } else { fmt.Println("CPU value not valid: it should be a float between 0.0 and 1.0") } default: fmt.Println("Unrecognized parameter ", what) } }