Переглянути джерело

feat(go): allow force upgrade of hosts.

Vishal Dalwadi 8 місяців тому
батько
коміт
25e4604d26
2 змінених файлів з 19 додано та 2 видалено
  1. 17 2
      controllers/hosts.go
  2. 2 0
      models/host.go

+ 17 - 2
controllers/hosts.go

@@ -57,10 +57,17 @@ func hostHandlers(r *mux.Router) {
 // @Router      /api/hosts/upgrade [post]
 // @Router      /api/hosts/upgrade [post]
 // @Tags        Hosts
 // @Tags        Hosts
 // @Security    oauth
 // @Security    oauth
+// @Param       force query bool false "Force upgrade"
 // @Success     200 {string} string "upgrade all hosts request received"
 // @Success     200 {string} string "upgrade all hosts request received"
 func upgradeHosts(w http.ResponseWriter, r *http.Request) {
 func upgradeHosts(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Content-Type", "application/json")
 
 
+	action := models.Upgrade
+
+	if r.URL.Query().Get("force") == "true" {
+		action = models.ForceUpgrade
+	}
+
 	user := r.Header.Get("user")
 	user := r.Header.Get("user")
 
 
 	go func() {
 	go func() {
@@ -75,7 +82,7 @@ func upgradeHosts(w http.ResponseWriter, r *http.Request) {
 		for _, host := range hosts {
 		for _, host := range hosts {
 			go func(host models.Host) {
 			go func(host models.Host) {
 				hostUpdate := models.HostUpdate{
 				hostUpdate := models.HostUpdate{
-					Action: models.Upgrade,
+					Action: action,
 					Host:   host,
 					Host:   host,
 				}
 				}
 				if err = mq.HostUpdate(&hostUpdate); err != nil {
 				if err = mq.HostUpdate(&hostUpdate); err != nil {
@@ -96,6 +103,7 @@ func upgradeHosts(w http.ResponseWriter, r *http.Request) {
 // @Tags        Hosts
 // @Tags        Hosts
 // @Security    oauth
 // @Security    oauth
 // @Param       hostid path string true "Host ID"
 // @Param       hostid path string true "Host ID"
+// @Param       force query bool false "Force upgrade"
 // @Success     200 {string} string "passed message to upgrade host"
 // @Success     200 {string} string "passed message to upgrade host"
 // @Failure     500 {object} models.ErrorResponse
 // @Failure     500 {object} models.ErrorResponse
 // upgrade host is a handler to send upgrade message to a host
 // upgrade host is a handler to send upgrade message to a host
@@ -106,7 +114,14 @@ func upgradeHost(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "notfound"))
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "notfound"))
 		return
 		return
 	}
 	}
-	if err := mq.HostUpdate(&models.HostUpdate{Action: models.Upgrade, Host: *host}); err != nil {
+
+	action := models.Upgrade
+
+	if r.URL.Query().Get("force") == "true" {
+		action = models.ForceUpgrade
+	}
+
+	if err := mq.HostUpdate(&models.HostUpdate{Action: action, Host: *host}); err != nil {
 		slog.Error("failed to upgrade host", "error", err)
 		slog.Error("failed to upgrade host", "error", err)
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 		return
 		return

+ 2 - 0
models/host.go

@@ -98,6 +98,8 @@ type HostMqAction string
 const (
 const (
 	// Upgrade - const to request host to update it's client
 	// Upgrade - const to request host to update it's client
 	Upgrade HostMqAction = "UPGRADE"
 	Upgrade HostMqAction = "UPGRADE"
+	// ForceUpgrade - const for forcing a host to upgrade its client binary
+	ForceUpgrade HostMqAction = "FORCE_UPGRADE"
 	// SignalHost - const for host signal action
 	// SignalHost - const for host signal action
 	SignalHost HostMqAction = "SIGNAL_HOST"
 	SignalHost HostMqAction = "SIGNAL_HOST"
 	// UpdateHost - constant for host update action
 	// UpdateHost - constant for host update action