1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"github.com/juju/errors"
10
"github.com/juju/replicaset"
11
"github.com/juju/utils"
14
"github.com/juju/juju/agent"
15
"github.com/juju/juju/mongo"
18
// TODO(katco): 2016-08-09: lp:1611427
19
var initiateAttemptStrategy = utils.AttemptStrategy{
20
Total: 60 * time.Second,
21
Delay: 5 * time.Second,
24
// InitiateMongoParams holds parameters for the MaybeInitiateMongo call.
25
type InitiateMongoParams struct {
26
// DialInfo specifies how to connect to the mongo server.
27
DialInfo *mgo.DialInfo
29
// MemberHostPort provides the address to use for
30
// the first replica set member.
33
// User holds the user to log as in to the mongo server.
34
// If it is empty, no login will take place.
39
// InitiateMongoServer checks for an existing mongo configuration.
40
// If no existing configuration is found one is created using Initiate.
41
func InitiateMongoServer(p InitiateMongoParams) error {
42
logger.Debugf("Initiating mongo replicaset; dialInfo %#v; memberHostport %q; user %q; password %q", p.DialInfo, p.MemberHostPort, p.User, p.Password)
43
defer logger.Infof("finished InitiateMongoServer")
45
if len(p.DialInfo.Addrs) > 1 {
46
logger.Infof("more than one member; replica set must be already initiated")
49
p.DialInfo.Direct = true
51
// Initiate may fail while mongo is initialising, so we retry until
52
// we successfully populate the replicaset config.
54
for attempt := initiateAttemptStrategy.Start(); attempt.Next(); {
55
err = attemptInitiateMongoServer(p.DialInfo, p.MemberHostPort)
57
logger.Infof("replica set initiated")
60
if attempt.HasNext() {
61
logger.Debugf("replica set initiation failed, will retry: %v", err)
64
return errors.Annotatef(err, "cannot initiate replica set")
67
// attemptInitiateMongoServer attempts to initiate the replica set.
68
func attemptInitiateMongoServer(dialInfo *mgo.DialInfo, memberHostPort string) error {
69
session, err := mgo.DialWithInfo(dialInfo)
71
return errors.Annotatef(err, "cannot dial mongo to initiate replicaset")
74
session.SetSocketTimeout(mongo.SocketTimeout)
76
return replicaset.Initiate(
81
jujuMachineKey: agent.BootstrapMachineId,