mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 17:29:15 +08:00
fix(NET-117): force delete hosts and assoc nodes (#2432)
This commit is contained in:
@@ -199,6 +199,8 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
|
|||||||
func deleteHost(w http.ResponseWriter, r *http.Request) {
|
func deleteHost(w http.ResponseWriter, r *http.Request) {
|
||||||
var params = mux.Vars(r)
|
var params = mux.Vars(r)
|
||||||
hostid := params["hostid"]
|
hostid := params["hostid"]
|
||||||
|
forceDelete := r.URL.Query().Get("force") == "true"
|
||||||
|
|
||||||
// confirm host exists
|
// confirm host exists
|
||||||
currHost, err := logic.GetHost(hostid)
|
currHost, err := logic.GetHost(hostid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -206,7 +208,7 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
|
|||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = logic.RemoveHost(currHost); err != nil {
|
if err = logic.RemoveHost(currHost, forceDelete); err != nil {
|
||||||
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
|
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
||||||
return
|
return
|
||||||
|
@@ -44,7 +44,7 @@ func TestCheckPorts(t *testing.T) {
|
|||||||
//not sure why this initialization is required but without it
|
//not sure why this initialization is required but without it
|
||||||
// RemoveHost returns database is closed
|
// RemoveHost returns database is closed
|
||||||
database.InitializeDatabase()
|
database.InitializeDatabase()
|
||||||
RemoveHost(&h)
|
RemoveHost(&h, true)
|
||||||
CreateHost(&h)
|
CreateHost(&h)
|
||||||
t.Run("no change", func(t *testing.T) {
|
t.Run("no change", func(t *testing.T) {
|
||||||
is := is.New(t)
|
is := is.New(t)
|
||||||
|
@@ -296,17 +296,26 @@ func UpsertHost(h *models.Host) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveHost - removes a given host from server
|
// RemoveHost - removes a given host from server
|
||||||
func RemoveHost(h *models.Host) error {
|
func RemoveHost(h *models.Host, forceDelete bool) error {
|
||||||
if len(h.Nodes) > 0 {
|
if !forceDelete && len(h.Nodes) > 0 {
|
||||||
return fmt.Errorf("host still has associated nodes")
|
return fmt.Errorf("host still has associated nodes")
|
||||||
}
|
}
|
||||||
|
|
||||||
if servercfg.IsUsingTurn() {
|
if servercfg.IsUsingTurn() {
|
||||||
DeRegisterHostWithTurn(h.ID.String())
|
DeRegisterHostWithTurn(h.ID.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(h.Nodes) > 0 {
|
||||||
|
if err := DisassociateAllNodesFromHost(h.ID.String()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err := database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
|
err := database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteHostFromCache(h.ID.String())
|
deleteHostFromCache(h.ID.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -120,7 +120,7 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(host.Nodes) == 0 {
|
if len(host.Nodes) == 0 {
|
||||||
if err := RemoveHost(host); err != nil {
|
if err := RemoveHost(host, true); err != nil {
|
||||||
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user