~lazypower/charms/trusty/mongodb/fixing_st00f

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Stuart Bishop
  • Date: 2014-07-29 10:22:55 UTC
  • mfrom: (47.2.1 mongodb)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: stuart@stuartbishop.net-20140729102255-ilig6wgm8wxyop54
[wesmason] Storage subordinate support for persistent storage, per https://code.launchpad.net/~wesmason/charms/precise/mongodb/add-storage-subordinate-support/+merge/223539

Show diffs side-by-side

added added

removed removed

Lines of Context:
212
212
        return(relation_data)
213
213
 
214
214
 
 
215
def relation_ids(relation_name=None):
 
216
    juju_log("relation_ids: relation_name: %s" % relation_name)
 
217
    try:
 
218
        relation_cmd_line = ['relation-ids', '--format=json']
 
219
        if relation_name is not None:
 
220
            relation_cmd_line.append(relation_name)
 
221
        relation_data = json.loads(subprocess.check_output(relation_cmd_line))
 
222
    except Exception, e:
 
223
        juju_log(str(e))
 
224
        relation_data = None
 
225
    finally:
 
226
        juju_log("relation_ids %s returns: %s" % (relation_name, relation_data))
 
227
        return(relation_data)
 
228
 
215
229
#------------------------------------------------------------------------------
216
230
# open_port:  Convenience function to open a port in juju to
217
231
#             expose a service
930
944
            "'volume-ephemeral-storage' and 'volume-map'")
931
945
        sys.exit(1)
932
946
    if volume_is_permanent(volid):
933
 
        ## config_changed_volume_apply will stop the service if it founds
 
947
        ## config_changed_volume_apply will stop the service if it finds
934
948
        ## it necessary, ie: new volume setup
935
949
        if config_changed_volume_apply():
936
950
            start_hook()
1159
1173
    return(True)
1160
1174
 
1161
1175
 
 
1176
def data_relation_joined():
 
1177
    juju_log("data_relation_joined")
 
1178
 
 
1179
    return(relation_set(
 
1180
        {
 
1181
            'mountpoint': '/srv/juju/mongodb-data'
 
1182
        }))
 
1183
 
 
1184
 
 
1185
def data_relation_changed():
 
1186
    juju_log("data_relation_changed")
 
1187
 
 
1188
    if volume_get_id_for_storage_subordinate() is None:
 
1189
        juju_log("mountpoint from storage subordinate not ready, let's wait")
 
1190
        return(True)
 
1191
 
 
1192
    return(config_changed())
 
1193
 
 
1194
 
 
1195
def data_relation_departed():
 
1196
    juju_log("data_relation_departed")
 
1197
    return(config_changed())
 
1198
 
 
1199
 
1162
1200
def configsvr_relation_joined():
1163
1201
    juju_log("configsvr_relation_joined")
1164
1202
    my_hostname = unit_get('public-address')
1315
1353
    return None
1316
1354
 
1317
1355
 
 
1356
#------------------------------
 
1357
# Returns a stub volume id based on the mountpoint from the storage
 
1358
# subordinate relation, if present.
 
1359
#
 
1360
# @return volid  eg vol-000012345
 
1361
#------------------------------
 
1362
def volume_get_id_for_storage_subordinate():
 
1363
    # storage charm is a subordinate so we should only ever have one
 
1364
    # relation_id for the data relation
 
1365
    ids = relation_ids('data')
 
1366
    if len(ids) > 0:
 
1367
        mountpoint = relation_get('mountpoint',
 
1368
                                   os.environ['JUJU_UNIT_NAME'],
 
1369
                                   ids[0])
 
1370
 
 
1371
        juju_log('mountpoint: %s' % (mountpoint,))
 
1372
        if mountpoint and os.path.exists(mountpoint):
 
1373
            return mountpoint.split('/')[-1]
 
1374
 
 
1375
 
 
1376
 
1318
1377
# Do we have a valid storage state?
1319
1378
# @returns  volid
1320
1379
#           None    config state is invalid - we should not serve
1321
1380
def volume_get_volume_id():
 
1381
 
 
1382
 
 
1383
    volid = volume_get_id_for_storage_subordinate()
 
1384
    if volid:
 
1385
        return volid
 
1386
 
1322
1387
    config_data = config_get()
1323
1388
    ephemeral_storage = config_data['volume-ephemeral-storage']
1324
1389
    volid = volume_get_volid_from_volume_map()
1523
1588
        retVal = mongos_relation_changed()
1524
1589
    elif hook_name == "mongos-relation-broken":
1525
1590
        retVal = mongos_relation_broken()
 
1591
    elif hook_name == "data-relation-joined":
 
1592
        retVal = data_relation_joined()
 
1593
    elif hook_name == "data-relation-changed":
 
1594
        retVal = data_relation_changed()
 
1595
    elif hook_name == "data-relation-departed":
 
1596
        retVal = data_relation_departed()
1526
1597
    else:
1527
1598
        print "Unknown hook"
1528
1599
        retVal = False