~marcoceppi/charms/trusty/mysql/action-jackson

« back to all changes in this revision

Viewing changes to hooks/ha_relations.py

  • Committer: Marco Ceppi
  • Date: 2013-05-30 16:00:47 UTC
  • mfrom: (91.2.17 mysql)
  • Revision ID: marco@ceppi.net-20130530160047-i68avmlzo6go3sny
ha-support: https://code.launchpad.net/~openstack-charmers/charms/precise/mysql/ha-support/+merge/165059

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import sys
 
4
import os
 
5
 
 
6
import lib.utils as utils
 
7
import lib.ceph_utils as ceph
 
8
import lib.cluster_utils as cluster
 
9
 
 
10
# CEPH
 
11
DATA_SRC_DST = '/var/lib/mysql'
 
12
SERVICE_NAME = os.getenv('JUJU_UNIT_NAME').split('/')[0]
 
13
POOL_NAME = SERVICE_NAME
 
14
LEADER_RES = 'res_mysql_vip'
 
15
 
 
16
 
 
17
def ha_relation_joined():
 
18
    vip = utils.config_get('vip')
 
19
    vip_iface = utils.config_get('vip_iface')
 
20
    vip_cidr = utils.config_get('vip_cidr')
 
21
    corosync_bindiface = utils.config_get('ha-bindiface')
 
22
    corosync_mcastport = utils.config_get('ha-mcastport')
 
23
 
 
24
    if None in [vip, vip_cidr, vip_iface]:
 
25
        utils.juju_log('WARNING',
 
26
                       'Insufficient VIP information to configure cluster')
 
27
        sys.exit(1)
 
28
 
 
29
    # Starting configuring resources.
 
30
    init_services = {
 
31
            'res_mysqld': 'mysql',
 
32
        }
 
33
 
 
34
    # If the 'ha' relation has been made *before* the 'ceph' relation,
 
35
    # it doesn't make sense to make it until after the 'ceph' relation is made
 
36
    if not utils.is_relation_made('ceph', 'auth'):
 
37
        utils.juju_log('INFO',
 
38
                       '*ceph* relation does not exist. '
 
39
                       'Not sending *ha* relation data yet')
 
40
        return
 
41
    else:
 
42
        utils.juju_log('INFO',
 
43
                       '*ceph* relation exists. Sending *ha* relation data')
 
44
 
 
45
        block_storage = 'ceph'
 
46
 
 
47
        resources = {
 
48
            'res_mysql_rbd': 'ocf:ceph:rbd',
 
49
            'res_mysql_fs': 'ocf:heartbeat:Filesystem',
 
50
            'res_mysql_vip': 'ocf:heartbeat:IPaddr2',
 
51
            'res_mysqld': 'upstart:mysql',
 
52
            }
 
53
 
 
54
        rbd_name = utils.config_get('rbd-name')
 
55
        resource_params = {
 
56
            'res_mysql_rbd': 'params name="%s" pool="%s" user="%s" '
 
57
                             'secret="%s"' % \
 
58
                             (rbd_name, POOL_NAME,
 
59
                              SERVICE_NAME, ceph.keyfile_path(SERVICE_NAME)),
 
60
            'res_mysql_fs': 'params device="/dev/rbd/%s/%s" directory="%s" '
 
61
                            'fstype="ext4" op start start-delay="10s"' % \
 
62
                            (POOL_NAME, rbd_name, DATA_SRC_DST),
 
63
            'res_mysql_vip': 'params ip="%s" cidr_netmask="%s" nic="%s"' % \
 
64
                             (vip, vip_cidr, vip_iface),
 
65
            'res_mysqld': 'op start start-delay="5s" op monitor interval="5s"',
 
66
            }
 
67
 
 
68
        groups = {
 
69
            'grp_mysql': 'res_mysql_rbd res_mysql_fs res_mysql_vip res_mysqld',
 
70
            }
 
71
 
 
72
        for rel_id in utils.relation_ids('ha'):
 
73
            utils.relation_set(rid=rel_id,
 
74
                               block_storage=block_storage,
 
75
                               corosync_bindiface=corosync_bindiface,
 
76
                               corosync_mcastport=corosync_mcastport,
 
77
                               resources=resources,
 
78
                               resource_params=resource_params,
 
79
                               init_services=init_services,
 
80
                               groups=groups)
 
81
 
 
82
 
 
83
def ha_relation_changed():
 
84
    clustered = utils.relation_get('clustered')
 
85
    if (clustered and cluster.is_leader(LEADER_RES)):
 
86
        utils.juju_log('INFO', 'Cluster configured, notifying other services')
 
87
        # Tell all related services to start using the VIP
 
88
        for r_id in utils.relation_ids('shared-db'):
 
89
            utils.relation_set(rid=r_id,
 
90
                               db_host=utils.config_get('vip'))
 
91
 
 
92
 
 
93
def ceph_joined():
 
94
    utils.juju_log('INFO', 'Start Ceph Relation Joined')
 
95
    ceph.install()
 
96
    utils.juju_log('INFO', 'Finish Ceph Relation Joined')
 
97
 
 
98
 
 
99
def ceph_changed():
 
100
    utils.juju_log('INFO', 'Start Ceph Relation Changed')
 
101
    auth = utils.relation_get('auth')
 
102
    key = utils.relation_get('key')
 
103
    if None in [auth, key]:
 
104
        utils.juju_log('INFO', 'Missing key or auth in relation')
 
105
        return
 
106
 
 
107
    ceph.configure(service=SERVICE_NAME, key=key, auth=auth)
 
108
 
 
109
    if cluster.eligible_leader(LEADER_RES):
 
110
        sizemb = int(utils.config_get('block-size')) * 1024
 
111
        rbd_img = utils.config_get('rbd-name')
 
112
        blk_device = '/dev/rbd/%s/%s' % (POOL_NAME, rbd_img)
 
113
        ceph.ensure_ceph_storage(service=SERVICE_NAME, pool=POOL_NAME,
 
114
                                 rbd_img=rbd_img, sizemb=sizemb,
 
115
                                 fstype='ext4', mount_point=DATA_SRC_DST,
 
116
                                 blk_device=blk_device,
 
117
                                 system_services=['mysql'])
 
118
    else:
 
119
        utils.juju_log('INFO',
 
120
                       'This is not the peer leader. Not configuring RBD.')
 
121
        # Stopping MySQL
 
122
        if utils.running('mysql'):
 
123
            utils.juju_log('INFO', 'Stopping MySQL...')
 
124
            utils.stop('mysql')
 
125
 
 
126
    # If 'ha' relation has been made before the 'ceph' relation
 
127
    # it is important to make sure the ha-relation data is being
 
128
    # sent.
 
129
    if utils.is_relation_made('ha'):
 
130
        utils.juju_log('INFO',
 
131
                       '*ha* relation exists. Making sure the ha'
 
132
                       ' relation data is sent.')
 
133
        ha_relation_joined()
 
134
        return
 
135
 
 
136
    utils.juju_log('INFO', 'Finish Ceph Relation Changed')
 
137
 
 
138
 
 
139
hooks = {
 
140
    "ha-relation-joined": ha_relation_joined,
 
141
    "ha-relation-changed": ha_relation_changed,
 
142
    "ceph-relation-joined": ceph_joined,
 
143
    "ceph-relation-changed": ceph_changed,
 
144
}
 
145
 
 
146
utils.do_hooks(hooks)