Esempio n. 1
0
//call dcos deployment module to delete nodes
func DeleteNodes(cluster entity.Cluster, hosts []entity.Host, originStatus map[string]string, x_auth_token string) (err error) {
	// var servers []dcosentity.Server
	servers := []dcosentity.Server{}
	for _, host := range hosts {
		server := dcosentity.Server{}
		server.Hostname = host.HostName
		server.IpAddress = host.IP
		server.PrivateIpAddress = host.PrivateIp
		server.IsMaster = host.IsMasterNode
		server.IsSlave = host.IsSlaveNode
		server.IsSwarmMaster = host.IsSwarmMaster
		server.StoragePath = host.StoragePath

		servers = append(servers, server)
	}

	request := new(dcosentity.DeleteRequest)
	request.UserName = cluster.Owner
	request.ClusterName = cluster.Name
	request.Servers = servers

	var deleted_servers *[]dcosentity.Server
	deleted_servers, err = SendDeleteNodesRequest(request)
	if err != nil {
		logrus.Errorf("send delete nodes requst to dcos deployment error [%v] \n", err)
		logrus.Infoln("rollback hosts status")
		// GetClusterService().UpdateStatusById(cluster.ObjectId.Hex(), CLUSTER_STATUS_RUNNING, x_auth_token)
		for _, host := range hosts {
			preStatus := originStatus[host.ObjectId.Hex()]
			_, _, err := GetHostService().UpdateStatusById(host.ObjectId.Hex(), preStatus, x_auth_token)
			if err != nil {
				logrus.Warnf("rollback host[%v] status to [%v] error [%v]", host.HostName, preStatus, err)
			}
		}

		return
	}

	//send deployment success, may success partially
	var hostnames []string
	for _, server := range *deleted_servers {
		hostnames = append(hostnames, server.Hostname)
	}

	for _, host := range hosts {
		var status string
		if StringInSlice(host.HostName, hostnames) {
			status = HOST_STATUS_TERMINATED
		} else {
			status = HOST_STATUS_FAILED
		}
		//update status
		_, _, err := GetHostService().UpdateStatusById(host.ObjectId.Hex(), status, x_auth_token)
		if err != nil {
			logrus.Errorf("update host status error is [%v]", err)
		}
	}

	//update cluster nodes number and status
	logrus.Infoln("update cluster number and status")
	cluster.Instances = cluster.Instances - len(*deleted_servers)
	cluster.Status = CLUSTER_STATUS_RUNNING
	GetClusterService().UpdateCluster(cluster, x_auth_token)
	// //update cluster status
	// logrus.Infoln("set cluster status to RUNNING")
	// GetClusterService().UpdateStatusById(cluster.ObjectId.Hex(), CLUSTER_STATUS_RUNNING, x_auth_token)
	return
}
Esempio n. 2
0
//call dcos deployment module to add nodes
func AddNodes(cluster entity.Cluster, createNumber int, hosts []entity.Host, x_auth_token string) (err error) {
	request := new(dcosentity.AddNodeRequest)
	request.UserName = cluster.Owner
	request.ClusterName = cluster.Name
	request.CreateNumber = createNumber
	request.ExistedNumber = cluster.Instances
	request.ProviderInfo = getProvider(cluster.UserId, x_auth_token)

	//create hosts
	_, currentHosts, _, err := GetHostService().QueryHosts(cluster.ObjectId.Hex(), 0, 0, "unterminated", x_auth_token)
	if err != nil {
		logrus.Errorf("get current hosts by clusterId error %v", err)
		removeAddedNodes(hosts, x_auth_token)
		return err
	}

	request.ConsulServer, request.DnsServers, request.SwarmMaster = getNodeInfo(currentHosts)

	logrus.Debugf("add node request is %v", request)

	//call
	//dcos deployment module returns newly-created nodes info
	var servers *[]dcosentity.Server
	servers, err = SendAddNodesRequest(request)
	if err != nil {
		logrus.Errorf("send request to dcos deploy error is %v", err)
		removeAddedNodes(hosts, x_auth_token)
		return
	}

	ok_count := len(*servers)
	logrus.Infof("try to add %d nodes, %d success", createNumber, ok_count)

	//update newly-created hosts
	for i, server := range *servers {
		host := hosts[i]
		id := host.ObjectId.Hex()
		host.HostName = server.Hostname
		host.Status = HOST_STATUS_RUNNING
		host.IP = server.IpAddress
		host.PrivateIp = server.PrivateIpAddress
		host.IsMasterNode = server.IsMaster
		host.IsSlaveNode = server.IsSlave
		host.IsSwarmMaster = server.IsSwarmMaster
		host.TimeUpdate = dao.GetCurrentTime()
		_, _, err := GetHostService().UpdateById(id, host, x_auth_token)
		if err != nil {
			logrus.Errorf("update host error is [%v] \n", err)
		}
	}

	//update status of failed nodes
	for i := ok_count; i < createNumber; i++ {
		_, _, err := GetHostService().UpdateStatusById(hosts[i].ObjectId.Hex(), HOST_STATUS_FAILED, x_auth_token)
		if err != nil {
			logrus.Errorf("update host error is [%v] \n", err)
		}
	}

	//update cluster nodes number and status
	logrus.Infoln("update cluster number and status")
	cluster.Instances = cluster.Instances + len(hosts)
	cluster.Status = CLUSTER_STATUS_RUNNING
	GetClusterService().UpdateCluster(cluster, x_auth_token)
	// //update cluster status
	// logrus.Infoln("set cluster status to RUNNING")
	// GetClusterService().UpdateStatusById(cluster.ObjectId.Hex(), CLUSTER_STATUS_RUNNING, x_auth_token)
	return
}