func DeleteCluster(w rest.ResponseWriter, r *rest.Request) { dbConn, err := util.GetConnection(CLUSTERADMIN_DB) if err != nil { logit.Error.Println("BackupNow: error " + err.Error()) rest.Error(w, err.Error(), 400) return } defer dbConn.Close() err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-cluster") if err != nil { logit.Error.Println("DeleteCluster: authorize error " + err.Error()) rest.Error(w, err.Error(), http.StatusUnauthorized) return } ID := r.PathParam("ID") if ID == "" { logit.Error.Println("DeleteCluster: error cluster ID required") rest.Error(w, "cluster ID required", http.StatusBadRequest) return } cluster, err := admindb.GetCluster(dbConn, ID) if err != nil { logit.Error.Println("DeleteCluster:" + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) } //delete docker containers containers, err := admindb.GetAllContainersForCluster(dbConn, cluster.ID) if err != nil { logit.Error.Println("DeleteCluster:" + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) } i := 0 i = 0 server := admindb.Server{} for i = range containers { //go get the docker server IPAddress server, err = admindb.GetServer(dbConn, containers[i].ServerID) if err != nil { logit.Error.Println("DeleteCluster:" + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } logit.Info.Println("DeleteCluster: got server IP " + server.IPAddress) //it is possible that someone can remove a container //outside of us, so we let it pass that we can't remove //it //err = removeContainer(server.IPAddress, containers[i].Name) dremreq := &cpmserverapi.DockerRemoveRequest{} dremreq.ContainerName = containers[i].Name logit.Info.Println("will attempt to delete container " + dremreq.ContainerName) var url = "http://" + server.IPAddress + ":10001" _, err = cpmserverapi.DockerRemoveClient(url, dremreq) if err != nil { logit.Error.Println("DeleteCluster: error when trying to remove container" + err.Error()) } //send the server a deletevolume command url = "http://" + server.IPAddress + ":10001" ddreq := &cpmserverapi.DiskDeleteRequest{} ddreq.Path = server.PGDataPath + "/" + containers[i].Name _, err = cpmserverapi.DiskDeleteClient(url, ddreq) if err != nil { logit.Error.Println("DeleteCluster: error when trying to remove disk volume" + err.Error()) } i++ } //delete the container entries //delete the cluster entry admindb.DeleteCluster(dbConn, ID) for i = range containers { err = admindb.DeleteContainer(dbConn, containers[i].ID) if err != nil { logit.Error.Println("DeleteCluster:" + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } } status := SimpleStatus{} status.Status = "OK" w.WriteHeader(http.StatusOK) w.WriteJson(&status) }
/* TODO refactor this to share code with DeleteCluster!!!!! */ func DeleteNode(w rest.ResponseWriter, r *rest.Request) { dbConn, err := util.GetConnection(CLUSTERADMIN_DB) if err != nil { logit.Error.Println("BackupNow: error " + err.Error()) rest.Error(w, err.Error(), 400) return } defer dbConn.Close() err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-container") if err != nil { logit.Error.Println("DeleteNode: validate token error " + err.Error()) rest.Error(w, err.Error(), http.StatusUnauthorized) return } ID := r.PathParam("ID") if ID == "" { logit.Error.Println("DeleteNode: error node ID required") rest.Error(w, "node ID required", http.StatusBadRequest) return } //go get the node we intend to delete var dbNode admindb.Container dbNode, err = admindb.GetContainer(dbConn, ID) if err != nil { logit.Error.Println("DeleteNode: " + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } //go get the docker server IPAddress server := admindb.Server{} server, err = admindb.GetServer(dbConn, dbNode.ServerID) if err != nil { logit.Error.Println("DeleteNode: " + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } var url = "http://" + server.IPAddress + ":10001" err = admindb.DeleteContainer(dbConn, ID) if err != nil { logit.Error.Println("DeleteNode: " + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } logit.Info.Println("got server IP " + server.IPAddress) //it is possible that someone can remove a container //outside of us, so we let it pass that we can't remove //it request := &cpmserverapi.DockerRemoveRequest{} request.ContainerName = dbNode.Name _, err = cpmserverapi.DockerRemoveClient(url, request) if err != nil { logit.Error.Println("DeleteNode: error when trying to remove container " + err.Error()) } //send the server a deletevolume command request2 := &cpmserverapi.DiskDeleteRequest{} request2.Path = server.PGDataPath + "/" + dbNode.Name _, err = cpmserverapi.DiskDeleteClient(url, request2) if err != nil { fmt.Println(err.Error()) } //we should not have to delete the DNS entries because //of the dnsbridge, it should remove them when we remove //the containers via the docker api w.WriteHeader(http.StatusOK) status := SimpleStatus{} status.Status = "OK" w.WriteJson(&status) }