2019-04-03 20:13:39 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-10-20 08:56:23 +01:00
|
|
|
package checker
|
2019-04-03 20:13:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-04 16:40:07 +01:00
|
|
|
"fmt"
|
2019-04-03 20:13:39 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/sync2"
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2019-04-03 20:13:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains the necessary Information to check the Software Version
|
|
|
|
type Config struct {
|
2019-10-20 08:56:23 +01:00
|
|
|
ClientConfig
|
|
|
|
|
|
|
|
CheckInterval time.Duration `help:"Interval to check the version" default:"0h15m0s"`
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service contains the information and variables to ensure the Software is up to date
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-04-03 20:13:39 +01:00
|
|
|
type Service struct {
|
2019-07-31 15:38:44 +01:00
|
|
|
log *zap.Logger
|
2019-04-03 20:13:39 +01:00
|
|
|
config Config
|
2019-10-20 08:56:23 +01:00
|
|
|
client *Client
|
2020-02-21 17:41:54 +00:00
|
|
|
Info version.Info
|
2019-04-03 20:13:39 +01:00
|
|
|
service string
|
|
|
|
|
2019-11-26 16:08:24 +00:00
|
|
|
checked sync2.Fence
|
|
|
|
mu sync.Mutex
|
|
|
|
allowed bool
|
|
|
|
acceptedVersion version.SemVer
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a Version Check Client with default configuration
|
2019-10-20 08:56:23 +01:00
|
|
|
func NewService(log *zap.Logger, config Config, info version.Info, service string) (client *Service) {
|
2019-04-03 20:13:39 +01:00
|
|
|
return &Service{
|
2019-07-31 15:38:44 +01:00
|
|
|
log: log,
|
2019-04-03 20:13:39 +01:00
|
|
|
config: config,
|
2019-10-20 08:56:23 +01:00
|
|
|
client: New(config.ClientConfig),
|
2020-02-21 17:41:54 +00:00
|
|
|
Info: info,
|
2019-04-03 20:13:39 +01:00
|
|
|
service: service,
|
2019-04-04 16:40:07 +01:00
|
|
|
allowed: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckProcessVersion is not meant to be used for peers but is meant to be
|
|
|
|
// used for other utilities
|
2019-10-20 08:56:23 +01:00
|
|
|
func CheckProcessVersion(ctx context.Context, log *zap.Logger, config Config, info version.Info, service string) (err error) {
|
2019-06-05 16:03:11 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-02-21 17:41:54 +00:00
|
|
|
_, err = NewService(log, config, info, service).CheckVersion(ctx)
|
|
|
|
|
|
|
|
return err
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
// IsAllowed returns whether if the Service is allowed to operate or not.
|
|
|
|
func (service *Service) IsAllowed(ctx context.Context) (version.SemVer, bool) {
|
|
|
|
if !service.checked.Wait(ctx) {
|
|
|
|
return version.SemVer{}, false
|
2019-04-04 16:40:07 +01:00
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
service.mu.Lock()
|
|
|
|
defer service.mu.Unlock()
|
|
|
|
return service.acceptedVersion, service.allowed
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
// CheckVersion checks to make sure the version is still relevant and returns suggested version, returning an error if not.
|
|
|
|
func (service *Service) CheckVersion(ctx context.Context) (latest version.SemVer, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
latest, allowed := service.checkVersion(ctx)
|
|
|
|
if !allowed {
|
|
|
|
return latest, fmt.Errorf("outdated software version (%s), please update", service.Info.Version.String())
|
2019-10-28 14:04:31 +00:00
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
return latest, nil
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
// checkVersion checks if the client is running latest/allowed version.
|
|
|
|
func (service *Service) checkVersion(ctx context.Context) (latestVersion version.SemVer, allowed bool) {
|
2019-11-26 16:08:24 +00:00
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var minimum version.SemVer
|
2019-04-04 16:40:07 +01:00
|
|
|
|
|
|
|
defer func() {
|
2020-02-21 17:41:54 +00:00
|
|
|
service.mu.Lock()
|
|
|
|
service.allowed = allowed
|
2019-11-26 16:08:24 +00:00
|
|
|
if err == nil {
|
2020-02-21 17:41:54 +00:00
|
|
|
service.acceptedVersion = minimum
|
2019-11-26 16:08:24 +00:00
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
service.mu.Unlock()
|
|
|
|
service.checked.Release()
|
2019-04-04 16:40:07 +01:00
|
|
|
}()
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
allowedVersions, err := service.client.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return service.acceptedVersion, true
|
|
|
|
}
|
|
|
|
suggestedVersion, err := allowedVersions.Processes.Storagenode.Suggested.SemVer()
|
|
|
|
if err != nil {
|
|
|
|
return service.acceptedVersion, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !service.Info.Release {
|
|
|
|
minimum = service.Info.Version
|
|
|
|
return suggestedVersion, true
|
2019-04-04 16:40:07 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
minimumOld, err := service.client.OldMinimum(ctx, service.service)
|
2019-04-03 20:13:39 +01:00
|
|
|
if err != nil {
|
2020-02-21 17:41:54 +00:00
|
|
|
// Log about the error, but dont crash the Service and allow further operation
|
|
|
|
service.log.Sugar().Errorf("Failed to do periodic version check: %s", err.Error())
|
|
|
|
return suggestedVersion, true
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 16:08:24 +00:00
|
|
|
minimum, err = version.NewSemVer(minimumOld.String())
|
|
|
|
if err != nil {
|
2020-02-21 17:41:54 +00:00
|
|
|
service.log.Sugar().Errorf("failed to convert old sem version to sem version")
|
|
|
|
return suggestedVersion, true
|
2019-11-26 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
service.log.Sugar().Debugf("allowed minimum version from control server is: %s", minimum.String())
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
if isAcceptedVersion(service.Info.Version, minimumOld) {
|
|
|
|
service.log.Sugar().Infof("running on version %s", service.Info.Version.String())
|
|
|
|
return suggestedVersion, true
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
|
|
|
|
service.log.Sugar().Errorf("running on not allowed/outdated version %s", service.Info.Version.String())
|
|
|
|
|
|
|
|
return suggestedVersion, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checked returns whether the version has been updated.
|
|
|
|
func (service *Service) Checked() bool {
|
|
|
|
return service.checked.Released()
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 08:56:23 +01:00
|
|
|
// isAcceptedVersion compares and checks if the passed version is greater/equal than the minimum required version
|
2019-10-25 12:24:23 +01:00
|
|
|
func isAcceptedVersion(test version.SemVer, target version.OldSemVer) bool {
|
|
|
|
return test.Major > uint64(target.Major) || (test.Major == uint64(target.Major) && (test.Minor > uint64(target.Minor) || (test.Minor == uint64(target.Minor) && test.Patch >= uint64(target.Patch))))
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|