2019-04-03 20:13:39 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package versioncontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-04-17 11:09:44 +01:00
|
|
|
"storj.io/storj/internal/errs2"
|
2019-04-03 20:13:39 +01:00
|
|
|
"storj.io/storj/internal/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is all the configuration parameters for a Version Control Server
|
|
|
|
type Config struct {
|
|
|
|
Address string `user:"true" help:"public address to listen on" default:":8080"`
|
|
|
|
Versions ServiceVersions
|
2019-09-11 15:01:36 +01:00
|
|
|
|
|
|
|
Binary Versions
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceVersions provides a list of allowed Versions per Service
|
|
|
|
type ServiceVersions struct {
|
|
|
|
Satellite string `user:"true" help:"Allowed Satellite Versions" default:"v0.0.1"`
|
|
|
|
Storagenode string `user:"true" help:"Allowed Storagenode Versions" default:"v0.0.1"`
|
|
|
|
Uplink string `user:"true" help:"Allowed Uplink Versions" default:"v0.0.1"`
|
|
|
|
Gateway string `user:"true" help:"Allowed Gateway Versions" default:"v0.0.1"`
|
2019-05-01 17:21:51 +01:00
|
|
|
Identity string `user:"true" help:"Allowed Identity Versions" default:"v0.0.1"`
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 15:01:36 +01:00
|
|
|
// Versions represents versions for all binaries
|
|
|
|
type Versions struct {
|
|
|
|
Satellite Binary
|
|
|
|
Storagenode Binary
|
|
|
|
Uplink Binary
|
|
|
|
Gateway Binary
|
|
|
|
Identity Binary
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary represents versions for single binary
|
|
|
|
type Binary struct {
|
|
|
|
Minimum Version
|
|
|
|
Suggested Version
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version single version
|
|
|
|
type Version struct {
|
|
|
|
Version string `user:"true" help:"peer version" default:"v0.0.1"`
|
|
|
|
URL string `user:"true" help:"URL for specific binary" default:""`
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
// Peer is the representation of a VersionControl Server.
|
|
|
|
type Peer struct {
|
|
|
|
// core dependencies
|
|
|
|
Log *zap.Logger
|
|
|
|
|
|
|
|
// Web server
|
|
|
|
Server struct {
|
|
|
|
Endpoint http.Server
|
|
|
|
Listener net.Listener
|
|
|
|
}
|
|
|
|
Versions version.AllowedVersions
|
|
|
|
|
|
|
|
// response contains the byte version of current allowed versions
|
|
|
|
response []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGet contains the request handler for the version control web server
|
|
|
|
func (peer *Peer) HandleGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Only handle GET Requests
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var xfor string
|
|
|
|
if xfor = r.Header.Get("X-Forwarded-For"); xfor == "" {
|
|
|
|
xfor = r.RemoteAddr
|
|
|
|
}
|
2019-07-31 15:38:44 +01:00
|
|
|
peer.Log.Sugar().Debugf("Request from: %s for %s", r.RemoteAddr, xfor)
|
2019-04-03 20:13:39 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err := w.Write(peer.response)
|
|
|
|
if err != nil {
|
2019-07-31 15:38:44 +01:00
|
|
|
peer.Log.Sugar().Errorf("error writing response to client: %v", err)
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new VersionControl Server.
|
|
|
|
func New(log *zap.Logger, config *Config) (peer *Peer, err error) {
|
|
|
|
peer = &Peer{
|
|
|
|
Log: log,
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
// Convert each Service's Version String to SemVer
|
|
|
|
peer.Versions.Satellite, err = version.NewSemVer(config.Versions.Satellite)
|
|
|
|
if err != nil {
|
|
|
|
return &Peer{}, err
|
|
|
|
}
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
peer.Versions.Storagenode, err = version.NewSemVer(config.Versions.Storagenode)
|
|
|
|
if err != nil {
|
|
|
|
return &Peer{}, err
|
|
|
|
}
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
peer.Versions.Uplink, err = version.NewSemVer(config.Versions.Uplink)
|
|
|
|
if err != nil {
|
|
|
|
return &Peer{}, err
|
|
|
|
}
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
peer.Versions.Gateway, err = version.NewSemVer(config.Versions.Gateway)
|
|
|
|
if err != nil {
|
|
|
|
return &Peer{}, err
|
|
|
|
}
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
peer.Versions.Identity, err = version.NewSemVer(config.Versions.Identity)
|
|
|
|
if err != nil {
|
|
|
|
return &Peer{}, err
|
|
|
|
}
|
2019-05-01 17:21:51 +01:00
|
|
|
|
2019-09-11 15:01:36 +01:00
|
|
|
peer.Versions.Processes = version.Processes{}
|
|
|
|
peer.Versions.Processes.Satellite = configToProcess(config.Binary.Satellite)
|
|
|
|
peer.Versions.Processes.Storagenode = configToProcess(config.Binary.Storagenode)
|
|
|
|
peer.Versions.Processes.Uplink = configToProcess(config.Binary.Uplink)
|
|
|
|
peer.Versions.Processes.Gateway = configToProcess(config.Binary.Gateway)
|
|
|
|
peer.Versions.Processes.Identity = configToProcess(config.Binary.Identity)
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
peer.response, err = json.Marshal(peer.Versions)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
peer.Log.Sugar().Fatalf("Error marshalling version info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Log.Sugar().Debugf("setting version info to: %v", string(peer.response))
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", peer.HandleGet)
|
|
|
|
peer.Server.Endpoint = http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Server.Listener, err = net.Listen("tcp", config.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs versioncontrol server until it's either closed or it errors.
|
|
|
|
func (peer *Peer) Run(ctx context.Context) (err error) {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Server.Endpoint.Shutdown(ctx))
|
2019-04-03 20:13:39 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
peer.Log.Sugar().Infof("Versioning server started on %s", peer.Addr())
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Server.Endpoint.Serve(peer.Server.Listener))
|
2019-04-03 20:13:39 +01:00
|
|
|
})
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *Peer) Close() (err error) {
|
|
|
|
return peer.Server.Endpoint.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the public address.
|
|
|
|
func (peer *Peer) Addr() string { return peer.Server.Listener.Addr().String() }
|
2019-09-11 15:01:36 +01:00
|
|
|
|
|
|
|
func configToProcess(binary Binary) version.Process {
|
|
|
|
return version.Process{
|
|
|
|
Minimum: version.Version{
|
|
|
|
Version: binary.Minimum.Version,
|
|
|
|
URL: binary.Minimum.URL,
|
|
|
|
},
|
|
|
|
Suggested: version.Version{
|
|
|
|
Version: binary.Suggested.Version,
|
|
|
|
URL: binary.Suggested.URL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|