-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodfinder.go
163 lines (147 loc) · 5.48 KB
/
mongodfinder.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mongo
import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/juju/errors"
)
// SearchTools represents the OS functionality we need to find the correct MongoDB executable.
// The mock for this (used in testing) is automatically generated by 'go generate' from the following line
//go:generate go run github.com/golang/mock/mockgen -package mongo -destination searchtoolsmock_test.go github.com/juju/juju/mongo SearchTools
type SearchTools interface {
// GetCommandOutput execs the given command, and returns the CombinedOutput, or any error that occurred.
GetCommandOutput(name string, arg ...string) (string, error)
// Exists just returns if a given path is available (eg os.Stat() has a value)
Exists(string) bool
}
// MongodFinder searches expected paths to find a version of Mongo and determine what version it is.
type MongodFinder struct {
search SearchTools
}
// NewMongodFinder returns a type that will help search for mongod, using normal OS tools.
func NewMongodFinder() *MongodFinder {
return &MongodFinder{
search: OSSearchTools{},
}
}
// FindBest tries to find the mongo version that best fits what we want to use.
func (m *MongodFinder) FindBest() (string, Version, error) {
// Look for the snap version; focal and beyond OR if the relevant snap
// feature flag is enabled.
if m.search.Exists(JujuDbSnapMongodPath) {
v, err := m.findVersion(JujuDbSnapMongodPath)
if err != nil {
return "", Version{}, errors.Errorf("%s snap not installed correctly. Executable %s\nError: %v", JujuDbSnap, JujuDbSnapMongodPath, err)
}
return JujuDbSnapMongodPath, v, nil
}
// In Bionic and beyond (and early trusty) we just use the system mongo.
// We only use the system mongo if it is at least Mongo 3.4
if m.search.Exists(MongodSystemPath) {
// We found Mongo in the system directory, check to see if the version is valid
if v, err := m.findVersion(MongodSystemPath); err != nil {
logger.Warningf("system mongo %q found, but ignoring error trying to get version: %v",
MongodSystemPath, err)
} else if v.NewerThan(minimumSystemMongoVersion) >= 0 {
// We only support mongo 3.4 and newer from the system
return MongodSystemPath, v, nil
}
}
// the system mongo is either too old, or not valid, keep trying
if m.search.Exists(JujuMongod32Path) {
// juju-mongod32 is available, check its version as well. Mostly just as a reporting convenience
// Do we want to use it even if we can't deal with --version?
v, err := m.findVersion(JujuMongod32Path)
if err != nil {
logger.Warningf("juju-mongodb3.2 %q found, but ignoring error trying to get version: %v",
JujuMongod32Path, err)
v = Mongo32wt
}
return JujuMongod32Path, v, nil
}
if m.search.Exists(JujuMongod24Path) {
// juju-mongod is available, check its version as well. Mostly just as a reporting convenience
if v, err := m.findVersion(JujuMongod24Path); err != nil {
logger.Warningf("juju-mongodb %q found, but ignoring error trying to get version: %v",
JujuMongod24Path, err)
} else {
return JujuMongod24Path, v, nil
}
}
return "", Version{}, errors.NotFoundf("viable 'mongod'")
}
// all mongo versions start with "db version v" and then the version is a X.Y.Z-extra
// we don't really care about the 'extra' portion of it, so we just track the rest.
var mongoVersionRegex = regexp.MustCompile(`^db version v(\d{1,9})\.(\d{1,9}).(\d{1,9})([.-].*)?`)
// ParseMongoVersion parses the output from "mongod --version" and returns a Version struct
func ParseMongoVersion(versionInfo string) (Version, error) {
lines := strings.Split(versionInfo, "\n")
var match []string
for _, line := range lines {
match = mongoVersionRegex.FindStringSubmatch(line)
if match == nil {
continue
}
if len(match) < 4 {
return Version{}, errors.Errorf("did not find enough version parts in:\n%s", versionInfo)
}
break
}
if len(match) == 0 {
return Version{}, errors.Errorf("'mongod --version' reported:\n%s", versionInfo)
}
var v Version
var err error
// Index '[0]' is the full matched string,
// [1] is the Major
// [2] is the Minor
// [3] is the Point
// [4] is the Patch to the end of the line
if v.Major, err = strconv.Atoi(match[1]); err != nil {
return Version{}, errors.Annotatef(err, "invalid major version: %q", versionInfo)
}
if v.Minor, err = strconv.Atoi(match[2]); err != nil {
return Version{}, errors.Annotatef(err, "invalid minor version: %q", versionInfo)
}
if v.Point, err = strconv.Atoi(match[3]); err != nil {
return Version{}, errors.Annotatef(err, "invalid point version: %q", versionInfo)
}
if len(match) > 4 {
// strip off the beginning '.' or '-', and make sure there is something after it
tail := match[4]
if len(tail) > 1 {
v.Patch = tail[1:]
}
}
return v, nil
}
func (m *MongodFinder) findVersion(path string) (Version, error) {
out, err := m.search.GetCommandOutput(path, "--version")
if err != nil {
return Version{}, errors.Trace(err)
}
v, err := ParseMongoVersion(out)
if err != nil {
return Version{}, errors.Trace(err)
}
if v.NewerThan(Mongo26) > 0 {
v.StorageEngine = WiredTiger
} else {
v.StorageEngine = MMAPV1
}
return v, nil
}
type OSSearchTools struct{}
func (OSSearchTools) Exists(name string) bool {
_, err := os.Stat(name)
return err == nil
}
func (OSSearchTools) GetCommandOutput(name string, arg ...string) (string, error) {
cmd := exec.Command(name, arg...)
output, err := cmd.CombinedOutput()
return string(output), errors.Trace(err)
}