Skip to content

Commit

Permalink
warn user if either the client or server is out of date (#401)
Browse files Browse the repository at this point in the history
* warn if client version behind server

* get update info from upstream

* publish version as "latest"
  • Loading branch information
mxyng authored Oct 6, 2021
1 parent 0a66e95 commit ef270a1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ jobs:
- run: |
go get
make tools
- run: make release
env:
- run: |
echo $GITHUB_REF | cut -d/ -f3- | tee latest
make release
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
GEMFURY_TOKEN: ${{ secrets.GORELEASER_GEMFURY_TOKEN }}
Expand All @@ -54,4 +56,4 @@ jobs:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- run: make release/helm
- run: make release/helm
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ blobs:
ids:
- zip
- packages
- provider: s3
region: us-east-2
bucket: releases.infrahq.com
folder: "{{ .ProjectName }}"
extra_files:
- glob: latest
brews:
- tap:
owner: infrahq
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.1
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/mod v0.5.0
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
gopkg.in/square/go-jose.v2 v2.6.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0 h1:UG21uOlmZabA4fW5i7ZX6bjw1xELEGg/ZLgZq9auk/Q=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,19 @@ func newEngineCmd() (*cobra.Command, error) {
}

func newVersionCmd() (*cobra.Command, error) {
var options VersionOptions

cmd := &cobra.Command{
Use: "version",
Short: "Display the Infra build version",
RunE: func(cmd *cobra.Command, args []string) error {
return version()
return version(options)
},
}

cmd.Flags().BoolVarP(&options.Client, "client", "c", false, "Display client version only")
cmd.Flags().BoolVarP(&options.Registry, "registry", "r", false, "Display registry version only")

return cmd, nil
}

Expand Down
75 changes: 66 additions & 9 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,90 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"text/tabwriter"

"github.com/infrahq/infra/internal"
"golang.org/x/mod/semver"
)

func version() error {
type VersionOptions struct {
Client bool
Registry bool
}

func version(options VersionOptions) error {
clientSemVer := fmt.Sprintf("v%s", internal.Version)
serverVersion := "not connected"

// Note that we use the client to get this version, but it is in fact the server version
client, err := apiClientFromConfig()
if err == nil {
res, _, err := client.VersionApi.Version(context.Background()).Execute()
if err == nil {
serverVersion = res.Version
}
}

serverSemVer := fmt.Sprintf("v%s", serverVersion)

err = checkUpdate(clientSemVer, serverSemVer)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed checking for updates:", err.Error())
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
defer w.Flush()

fmt.Fprintln(w)
fmt.Fprintln(w, "Client:\t", internal.Version)

client, err := apiClientFromConfig()
if !options.Registry {
fmt.Fprintln(w, "Client:\t", clientSemVer)
}

if !options.Client {
fmt.Fprintln(w, "Registry:\t", serverSemVer)
}

fmt.Fprintln(w)

return nil
}

func checkUpdate(clientSemVer, serverSemVer string) error {
latestSemVer := "nonexistent"

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://releases.infrahq.com/infra/latest", nil)
if err != nil {
fmt.Fprintln(w, blue("✕")+" Could not retrieve client version")
return err
}

// Note that we use the client to get this version, but it is in fact the server version
res, _, err := client.VersionApi.Version(context.Background()).Execute()
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Fprintln(w, "Registry:\t", "not connected")
return err
}

fmt.Fprintln(w, "Registry:\t", res.Version)
fmt.Fprintln(w)
if res.StatusCode >= 400 {
return fmt.Errorf("%s", res.Status)
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err == nil {
latestSemVer = strings.TrimSpace(string(body))
}

if semver.Compare(latestSemVer, clientSemVer) > 0 {
fmt.Fprintf(os.Stderr, "Your client (%s) is out of date. Please update to %s.\n", clientSemVer, latestSemVer)
}

if semver.IsValid(serverSemVer) && semver.Compare(latestSemVer, serverSemVer) > 0 {
fmt.Fprintf(os.Stderr, "Your server (%s) is out of date. Please update to %s.\n", serverSemVer, latestSemVer)
}

return nil
}

0 comments on commit ef270a1

Please sign in to comment.