2020-02-21 17:41:54 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2020-06-01 16:41:59 +01:00
|
|
|
"go.uber.org/zap"
|
2020-02-21 17:41:54 +00:00
|
|
|
|
2020-05-29 09:52:10 +01:00
|
|
|
"storj.io/common/storj"
|
2020-02-21 17:41:54 +00:00
|
|
|
"storj.io/common/sync2"
|
2020-06-01 16:41:59 +01:00
|
|
|
"storj.io/private/version"
|
2020-02-21 17:41:54 +00:00
|
|
|
"storj.io/storj/private/version/checker"
|
|
|
|
"storj.io/storj/storagenode/notifications"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Chore contains the information and variables to ensure the Software is up to date for storagenode.
|
|
|
|
type Chore struct {
|
2020-06-01 16:41:59 +01:00
|
|
|
log *zap.Logger
|
2020-02-21 17:41:54 +00:00
|
|
|
service *checker.Service
|
|
|
|
|
|
|
|
Loop *sync2.Cycle
|
|
|
|
nodeID storj.NodeID
|
|
|
|
notifications *notifications.Service
|
2020-06-01 16:41:59 +01:00
|
|
|
|
|
|
|
version Relevance
|
2020-07-16 15:40:48 +01:00
|
|
|
// nowFn used to mock time is tests.
|
|
|
|
nowFn func() time.Time
|
2020-02-21 17:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore creates a Version Check Client with default configuration for storagenode.
|
2020-06-01 16:41:59 +01:00
|
|
|
func NewChore(log *zap.Logger, service *checker.Service, notifications *notifications.Service, nodeID storj.NodeID, checkInterval time.Duration) *Chore {
|
2020-02-21 17:41:54 +00:00
|
|
|
return &Chore{
|
2020-06-01 16:41:59 +01:00
|
|
|
log: log,
|
2020-02-21 17:41:54 +00:00
|
|
|
service: service,
|
|
|
|
nodeID: nodeID,
|
|
|
|
notifications: notifications,
|
|
|
|
Loop: sync2.NewCycle(checkInterval),
|
2020-07-16 15:40:48 +01:00
|
|
|
nowFn: time.Now().UTC,
|
2020-02-21 17:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:41:59 +01:00
|
|
|
// Run logs the current version information and detects if software outdated, if so - sends notifications.
|
2020-02-21 17:41:54 +00:00
|
|
|
func (chore *Chore) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if !chore.service.Checked() {
|
|
|
|
_, err = chore.service.CheckVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
currentVer := chore.service.Info.Version
|
|
|
|
chore.version.init(currentVer)
|
2020-06-01 16:41:59 +01:00
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
return chore.Loop.Run(ctx, func(ctx context.Context) error {
|
2020-06-01 16:41:59 +01:00
|
|
|
suggested, err := chore.service.CheckVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
err = chore.checkRelevance(ctx, suggested, currentVer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-01 16:41:59 +01:00
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
if !chore.version.IsOutdated {
|
2020-06-01 16:41:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var notification notifications.NewNotification
|
2020-07-16 15:40:48 +01:00
|
|
|
now := chore.nowFn()
|
2020-06-01 16:41:59 +01:00
|
|
|
switch {
|
2020-07-16 15:40:48 +01:00
|
|
|
case chore.version.FirstTimeSpotted.Add(time.Hour*336).Before(now) && chore.version.TimesNotified == notifications.TimesNotifiedSecond:
|
2020-06-01 16:41:59 +01:00
|
|
|
notification = notifications.NewVersionNotification(notifications.TimesNotifiedSecond, suggested, chore.nodeID)
|
2020-07-16 15:40:48 +01:00
|
|
|
chore.version.TimesNotified = notifications.TimesNotifiedLast
|
2020-06-01 16:41:59 +01:00
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
case chore.version.FirstTimeSpotted.Add(time.Hour*144).Before(now) && chore.version.TimesNotified == notifications.TimesNotifiedFirst:
|
2020-06-01 16:41:59 +01:00
|
|
|
notification = notifications.NewVersionNotification(notifications.TimesNotifiedFirst, suggested, chore.nodeID)
|
2020-07-16 15:40:48 +01:00
|
|
|
chore.version.TimesNotified = notifications.TimesNotifiedSecond
|
2020-06-01 16:41:59 +01:00
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
case chore.version.FirstTimeSpotted.Add(time.Hour*96).Before(now) && chore.version.TimesNotified == notifications.TimesNotifiedZero:
|
2020-06-01 16:41:59 +01:00
|
|
|
notification = notifications.NewVersionNotification(notifications.TimesNotifiedZero, suggested, chore.nodeID)
|
2020-07-16 15:40:48 +01:00
|
|
|
chore.version.TimesNotified = notifications.TimesNotifiedFirst
|
2020-06-01 16:41:59 +01:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = chore.notifications.Receive(ctx, notification)
|
|
|
|
if err != nil {
|
|
|
|
chore.log.Sugar().Errorf("Failed to receive notification", err.Error())
|
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-06-01 16:41:59 +01:00
|
|
|
|
2020-07-16 15:40:48 +01:00
|
|
|
func (chore *Chore) checkRelevance(ctx context.Context, suggested version.SemVer, current version.SemVer) error {
|
2020-06-01 16:41:59 +01:00
|
|
|
if current.Compare(suggested) < 0 {
|
2020-07-16 15:40:48 +01:00
|
|
|
cursor, err := chore.service.GetCursor(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := cursor.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorString := string(bytes)
|
|
|
|
if cursorString != "" {
|
|
|
|
cursorString = cursorString[1 : len(cursorString)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if cursorString == "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" {
|
|
|
|
chore.version.IsOutdated = true
|
|
|
|
|
|
|
|
if chore.version.ExpectedVersion.Compare(suggested) < 0 {
|
|
|
|
chore.version.ExpectedVersion = suggested
|
|
|
|
chore.version.FirstTimeSpotted = time.Now().UTC()
|
|
|
|
chore.version.TimesNotified = notifications.TimesNotifiedZero
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chore.version.IsOutdated = false
|
|
|
|
chore.version.TimesNotified = notifications.TimesNotifiedZero
|
|
|
|
return nil
|
2020-06-01 16:41:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 15:40:48 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relevance contains information about software being outdated.
|
|
|
|
type Relevance struct {
|
|
|
|
ExpectedVersion version.SemVer
|
|
|
|
IsOutdated bool
|
|
|
|
FirstTimeSpotted time.Time
|
|
|
|
TimesNotified notifications.TimesNotified
|
|
|
|
}
|
|
|
|
|
|
|
|
func (relevance *Relevance) init(currentVer version.SemVer) {
|
|
|
|
relevance.ExpectedVersion = currentVer
|
|
|
|
relevance.FirstTimeSpotted = time.Now().UTC()
|
|
|
|
relevance.TimesNotified = notifications.TimesNotifiedZero
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSetNow allows tests to have the Service act as if the current time is whatever
|
|
|
|
// they want. This avoids races and sleeping, making tests more reliable and efficient.
|
|
|
|
func (chore *Chore) TestSetNow(now func() time.Time) {
|
|
|
|
chore.nowFn = now
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckVersion returns chore.relevance, used for chore tests only.
|
|
|
|
func (chore *Chore) TestCheckVersion() (relevance Relevance) {
|
|
|
|
return chore.version
|
2020-06-01 16:41:59 +01:00
|
|
|
}
|