Rework assess_status location, call on service resume action

This commit is contained in:
James Page 2015-10-10 09:07:05 -07:00
parent 22b97b3a99
commit 1b19f34c55
6 changed files with 79 additions and 67 deletions

View File

@ -5,6 +5,7 @@ import sys
from charmhelpers.core.host import service_pause, service_resume
from charmhelpers.core.hookenv import action_fail, status_set
from percona_utils import assess_status
MYSQL_SERVICE = "mysql"
@ -18,7 +19,8 @@ def pause(args):
if not service_pause(MYSQL_SERVICE):
raise Exception("Failed to pause MySQL service.")
status_set(
"maintenance", "Paused. Use 'resume' action to resume normal service.")
"maintenance",
"Unit paused. Use 'resume' action to resume normal service.")
def resume(args):
@ -27,7 +29,7 @@ def resume(args):
@raises Exception should the service fail to start."""
if not service_resume(MYSQL_SERVICE):
raise Exception("Failed to resume MySQL service.")
status_set("active", "")
assess_status()
# A dictionary of all the defined actions to callables (which take

1
actions/percona_utils.py Symbolic link
View File

@ -0,0 +1 @@
../hooks/percona_utils.py

View File

@ -24,7 +24,6 @@ from charmhelpers.core.hookenv import (
INFO,
WARNING,
is_leader,
status_set,
)
from charmhelpers.core.host import (
service,
@ -62,7 +61,7 @@ from percona_utils import (
notify_bootstrapped,
is_bootstrapped,
get_wsrep_value,
cluster_in_sync,
assess_status,
)
from charmhelpers.contrib.database.mysql import (
PerconaClusterHelper,
@ -622,27 +621,6 @@ def update_nrpe_config():
nrpe_setup.write()
def assess_status():
'''Assess the status of the current unit'''
min_size = config('min-cluster-size')
# Ensure that number of peers > cluster size configuration
if not is_sufficient_peers():
status_set('blocked', 'Insufficient peers to bootstrap cluster')
return
if min_size and int(min_size) > 1:
# Once running, ensure that cluster is in sync
# and has the required peers
if not is_bootstrapped():
status_set('waiting', 'Unit waiting for cluster bootstrap')
elif is_bootstrapped() and cluster_in_sync():
status_set('active', 'Unit is ready and clustered')
else:
status_set('blocked', 'Unit is not in sync')
else:
status_set('active', 'Unit is ready')
def main():
try:
hooks.execute(sys.argv)

View File

@ -25,6 +25,7 @@ from charmhelpers.core.hookenv import (
INFO,
WARNING,
ERROR,
status_set,
)
from charmhelpers.fetch import (
apt_install,
@ -372,3 +373,24 @@ def cluster_in_sync():
if ready and int(sync_status) in [2, 4]:
return True
return False
def assess_status():
'''Assess the status of the current unit'''
min_size = config('min-cluster-size')
# Ensure that number of peers > cluster size configuration
if not is_sufficient_peers():
status_set('blocked', 'Insufficient peers to bootstrap cluster')
return
if min_size and int(min_size) > 1:
# Once running, ensure that cluster is in sync
# and has the required peers
if not is_bootstrapped():
status_set('waiting', 'Unit waiting for cluster bootstrap')
elif is_bootstrapped() and cluster_in_sync():
status_set('active', 'Unit is ready and clustered')
else:
status_set('blocked', 'Unit is not in sync')
else:
status_set('active', 'Unit is ready')

View File

@ -12,9 +12,7 @@ TO_PATCH = ['log', 'config',
'update_nrpe_config',
'get_iface_for_address',
'get_netmask_for_address',
'cluster_in_sync',
'is_bootstrapped',
'status_set',
'is_sufficient_peers']
@ -131,43 +129,3 @@ class TestHaRelation(CharmTestCase):
call_args, call_kwargs = self.relation_set.call_args
self.assertEqual(resource_params, call_kwargs['resource_params'])
class TestAssessStatus(CharmTestCase):
def setUp(self):
CharmTestCase.setUp(self, hooks, TO_PATCH)
def test_single_unit(self):
self.config.return_value = None
self.is_sufficient_peers.return_value = True
hooks.assess_status()
self.status_set.assert_called_with('active', mock.ANY)
def test_insufficient_peers(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = False
hooks.assess_status()
self.status_set.assert_called_with('blocked', mock.ANY)
def test_not_bootstrapped(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = False
hooks.assess_status()
self.status_set.assert_called_with('waiting', mock.ANY)
def test_bootstrapped_in_sync(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = True
self.cluster_in_sync.return_value = True
hooks.assess_status()
self.status_set.assert_called_with('active', mock.ANY)
def test_bootstrapped_not_in_sync(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = True
self.cluster_in_sync.return_value = False
hooks.assess_status()
self.status_set.assert_called_with('blocked', mock.ANY)

View File

@ -7,6 +7,8 @@ import sys
sys.modules['MySQLdb'] = mock.Mock()
import percona_utils
from test_utils import CharmTestCase
class UtilsTests(unittest.TestCase):
def setUp(self):
@ -180,3 +182,52 @@ class UtilsTests(unittest.TestCase):
def test_cluster_in_sync_ready_sync_donor(self, _wsrep_value):
_wsrep_value.side_effect = [True, 2]
self.assertTrue(percona_utils.cluster_in_sync())
TO_PATCH = [
'status_set',
'is_sufficient_peers',
'is_bootstrapped',
'config',
'cluster_in_sync',
]
class TestAssessStatus(CharmTestCase):
def setUp(self):
CharmTestCase.setUp(self, percona_utils, TO_PATCH)
def test_single_unit(self):
self.config.return_value = None
self.is_sufficient_peers.return_value = True
percona_utils.assess_status()
self.status_set.assert_called_with('active', mock.ANY)
def test_insufficient_peers(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = False
percona_utils.assess_status()
self.status_set.assert_called_with('blocked', mock.ANY)
def test_not_bootstrapped(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = False
percona_utils.assess_status()
self.status_set.assert_called_with('waiting', mock.ANY)
def test_bootstrapped_in_sync(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = True
self.cluster_in_sync.return_value = True
percona_utils.assess_status()
self.status_set.assert_called_with('active', mock.ANY)
def test_bootstrapped_not_in_sync(self):
self.config.return_value = 3
self.is_sufficient_peers.return_value = True
self.is_bootstrapped.return_value = True
self.cluster_in_sync.return_value = False
percona_utils.assess_status()
self.status_set.assert_called_with('blocked', mock.ANY)