2019-10-20 08:56:23 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2019-10-20 08:56:23 +01:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-10-20 08:56:23 +01:00
|
|
|
"github.com/zeebo/errs"
|
2022-03-31 08:36:11 +01:00
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2019-10-20 08:56:23 +01:00
|
|
|
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2019-10-20 08:56:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
2021-04-28 09:06:17 +01:00
|
|
|
// Error is the error class for version checker client errors.
|
|
|
|
Error = errs.Class("version checker client")
|
2019-10-20 08:56:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientConfig is the config struct for the version control client.
|
|
|
|
type ClientConfig struct {
|
|
|
|
ServerAddress string `help:"server address to check its version against" default:"https://version.storj.io"`
|
|
|
|
RequestTimeout time.Duration `help:"Request timeout for version checks" default:"0h1m0s"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client defines helper methods for using version control server response data.
|
|
|
|
//
|
|
|
|
// architecture: Client
|
|
|
|
type Client struct {
|
|
|
|
config ClientConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new verson control server client.
|
|
|
|
func New(config ClientConfig) *Client {
|
|
|
|
return &Client{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All handles the HTTP request to gather the latest version information.
|
|
|
|
func (client *Client) All(ctx context.Context) (ver version.AllowedVersions, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// Tune Client to have a custom Timeout (reduces hanging software)
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: client.config.RequestTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
// New Request that used the passed in context
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.config.ServerAddress, nil)
|
|
|
|
if err != nil {
|
|
|
|
return version.AllowedVersions{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return version.AllowedVersions{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2019-10-20 08:56:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return version.AllowedVersions{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return version.AllowedVersions{}, Error.New("non-success http status code: %d; body: %s\n", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(bytes.NewReader(body)).Decode(&ver)
|
|
|
|
return ver, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OldMinimum returns the version with the given name at the root-level of the version control response.
|
|
|
|
// NB: This will be deprecated eventually in favor of what is currently the `processes` root-level object.
|
2019-10-25 12:24:23 +01:00
|
|
|
func (client *Client) OldMinimum(ctx context.Context, serviceName string) (ver version.OldSemVer, err error) {
|
2019-10-20 08:56:23 +01:00
|
|
|
defer mon.Task()(&ctx, serviceName)(&err)
|
|
|
|
|
|
|
|
versions, err := client.All(ctx)
|
|
|
|
if err != nil {
|
2019-10-25 12:24:23 +01:00
|
|
|
return version.OldSemVer{}, Error.Wrap(err)
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r := reflect.ValueOf(&versions)
|
|
|
|
f := reflect.Indirect(r).FieldByName(serviceName).Interface()
|
2019-10-25 12:24:23 +01:00
|
|
|
result, ok := f.(version.OldSemVer)
|
2019-10-20 08:56:23 +01:00
|
|
|
if !ok {
|
2019-10-25 12:24:23 +01:00
|
|
|
return version.OldSemVer{}, Error.New("invalid process name: %s", serviceName)
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process returns the version info for the named process from the version control server response.
|
|
|
|
func (client *Client) Process(ctx context.Context, processName string) (process version.Process, err error) {
|
|
|
|
defer mon.Task()(&ctx, processName)(&err)
|
|
|
|
|
|
|
|
versions, err := client.All(ctx)
|
|
|
|
if err != nil {
|
2019-10-21 11:50:59 +01:00
|
|
|
return version.Process{}, Error.Wrap(err)
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
processesValue := reflect.ValueOf(versions.Processes)
|
2019-10-31 12:27:53 +00:00
|
|
|
field := processesValue.FieldByName(kebabToPascal(processName))
|
2019-10-20 08:56:23 +01:00
|
|
|
|
|
|
|
processNameErr := Error.New("invalid process name: %s\n", processName)
|
|
|
|
if field == (reflect.Value{}) {
|
|
|
|
return version.Process{}, processNameErr
|
|
|
|
}
|
|
|
|
|
|
|
|
process, ok := field.Interface().(version.Process)
|
|
|
|
if !ok {
|
|
|
|
return version.Process{}, processNameErr
|
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
|
2019-10-20 08:56:23 +01:00
|
|
|
return process, nil
|
|
|
|
}
|
2019-10-21 11:50:59 +01:00
|
|
|
|
2022-03-31 11:52:27 +01:00
|
|
|
// kebabToPascal converts `alpha-beta` to `AlphaBeta`.
|
2019-10-31 12:27:53 +00:00
|
|
|
func kebabToPascal(str string) string {
|
2022-03-31 08:36:11 +01:00
|
|
|
return strings.ReplaceAll(cases.Title(language.Und, cases.NoLower).String(str), "-", "")
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|