6
import lib.utils as utils
7
import lib.ceph_utils as ceph
8
import lib.cluster_utils as cluster
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'
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')
24
if None in [vip, vip_cidr, vip_iface]:
25
utils.juju_log('WARNING',
26
'Insufficient VIP information to configure cluster')
29
# Starting configuring resources.
31
'res_mysqld': 'mysql',
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')
42
utils.juju_log('INFO',
43
'*ceph* relation exists. Sending *ha* relation data')
45
block_storage = 'ceph'
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',
54
rbd_name = utils.config_get('rbd-name')
56
'res_mysql_rbd': 'params name="%s" pool="%s" user="%s" '
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"',
69
'grp_mysql': 'res_mysql_rbd res_mysql_fs res_mysql_vip res_mysqld',
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,
78
resource_params=resource_params,
79
init_services=init_services,
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'))
94
utils.juju_log('INFO', 'Start Ceph Relation Joined')
96
utils.juju_log('INFO', 'Finish Ceph Relation Joined')
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')
107
ceph.configure(service=SERVICE_NAME, key=key, auth=auth)
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'])
119
utils.juju_log('INFO',
120
'This is not the peer leader. Not configuring RBD.')
122
if utils.running('mysql'):
123
utils.juju_log('INFO', 'Stopping MySQL...')
126
# If 'ha' relation has been made before the 'ceph' relation
127
# it is important to make sure the ha-relation data is being
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.')
136
utils.juju_log('INFO', 'Finish Ceph Relation Changed')
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,
146
utils.do_hooks(hooks)