[corey.bryant, r=thedac] Workload Status
This commit is contained in:
commit
55b3938836
|
@ -5,10 +5,13 @@ import os
|
|||
import sys
|
||||
import yaml
|
||||
|
||||
from charmhelpers.core.host import service_pause, service_resume
|
||||
from charmhelpers.core.hookenv import action_fail, status_set
|
||||
sys.path.append('hooks/')
|
||||
|
||||
from lib.swift_utils import services
|
||||
from charmhelpers.core.host import service_pause, service_resume
|
||||
from charmhelpers.core.hookenv import action_fail
|
||||
from charmhelpers.core.unitdata import HookData, kv
|
||||
from lib.swift_utils import assess_status, services
|
||||
from swift_hooks import CONFIGS
|
||||
|
||||
|
||||
def get_action_parser(actions_yaml_path, action_name,
|
||||
|
@ -31,8 +34,9 @@ def pause(args):
|
|||
stopped = service_pause(service)
|
||||
if not stopped:
|
||||
raise Exception("{} didn't stop cleanly.".format(service))
|
||||
status_set(
|
||||
"maintenance", "Paused. Use 'resume' action to resume normal service.")
|
||||
with HookData()():
|
||||
kv().set('unit-paused', True)
|
||||
assess_status(CONFIGS)
|
||||
|
||||
|
||||
def resume(args):
|
||||
|
@ -44,7 +48,9 @@ def resume(args):
|
|||
started = service_resume(service)
|
||||
if not started:
|
||||
raise Exception("{} didn't start cleanly.".format(service))
|
||||
status_set("active", "")
|
||||
with HookData()():
|
||||
kv().set('unit-paused', False)
|
||||
assess_status(CONFIGS)
|
||||
|
||||
|
||||
# A dictionary of all the defined actions to callables (which take
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
../hooks/
|
|
@ -33,7 +33,8 @@ from lib.swift_utils import (
|
|||
all_responses_equal,
|
||||
ensure_www_dir_permissions,
|
||||
is_paused,
|
||||
pause_aware_restart_on_change
|
||||
pause_aware_restart_on_change,
|
||||
assess_status,
|
||||
)
|
||||
|
||||
import charmhelpers.contrib.openstack.utils as openstack
|
||||
|
@ -56,6 +57,7 @@ from charmhelpers.core.hookenv import (
|
|||
ERROR,
|
||||
Hooks, UnregisteredHookError,
|
||||
open_port,
|
||||
status_set,
|
||||
)
|
||||
from charmhelpers.core.host import (
|
||||
service_reload,
|
||||
|
@ -96,11 +98,13 @@ CONFIGS = register_configs()
|
|||
|
||||
@hooks.hook('install.real')
|
||||
def install():
|
||||
status_set('maintenance', 'Executing pre-install')
|
||||
execd_preinstall()
|
||||
src = config('openstack-origin')
|
||||
if src != 'distro':
|
||||
openstack.configure_installation_source(src)
|
||||
|
||||
status_set('maintenance', 'Installing apt packages')
|
||||
apt_update(fatal=True)
|
||||
rel = openstack.get_os_codename_install_source(src)
|
||||
pkgs = determine_packages(rel)
|
||||
|
@ -125,6 +129,7 @@ def config_changed():
|
|||
config('min-hours'))
|
||||
|
||||
if config('prefer-ipv6'):
|
||||
status_set('maintenance', 'Configuring ipv6')
|
||||
setup_ipv6()
|
||||
|
||||
configure_https()
|
||||
|
@ -135,7 +140,9 @@ def config_changed():
|
|||
if not config('action-managed-upgrade') and \
|
||||
openstack.openstack_upgrade_available('python-swift'):
|
||||
do_openstack_upgrade(CONFIGS)
|
||||
status_set('maintenance', 'Running openstack upgrade')
|
||||
|
||||
status_set('maintenance', 'Updating and balancing rings')
|
||||
update_rings(min_part_hours=config('min-hours'))
|
||||
|
||||
if not config('disable-ring-balance') and is_elected_leader(SWIFT_HA_RES):
|
||||
|
@ -519,6 +526,7 @@ def main():
|
|||
hooks.execute(sys.argv)
|
||||
except UnregisteredHookError as e:
|
||||
log('Unknown hook {} - skipping.'.format(e), level=DEBUG)
|
||||
assess_status(CONFIGS)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -26,7 +26,8 @@ from charmhelpers.contrib.openstack.utils import (
|
|||
os_release,
|
||||
get_os_codename_package,
|
||||
get_os_codename_install_source,
|
||||
configure_installation_source
|
||||
configure_installation_source,
|
||||
set_os_workload_status,
|
||||
)
|
||||
from charmhelpers.contrib.hahelpers.cluster import (
|
||||
is_elected_leader,
|
||||
|
@ -43,7 +44,8 @@ from charmhelpers.core.hookenv import (
|
|||
relation_set,
|
||||
relation_ids,
|
||||
related_units,
|
||||
status_get
|
||||
status_get,
|
||||
status_set,
|
||||
)
|
||||
from charmhelpers.fetch import (
|
||||
apt_update,
|
||||
|
@ -62,6 +64,11 @@ from charmhelpers.contrib.network.ip import (
|
|||
from charmhelpers.core.decorators import (
|
||||
retry_on_exception,
|
||||
)
|
||||
from charmhelpers.core.unitdata import (
|
||||
HookData,
|
||||
kv,
|
||||
)
|
||||
|
||||
|
||||
# Various config files that are managed via templating.
|
||||
SWIFT_CONF_DIR = '/etc/swift'
|
||||
|
@ -547,17 +554,7 @@ def should_balance(rings):
|
|||
if config('disable-ring-balance'):
|
||||
return False
|
||||
|
||||
for ring in rings:
|
||||
builder = _load_builder(ring).to_dict()
|
||||
replicas = builder['replicas']
|
||||
zones = [dev['zone'] for dev in builder['devs']]
|
||||
num_zones = len(set(zones))
|
||||
if num_zones < replicas:
|
||||
log("Not enough zones (%d) defined to allow ring balance "
|
||||
"(need >= %d)" % (num_zones, replicas), level=INFO)
|
||||
return False
|
||||
|
||||
return True
|
||||
return has_minimum_zones(rings)
|
||||
|
||||
|
||||
def do_openstack_upgrade(configs):
|
||||
|
@ -1001,8 +998,11 @@ def get_hostaddr():
|
|||
|
||||
def is_paused(status_get=status_get):
|
||||
"""Is the unit paused?"""
|
||||
status, message = status_get()
|
||||
return status == "maintenance" and message.startswith("Paused")
|
||||
with HookData()():
|
||||
if kv().get('unit-paused'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def pause_aware_restart_on_change(restart_map):
|
||||
|
@ -1013,3 +1013,53 @@ def pause_aware_restart_on_change(restart_map):
|
|||
else:
|
||||
return restart_on_change(restart_map)(f)
|
||||
return wrapper
|
||||
|
||||
|
||||
def has_minimum_zones(rings):
|
||||
"""Determine if enough zones exist to satisfy minimum replicas"""
|
||||
for ring in rings:
|
||||
builder = _load_builder(ring).to_dict()
|
||||
replicas = builder['replicas']
|
||||
zones = [dev['zone'] for dev in builder['devs']]
|
||||
num_zones = len(set(zones))
|
||||
if num_zones < replicas:
|
||||
log("Not enough zones (%d) defined to satisfy minimum replicas "
|
||||
"(need >= %d)" % (num_zones, replicas), level=INFO)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def assess_status(configs):
|
||||
"""Assess status of current unit"""
|
||||
required_interfaces = {}
|
||||
|
||||
if is_paused():
|
||||
status_set("maintenance",
|
||||
"Paused. Use 'resume' action to resume normal service.")
|
||||
return
|
||||
|
||||
# Check for required swift-storage relation
|
||||
if len(relation_ids('swift-storage')) < 1:
|
||||
status_set('blocked', 'Missing relation: storage')
|
||||
return
|
||||
|
||||
# Verify allowed_hosts is populated with enough unit IP addresses
|
||||
ctxt = SwiftRingContext()()
|
||||
if len(ctxt['allowed_hosts']) < config('replicas'):
|
||||
status_set('blocked', 'Not enough related storage nodes')
|
||||
return
|
||||
|
||||
# Verify there are enough storage zones to satisfy minimum replicas
|
||||
rings = [r for r in SWIFT_RINGS.itervalues()]
|
||||
if not has_minimum_zones(rings):
|
||||
status_set('blocked', 'Not enough storage zones for minimum replicas')
|
||||
return
|
||||
|
||||
if relation_ids('identity-service'):
|
||||
required_interfaces['identity'] = ['identity-service']
|
||||
|
||||
if required_interfaces:
|
||||
set_os_workload_status(configs, required_interfaces)
|
||||
else:
|
||||
status_set('active', 'Unit is ready')
|
||||
|
|
|
@ -635,7 +635,7 @@ class SwiftProxyBasicDeployment(OpenStackAmuletDeployment):
|
|||
msg = ("Resume action failed to move unit to active "
|
||||
"status (got {} instead)".format(status))
|
||||
amulet.raise_status(amulet.FAIL, msg=msg)
|
||||
if message != "":
|
||||
if message != "Unit is ready":
|
||||
msg = ("Resume action failed to clear message"
|
||||
" (got {} instead)".format(message))
|
||||
amulet.raise_status(amulet.FAIL, msg=msg)
|
||||
|
|
|
@ -5,7 +5,9 @@ import unittest
|
|||
import mock
|
||||
import yaml
|
||||
|
||||
import actions.actions
|
||||
from mock import patch
|
||||
with patch('lib.swift_utils.is_paused') as is_paused:
|
||||
import actions.actions
|
||||
|
||||
|
||||
class CharmTestCase(unittest.TestCase):
|
||||
|
@ -31,7 +33,8 @@ class PauseTestCase(CharmTestCase):
|
|||
|
||||
def setUp(self):
|
||||
super(PauseTestCase, self).setUp(
|
||||
actions.actions, ["service_pause", "status_set"])
|
||||
actions.actions, ["service_pause", "HookData", "kv",
|
||||
"assess_status"])
|
||||
|
||||
class FakeArgs(object):
|
||||
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
|
||||
|
@ -68,32 +71,20 @@ class PauseTestCase(CharmTestCase):
|
|||
actions.actions.pause, self.args)
|
||||
self.assertEqual(pause_calls, ["swift-proxy"])
|
||||
|
||||
def test_status_mode(self):
|
||||
"""Pause action sets the status to maintenance."""
|
||||
status_calls = []
|
||||
self.status_set.side_effect = lambda state, msg: status_calls.append(
|
||||
state)
|
||||
def test_pause_sets_value(self):
|
||||
"""Pause action sets the unit-paused value to True."""
|
||||
self.HookData()().return_value = True
|
||||
|
||||
actions.actions.pause(self.args)
|
||||
self.assertEqual(status_calls, ["maintenance"])
|
||||
|
||||
def test_status_message(self):
|
||||
"""Pause action sets a status message reflecting that it's paused."""
|
||||
status_calls = []
|
||||
self.status_set.side_effect = lambda state, msg: status_calls.append(
|
||||
msg)
|
||||
|
||||
actions.actions.pause(self.args)
|
||||
self.assertEqual(
|
||||
status_calls, ["Paused. "
|
||||
"Use 'resume' action to resume normal service."])
|
||||
self.kv().set.assert_called_with('unit-paused', True)
|
||||
|
||||
|
||||
class ResumeTestCase(CharmTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(ResumeTestCase, self).setUp(
|
||||
actions.actions, ["service_resume", "status_set"])
|
||||
actions.actions, ["service_resume", "HookData", "kv",
|
||||
"assess_status"])
|
||||
|
||||
class FakeArgs(object):
|
||||
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
|
||||
|
@ -129,23 +120,12 @@ class ResumeTestCase(CharmTestCase):
|
|||
actions.actions.resume, self.args)
|
||||
self.assertEqual(resume_calls, ['swift-proxy'])
|
||||
|
||||
def test_status_mode(self):
|
||||
"""Resume action sets the status to maintenance."""
|
||||
status_calls = []
|
||||
self.status_set.side_effect = lambda state, msg: status_calls.append(
|
||||
state)
|
||||
def test_resume_sets_value(self):
|
||||
"""Resume action sets the unit-paused value to False."""
|
||||
self.HookData()().return_value = True
|
||||
|
||||
actions.actions.resume(self.args)
|
||||
self.assertEqual(status_calls, ["active"])
|
||||
|
||||
def test_status_message(self):
|
||||
"""Resume action sets an empty status message."""
|
||||
status_calls = []
|
||||
self.status_set.side_effect = lambda state, msg: status_calls.append(
|
||||
msg)
|
||||
|
||||
actions.actions.resume(self.args)
|
||||
self.assertEqual(status_calls, [""])
|
||||
self.kv().set.assert_called_with('unit-paused', False)
|
||||
|
||||
|
||||
class GetActionParserTestCase(unittest.TestCase):
|
||||
|
|
|
@ -5,8 +5,9 @@ import unittest
|
|||
os.environ['JUJU_UNIT_NAME'] = 'swift-proxy'
|
||||
|
||||
with patch('actions.charmhelpers.core.hookenv.config') as config:
|
||||
config.return_value = 'swift'
|
||||
import actions.openstack_upgrade as openstack_upgrade
|
||||
with patch('lib.swift_utils.is_paused') as is_paused:
|
||||
config.return_value = 'swift'
|
||||
import actions.openstack_upgrade as openstack_upgrade
|
||||
|
||||
|
||||
TO_PATCH = [
|
||||
|
|
|
@ -5,7 +5,8 @@ import uuid
|
|||
|
||||
sys.path.append("hooks")
|
||||
with patch('charmhelpers.core.hookenv.log'):
|
||||
import swift_hooks
|
||||
with patch('lib.swift_utils.is_paused') as is_paused:
|
||||
import swift_hooks
|
||||
|
||||
|
||||
class SwiftHooksTestCase(unittest.TestCase):
|
||||
|
|
|
@ -5,8 +5,10 @@ import tempfile
|
|||
import uuid
|
||||
import unittest
|
||||
|
||||
from mock import patch
|
||||
with mock.patch('charmhelpers.core.hookenv.config'):
|
||||
import lib.swift_utils as swift_utils
|
||||
with patch('lib.swift_utils.is_paused') as is_paused:
|
||||
import lib.swift_utils as swift_utils
|
||||
|
||||
|
||||
def init_ring_paths(tmpdir):
|
||||
|
@ -223,14 +225,39 @@ class SwiftUtilsTestCase(unittest.TestCase):
|
|||
rsps = []
|
||||
self.assertIsNone(swift_utils.get_first_available_value(rsps, 'key3'))
|
||||
|
||||
def test_is_paused_unknown(self):
|
||||
fake_status_get = lambda: ("unknown", "")
|
||||
self.assertFalse(swift_utils.is_paused(status_get=fake_status_get))
|
||||
@mock.patch('lib.swift_utils.is_paused')
|
||||
@mock.patch('lib.swift_utils.config')
|
||||
@mock.patch('lib.swift_utils.set_os_workload_status')
|
||||
@mock.patch('lib.swift_utils.SwiftRingContext.__call__')
|
||||
@mock.patch('lib.swift_utils.status_set')
|
||||
@mock.patch('lib.swift_utils.has_minimum_zones')
|
||||
@mock.patch('lib.swift_utils.relation_ids')
|
||||
def test_assess_status(self, relation_ids, has_min_zones, status_set,
|
||||
ctxt, workload_status, config, is_paused):
|
||||
config.return_value = 3
|
||||
|
||||
def test_is_paused_paused(self):
|
||||
fake_status_get = lambda: ("maintenance", "Paused")
|
||||
self.assertTrue(swift_utils.is_paused(status_get=fake_status_get))
|
||||
is_paused.return_value = True
|
||||
swift_utils.assess_status(None)
|
||||
status_set.assert_called_with('maintenance',
|
||||
"Paused. Use 'resume' action to resume "
|
||||
"normal service.")
|
||||
|
||||
def test_is_paused_other_maintenance(self):
|
||||
fake_status_get = lambda: ("maintenance", "Hook")
|
||||
self.assertFalse(swift_utils.is_paused(status_get=fake_status_get))
|
||||
is_paused.return_value = False
|
||||
relation_ids.return_value = []
|
||||
swift_utils.assess_status(None)
|
||||
status_set.assert_called_with('blocked', 'Missing relation: storage')
|
||||
|
||||
relation_ids.return_value = ['swift-storage:1']
|
||||
|
||||
ctxt.return_value = {'allowed_hosts': ['1.2.3.4']}
|
||||
swift_utils.assess_status(None)
|
||||
status_set.assert_called_with('blocked',
|
||||
'Not enough related storage nodes')
|
||||
|
||||
ctxt.return_value = {'allowed_hosts': ['1.2.3.4', '2.3.4.5',
|
||||
'3.4.5.6']}
|
||||
has_min_zones.return_value = False
|
||||
swift_utils.assess_status(None)
|
||||
status_set.assert_called_with('blocked',
|
||||
'Not enough storage zones for minimum '
|
||||
'replicas')
|
||||
|
|
Loading…
Reference in New Issue