-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
82 lines (70 loc) · 1.82 KB
/
system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmlclient
import (
"context"
"fmt"
"log"
"regexp"
"github.com/Masterminds/semver/v3"
)
// the 2.4.0.dev is likely wrong, should be -dev (dash, not dot):
// {
// "version": "2.4.0.dev0+build.f904bdf8",
// "ready": true
// }
// 2.5.0-dev0+build.3.2f7875762
type systemVersion struct {
Version string `json:"version"`
Ready bool `json:"ready"`
}
const versionConstraint = ">=2.4.0,<3.0.0"
func versionError(got string) error {
return fmt.Errorf(
"server not compatible, want %s, got %s (%w)",
versionConstraint, got, ErrSystemNotReady,
)
}
func (c *Client) versionCheck(ctx context.Context, depth int32) error {
c.compatibilityErr = nil
sv := systemVersion{}
if err := c.jsonGet(ctx, systeminfoAPI, &sv, depth); err != nil {
return fmt.Errorf("system info error %d (%w)", depth, err)
}
if !sv.Ready {
return ErrSystemNotReady
}
constraint, err := semver.NewConstraint(versionConstraint)
if err != nil {
panic("unparsable semver version constant")
}
re := regexp.MustCompile(`^(\d\.\d\.\d)((-dev0)?\+build.*)?$`)
m := re.FindStringSubmatch(sv.Version)
if m == nil {
return versionError(sv.Version)
}
log.Printf("controller version: %s", sv.Version)
if len(m[3]) > 0 {
log.Printf("Warning, this is a DEV version %s", sv.Version)
}
stem := m[1]
v, err := semver.NewVersion(stem)
if err != nil {
return err
}
// Check if the version meets the constraints
ok := constraint.Check(v)
if !ok {
return versionError(sv.Version)
}
c.version = sv.Version
return nil
}
// Version returns the CML controller version
func (c *Client) Version() string {
return c.version
}
// Ready returns nil if the system is compatible and ready
func (c *Client) Ready(ctx context.Context) error {
// we can safely assume depth 0 as the API endpoint does not
// require authentication
return c.versionCheck(ctx, 0)
}