~billy-olsen/charms/trusty/hacluster/lp1394008.quorum-fix

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: James Page
  • Date: 2014-04-11 10:25:09 UTC
  • mfrom: (18.2.5 hacluster)
  • Revision ID: james.page@canonical.com-20140411102509-ulrtgrpgnjt8x9g4
Rebase on precise charm for pingd and lint tidy

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
        for unit in utils.relation_list(relid):
40
40
            conf = {
41
41
                'corosync_bindnetaddr':
42
 
                    hacluster.get_network_address(
43
 
                              utils.relation_get('corosync_bindiface',
44
 
                                                 unit, relid)
45
 
                              ),
 
42
                hacluster.get_network_address(
 
43
                    utils.relation_get('corosync_bindiface',
 
44
                                       unit, relid)
 
45
                ),
46
46
                'corosync_mcastport': utils.relation_get('corosync_mcastport',
47
47
                                                         unit, relid),
48
48
                'corosync_mcastaddr': utils.config_get('corosync_mcastaddr'),
49
49
                }
50
50
            if None not in conf.itervalues():
51
51
                return conf
52
 
    missing = [k for k, v in conf.iteritems() if v == None]
 
52
    missing = [k for k, v in conf.iteritems() if v is None]
53
53
    utils.juju_log('INFO',
54
54
                   'Missing required principle configuration: %s' % missing)
55
55
    return None
75
75
        # write the authkey
76
76
        with open('/etc/corosync/authkey', 'w') as corosync_key_file:
77
77
            corosync_key_file.write(b64decode(corosync_key))
78
 
        os.chmod = ('/etc/corosync/authkey', 0400)
 
78
        os.chmod = ('/etc/corosync/authkey', 0o400)
79
79
 
80
80
 
81
81
def config_changed():
138
138
    # Check that there's enough nodes in order to perform the
139
139
    # configuration of the HA cluster
140
140
    if (len(get_cluster_nodes()) <
141
 
        int(utils.config_get('cluster_count'))):
 
141
            int(utils.config_get('cluster_count'))):
142
142
        utils.juju_log('WARNING', 'Not enough nodes in cluster, bailing')
143
143
        return
144
144
 
153
153
        resources = \
154
154
            {} if utils.relation_get("resources",
155
155
                                     unit, relid) is None \
156
 
               else ast.literal_eval(utils.relation_get("resources",
157
 
                                                        unit, relid))
 
156
            else ast.literal_eval(utils.relation_get("resources",
 
157
                                                     unit, relid))
158
158
        resource_params = \
159
159
            {} if utils.relation_get("resource_params",
160
160
                                     unit, relid) is None \
161
 
               else ast.literal_eval(utils.relation_get("resource_params",
162
 
                                                        unit, relid))
 
161
            else ast.literal_eval(utils.relation_get("resource_params",
 
162
                                                     unit, relid))
163
163
        groups = \
164
164
            {} if utils.relation_get("groups",
165
165
                                     unit, relid) is None \
166
 
               else ast.literal_eval(utils.relation_get("groups",
167
 
                                                        unit, relid))
 
166
            else ast.literal_eval(utils.relation_get("groups",
 
167
                                                     unit, relid))
168
168
        ms = \
169
169
            {} if utils.relation_get("ms",
170
170
                                     unit, relid) is None \
171
 
               else ast.literal_eval(utils.relation_get("ms",
172
 
                                                        unit, relid))
 
171
            else ast.literal_eval(utils.relation_get("ms",
 
172
                                                     unit, relid))
173
173
        orders = \
174
174
            {} if utils.relation_get("orders",
175
175
                                     unit, relid) is None \
176
 
               else ast.literal_eval(utils.relation_get("orders",
177
 
                                                        unit, relid))
 
176
            else ast.literal_eval(utils.relation_get("orders",
 
177
                                                     unit, relid))
178
178
        colocations = \
179
179
            {} if utils.relation_get("colocations",
180
180
                                     unit, relid) is None \
181
 
               else ast.literal_eval(utils.relation_get("colocations",
182
 
                                                        unit, relid))
 
181
            else ast.literal_eval(utils.relation_get("colocations",
 
182
                                                     unit, relid))
183
183
        clones = \
184
184
            {} if utils.relation_get("clones",
185
185
                                     unit, relid) is None \
186
 
               else ast.literal_eval(utils.relation_get("clones",
187
 
                                                        unit, relid))
 
186
            else ast.literal_eval(utils.relation_get("clones",
 
187
                                                     unit, relid))
188
188
        init_services = \
189
189
            {} if utils.relation_get("init_services",
190
190
                                     unit, relid) is None \
191
 
               else ast.literal_eval(utils.relation_get("init_services",
192
 
                                                        unit, relid))
 
191
            else ast.literal_eval(utils.relation_get("init_services",
 
192
                                                     unit, relid))
193
193
 
194
194
    else:
195
195
        utils.juju_log('WARNING',
219
219
          ' resource-stickiness="100"'
220
220
    pcmk.commit(cmd)
221
221
 
 
222
    # Configure Ping service
 
223
    monitor_host = utils.config_get('monitor_host')
 
224
    if monitor_host:
 
225
        if not pcmk.crm_opt_exists('ping'):
 
226
            monitor_interval = utils.config_get('monitor_interval')
 
227
            cmd = 'crm -w -F configure primitive ping' \
 
228
                  ' ocf:pacemaker:ping params host_list="%s"' \
 
229
                  ' multiplier="100" op monitor interval="%s"' %\
 
230
                  (monitor_host, monitor_interval)
 
231
            cmd2 = 'crm -w -F configure clone cl_ping ping' \
 
232
                   ' meta interleave="true"'
 
233
            pcmk.commit(cmd)
 
234
            pcmk.commit(cmd2)
 
235
 
222
236
    # Only configure the cluster resources
223
237
    # from the oldest peer unit.
224
238
    if cluster.oldest_peer(cluster.peer_units()):
237
251
                if utils.running(init_services[res_name]):
238
252
                    utils.stop(init_services[res_name])
239
253
            # Put the services in HA, if not already done so
240
 
            #if not pcmk.is_resource_present(res_name):
 
254
            # if not pcmk.is_resource_present(res_name):
241
255
            if not pcmk.crm_opt_exists(res_name):
242
256
                if not res_name in resource_params:
243
257
                    cmd = 'crm -w -F configure primitive %s %s' % (res_name,
244
258
                                                                   res_type)
245
259
                else:
246
260
                    cmd = 'crm -w -F configure primitive %s %s %s' % \
247
 
                                (res_name,
248
 
                                 res_type,
249
 
                                 resource_params[res_name])
 
261
                        (res_name,
 
262
                         res_type,
 
263
                         resource_params[res_name])
250
264
                pcmk.commit(cmd)
251
265
                utils.juju_log('INFO', '%s' % cmd)
 
266
                if monitor_host:
 
267
                    cmd = 'crm -F configure location Ping-%s %s rule' \
 
268
                          ' -inf: pingd lte 0' % (res_name, res_name)
 
269
                    pcmk.commit(cmd)
252
270
 
253
271
        utils.juju_log('INFO', 'Configuring Groups')
254
272
        utils.juju_log('INFO', str(groups))
303
321
                # than as individual resources.
304
322
                if (res_name not in clones.values() and
305
323
                    res_name not in groups.values() and
306
 
                    not pcmk.crm_res_running(res_name)):
 
324
                        not pcmk.crm_res_running(res_name)):
307
325
                    # Just in case, cleanup the resources to ensure they get
308
326
                    # started in case they failed for some unrelated reason.
309
327
                    cmd = 'crm resource cleanup %s' % res_name
346
364
    url = utils.config_get('maas_url')
347
365
    creds = utils.config_get('maas_credentials')
348
366
    if None in [url, creds]:
349
 
        utils.juju_log('ERROR', 'maas_url and maas_credentials must be set'\
 
367
        utils.juju_log('ERROR', 'maas_url and maas_credentials must be set'
350
368
                       ' in config to enable STONITH.')
351
369
        sys.exit(1)
352
370
 
353
371
    maas = MAAS.MAASHelper(url, creds)
354
372
    nodes = maas.list_nodes()
355
373
    if not nodes:
356
 
        utils.juju_log('ERROR', 'Could not obtain node inventory from '\
 
374
        utils.juju_log('ERROR', 'Could not obtain node inventory from '
357
375
                       'MAAS @ %s.' % url)
358
376
        sys.exit(1)
359
377
 
362
380
        rsc, constraint = pcmk.maas_stonith_primitive(nodes, node)
363
381
        if not rsc:
364
382
            utils.juju_log('ERROR',
365
 
                           'Failed to determine STONITH primitive for node'\
 
383
                           'Failed to determine STONITH primitive for node'
366
384
                           ' %s' % node)
367
385
            sys.exit(1)
368
386
 
369
387
        rsc_name = str(rsc).split(' ')[1]
370
388
        if not pcmk.is_resource_present(rsc_name):
371
 
            utils.juju_log('INFO', 'Creating new STONITH primitive %s.' %\
 
389
            utils.juju_log('INFO', 'Creating new STONITH primitive %s.' %
372
390
                           rsc_name)
373
391
            cmd = 'crm -F configure %s' % rsc
374
392
            pcmk.commit(cmd)
376
394
                cmd = 'crm -F configure %s' % constraint
377
395
                pcmk.commit(cmd)
378
396
        else:
379
 
            utils.juju_log('INFO', 'STONITH primitive already exists '\
 
397
            utils.juju_log('INFO', 'STONITH primitive already exists '
380
398
                           'for node.')
381
399
 
382
400
    cmd = "crm configure property stonith-enabled=true"
405
423
    'ha-relation-changed': configure_cluster,
406
424
    'hanode-relation-joined': configure_cluster,
407
425
    'hanode-relation-changed': configure_cluster,
408
 
    }
 
426
}
409
427
 
410
428
utils.do_hooks(hooks)