2cf86703a3
* Initial Webserver Draft for Version Controlling * Rename type to avoid confusion * Move Function Calls into Version Package * Fix Linting and Language Typos * Fix Linting and Spelling Mistakes * Include Copyright * Include Copyright * Adjust Version-Control Server to return list of Versions * Linting * Improve Request Handling and Readability * Add Configuration File Option Add Systemd Service file * Add Logging to File * Smaller Changes * Add Semantic Versioning and refuses outdated Software from Startup (#1612) * implements internal Semantic Version library * adds version logging + reporting to process * Advance SemVer struct for easier handling * Add Accepted Version Store * Fix Function * Restructure * Type Conversion * Handle Version String properly * Add Note about array index * Set temporary Default Version * Add Copyright * Adding Version to Dashboard * Adding Version Info Log * Renaming and adding CheckerProcess * Iteration Sync * Iteration V2 * linting * made LogAndReportVersion a go routine * Refactor to Go Routine * Add Context to Go Routine and allow Operation if Lookup to Control Server fails * Handle Unmarshal properly * Linting * Relocate Version Checks * Relocating Version Check and specified default Version for now * Linting Error Prevention * Refuse Startup on outdated Version * Add Startup Check Function * Straighten Logging * Dont force Shutdown if --dev flag is set * Create full Service/Peer Structure for ControlServer * Linting * Straighting Naming * Finish VersionControl Service Layout * Improve Error Handling * Change Listening Address * Move Checker Function * Remove VersionControl Peer * Linting * Linting * Create VersionClient Service * Renaming * Add Version Client to Peer Definitions * Linting and Renaming * Linting * Remove Transport Checks for now * Move to Client Side Flag * Remove check * Linting * Transport Client Version Intro * Adding Version Client to Transport Client * Add missing parameter * Adding Version Check, to set Allowed = true * Set Default to true, testing * Restructuring Code * Uplink Changes * Add more proper Defaults * Renaming of Version struct * Dont pass Service use Pointer * Set Defaults for Versioning Checks * Put HTTP Server in go routine * Add Versioncontrol to Storj-Sim * Testplanet Fixes * Linting * Add Error Handling and new Server Struct * Move Lock slightly * Reduce Race Potentials * Remove unnecessary files * Linting * Add Proper Transport Handling * small fixes * add fence for allowed check * Add Startup Version Check and Service Naming * make errormessage private * Add Comments about VersionedClient * Linting * Remove Checks that refuse outgoing connections * Remove release cmd * Add Release Script * Linting * Update to use correct Values * Move vars private and set minimum default versions for testing builds * Remove VersionedClient * Better Error Handling and naked return removal * Straighten the Regex and string conversion * Change Check to allows testplanet and storj-sim to run without the need to pass an LDFlag * Cosmetic Change to Dashboard * Cleanup Returns and remove commented code * Remove Version Check if no build options are passed in * Pass in Config Values instead of Pointers * Handle missed Error * Update Endpoint URL * Change Type of Release Flag * Add additional Logging * Remove Versions Logging of other Services * minor fixes Change-Id: I5cc04a410ea6b2008d14dffd63eb5f36dd348a8b
169 lines
4.2 KiB
Go
169 lines
4.2 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package version
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"reflect"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
|
|
"storj.io/storj/internal/sync2"
|
|
)
|
|
|
|
const (
|
|
errOldVersion = "Outdated Software Version, please update!"
|
|
)
|
|
|
|
// Config contains the necessary Information to check the Software Version
|
|
type Config struct {
|
|
ServerAddress string `help:"server address to check its version against" default:"https://version.alpha.storj.io"`
|
|
RequestTimeout time.Duration `help:"Request timeout for version checks" default:"0h1m0s"`
|
|
CheckInterval time.Duration `help:"Interval to check the version" default:"0h15m0s"`
|
|
}
|
|
|
|
// Service contains the information and variables to ensure the Software is up to date
|
|
type Service struct {
|
|
config Config
|
|
info Info
|
|
service string
|
|
|
|
Loop *sync2.Cycle
|
|
|
|
checked chan struct{}
|
|
mu sync.Mutex
|
|
allowed bool
|
|
}
|
|
|
|
// NewService creates a Version Check Client with default configuration
|
|
func NewService(config Config, info Info, service string) (client *Service) {
|
|
return &Service{
|
|
config: config,
|
|
info: info,
|
|
service: service,
|
|
Loop: sync2.NewCycle(config.CheckInterval),
|
|
checked: make(chan struct{}, 0),
|
|
allowed: false,
|
|
}
|
|
}
|
|
|
|
// Run logs the current version information
|
|
func (srv *Service) Run(ctx context.Context) error {
|
|
firstCheck := true
|
|
return srv.Loop.Run(ctx, func(ctx context.Context) error {
|
|
var err error
|
|
allowed, err := srv.checkVersion(ctx)
|
|
if err != nil {
|
|
// Log about the error, but dont crash the service and allow further operation
|
|
zap.S().Errorf("Failed to do periodic version check: ", err)
|
|
allowed = true
|
|
}
|
|
|
|
srv.mu.Lock()
|
|
srv.allowed = allowed
|
|
srv.mu.Unlock()
|
|
|
|
if firstCheck {
|
|
close(srv.checked)
|
|
firstCheck = false
|
|
if !allowed {
|
|
zap.S().Fatal(errOldVersion)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// IsUpToDate returns whether if the Service is allowed to operate or not
|
|
func (srv *Service) IsUpToDate() bool {
|
|
<-srv.checked
|
|
|
|
srv.mu.Lock()
|
|
defer srv.mu.Unlock()
|
|
|
|
return srv.allowed
|
|
}
|
|
|
|
// CheckVersion checks if the client is running latest/allowed code
|
|
func (srv *Service) checkVersion(ctx context.Context) (allowed bool, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
accepted, err := srv.queryVersionFromControlServer(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
list := getFieldString(&accepted, srv.service)
|
|
zap.S().Debugf("allowed versions from Control Server: %v", list)
|
|
|
|
if list == nil {
|
|
return true, errs.New("Empty List from Versioning Server")
|
|
}
|
|
if containsVersion(list, srv.info.Version) {
|
|
zap.S().Infof("running on version %s", srv.info.Version.String())
|
|
allowed = true
|
|
} else {
|
|
zap.S().Errorf("running on not allowed/outdated version %s", srv.info.Version.String())
|
|
allowed = false
|
|
}
|
|
return allowed, err
|
|
}
|
|
|
|
// QueryVersionFromControlServer handles the HTTP request to gather the allowed and latest version information
|
|
func (srv *Service) queryVersionFromControlServer(ctx context.Context) (ver AllowedVersions, err error) {
|
|
// Tune Client to have a custom Timeout (reduces hanging software)
|
|
client := http.Client{
|
|
Timeout: srv.config.RequestTimeout,
|
|
}
|
|
|
|
// New Request that used the passed in context
|
|
req, err := http.NewRequest("GET", srv.config.ServerAddress, nil)
|
|
if err != nil {
|
|
return AllowedVersions{}, err
|
|
}
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return AllowedVersions{}, err
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&ver)
|
|
return ver, err
|
|
}
|
|
|
|
// DebugHandler returns a json representation of the current version information for the binary
|
|
func (srv *Service) DebugHandler(w http.ResponseWriter, r *http.Request) {
|
|
j, err := Build.Marshal()
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, err = w.Write(j)
|
|
if err != nil {
|
|
zap.S().Errorf("error writing data to client %v", err)
|
|
}
|
|
}
|
|
|
|
func getFieldString(array *AllowedVersions, field string) []SemVer {
|
|
r := reflect.ValueOf(array)
|
|
f := reflect.Indirect(r).FieldByName(field).Interface()
|
|
result, ok := f.([]SemVer)
|
|
if ok {
|
|
return result
|
|
}
|
|
return nil
|
|
}
|