Added db and db-admin hooks
This commit is contained in:
parent
dfa83bf396
commit
b52a8b2ffd
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ PYTHON := /usr/bin/env python
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
@flake8 --exclude hooks/charmhelpers hooks
|
@flake8 --exclude hooks/charmhelpers hooks
|
||||||
@flake8 --exclude hooks/charmhelpers unit_tests
|
#@flake8 --exclude hooks/charmhelpers unit_tests
|
||||||
@charm proof
|
@charm proof
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
percona_hooks.py
|
|
@ -0,0 +1 @@
|
||||||
|
percona_hooks.py
|
|
@ -35,7 +35,8 @@ class MySQLHelper():
|
||||||
def create_database(self, db_name):
|
def create_database(self, db_name):
|
||||||
cursor = self.connection.cursor()
|
cursor = self.connection.cursor()
|
||||||
try:
|
try:
|
||||||
cursor.execute("CREATE DATABASE {}".format(db_name))
|
cursor.execute("CREATE DATABASE {} CHARACTER SET UTF8"
|
||||||
|
.format(db_name))
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
|
@ -65,6 +66,17 @@ class MySQLHelper():
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
|
def create_admin_grant(self, db_user,
|
||||||
|
remote_ip, password):
|
||||||
|
cursor = self.connection.cursor()
|
||||||
|
try:
|
||||||
|
cursor.execute("GRANT ALL PRIVILEGES ON *.* TO '{}'@'{}' "
|
||||||
|
"IDENTIFIED BY '{}'".format(db_user,
|
||||||
|
remote_ip,
|
||||||
|
password))
|
||||||
|
finally:
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
def cleanup_grant(self, db_user,
|
def cleanup_grant(self, db_user,
|
||||||
remote_ip):
|
remote_ip):
|
||||||
cursor = self.connection.cursor()
|
cursor = self.connection.cursor()
|
||||||
|
@ -75,6 +87,15 @@ class MySQLHelper():
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
|
def execute(self, sql):
|
||||||
|
''' Execute arbitary SQL against the database '''
|
||||||
|
cursor = self.connection.cursor()
|
||||||
|
try:
|
||||||
|
cursor.execute(sql)
|
||||||
|
finally:
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
_root_passwd = '/var/lib/charm/{}/mysql.passwd'
|
_root_passwd = '/var/lib/charm/{}/mysql.passwd'
|
||||||
_named_passwd = '/var/lib/charm/{}/mysql-{}.passwd'
|
_named_passwd = '/var/lib/charm/{}/mysql-{}.passwd'
|
||||||
|
|
||||||
|
@ -111,7 +132,8 @@ def get_mysql_root_password(password=None):
|
||||||
|
|
||||||
def configure_db(hostname,
|
def configure_db(hostname,
|
||||||
database,
|
database,
|
||||||
username):
|
username,
|
||||||
|
admin=False):
|
||||||
''' Configure access to database for username from hostname '''
|
''' Configure access to database for username from hostname '''
|
||||||
if hostname != unit_get('private-address'):
|
if hostname != unit_get('private-address'):
|
||||||
remote_ip = socket.gethostbyname(hostname)
|
remote_ip = socket.gethostbyname(hostname)
|
||||||
|
@ -126,7 +148,11 @@ def configure_db(hostname,
|
||||||
if not m_helper.grant_exists(database,
|
if not m_helper.grant_exists(database,
|
||||||
username,
|
username,
|
||||||
remote_ip):
|
remote_ip):
|
||||||
|
if not admin:
|
||||||
m_helper.create_grant(database,
|
m_helper.create_grant(database,
|
||||||
username,
|
username,
|
||||||
remote_ip, password)
|
remote_ip, password)
|
||||||
|
else:
|
||||||
|
m_helper.create_admin_grant(username,
|
||||||
|
remote_ip, password)
|
||||||
return password
|
return password
|
||||||
|
|
|
@ -15,6 +15,8 @@ from charmhelpers.core.hookenv import (
|
||||||
unit_get,
|
unit_get,
|
||||||
config,
|
config,
|
||||||
service_name,
|
service_name,
|
||||||
|
remote_unit,
|
||||||
|
relation_type
|
||||||
)
|
)
|
||||||
from charmhelpers.core.host import (
|
from charmhelpers.core.host import (
|
||||||
service_restart,
|
service_restart,
|
||||||
|
@ -109,18 +111,53 @@ def cluster_changed():
|
||||||
service_restart('mysql')
|
service_restart('mysql')
|
||||||
|
|
||||||
if eligible_leader(LEADER_RES):
|
if eligible_leader(LEADER_RES):
|
||||||
files = glob.glob('/var/lib/charm/{}/*.passwd'.format(service_name()))
|
sync_files()
|
||||||
|
|
||||||
|
|
||||||
|
def sync_files():
|
||||||
|
''' Sync shared charm state files to all peers '''
|
||||||
|
files = glob.glob('/var/lib/charm/{}/*'.format(service_name()))
|
||||||
sync_to_peers(peer_interface='cluster',
|
sync_to_peers(peer_interface='cluster',
|
||||||
user='juju_ssh', paths=files)
|
user='juju_ssh', paths=files)
|
||||||
|
|
||||||
LEADER_RES = 'res_mysql_vip'
|
LEADER_RES = 'res_mysql_vip'
|
||||||
|
|
||||||
|
|
||||||
|
@hooks.hook('db-relation-changed')
|
||||||
|
@hooks.hook('db-admin-relation-changed')
|
||||||
|
def db_changed():
|
||||||
|
if not eligible_leader(LEADER_RES):
|
||||||
|
log('Service is peered, clearing db relation'
|
||||||
|
' as this service unit is not the leader')
|
||||||
|
relation_clear()
|
||||||
|
return
|
||||||
|
|
||||||
|
if is_clustered():
|
||||||
|
db_host = config('vip')
|
||||||
|
else:
|
||||||
|
db_host = unit_get('private-address')
|
||||||
|
|
||||||
|
admin = relation_type() == 'db-admin-relation-changed'
|
||||||
|
database_name, _ = remote_unit().split("/")
|
||||||
|
username = database_name # TODO: is this OK? mysql used a random username
|
||||||
|
password = configure_db(relation_get('private-address'),
|
||||||
|
database_name,
|
||||||
|
username,
|
||||||
|
admin=admin)
|
||||||
|
relation_set(database=database_name,
|
||||||
|
user=username,
|
||||||
|
password=password,
|
||||||
|
host=db_host)
|
||||||
|
|
||||||
|
sync_files()
|
||||||
|
|
||||||
|
|
||||||
@hooks.hook('shared-db-relation-changed')
|
@hooks.hook('shared-db-relation-changed')
|
||||||
def shared_db_changed():
|
def shared_db_changed():
|
||||||
if not eligible_leader(LEADER_RES):
|
if not eligible_leader(LEADER_RES):
|
||||||
log('MySQL service is peered, bailing shared-db relation'
|
log('Service is peered, clearing shared-db relation'
|
||||||
' as this service unit is not the leader')
|
' as this service unit is not the leader')
|
||||||
|
relation_clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
settings = relation_get()
|
settings = relation_get()
|
||||||
|
@ -178,9 +215,7 @@ def shared_db_changed():
|
||||||
relation_set(**return_data)
|
relation_set(**return_data)
|
||||||
relation_set(db_host=db_host)
|
relation_set(db_host=db_host)
|
||||||
|
|
||||||
files = glob.glob('/var/lib/charm/{}/*.passwd'.format(service_name()))
|
sync_files()
|
||||||
sync_to_peers(peer_interface='cluster',
|
|
||||||
user='juju_ssh', paths=files)
|
|
||||||
|
|
||||||
|
|
||||||
@hooks.hook('ha-relation-joined')
|
@hooks.hook('ha-relation-joined')
|
||||||
|
@ -220,11 +255,21 @@ def ha_relation_changed():
|
||||||
for r_id in relation_ids('shared-db'):
|
for r_id in relation_ids('shared-db'):
|
||||||
relation_set(rid=r_id,
|
relation_set(rid=r_id,
|
||||||
db_host=config('vip'))
|
db_host=config('vip'))
|
||||||
|
for r_id in relation_ids('db'):
|
||||||
|
relation_set(rid=r_id,
|
||||||
|
host=config('vip'))
|
||||||
|
for r_id in relation_ids('db-admin'):
|
||||||
|
relation_set(rid=r_id,
|
||||||
|
host=config('vip'))
|
||||||
else:
|
else:
|
||||||
# Clear any settings data for non-leader units
|
# Clear any settings data for non-leader units
|
||||||
log('Cluster configured, not leader, clearing relation data')
|
log('Cluster configured, not leader, clearing relation data')
|
||||||
for r_id in relation_ids('shared-db'):
|
for r_id in relation_ids('shared-db'):
|
||||||
relation_clear(r_id)
|
relation_clear(r_id)
|
||||||
|
for r_id in relation_ids('db'):
|
||||||
|
relation_clear(r_id)
|
||||||
|
for r_id in relation_ids('db-admin'):
|
||||||
|
relation_clear(r_id)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -126,7 +126,7 @@ def configure_mysql_root_password(password):
|
||||||
|
|
||||||
|
|
||||||
# TODO: Submit for charmhelper
|
# TODO: Submit for charmhelper
|
||||||
def relation_clear(r_id):
|
def relation_clear(r_id=None):
|
||||||
''' Clears any relation data already set on relation r_id '''
|
''' Clears any relation data already set on relation r_id '''
|
||||||
settings = relation_get(rid=r_id,
|
settings = relation_get(rid=r_id,
|
||||||
unit=local_unit())
|
unit=local_unit())
|
||||||
|
|
|
@ -127,7 +127,8 @@ def ensure_user(user, group=None):
|
||||||
subprocess.check_call(cmd)
|
subprocess.check_call(cmd)
|
||||||
|
|
||||||
|
|
||||||
def ssh_authorized_peers(peer_interface, user, group=None, ensure_local_user=False):
|
def ssh_authorized_peers(peer_interface, user, group=None,
|
||||||
|
ensure_local_user=False):
|
||||||
"""
|
"""
|
||||||
Main setup function, should be called from both peer -changed and -joined
|
Main setup function, should be called from both peer -changed and -joined
|
||||||
hooks with the same parameters.
|
hooks with the same parameters.
|
||||||
|
|
Loading…
Reference in New Issue