-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmemongo.go
347 lines (288 loc) · 9.58 KB
/
memongo.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package memongo
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/tryvium-travels/memongo/memongolog"
"github.com/tryvium-travels/memongo/monitor"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const mongoConnectionTemplate = "mongodb://localhost:%d/?directConnection=true"
// Server represents a running MongoDB server
type Server struct {
cmd *exec.Cmd
watcherCmd *exec.Cmd
dbDir string
logger *memongolog.Logger
port int
}
// Start runs a MongoDB server at a given MongoDB version using default options
// and returns the Server.
func Start(version string) (*Server, error) {
return StartWithOptions(&Options{
MongoVersion: version,
})
}
// StartWithOptions is like Start(), but accepts options.
func StartWithOptions(opts *Options) (*Server, error) {
err := opts.fillDefaults()
if err != nil {
return nil, err
}
logger := opts.getLogger()
logger.Infof("Starting MongoDB with options %#v", opts)
binPath, err := opts.getOrDownloadBinPath()
if err != nil {
return nil, err
}
logger.Debugf("Using binary %s", binPath)
// Create a db dir. Even the ephemeralForTest engine needs a dbpath.
dbDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, err
}
// Construct the command and attach stdout/stderr handlers
engine := "ephemeralForTest"
args := []string{"--dbpath", dbDir, "--port", strconv.Itoa(opts.Port)}
if opts.ShouldUseReplica {
engine = "wiredTiger"
args = append(args, "--replSet", "rs0")
} else if strings.HasPrefix(opts.MongoVersion, "7.") {
engine = "wiredTiger"
}
if engine == "wiredTiger" {
args = append(args, "--bind_ip", "localhost")
}
if opts.Auth {
args = append(args, "--auth")
// A keyfile needs to be specified if auth and a replicaset are used
if opts.ShouldUseReplica {
tmpFile, err := ioutil.TempFile("", "keyfile")
// This library is specifically intended for ephemeral mongo
// databases so we don't need a lot of security here, however
// if you're reading this file trying to figure out how to generate
// a keyfile, please see the official MongoDB documentation on how
// to do this correctly and securely for a production environment.
tmpFile.Write([]byte("insecurekeyfile"))
if err != nil {
return nil, err
}
args = append(args, "--keyFile", tmpFile.Name())
}
}
args = append(args, []string{"--storageEngine", engine}...)
// Safe to pass binPath and dbDir
//nolint:gosec
cmd := exec.Command(binPath, args...)
stdoutHandler, startupErrCh, startupPortCh := stdoutHandler(logger)
cmd.Stdout = stdoutHandler
cmd.Stderr = stderrHandler(logger)
logger.Debugf("Starting mongod")
// Run the server
err = cmd.Start()
if err != nil {
remErr := os.RemoveAll(dbDir)
if remErr != nil {
logger.Warnf("error removing data directory: %s", remErr)
}
return nil, err
}
logger.Debugf("Started mongod; starting watcher")
// Start a watcher: the watcher is a subprocess that ensure if this process
// dies, the mongo server will be killed (and not reparented under init)
watcherCmd, err := monitor.RunMonitor(os.Getpid(), cmd.Process.Pid)
if err != nil {
killErr := cmd.Process.Kill()
if killErr != nil {
logger.Warnf("error stopping mongo process: %s", killErr)
}
remErr := os.RemoveAll(dbDir)
if remErr != nil {
logger.Warnf("error removing data directory: %s", remErr)
}
return nil, err
}
logger.Debugf("Started watcher; waiting for mongod to report port number")
startupTime := time.Now()
// Wait for the stdout handler to report the server's port number (or a
// startup error)
var port int
select {
case p := <-startupPortCh:
port = p
case err := <-startupErrCh:
killErr := cmd.Process.Kill()
if killErr != nil {
logger.Warnf("error stopping mongo process: %s", killErr)
}
remErr := os.RemoveAll(dbDir)
if remErr != nil {
logger.Warnf("error removing data directory: %s", remErr)
}
return nil, err
case <-time.After(opts.StartupTimeout):
killErr := cmd.Process.Kill()
if killErr != nil {
logger.Warnf("error stopping mongo process: %s", killErr)
}
remErr := os.RemoveAll(dbDir)
if remErr != nil {
logger.Warnf("error removing data directory: %s", remErr)
}
return nil, fmt.Errorf("timed out waiting for mongod to start")
}
logger.Debugf("mongod started up and reported a port number after %s", time.Since(startupTime).String())
// ---------- START OF REPLICA CODE ----------
if opts.ShouldUseReplica {
ctx := context.Background()
connectionURL := fmt.Sprintf(mongoConnectionTemplate, opts.Port)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connectionURL))
if err != nil {
logger.Warnf("error while connect to localhost database: %w", err)
return nil, err
}
if err := client.Ping(ctx, nil); err != nil {
logger.Warnf("error while ping to localhost database: %w", err)
return nil, err
}
var result bson.M
err = client.Database("admin").RunCommand(ctx, bson.D{{Key: "replSetInitiate", Value: nil}}).Decode(&result)
if err != nil {
logger.Warnf("error while init replica set: %w", err)
return nil, err
}
if err := client.Disconnect(ctx); err != nil {
logger.Warnf("error while disconnect from localhost database: %w", err)
return nil, err
}
logger.Debugf("Started mongo replica")
}
// ---------- END OF REPLICA CODE ----------
// Return a Memongo server
return &Server{
cmd: cmd,
watcherCmd: watcherCmd,
dbDir: dbDir,
logger: logger,
port: port,
}, nil
}
// Port returns the port the server is listening on.
func (s *Server) Port() int {
return s.port
}
// URI returns a mongodb:// URI to connect to
func (s *Server) URI() string {
return fmt.Sprintf("mongodb://localhost:%d", s.port)
}
// URIWithRandomDB returns a mongodb:// URI to connect to, with
// a random database name (e.g. mongodb://localhost:1234/somerandomname)
func (s *Server) URIWithRandomDB() string {
return fmt.Sprintf("mongodb://localhost:%d/%s", s.port, RandomDatabase())
}
// Stop kills the mongo server
func (s *Server) Stop() {
err := s.cmd.Process.Kill()
if err != nil {
s.logger.Warnf("error stopping mongod process: %s", err)
return
}
err = s.watcherCmd.Process.Kill()
if err != nil {
s.logger.Warnf("error stopping watcher process: %s", err)
return
}
err = os.RemoveAll(s.dbDir)
if err != nil {
s.logger.Warnf("error removing data directory: %s", err)
return
}
}
// Cribbed from https://github.com/nodkz/mongodb-memory-server/blob/master/packages/mongodb-memory-server-core/src/util/MongoInstance.ts#L206
var (
reReady = regexp.MustCompile(`waiting for connections.*port\D*(\d+)`)
reAlreadyInUse = regexp.MustCompile("addr already in use")
reAlreadyRunning = regexp.MustCompile("mongod already running")
rePermissionDenied = regexp.MustCompile("mongod permission denied")
reDataDirectoryNotFound = regexp.MustCompile("data directory .*? not found")
reShuttingDown = regexp.MustCompile("shutting down with code")
)
// The stdout handler relays lines from mongod's stout to our logger, and also
// watches during startup for error or success messages.
//
// It returns two channels: an error channel and a port channel. Only one
// message will be sent to one of these two channels. A port number will
// be sent to the port channel if the server start up correctly, and an
// error will be send to the error channel if the server does not start up
// correctly.
func stdoutHandler(log *memongolog.Logger) (io.Writer, <-chan error, <-chan int) {
errChan := make(chan error)
portChan := make(chan int)
reader, writer := io.Pipe()
go func() {
scanner := bufio.NewScanner(reader)
haveSentMessage := false
for scanner.Scan() {
line := scanner.Text()
log.Debugf("[Mongod stdout] %s", line)
if !haveSentMessage {
downcaseLine := strings.ToLower(line)
if match := reReady.FindStringSubmatch(downcaseLine); match != nil {
port, err := strconv.Atoi(match[1])
if err != nil {
errChan <- fmt.Errorf("could not parse port from mongod log line: %s", downcaseLine)
} else {
portChan <- port
}
haveSentMessage = true
} else if reAlreadyInUse.MatchString(downcaseLine) {
errChan <- fmt.Errorf("mongod startup failed, address in use")
haveSentMessage = true
} else if reAlreadyRunning.MatchString(downcaseLine) {
errChan <- fmt.Errorf("mongod startup failed, already running")
haveSentMessage = true
} else if rePermissionDenied.MatchString(downcaseLine) {
errChan <- fmt.Errorf("mongod startup failed, permission denied")
haveSentMessage = true
} else if reDataDirectoryNotFound.MatchString(downcaseLine) {
errChan <- fmt.Errorf("mongod startup failed, data directory not found")
haveSentMessage = true
} else if reShuttingDown.MatchString(downcaseLine) {
errChan <- fmt.Errorf("mongod startup failed, server shut down")
haveSentMessage = true
}
}
}
if err := scanner.Err(); err != nil {
log.Warnf("reading mongod stdin failed: %s", err)
}
if !haveSentMessage {
errChan <- fmt.Errorf("mongod exited before startup completed")
}
}()
return writer, errChan, portChan
}
// The stderr handler just relays messages from stderr to our logger
func stderrHandler(log *memongolog.Logger) io.Writer {
reader, writer := io.Pipe()
go func() {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
log.Debugf("[Mongod stderr] %s", scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Warnf("reading mongod stdin failed: %s", err)
}
}()
return writer
}