Skip to content

Commit 7ee7be3

Browse files
authored
feat: add version checking to CLI (coder#2643)
* feat: add version checking to CLI
1 parent 4b6189c commit 7ee7be3

File tree

6 files changed

+174
-5
lines changed

6 files changed

+174
-5
lines changed

buildinfo/buildinfo.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package buildinfo
33
import (
44
"fmt"
55
"runtime/debug"
6+
"strings"
67
"sync"
78
"time"
89

@@ -24,6 +25,11 @@ var (
2425
tag string
2526
)
2627

28+
const (
29+
// develPrefix is prefixed to developer versions of the application.
30+
develPrefix = "v0.0.0-devel"
31+
)
32+
2733
// Version returns the semantic version of the build.
2834
// Use golang.org/x/mod/semver to compare versions.
2935
func Version() string {
@@ -35,7 +41,7 @@ func Version() string {
3541
if tag == "" {
3642
// This occurs when the tag hasn't been injected,
3743
// like when using "go run".
38-
version = "v0.0.0-devel" + revision
44+
version = develPrefix + revision
3945
return
4046
}
4147
version = "v" + tag
@@ -48,6 +54,20 @@ func Version() string {
4854
return version
4955
}
5056

57+
// VersionsMatch compares the two versions. It assumes the versions match if
58+
// the major and the minor versions are equivalent. Patch versions are
59+
// disregarded. If it detects that either version is a developer build it
60+
// returns true.
61+
func VersionsMatch(v1, v2 string) bool {
62+
// Developer versions are disregarded...hopefully they know what they are
63+
// doing.
64+
if strings.HasPrefix(v1, develPrefix) || strings.HasPrefix(v2, develPrefix) {
65+
return true
66+
}
67+
68+
return semver.MajorMinor(v1) == semver.MajorMinor(v2)
69+
}
70+
5171
// ExternalURL returns a URL referencing the current Coder version.
5272
// For production builds, this will link directly to a release.
5373
// For development builds, this will link to a commit.

buildinfo/buildinfo_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package buildinfo_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -29,4 +30,70 @@ func TestBuildInfo(t *testing.T) {
2930
_, valid := buildinfo.Time()
3031
require.False(t, valid)
3132
})
33+
34+
t.Run("VersionsMatch", func(t *testing.T) {
35+
t.Parallel()
36+
37+
type testcase struct {
38+
name string
39+
v1 string
40+
v2 string
41+
expectMatch bool
42+
}
43+
44+
cases := []testcase{
45+
{
46+
name: "OK",
47+
v1: "v1.2.3",
48+
v2: "v1.2.3",
49+
expectMatch: true,
50+
},
51+
// Test that we return true if a developer version is detected.
52+
// Developers do not need to be warned of mismatched versions.
53+
{
54+
name: "DevelIgnored",
55+
v1: "v0.0.0-devel+123abac",
56+
v2: "v1.2.3",
57+
expectMatch: true,
58+
},
59+
// Our CI instance uses a "-devel" prerelease
60+
// flag. This is not the same as a developer WIP build.
61+
{
62+
name: "DevelPreleaseNotIgnored",
63+
v1: "v1.1.1-devel+123abac",
64+
v2: "v1.2.3",
65+
expectMatch: false,
66+
},
67+
{
68+
name: "MajorMismatch",
69+
v1: "v1.2.3",
70+
v2: "v0.1.2",
71+
expectMatch: false,
72+
},
73+
{
74+
name: "MinorMismatch",
75+
v1: "v1.2.3",
76+
v2: "v1.3.2",
77+
expectMatch: false,
78+
},
79+
// Different patches are ok, breaking changes are not allowed
80+
// in patches.
81+
{
82+
name: "PatchMismatch",
83+
v1: "v1.2.3+hash.whocares",
84+
v2: "v1.2.4+somestuff.hm.ok",
85+
expectMatch: true,
86+
},
87+
}
88+
89+
for _, c := range cases {
90+
c := c
91+
t.Run(c.name, func(t *testing.T) {
92+
t.Parallel()
93+
require.Equal(t, c.expectMatch, buildinfo.VersionsMatch(c.v1, c.v2),
94+
fmt.Sprintf("expected match=%v for version %s and %s", c.expectMatch, c.v1, c.v2),
95+
)
96+
})
97+
}
98+
})
3299
}

cli/login.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func login() *cobra.Command {
6767
}
6868

6969
client := codersdk.New(serverURL)
70+
71+
// Try to check the version of the server prior to logging in.
72+
// It may be useful to warn the user if they are trying to login
73+
// on a very old client.
74+
err = checkVersions(cmd, client)
75+
if err != nil {
76+
return xerrors.Errorf("check versions: %w", err)
77+
}
78+
7079
hasInitialUser, err := client.HasFirstUser(cmd.Context())
7180
if err != nil {
7281
return xerrors.Errorf("has initial user: %w", err)

cli/root.go

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"fmt"
55
"net/url"
66
"os"
7+
"strconv"
78
"strings"
89
"time"
910

1011
"golang.org/x/xerrors"
1112

13+
"github.com/charmbracelet/lipgloss"
1214
"github.com/kirsle/configdir"
1315
"github.com/mattn/go-isatty"
1416
"github.com/spf13/cobra"
@@ -40,7 +42,13 @@ const (
4042
varForceTty = "force-tty"
4143
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."
4244

43-
envSessionToken = "CODER_SESSION_TOKEN"
45+
noVersionCheckFlag = "no-version-warning"
46+
envNoVersionCheck = "CODER_NO_VERSION_WARNING"
47+
)
48+
49+
var (
50+
errUnauthenticated = xerrors.New(notLoggedInMessage)
51+
envSessionToken = "CODER_SESSION_TOKEN"
4452
)
4553

4654
func init() {
@@ -53,12 +61,37 @@ func init() {
5361
}
5462

5563
func Root() *cobra.Command {
64+
var varSuppressVersion bool
65+
5666
cmd := &cobra.Command{
5767
Use: "coder",
5868
SilenceErrors: true,
5969
SilenceUsage: true,
6070
Long: `Coder — A tool for provisioning self-hosted development environments.
6171
`,
72+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
73+
if varSuppressVersion {
74+
return nil
75+
}
76+
77+
// Login handles checking the versions itself since it
78+
// has a handle to an unauthenticated client.
79+
if cmd.Name() == "login" {
80+
return nil
81+
}
82+
83+
client, err := createClient(cmd)
84+
// If the client is unauthenticated we can ignore the check.
85+
// The child commands should handle an unauthenticated client.
86+
if xerrors.Is(err, errUnauthenticated) {
87+
return nil
88+
}
89+
if err != nil {
90+
return xerrors.Errorf("create client: %w", err)
91+
}
92+
return checkVersions(cmd, client)
93+
},
94+
6295
Example: ` Start a Coder server.
6396
` + cliui.Styles.Code.Render("$ coder server") + `
6497
@@ -97,6 +130,7 @@ func Root() *cobra.Command {
97130
cmd.SetUsageTemplate(usageTemplate())
98131

99132
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
133+
cliflag.BoolVarP(cmd.PersistentFlags(), &varSuppressVersion, noVersionCheckFlag, "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
100134
cliflag.String(cmd.PersistentFlags(), varToken, "", envSessionToken, "", fmt.Sprintf("Specify an authentication token. For security reasons setting %s is preferred.", envSessionToken))
101135
cliflag.String(cmd.PersistentFlags(), varAgentToken, "", "CODER_AGENT_TOKEN", "", "Specify an agent authentication token.")
102136
_ = cmd.PersistentFlags().MarkHidden(varAgentToken)
@@ -142,7 +176,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
142176
if err != nil {
143177
// If the configuration files are absent, the user is logged out
144178
if os.IsNotExist(err) {
145-
return nil, xerrors.New(notLoggedInMessage)
179+
return nil, errUnauthenticated
146180
}
147181
return nil, err
148182
}
@@ -157,7 +191,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
157191
if err != nil {
158192
// If the configuration files are absent, the user is logged out
159193
if os.IsNotExist(err) {
160-
return nil, xerrors.New(notLoggedInMessage)
194+
return nil, errUnauthenticated
161195
}
162196
return nil, err
163197
}
@@ -331,3 +365,30 @@ func FormatCobraError(err error, cmd *cobra.Command) string {
331365
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())
332366
return cliui.Styles.Error.Render(err.Error() + "\n" + helpErrMsg)
333367
}
368+
369+
func checkVersions(cmd *cobra.Command, client *codersdk.Client) error {
370+
flag := cmd.Flag("no-version-warning")
371+
if suppress, _ := strconv.ParseBool(flag.Value.String()); suppress {
372+
return nil
373+
}
374+
375+
clientVersion := buildinfo.Version()
376+
377+
info, err := client.BuildInfo(cmd.Context())
378+
if err != nil {
379+
return xerrors.Errorf("build info: %w", err)
380+
}
381+
382+
fmtWarningText := `version mismatch: client %s, server %s
383+
download the server version with: 'curl -L https://coder.com/install.sh | sh -s -- --version %s'
384+
`
385+
386+
if !buildinfo.VersionsMatch(clientVersion, info.Version) {
387+
warn := cliui.Styles.Warn.Copy().Align(lipgloss.Left)
388+
// Trim the leading 'v', our install.sh script does not handle this case well.
389+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), warn.Render(fmtWarningText), clientVersion, info.Version, strings.TrimPrefix(info.CanonicalVersion(), "v"))
390+
_, _ = fmt.Fprintln(cmd.OutOrStdout())
391+
}
392+
393+
return nil
394+
}

codersdk/buildinfo.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"net/http"
7+
"strings"
8+
9+
"golang.org/x/mod/semver"
710
)
811

912
// BuildInfoResponse contains build information for this instance of Coder.
@@ -16,6 +19,15 @@ type BuildInfoResponse struct {
1619
Version string `json:"version"`
1720
}
1821

22+
// CanonicalVersion trims build information from the version.
23+
// E.g. 'v0.7.4-devel+11573034' -> 'v0.7.4'.
24+
func (b BuildInfoResponse) CanonicalVersion() string {
25+
// We do a little hack here to massage the string into a form
26+
// that works well with semver.
27+
trimmed := strings.ReplaceAll(b.Version, "-devel+", "+devel-")
28+
return semver.Canonical(trimmed)
29+
}
30+
1931
// BuildInfo returns build information for this instance of Coder.
2032
func (c *Client) BuildInfo(ctx context.Context) (BuildInfoResponse, error) {
2133
res, err := c.Request(ctx, http.MethodGet, "/api/v2/buildinfo", nil)

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface AzureInstanceIdentityToken {
3636
readonly encoding: string
3737
}
3838

39-
// From codersdk/buildinfo.go:10:6
39+
// From codersdk/buildinfo.go:13:6
4040
export interface BuildInfoResponse {
4141
readonly external_url: string
4242
readonly version: string

0 commit comments

Comments
 (0)