From 954bc5cb044cf2bba7a72d91ee6c870e7ceea82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Guid=C3=A9e?= Date: Mon, 18 Sep 2023 00:23:33 -0400 Subject: [PATCH] Auto-upgrade service.yaml when found --- services/instance.go | 36 ++++++++++++++++++++++++++++++++++++ types/service.go | 6 +++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/services/instance.go b/services/instance.go index 9c6983e3..b3fab75b 100644 --- a/services/instance.go +++ b/services/instance.go @@ -560,6 +560,42 @@ func (s *InstanceService) load(uuid uuid.UUID) error { return err } + err = s.UpgradeService(uuid) + if err != nil { + return err + } + + return nil +} + +func (s *InstanceService) UpgradeService(uuid uuid.UUID) error { + instance, err := s.Get(uuid) + if err != nil { + return err + } + + service, err := s.serviceAdapter.Get(instance.Service.ID) + if err != nil && errors.Is(err, types.ErrServiceNotFound) { + log.Debug("service does not exist in the service repository, using the instance's service", + vlog.String("uuid", uuid.String()), + ) + } else if err != nil { + log.Error(err) + } + + if service.Version > instance.Service.Version && service.Version <= types.MaxSupportedVersion { + log.Info("service version is outdated, upgrading.", + vlog.String("uuid", uuid.String()), + vlog.Int("old_version", int(instance.Service.Version)), + vlog.Int("new_version", int(service.Version)), + ) + instance.Service = service + err := s.instanceAdapter.SaveService(instance) + if err != nil { + return err + } + } + return nil } diff --git a/types/service.go b/types/service.go index ffc9d8ad..53c2d4d5 100644 --- a/types/service.go +++ b/types/service.go @@ -8,7 +8,7 @@ import ( ) const ( - URLKindClient = "client" + MaxSupportedVersion Version = 1 ) var ( @@ -18,12 +18,12 @@ var ( type Version int type ServiceVersioning struct { - // Version is the version of the service format used. + // Version of the service format used. Version Version `yaml:"version" json:"version"` } type Service struct { - ServiceVersioning + ServiceVersioning `yaml:",inline"` // ID is the identifier of the service. It must be unique. ID string `yaml:"id" json:"id"`