示例#1
0
//call dcos deployment module to delete a cluster
func DeleteCluster(cluster entity.Cluster, x_auth_token string) (err error) {
	clusterId := cluster.ObjectId.Hex()
	//query all unterminated hosts
	// _, hosts, _, err := GetHostService().QueryAllByClusterId(clusterId, 0, 0, x_auth_token)
	_, hosts, _, err := GetHostService().QueryHosts(clusterId, 0, 0, "unterminated", x_auth_token)
	if err != nil {
		logrus.Errorf("query hosts in cluster %s error is %v", clusterId, err)
		return
	}

	// var servers []dcosentity.Server
	servers := []dcosentity.Server{}
	for _, host := range hosts {
		//convert entity.Host -> dcosentity.Server
		server := dcosentity.Server{}
		//need hostname only when delete a node
		server.Hostname = host.HostName

		servers = append(servers, server)
	}

	if len(servers) <= 0 {
		logrus.Infof("no running host in cluser %s, the cluster will be terminated directly!", cluster.Name)
		GetClusterService().UpdateStatusById(clusterId, CLUSTER_STATUS_TERMINATED, x_auth_token)
		return
	}

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

	_, err = SendDeleteClusterRequest(request)
	if err != nil {
		logrus.Errorf("send delete cluster requst to dcos deployment error [%v] \n", err)
		//update status of all hosts in the cluster FAILED
		updateHostStatusInCluster(clusterId, HOST_STATUS_FAILED, x_auth_token)
		//update status FAILED
		logrus.Infoln("set cluster status to FAILED")
		GetClusterService().UpdateStatusById(clusterId, CLUSTER_STATUS_FAILED, x_auth_token)
		return
	}
	//update status of all hosts in the cluster TERMINATED
	logrus.Infoln("set cluster hosts to TERMINATED")
	updateHostStatusInCluster(clusterId, HOST_STATUS_TERMINATED, x_auth_token)
	//update status TERMINATED
	logrus.Infoln("set cluster status to TERMINATED")
	GetClusterService().UpdateStatusById(clusterId, CLUSTER_STATUS_TERMINATED, x_auth_token)
	return
}
示例#2
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
}