Пример #1
0
func (m *Manager) update(cmd string) {
	args := strings.Split(strings.TrimSuffix(strings.TrimSpace(cmd), ";"), " ")
	if len(args) < 2 {
		fmt.Println("not enough arguments to 'update' command")
		return
	}

	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")

	nodes := cluster.ListNodes(m.Cluster, false)
	for node, address := range nodes {
		status := "done"
		err = network.SendUpdateCommand(address, args[1], m.Cluster)
		if err != nil {
			fmt.Println("Error sending update command to node ", node)
			status = "error"
		}

		fmt.Fprintf(w, "%s\t%s\n",
			node,
			status,
		)
	}

	w.Flush()
}
Пример #2
0
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()
}
Пример #3
0
func getAllPeers() map[string]string {
	myCluster, err := cluster.GetMyCluster()
	if err != nil {
		log.WithField("err", err).Errorln("Error getting my cluster")
		return map[string]string{}
	}
	peers := cluster.ListNodes(myCluster.Name, true)
	delete(peers, cfg.GetNodeConfig().Name)
	return peers
}
Пример #4
0
func nameExist(nodeName string, clusterName string) bool {
	names := cluster.ListNodes(clusterName, false)
	log.Debugln("Nodes list: ", names)
	for name, _ := range names {
		if name == nodeName {
			return true
		}
	}
	return false
}
Пример #5
0
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()
}
Пример #6
0
func (m *Manager) stopService(what string, where []string) {
	var err error
	nodes := cluster.ListNodes(m.Cluster, false)
	if where[0] == "all" {
		for name, addr := range nodes {
			err = network.SendStopServiceCommand(addr, what)
			if err != nil {
				fmt.Println("Error sending start service command to node ", name)
			}
		}
	} else {
		for _, node := range where {
			if addr, ok := nodes[node]; ok {
				err = network.SendStopServiceCommand(addr, what)
				if err != nil {
					fmt.Println("Error sending start service command to node ", node)
				}
			} else {
				fmt.Println("Cannot get address of node ", node)
			}
		}
	}
}
Пример #7
0
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)
	}
}
Пример #8
0
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()
}
Пример #9
0
func showNode(clusterName string, nodeName string, what string) {
	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 0, 8, 1, '\t', 0)
	nodes := cluster.GetNodes(clusterName, false)
	if nodeName != "all" {
		nodes := cluster.ListNodes(clusterName, false)
		if _, ok := nodes[nodeName]; !ok {
			fmt.Println("Unrecognized node ", nodeName)
			return
		}
	}

	switch what {
	case "config":
		fmt.Fprintf(w, "NAME\tUUID\tADDRESS\tCLUSTER\tREMOTE\n")
		for _, node := range nodes {
			config := node.Configuration
			if nodeName != "all" {
				if node.Configuration.Name == nodeName {
					fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
						config.Name,
						config.UUID,
						config.Address,
						config.Cluster,
						config.Remote,
					)
				}
			} else {
				fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
					config.Name,
					config.UUID,
					config.Address,
					config.Cluster,
					config.Remote,
				)
			}
		}
	case "constraints":
		fmt.Fprintf(w, "NAME\tBASE-SERVICES\tCPU-MIN\tCPU-MAX\n")
		for _, node := range nodes {
			constraints := node.Constraints
			if nodeName != "all" {
				if node.Configuration.Name == nodeName {
					fmt.Fprintf(w, "%s\t%v\t%f\t%f\n",
						node.Configuration.Name,
						constraints.BaseServices,
						constraints.CpuMin,
						constraints.CpuMax,
					)
				}
			} else {
				fmt.Fprintf(w, "%s\t%v\t%f\t%f\n",
					node.Configuration.Name,
					constraints.BaseServices,
					constraints.CpuMin,
					constraints.CpuMax,
				)
			}
		}
	case "resources":
		fmt.Fprintf(w, "NAME\tCORES\tMEMORY\n")
		for _, node := range nodes {
			resources := node.Resources
			if nodeName != "all" {
				if node.Configuration.Name == nodeName {
					fmt.Fprintf(w, "%s\t%d\t%d\n",
						node.Configuration.Name,
						resources.TotalCpus,
						resources.TotalMemory,
					)
				}
			} else {
				fmt.Fprintf(w, "%s\t%d\t%d\n",
					node.Configuration.Name,
					resources.TotalCpus,
					resources.TotalMemory,
				)
			}
		}
	default:
		fmt.Println("Unrecognized node property ", what)
	}

	w.Flush()
}
Пример #10
0
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)
	}
}