1
1
// Copyright 2012, 2013 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
// The version package implements version parsing.
5
// It also acts as guardian of the current client Juju version number.
4
// Package version contains versioning information for juju. It also
5
// acts as guardian of the current client Juju version number.
18
"github.com/juju/utils/series"
19
"gopkg.in/mgo.v2/bson"
16
semversion "github.com/juju/version"
22
19
// The presence and format of this constant is very important.
23
20
// The debian/rules build recipe uses this value for the version
24
21
// number of the release package.
25
const version = "2.0-beta2"
22
const version = "2.0-beta3"
27
24
// The version that we switched over from old style numbering to new style.
28
var switchOverVersion = MustParse("1.19.9")
30
// osReleaseFile is the name of the file that is read in order to determine
31
// the linux type release version.
32
var osReleaseFile = "/etc/os-release"
25
var switchOverVersion = semversion.MustParse("1.19.9")
34
27
// Current gives the current version of the system. If the file
35
28
// "FORCE-VERSION" is present in the same directory as the running
36
29
// binary, it will override this.
37
var Current = MustParse(version)
30
var Current = semversion.MustParse(version)
32
var Compiler = runtime.Compiler
40
35
toolsDir := filepath.Dir(os.Args[0])
48
Current = MustParse(strings.TrimSpace(string(v)))
51
// Number represents a juju version. When bugs are fixed the patch number is
52
// incremented; when new features are added the minor number is incremented
53
// and patch is reset; and when compatibility is broken the major version is
54
// incremented and minor and patch are reset. The build number is
55
// automatically assigned and has no well defined sequence. If the build
56
// number is greater than zero or the tag is non-empty it indicates that the
57
// release is still in development. For versions older than 1.19.3,
58
// development releases were indicated by an odd Minor number of any non-zero
68
// Zero is occasionally convenient and readable.
69
// Please don't change its value.
72
// Binary specifies a binary version of juju.
79
func (v Binary) String() string {
80
return fmt.Sprintf("%v-%s-%s", v.Number, v.Series, v.Arch)
83
// GetBSON turns v into a bson.Getter so it can be saved directly
84
// on a MongoDB database with mgo.
85
func (v Binary) GetBSON() (interface{}, error) {
86
return v.String(), nil
89
// SetBSON turns v into a bson.Setter so it can be loaded directly
90
// from a MongoDB database with mgo.
91
func (vp *Binary) SetBSON(raw bson.Raw) error {
93
err := raw.Unmarshal(&s)
97
v, err := ParseBinary(s)
105
func (v Binary) MarshalJSON() ([]byte, error) {
106
return json.Marshal(v.String())
109
func (vp *Binary) UnmarshalJSON(data []byte) error {
111
if err := json.Unmarshal(data, &s); err != nil {
114
v, err := ParseBinary(s)
122
// MarshalYAML implements yaml.v2.Marshaller interface
123
func (v Binary) MarshalYAML() (interface{}, error) {
124
return v.String(), nil
127
// UnmarshalYAML implements the yaml.Unmarshaller interface
128
func (vp *Binary) UnmarshalYAML(unmarshal func(interface{}) error) error {
130
err := unmarshal(&vstr)
134
v, err := ParseBinary(vstr)
143
binaryPat = regexp.MustCompile(`^(\d{1,9})\.(\d{1,9})(\.|-(\w+))(\d{1,9})(\.\d{1,9})?-([^-]+)-([^-]+)$`)
144
numberPat = regexp.MustCompile(`^(\d{1,9})\.(\d{1,9})(\.|-(\w+))(\d{1,9})(\.\d{1,9})?$`)
147
// MustParse parses a version and panics if it does
148
// not parse correctly.
149
func MustParse(s string) Number {
157
// MustParseBinary parses a binary version and panics if it does
158
// not parse correctly.
159
func MustParseBinary(s string) Binary {
160
v, err := ParseBinary(s)
167
// ParseBinary parses a binary version of the form "1.2.3-series-arch".
168
func ParseBinary(s string) (Binary, error) {
169
m := binaryPat.FindStringSubmatch(s)
171
return Binary{}, fmt.Errorf("invalid binary version %q", s)
179
v.Build = atoi(m[6][1:])
183
_, err := series.GetOSFromSeries(v.Series)
187
// Parse parses the version, which is of the form 1.2.3
188
// giving the major, minor and release versions
190
func Parse(s string) (Number, error) {
191
m := numberPat.FindStringSubmatch(s)
193
return Number{}, fmt.Errorf("invalid version %q", s)
201
v.Build = atoi(m[6][1:])
206
// atoi is the same as strconv.Atoi but assumes that
207
// the string has been verified to be a valid integer.
208
func atoi(s string) int {
209
n, err := strconv.Atoi(s)
216
func (v Number) String() string {
219
s = fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
221
s = fmt.Sprintf("%d.%d-%s%d", v.Major, v.Minor, v.Tag, v.Patch)
224
s += fmt.Sprintf(".%d", v.Build)
229
// Compare returns -1, 0 or 1 depending on whether
230
// v is less than, equal to or greater than w.
231
func (v Number) Compare(w Number) int {
237
case v.Major != w.Major:
238
less = v.Major < w.Major
239
case v.Minor != w.Minor:
240
less = v.Minor < w.Minor
250
case v.Patch != w.Patch:
251
less = v.Patch < w.Patch
252
case v.Build != w.Build:
253
less = v.Build < w.Build
261
// GetBSON turns v into a bson.Getter so it can be saved directly
262
// on a MongoDB database with mgo.
263
func (v Number) GetBSON() (interface{}, error) {
264
return v.String(), nil
267
// SetBSON turns v into a bson.Setter so it can be loaded directly
268
// from a MongoDB database with mgo.
269
func (vp *Number) SetBSON(raw bson.Raw) error {
271
err := raw.Unmarshal(&s)
283
func (v Number) MarshalJSON() ([]byte, error) {
284
return json.Marshal(v.String())
287
func (vp *Number) UnmarshalJSON(data []byte) error {
289
if err := json.Unmarshal(data, &s); err != nil {
300
// MarshalYAML implements yaml.v2.Marshaller interface
301
func (v Number) MarshalYAML() (interface{}, error) {
302
return v.String(), nil
305
// UnmarshalYAML implements the yaml.Unmarshaller interface
306
func (vp *Number) UnmarshalYAML(unmarshal func(interface{}) error) error {
308
err := unmarshal(&vstr)
312
v, err := Parse(vstr)
43
Current = semversion.MustParse(strings.TrimSpace(string(v)))
320
46
func isOdd(x int) bool {
325
51
// version with a tag or a nonzero build component is considered to be a
326
52
// development version. Versions older than or equal to 1.19.3 (the switch
327
53
// over time) check for odd minor versions.
328
func (v Number) IsDev() bool {
54
func IsDev(v semversion.Number) bool {
329
55
if v.Compare(switchOverVersion) <= 0 {
330
56
return isOdd(v.Minor) || v.Build > 0
332
58
return v.Tag != "" || v.Build > 0
335
// ParseMajorMinor takes an argument of the form "major.minor" and returns ints major and minor.
336
func ParseMajorMinor(vers string) (int, int, error) {
337
parts := strings.Split(vers, ".")
338
major, err := strconv.Atoi(parts[0])
341
return -1, -1, fmt.Errorf("invalid major version number %s: %v", parts[0], err)
344
minor, err = strconv.Atoi(parts[1])
346
return -1, -1, fmt.Errorf("invalid minor version number %s: %v", parts[1], err)
348
} else if len(parts) > 2 {
349
return -1, -1, fmt.Errorf("invalid major.minor version number %s", vers)
351
return major, minor, nil