@@ -1167,79 +1167,36 @@ func (a *MachineAgent) ensureMongoServer(agentConfig agent.Config) (err error) {
1167
1167
}
1168
1168
}()
1169
1169
1170
- // Many of the steps here, such as adding the controller to the
1171
- // admin DB and initiating the replicaset, are once-only actions,
1172
- // required when upgrading from a pre-HA-capable
1173
- // environment. These calls won't do anything if the thing they
1174
- // need to set up has already been done.
1175
- var needReplicasetInit = false
1176
- var machineAddrs []network.Address
1177
-
1178
1170
mongoInstalled , err := mongo .IsServiceInstalled ()
1179
1171
if err != nil {
1180
1172
return errors .Annotate (err , "error while checking if mongodb service is installed" )
1181
1173
}
1182
1174
1183
- if mongoInstalled {
1184
- logger .Debugf ("mongodb service is installed" )
1185
-
1186
- if _ , err := a .ensureMongoAdminUser (agentConfig ); err != nil {
1187
- return errors .Trace (err )
1188
- }
1189
-
1190
- if err := a .ensureMongoSharedSecret (agentConfig ); err != nil {
1191
- return errors .Trace (err )
1192
- }
1193
- agentConfig = a .CurrentConfig () // ensureMongoSharedSecret may have updated the config
1194
-
1195
- mongoInfo , ok := agentConfig .MongoInfo ()
1196
- if ! ok {
1197
- return errors .New ("unable to retrieve mongo info to check replicaset" )
1198
- }
1199
-
1200
- needReplicasetInit , err = isReplicasetInitNeeded (mongoInfo )
1175
+ if ! mongoInstalled {
1176
+ // EnsureMongoServer installs/upgrades the init config as necessary.
1177
+ ensureServerParams , err := cmdutil .NewEnsureServerParams (agentConfig )
1201
1178
if err != nil {
1202
- return errors . Annotate ( err , "error while checking replicaset" )
1179
+ return err
1203
1180
}
1204
-
1205
- // If the replicaset is to be initialised the machine addresses
1206
- // need to be retrieved *before* MongoDB is restarted with the
1207
- // --replset option (in EnsureMongoServer). Once MongoDB is
1208
- // started with --replset it won't respond to queries until the
1209
- // replicaset is initiated.
1210
- if needReplicasetInit {
1211
- logger .Infof ("replicaset not yet configured" )
1212
- machineAddrs , err = getMachineAddresses (agentConfig )
1213
- if err != nil {
1214
- return errors .Trace (err )
1215
- }
1181
+ if err := cmdutil .EnsureMongoServer (ensureServerParams ); err != nil {
1182
+ return err
1216
1183
}
1217
1184
}
1185
+ logger .Debugf ("mongodb service is installed" )
1218
1186
1219
- // EnsureMongoServer installs/upgrades the init config as necessary.
1220
- ensureServerParams , err := cmdutil .NewEnsureServerParams (agentConfig )
1187
+ // Mongo is installed, record the version.
1188
+ err = a .ChangeConfig (func (config agent.ConfigSetter ) error {
1189
+ config .SetMongoVersion (mongo .InstalledVersion ())
1190
+ return nil
1191
+ })
1221
1192
if err != nil {
1222
- return err
1223
- }
1224
- if err := cmdutil .EnsureMongoServer (ensureServerParams ); err != nil {
1225
- return err
1193
+ return errors .Annotate (err , "cannot set mongo version" )
1226
1194
}
1195
+ agentConfig = a .CurrentConfig ()
1227
1196
1228
- // Initiate the replicaset if required.
1229
- if needReplicasetInit {
1230
- servingInfo , ok := agentConfig .StateServingInfo ()
1231
- if ! ok {
1232
- return stateWorkerServingConfigErr
1233
- }
1234
- mongoInfo , ok := agentConfig .MongoInfo ()
1235
- if ! ok {
1236
- return errors .New ("unable to retrieve mongo info to initiate replicaset" )
1237
- }
1238
- if err := initiateReplicaSet (mongoInfo , servingInfo .StatePort , machineAddrs ); err != nil {
1239
- return err
1240
- }
1197
+ if _ , err := a .ensureMongoAdminUser (agentConfig ); err != nil {
1198
+ return errors .Trace (err )
1241
1199
}
1242
-
1243
1200
return nil
1244
1201
}
1245
1202
@@ -1269,119 +1226,6 @@ func (a *MachineAgent) ensureMongoAdminUser(agentConfig agent.Config) (added boo
1269
1226
})
1270
1227
}
1271
1228
1272
- // ensureMongoSharedSecret generates a MongoDB shared secret if
1273
- // required, updating the agent's config and state.
1274
- func (a * MachineAgent ) ensureMongoSharedSecret (agentConfig agent.Config ) error {
1275
- servingInfo , ok := agentConfig .StateServingInfo ()
1276
- if ! ok {
1277
- return stateWorkerServingConfigErr
1278
- }
1279
-
1280
- if servingInfo .SharedSecret != "" {
1281
- return nil // Already done
1282
- }
1283
-
1284
- logger .Infof ("state serving info has no shared secret - generating" )
1285
-
1286
- var err error
1287
- servingInfo .SharedSecret , err = mongo .GenerateSharedSecret ()
1288
- if err != nil {
1289
- return err
1290
- }
1291
- logger .Debugf ("updating state serving info in agent config" )
1292
- if err = a .ChangeConfig (func (config agent.ConfigSetter ) error {
1293
- config .SetStateServingInfo (servingInfo )
1294
- return nil
1295
- }); err != nil {
1296
- return err
1297
- }
1298
- agentConfig = a .CurrentConfig ()
1299
-
1300
- logger .Debugf ("updating state serving info in state" )
1301
-
1302
- // Note: we set Direct=true in the mongo options because it's
1303
- // possible that we've previously upgraded the mongo server's
1304
- // configuration to form a replicaset, but failed to initiate it.
1305
- dialOpts := mongo .DefaultDialOpts ()
1306
- dialOpts .Direct = true
1307
- st , _ , err := openState (agentConfig , dialOpts )
1308
- if err != nil {
1309
- return err
1310
- }
1311
- defer st .Close ()
1312
-
1313
- ssi := cmdutil .ParamsStateServingInfoToStateStateServingInfo (servingInfo )
1314
- if err := st .SetStateServingInfo (ssi ); err != nil {
1315
- return errors .Errorf ("cannot set state serving info: %v" , err )
1316
- }
1317
-
1318
- logger .Infof ("shared secret updated in state serving info" )
1319
- return nil
1320
- }
1321
-
1322
- // isReplicasetInitNeeded returns true if the replicaset needs to be
1323
- // initiated.
1324
- func isReplicasetInitNeeded (mongoInfo * mongo.MongoInfo ) (bool , error ) {
1325
- dialInfo , err := mongo .DialInfo (mongoInfo .Info , mongo .DefaultDialOpts ())
1326
- if err != nil {
1327
- return false , errors .Annotate (err , "cannot generate dial info to check replicaset" )
1328
- }
1329
- dialInfo .Username = mongoInfo .Tag .String ()
1330
- dialInfo .Password = mongoInfo .Password
1331
-
1332
- session , err := mgo .DialWithInfo (dialInfo )
1333
- if err != nil {
1334
- return false , errors .Annotate (err , "cannot dial mongo to check replicaset" )
1335
- }
1336
- defer session .Close ()
1337
-
1338
- cfg , err := replicaset .CurrentConfig (session )
1339
- if err != nil {
1340
- logger .Debugf ("couldn't retrieve replicaset config (not fatal): %v" , err )
1341
- return true , nil
1342
- }
1343
- numMembers := len (cfg .Members )
1344
- logger .Debugf ("replicaset member count: %d" , numMembers )
1345
- return numMembers < 1 , nil
1346
- }
1347
-
1348
- // getMachineAddresses connects to state to determine the machine's
1349
- // network addresses.
1350
- func getMachineAddresses (agentConfig agent.Config ) ([]network.Address , error ) {
1351
- logger .Debugf ("opening state to get machine addresses" )
1352
- dialOpts := mongo .DefaultDialOpts ()
1353
- dialOpts .Direct = true
1354
- st , m , err := openState (agentConfig , dialOpts )
1355
- if err != nil {
1356
- return nil , errors .Annotate (err , "failed to open state to retrieve machine addresses" )
1357
- }
1358
- defer st .Close ()
1359
- return m .Addresses (), nil
1360
- }
1361
-
1362
- // initiateReplicaSet connects to MongoDB and sets up the replicaset.
1363
- func initiateReplicaSet (mongoInfo * mongo.MongoInfo , statePort int , machineAddrs []network.Address ) error {
1364
- peerAddr := mongo .SelectPeerAddress (machineAddrs )
1365
- if peerAddr == "" {
1366
- return errors .Errorf ("no appropriate peer address found in %q" , machineAddrs )
1367
- }
1368
-
1369
- dialInfo , err := mongo .DialInfo (mongoInfo .Info , mongo .DefaultDialOpts ())
1370
- if err != nil {
1371
- return errors .Annotate (err , "cannot generate dial info to initiate replicaset" )
1372
- }
1373
-
1374
- if err := maybeInitiateMongoServer (peergrouper.InitiateMongoParams {
1375
- DialInfo : dialInfo ,
1376
- MemberHostPort : net .JoinHostPort (peerAddr , fmt .Sprint (statePort )),
1377
- User : mongoInfo .Tag .String (), // TODO(dfc) InitiateMongoParams should take a Tag
1378
- Password : mongoInfo .Password ,
1379
- }); err != nil && err != peergrouper .ErrReplicaSetAlreadyInitiated {
1380
- return err
1381
- }
1382
- return nil
1383
- }
1384
-
1385
1229
func openState (agentConfig agent.Config , dialOpts mongo.DialOpts ) (_ * state.State , _ * state.Machine , err error ) {
1386
1230
info , ok := agentConfig .MongoInfo ()
1387
1231
if ! ok {
0 commit comments