func (p *ClusterService) CreateUserCluster(cluster entity.Cluster, x_auth_token string) (newCluster *entity.Cluster, errorCode string, err error) { // generate ObjectId cluster.ObjectId = bson.NewObjectId() userId := cluster.UserId if len(userId) == 0 { err = errors.New("user_id not provided") errorCode = COMMON_ERROR_INVALIDATE logrus.Errorf("create cluster [%v] error is %v", cluster, err) return } user, err := GetUserById(userId, x_auth_token) if err != nil { logrus.Errorf("get user by id err is %v", err) errorCode = CLUSTER_ERROR_CALL_USERMGMT return nil, errorCode, err } cluster.TenantId = user.TenantId cluster.Owner = user.Username // set created_time and updated_time cluster.TimeCreate = dao.GetCurrentTime() cluster.TimeUpdate = cluster.TimeCreate cluster.Status = CLUSTER_STATUS_DEPLOYING // insert bson to mongodb err = dao.HandleInsert(p.collectionName, cluster) if err != nil { errorCode = CLUSTER_ERROR_CALL_MONGODB logrus.Errorf("create cluster [%v] to bson error is %v", cluster, err) return } //add records of hosts in db for i := 0; i < cluster.Instances; i++ { host := entity.Host{} host.ClusterId = cluster.ObjectId.Hex() host.ClusterName = cluster.Name host.Status = HOST_STATUS_DEPLOYING host.UserId = cluster.UserId host.TimeCreate = dao.GetCurrentTime() host.TimeUpdate = host.TimeCreate _, _, err := GetHostService().Create(host, x_auth_token) if err != nil { logrus.Errorf("insert host to db error is [%v]", err) } } if IsDeploymentEnabled() { //call deployment go CreateCluster(cluster, x_auth_token) } newCluster = &cluster return }
func (p *ClusterService) CreateMgmtCluster(cluster entity.Cluster, x_auth_token string) (newCluster *entity.Cluster, errorCode string, err error) { //check if mgmt cluster already exist //only 1 allowed in database query := bson.M{} query["type"] = "mgmt" n, _, _, err := p.queryByQuery(query, 0, 0, "", x_auth_token, false) if n > 0 { return nil, CLUSTER_ERROR_CALL_MONGODB, errors.New("mgmt cluster already exist") } if cluster.Name == "" { cluster.Name = "Management" } token, err := GetTokenById(x_auth_token) if err != nil { logrus.Errorf("get token by id error is %v", err) errorCode = CLUSTER_ERROR_CALL_USERMGMT return nil, errorCode, err } if cluster.UserId == "" { cluster.UserId = token.User.Id } if cluster.Owner == "" { cluster.Owner = token.User.Username } if cluster.Details == "" { cluster.Details = "Cluster to manage other clusters" } cluster.TimeCreate = dao.GetCurrentTime() cluster.TimeUpdate = cluster.TimeCreate cluster.Status = CLUSTER_STATUS_DEPLOYING // insert bson to mongodb err = dao.HandleInsert(p.collectionName, cluster) if err != nil { errorCode = CLUSTER_ERROR_CALL_MONGODB logrus.Errorf("create cluster [%v] to bson error is %v", cluster, err) return } for i := 0; i < cluster.Instances; i++ { host := entity.Host{} host.ClusterId = cluster.ObjectId.Hex() host.ClusterName = cluster.Name host.Status = HOST_STATUS_DEPLOYING host.UserId = cluster.UserId host.TimeCreate = dao.GetCurrentTime() host.TimeUpdate = host.TimeCreate _, _, err := GetHostService().Create(host, x_auth_token) if err != nil { logrus.Errorf("insert host to db error is [%v]", err) } } newCluster = &cluster return }
//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 }
//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 }