-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
568 lines (497 loc) · 15.9 KB
/
db.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package backups
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/bson"
"github.com/juju/juju/agent"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state/imagestorage"
)
// db is a surrogate for the proverbial DB layer abstraction that we
// wish we had for juju state. To that end, the package holds the DB
// implementation-specific details and functionality needed for backups.
// Currently that means mongo-specific details. However, as a stand-in
// for a future DB layer abstraction, the db package does not expose any
// low-level details publicly. Thus the backups implementation remains
// oblivious to the underlying DB implementation.
var runCommandFn = runCommand
// DBInfo wraps all the DB-specific information backups needs to dump
// the database. This includes a simplification of the information in
// authentication.MongoInfo.
type DBInfo struct {
// Address is the DB system's host address.
Address string
// Username is used when connecting to the DB system.
Username string
// Password is used when connecting to the DB system.
Password string
// Targets is a list of databases to dump.
Targets set.Strings
// MongoVersion the version of the running mongo db.
MongoVersion mongo.Version
}
// ignoredDatabases is the list of databases that should not be
// backed up, admin might be removed later, after determining
// mongo version.
var ignoredDatabases = set.NewStrings(
"admin",
storageDBName,
"presence", // note: this is still backed up anyway
imagestorage.ImagesDB, // note: this is still backed up anyway
)
type DBSession interface {
DatabaseNames() ([]string, error)
}
// NewDBInfo returns the information needed by backups to dump
// the database.
func NewDBInfo(mgoInfo *mongo.MongoInfo, session DBSession, version mongo.Version) (*DBInfo, error) {
targets, err := getBackupTargetDatabases(session)
if err != nil {
return nil, errors.Trace(err)
}
info := DBInfo{
Address: mgoInfo.Addrs[0],
Password: mgoInfo.Password,
Targets: targets,
MongoVersion: version,
}
// TODO(dfc) Backup should take a Tag.
if mgoInfo.Tag != nil {
info.Username = mgoInfo.Tag.String()
}
return &info, nil
}
func getBackupTargetDatabases(session DBSession) (set.Strings, error) {
dbNames, err := session.DatabaseNames()
if err != nil {
return nil, errors.Annotate(err, "unable to get DB names")
}
targets := set.NewStrings(dbNames...).Difference(ignoredDatabases)
return targets, nil
}
const (
dumpName = "mongodump"
restoreName = "mongorestore"
snapToolPrefix = "juju-db."
snapTmpDir = "/tmp/snap.juju-db"
)
// DBDumper is any type that dumps something to a dump dir.
type DBDumper interface {
// Dump something to dumpDir.
Dump(dumpDir string) error
}
var getMongodumpPath = func() (string, error) {
return getMongoToolPath(dumpName, os.Stat, exec.LookPath)
}
var getMongodPath = func() (string, error) {
finder := mongo.NewMongodFinder()
path, _, err := finder.FindBest()
return path, err
}
func getMongoToolPath(toolName string, stat func(name string) (os.FileInfo, error), lookPath func(file string) (string, error)) (string, error) {
mongod, err := getMongodPath()
if err != nil {
return "", errors.Annotate(err, "failed to get mongod path")
}
mongodDir := filepath.Dir(mongod)
mongoTool := filepath.Join(mongodDir, toolName)
if _, err := stat(mongoTool); err == nil {
// Found it alongside mongod binary, so no need to continue.
return mongoTool, nil
}
logger.Tracef("didn't find MongoDB tool %q in %q", toolName, mongodDir)
// Also try "juju-db.tool" (how it's named in the Snap).
mongoTool = filepath.Join(mongodDir, snapToolPrefix+toolName)
if _, err := stat(mongoTool); err == nil {
return mongoTool, nil
}
logger.Tracef("didn't find MongoDB tool %q in %q", snapToolPrefix+toolName, mongodDir)
path, err := lookPath(toolName)
if err != nil {
return "", errors.Trace(err)
}
return path, nil
}
type mongoDumper struct {
*DBInfo
// binPath is the path to the dump executable.
binPath string
}
// NewDBDumper returns a new value with a Dump method for dumping the
// juju state database.
func NewDBDumper(info *DBInfo) (DBDumper, error) {
mongodumpPath, err := getMongodumpPath()
if err != nil {
return nil, errors.Annotate(err, "mongodump not available")
}
dumper := mongoDumper{
DBInfo: info,
binPath: mongodumpPath,
}
return &dumper, nil
}
func (md *mongoDumper) options(dumpDir string) []string {
options := []string{
"--ssl",
"--sslAllowInvalidCertificates",
"--authenticationDatabase", "admin",
"--host", md.Address,
"--username", md.Username,
"--password", md.Password,
"--out", dumpDir,
"--oplog",
}
return options
}
func (md *mongoDumper) dump(dumpDir string) error {
options := md.options(dumpDir)
if err := runCommandFn(md.binPath, options...); err != nil {
return errors.Annotate(err, "error dumping databases")
}
// If running the juju-db.mongodump Snap, it outputs to
// /tmp/snap.juju-db/DUMPDIR, so move to /DUMPDIR as our code expects.
if md.isSnap() {
actualDir := filepath.Join(snapTmpDir, dumpDir)
logger.Tracef("moving from Snap dump dir %q to %q", actualDir, dumpDir)
err := os.Remove(dumpDir) // will be empty, delete
if err != nil {
return errors.Trace(err)
}
err = os.Rename(actualDir, dumpDir)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (md *mongoDumper) isSnap() bool {
return filepath.Base(md.binPath) == snapToolPrefix+dumpName
}
// Dump dumps the juju state-related databases. To do this we dump all
// databases and then remove any ignored databases from the dump results.
func (md *mongoDumper) Dump(baseDumpDir string) error {
logger.Tracef("dumping Mongo database to %q", baseDumpDir)
if err := md.dump(baseDumpDir); err != nil {
return errors.Trace(err)
}
found, err := listDatabases(baseDumpDir)
if err != nil {
return errors.Trace(err)
}
// Strip the ignored database from the dump dir.
ignored := found.Difference(md.Targets)
// Admin must be removed only if the mongo version is 3.x or
// above, since 2.x will not restore properly without admin.
if md.DBInfo.MongoVersion.NewerThan(mongo.Mongo26) == -1 {
ignored.Remove("admin")
}
err = stripIgnored(ignored, baseDumpDir)
return errors.Trace(err)
}
// stripIgnored removes the ignored DBs from the mongo dump files.
// This involves deleting DB-specific directories.
//
// NOTE(fwereade): the only directories we actually delete are "admin"
// and "backups"; and those only if they're in the `ignored` set. I have
// no idea why the code was structured this way; but I am, as requested
// as usual by management, *not* fixing anything about backup beyond the
// bug du jour.
//
// Basically, the ignored set is a filthy lie, and all the work we do to
// generate it is pure obfuscation.
func stripIgnored(ignored set.Strings, dumpDir string) error {
for _, dbName := range ignored.Values() {
switch dbName {
case storageDBName, "admin":
dirname := filepath.Join(dumpDir, dbName)
logger.Tracef("stripIgnored deleting dir %q", dirname)
if err := os.RemoveAll(dirname); err != nil {
return errors.Trace(err)
}
}
}
return nil
}
// listDatabases returns the name of each sub-directory of the dump
// directory. Each corresponds to a database dump generated by
// mongodump. Note that, while mongodump is unlikely to change behavior
// in this regard, this is not a documented guaranteed behavior.
func listDatabases(dumpDir string) (set.Strings, error) {
list, err := ioutil.ReadDir(dumpDir)
if err != nil {
return nil, errors.Trace(err)
}
logger.Tracef("%d files found in dump dir", len(list))
for _, info := range list {
logger.Tracef("file found in dump dir: %q dir=%v size=%d",
info.Name(), info.IsDir(), info.Size())
}
if len(list) < 2 {
// Should be *at least* oplog.bson and a data directory
return nil, errors.Errorf("too few files in dump dir (%d)", len(list))
}
databases := make(set.Strings)
for _, info := range list {
if !info.IsDir() {
// Notably, oplog.bson is thus excluded here.
continue
}
databases.Add(info.Name())
}
return databases, nil
}
var getMongorestorePath = func() (string, error) {
return getMongoToolPath(restoreName, os.Stat, exec.LookPath)
}
// DBDumper is any type that dumps something to a dump dir.
type DBRestorer interface {
// Dump something to dumpDir.
Restore(dumpDir string, dialInfo *mgo.DialInfo) error
}
type mongoRestorer struct {
*mgo.DialInfo
// binPath is the path to the dump executable.
binPath string
tagUser string
tagUserPassword string
runCommandFn func(string, ...string) error
}
type mongoRestorer32 struct {
mongoRestorer
getDB func(string, MongoSession) MongoDB
newMongoSession func(*mgo.DialInfo) (MongoSession, error)
}
type mongoRestorer24 struct {
mongoRestorer
stopMongo func() error
startMongo func() error
}
func (md *mongoRestorer24) options(dumpDir string) []string {
dbDir := filepath.Join(agent.DefaultPaths.DataDir, "db")
options := []string{
"--drop",
"--journal",
"--oplogReplay",
"--dbpath", dbDir,
dumpDir,
}
return options
}
func (md *mongoRestorer24) Restore(dumpDir string, _ *mgo.DialInfo) error {
logger.Debugf("stopping mongo service for restore")
if err := md.stopMongo(); err != nil {
return errors.Annotate(err, "cannot stop mongo to replace files")
}
options := md.options(dumpDir)
logger.Infof("restoring database with params %v", options)
if err := md.runCommandFn(md.binPath, options...); err != nil {
return errors.Annotate(err, "error restoring database")
}
if err := md.startMongo(); err != nil {
return errors.Annotate(err, "cannot start mongo after restore")
}
return nil
}
// GetDB wraps mgo.Session.DB to ease testing.
func GetDB(s string, session MongoSession) MongoDB {
return session.DB(s)
}
// NewMongoSession wraps mgo.DialInfo to ease testing.
func NewMongoSession(dialInfo *mgo.DialInfo) (MongoSession, error) {
return mgo.DialWithInfo(dialInfo)
}
type RestorerArgs struct {
DialInfo *mgo.DialInfo
NewMongoSession func(*mgo.DialInfo) (MongoSession, error)
Version mongo.Version
TagUser string
TagUserPassword string
GetDB func(string, MongoSession) MongoDB
RunCommandFn func(string, ...string) error
StartMongo func() error
StopMongo func() error
}
var mongoInstalledVersion = func() mongo.Version {
finder := mongo.NewMongodFinder()
// We ignore the error here. The old code always assumed that
// InstalledVersion always had a correct answer.
_, version, _ := finder.FindBest()
return version
}
// NewDBRestorer returns a new structure that can perform a restore
// on the db pointed in dialInfo.
func NewDBRestorer(args RestorerArgs) (DBRestorer, error) {
mongorestorePath, err := getMongorestorePath()
if err != nil {
return nil, errors.Annotate(err, "mongorestore not available")
}
installedMongo := mongoInstalledVersion()
logger.Debugf("args: is %#v", args)
logger.Infof("installed mongo is %s", installedMongo)
// NewerThan will check Major and Minor so migration between micro versions
// will work, before changing this beware, Mongo has been known to break
// compatibility between minors.
if args.Version.NewerThan(installedMongo) != 0 {
return nil, errors.NotSupportedf("restore mongo version %s into version %s", args.Version.String(), installedMongo.String())
}
var restorer DBRestorer
mgoRestorer := mongoRestorer{
DialInfo: args.DialInfo,
binPath: mongorestorePath,
tagUser: args.TagUser,
tagUserPassword: args.TagUserPassword,
runCommandFn: args.RunCommandFn,
}
switch args.Version.Major {
case 2:
restorer = &mongoRestorer24{
mongoRestorer: mgoRestorer,
startMongo: args.StartMongo,
stopMongo: args.StopMongo,
}
case 3:
restorer = &mongoRestorer32{
mongoRestorer: mgoRestorer,
getDB: args.GetDB,
newMongoSession: args.NewMongoSession,
}
default:
return nil, errors.Errorf("cannot restore from mongo version %q", args.Version.String())
}
return restorer, nil
}
func (md *mongoRestorer32) options(dumpDir string) []string {
// note the batchSize, which is known to mitigate EOF errors
// seen when using mongorestore; as seen and reported in
// https://jira.mongodb.org/browse/TOOLS-939 -- not guaranteed
// to *help* with lp:1605653, but observed not to hurt.
//
// The value of 10 was chosen because it's more pessimistic
// than the "1000" that many report success using in the bug.
options := []string{
"--ssl",
"--sslAllowInvalidCertificates",
"--authenticationDatabase", "admin",
"--host", md.Addrs[0],
"--username", md.Username,
"--password", md.Password,
"--drop",
"--oplogReplay",
"--batchSize", "10",
dumpDir,
}
return options
}
// MongoDB represents a mgo.DB.
type MongoDB interface {
UpsertUser(*mgo.User) error
}
// MongoSession represents mgo.Session.
type MongoSession interface {
Run(cmd interface{}, result interface{}) error
Close()
DB(string) *mgo.Database
}
// ensureOplogPermissions adds a special role to the admin user, this role
// is required by mongorestore when doing oplogreplay.
func (md *mongoRestorer32) ensureOplogPermissions(dialInfo *mgo.DialInfo) error {
s, err := md.newMongoSession(dialInfo)
if err != nil {
return errors.Trace(err)
}
defer s.Close()
roles := bson.D{
{"createRole", "oploger"},
{"privileges", []bson.D{
{
{"resource", bson.M{"anyResource": true}},
{"actions", []string{"anyAction"}},
},
}},
{"roles", []string{}},
}
var mgoErr bson.M
err = s.Run(roles, &mgoErr)
if err != nil && !mgo.IsDup(err) {
return errors.Trace(err)
}
result, ok := mgoErr["ok"]
success, isFloat := result.(float64)
if (!ok || !isFloat || success != 1) && mgoErr != nil && !mgo.IsDup(err) {
return errors.Errorf("could not create special role to replay oplog, result was: %#v", mgoErr)
}
// This will replace old user with the new credentials
admin := md.getDB("admin", s)
grant := bson.D{
{"grantRolesToUser", md.DialInfo.Username},
{"roles", []string{"oploger"}},
}
err = s.Run(grant, &mgoErr)
if err != nil {
return errors.Trace(err)
}
result, ok = mgoErr["ok"]
success, isFloat = result.(float64)
if (!ok || !isFloat || success != 1) && mgoErr != nil {
return errors.Errorf("could not grant special role to %q, result was: %#v", md.DialInfo.Username, mgoErr)
}
grant = bson.D{
{"grantRolesToUser", "admin"},
{"roles", []string{"oploger"}},
}
err = s.Run(grant, &mgoErr)
if err != nil {
return errors.Trace(err)
}
result, ok = mgoErr["ok"]
success, isFloat = result.(float64)
if (!ok || !isFloat || success != 1) && mgoErr != nil {
return errors.Errorf("could not grant special role to \"admin\", result was: %#v", mgoErr)
}
if err := admin.UpsertUser(&mgo.User{
Username: md.DialInfo.Username,
Password: md.DialInfo.Password,
}); err != nil {
return errors.Errorf("cannot set new admin credentials: %v", err)
}
return nil
}
func (md *mongoRestorer32) ensureTagUser() error {
s, err := md.newMongoSession(md.DialInfo)
if err != nil {
return errors.Trace(err)
}
defer s.Close()
admin := md.getDB("admin", s)
if err := admin.UpsertUser(&mgo.User{
Username: md.tagUser,
Password: md.tagUserPassword,
}); err != nil {
return fmt.Errorf("cannot set tag user credentials: %v", err)
}
return nil
}
func (md *mongoRestorer32) Restore(dumpDir string, dialInfo *mgo.DialInfo) error {
logger.Debugf("start restore, dumpDir %s", dumpDir)
if err := md.ensureOplogPermissions(dialInfo); err != nil {
return errors.Annotate(err, "setting special user permission in db")
}
options := md.options(dumpDir)
logger.Infof("restoring database with params %v", options)
if err := md.runCommandFn(md.binPath, options...); err != nil {
return errors.Annotate(err, "error restoring database")
}
logger.Infof("updating user credentials")
if err := md.ensureTagUser(); err != nil {
return errors.Trace(err)
}
return nil
}