From 5ec40e5c0207d752bcd545b6ddcdbb528c548abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Guid=C3=A9e?= Date: Mon, 18 Sep 2023 13:27:38 -0400 Subject: [PATCH] Update service route --- router/instance.go | 22 ++++++++++++++++++++++ services/instance.go | 14 ++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/router/instance.go b/router/instance.go index e64fb597..04da6f18 100644 --- a/router/instance.go +++ b/router/instance.go @@ -26,6 +26,7 @@ func addInstanceRoutes(r *gin.RouterGroup) { r.GET("/docker", handleGetDocker) r.POST("/docker/recreate", handleRecreateDockerContainer) r.GET("/logs", handleGetLogs) + r.POST("/update/service", handleUpdateService) } // getParamInstanceUUID returns the UUID of the instance in the URL. @@ -523,3 +524,24 @@ func handleGetLogs(c *gin.Context) { c.JSON(http.StatusOK, logs) } + +// handleUpdateService updates the service of the instance with the UUID in the URL. +// Errors can be: +// - missing_instance_uuid: the instance_uuid parameter was missing in the URL +func handleUpdateService(c *gin.Context) { + uid := getParamInstanceUUID(c) + if uid == nil { + return + } + + err := instanceService.UpdateService(*uid) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{ + Code: "failed_to_update_service", + Message: fmt.Sprintf("failed to update service: %v", err), + }) + return + } + + c.Status(http.StatusNoContent) +} diff --git a/services/instance.go b/services/instance.go index d1c23c27..f7532f9d 100644 --- a/services/instance.go +++ b/services/instance.go @@ -408,7 +408,7 @@ func (s *InstanceService) CheckForServiceUpdate(uuid uuid.UUID) error { return nil } -func (s *InstanceService) UpgradeService(uuid uuid.UUID) error { +func (s *InstanceService) UpdateService(uuid uuid.UUID) error { instance, err := s.Get(uuid) if err != nil { return err @@ -423,7 +423,7 @@ func (s *InstanceService) UpgradeService(uuid uuid.UUID) error { log.Error(err) } - if service.Version > instance.Service.Version && service.Version <= types.MaxSupportedVersion { + if service.Version <= types.MaxSupportedVersion { log.Info("service version is outdated, upgrading.", vlog.String("uuid", uuid.String()), vlog.Int("old_version", int(instance.Service.Version)), @@ -434,6 +434,16 @@ func (s *InstanceService) UpgradeService(uuid uuid.UUID) error { if err != nil { return err } + + err = s.CheckForServiceUpdate(uuid) + if err != nil { + return err + } + } else { + log.Info("service version is not supported, skipping.", + vlog.String("uuid", uuid.String()), + vlog.Int("version", int(service.Version)), + ) } return nil