~gandelman-a/charms/precise/hacluster/ocf

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
import utils
import commands
import re
import subprocess

#def is_quorum():
#import time

#def is_leader():


def wait_for_pcmk():
    crm_up = None
    while not crm_up:
        (status, output) = commands.getstatusoutput("crm node list")
        show_re = re.compile(utils.get_unit_hostname())
        crm_up = show_re.search(output)


def commit(cmd):
    subprocess.call(cmd.split())


def is_resource_present(resource):
    (status, output) = \
        commands.getstatusoutput("crm resource status %s" % resource)
    if status != 0:
        return False
    return True


def standby(node=None):
    if node is None:
        cmd = "crm -F node standby"
    else:
        cmd = "crm -F node standby %s" % node
    commit(cmd)


def online(node=None):
    if node is None:
        cmd = "crm -F node online"
    else:
        cmd = "crm -F node online %s" % node
    commit(cmd)


def crm_opt_exists(opt_name):
    (status, output) = commands.getstatusoutput("crm configure show")
    show_re = re.compile(opt_name)
    opt = show_re.search(output)
    if opt:
        return True
    return False


def list_nodes():
    cmd = ['crm', 'node', 'list']
    out = subprocess.check_output(cmd)
    nodes = []
    for line in out.split('\n'):
        if line != '':
            nodes.append(line.split(':')[0])
    return nodes


def _maas_ipmi_stonith_resource(node, power_params):
    rsc_name = 'res_stonith_%s' % node
    rsc = 'primitive %s stonith:external/ipmi' % rsc_name
    rsc += ' params hostname=%s ipaddr=%s userid=%s passwd=%s interface=lan' %\
           (node, power_params['power_address'],
            power_params['power_user'], power_params['power_pass'])

    # ensure ipmi stonith agents are not running on the nodes that
    # they manage.
    constraint = 'location const_loc_stonith_avoid_%s %s -inf: %s' %\
                  (node, rsc_name, node)

    return rsc, constraint


def maas_stonith_primitive(maas_nodes, crm_node):
    power_type = power_params = None
    for node in maas_nodes:
        if node['hostname'].startswith(crm_node):
            power_type = node['power_type']
            power_params = node['power_parameters']

    if not power_type or not power_params:
        return False, False

    rsc = constraint = None
    # we can extend to support other power flavors in the future?
    if power_type == 'ipmi':
        rsc, constraint = _maas_ipmi_stonith_resource(crm_node, power_params)
    else:
        utils.juju_log('ERROR',
                       'Unsupported STONITH power_type: %s' % power_type)
        return False, False

    if not rsc or not constraint:
        return False, False

    return rsc, constraint