~hopem/charms/trusty/swift-proxy/lp1448884

« back to all changes in this revision

Viewing changes to lib/swift_utils.py

  • Committer: David Ames
  • Date: 2015-10-13 17:17:22 UTC
  • mfrom: (117.2.20 swift-proxy)
  • Revision ID: david.ames@canonical.com-20151013171722-zb5j0lwi3y45jgl7
[corey.bryant, r=thedac] Workload Status

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    os_release,
27
27
    get_os_codename_package,
28
28
    get_os_codename_install_source,
29
 
    configure_installation_source
 
29
    configure_installation_source,
 
30
    set_os_workload_status,
30
31
)
31
32
from charmhelpers.contrib.hahelpers.cluster import (
32
33
    is_elected_leader,
43
44
    relation_set,
44
45
    relation_ids,
45
46
    related_units,
46
 
    status_get
 
47
    status_get,
 
48
    status_set,
47
49
)
48
50
from charmhelpers.fetch import (
49
51
    apt_update,
62
64
from charmhelpers.core.decorators import (
63
65
    retry_on_exception,
64
66
)
 
67
from charmhelpers.core.unitdata import (
 
68
    HookData,
 
69
    kv,
 
70
)
 
71
 
65
72
 
66
73
# Various config files that are managed via templating.
67
74
SWIFT_CONF_DIR = '/etc/swift'
547
554
    if config('disable-ring-balance'):
548
555
        return False
549
556
 
550
 
    for ring in rings:
551
 
        builder = _load_builder(ring).to_dict()
552
 
        replicas = builder['replicas']
553
 
        zones = [dev['zone'] for dev in builder['devs']]
554
 
        num_zones = len(set(zones))
555
 
        if num_zones < replicas:
556
 
            log("Not enough zones (%d) defined to allow ring balance "
557
 
                "(need >= %d)" % (num_zones, replicas), level=INFO)
558
 
            return False
559
 
 
560
 
    return True
 
557
    return has_minimum_zones(rings)
561
558
 
562
559
 
563
560
def do_openstack_upgrade(configs):
1001
998
 
1002
999
def is_paused(status_get=status_get):
1003
1000
    """Is the unit paused?"""
1004
 
    status, message = status_get()
1005
 
    return status == "maintenance" and message.startswith("Paused")
 
1001
    with HookData()():
 
1002
        if kv().get('unit-paused'):
 
1003
            return True
 
1004
        else:
 
1005
            return False
1006
1006
 
1007
1007
 
1008
1008
def pause_aware_restart_on_change(restart_map):
1013
1013
        else:
1014
1014
            return restart_on_change(restart_map)(f)
1015
1015
    return wrapper
 
1016
 
 
1017
 
 
1018
def has_minimum_zones(rings):
 
1019
    """Determine if enough zones exist to satisfy minimum replicas"""
 
1020
    for ring in rings:
 
1021
        builder = _load_builder(ring).to_dict()
 
1022
        replicas = builder['replicas']
 
1023
        zones = [dev['zone'] for dev in builder['devs']]
 
1024
        num_zones = len(set(zones))
 
1025
        if num_zones < replicas:
 
1026
            log("Not enough zones (%d) defined to satisfy minimum replicas "
 
1027
                "(need >= %d)" % (num_zones, replicas), level=INFO)
 
1028
            return False
 
1029
 
 
1030
    return True
 
1031
 
 
1032
 
 
1033
def assess_status(configs):
 
1034
    """Assess status of current unit"""
 
1035
    required_interfaces = {}
 
1036
 
 
1037
    if is_paused():
 
1038
        status_set("maintenance",
 
1039
                   "Paused. Use 'resume' action to resume normal service.")
 
1040
        return
 
1041
 
 
1042
    # Check for required swift-storage relation
 
1043
    if len(relation_ids('swift-storage')) < 1:
 
1044
        status_set('blocked', 'Missing relation: storage')
 
1045
        return
 
1046
 
 
1047
    # Verify allowed_hosts is populated with enough unit IP addresses
 
1048
    ctxt = SwiftRingContext()()
 
1049
    if len(ctxt['allowed_hosts']) < config('replicas'):
 
1050
        status_set('blocked', 'Not enough related storage nodes')
 
1051
        return
 
1052
 
 
1053
    # Verify there are enough storage zones to satisfy minimum replicas
 
1054
    rings = [r for r in SWIFT_RINGS.itervalues()]
 
1055
    if not has_minimum_zones(rings):
 
1056
        status_set('blocked', 'Not enough storage zones for minimum replicas')
 
1057
        return
 
1058
 
 
1059
    if relation_ids('identity-service'):
 
1060
        required_interfaces['identity'] = ['identity-service']
 
1061
 
 
1062
    if required_interfaces:
 
1063
        set_os_workload_status(configs, required_interfaces)
 
1064
    else:
 
1065
        status_set('active', 'Unit is ready')