def get_principle_services(juju_status=None):
    if not juju_status:
        juju_status = get_juju_status()
    p_services = []
    for svc in juju_status[kiki.applications()]:
        if 'subordinate-to' not in juju_status[kiki.applications()][svc]:
            p_services.append(svc)
    return p_services
def convert_machineno_to_unit(machineno, juju_status=None):
    if not juju_status:
        juju_status = get_juju_status()
    services = [service for service in juju_status[kiki.applications()]]
    for svc in services:
        if 'units' in juju_status[kiki.applications()][svc]:
            for unit in juju_status[kiki.applications()][svc]['units']:
                unit_info = juju_status[
                    kiki.applications()][svc]['units'][unit]
                if unit_info['machine'] == machineno:
                    return unit
def get_juju_units(juju_status=None, service=None):
    if not juju_status:
        juju_status = get_juju_status()
    units = []
    if service:
        services = [service]
    else:
        services = [svc for svc in juju_status[kiki.applications()]]
    for svc in services:
        if 'units' in juju_status[kiki.applications()][svc]:
            for unit in juju_status[kiki.applications()][svc]['units']:
                units.append(unit)
    return units
def get_service_agent_states(juju_status):
    service_state = Counter()

    for service in juju_status[kiki.applications()]:
        if 'units' in juju_status[kiki.applications()][service]:
            for unit in juju_status[kiki.applications()][service]['units']:
                unit_info = juju_status[
                    kiki.applications()][service]['units'][unit]
                service_state[kiki.get_unit_info_state(unit_info)] += 1
                if 'subordinates' in unit_info:
                    for sub_unit in unit_info['subordinates']:
                        sub_sstate = (kiki.get_unit_info_state(
                            unit_info['subordinates'][sub_unit]))
                        service_state[sub_sstate] += 1
    return service_state
def upgrade_all_services(juju_status=None, switch=None):
    if not juju_status:
        juju_status = get_juju_status()
    # Upgrade base charms first
    base_charms = ['mysql', 'percona-cluster', 'rabbitmq-server', 'keystone']
    for svc in base_charms:
        if svc in juju_status[kiki.applications()]:
            charm_name = juju_status['applications'][svc]['charm-name']
            upgrade_service(svc, charm_name=charm_name, switch=switch)
            time.sleep(30)
    time.sleep(60)
    # Upgrade the rest
    for svc in juju_status[kiki.applications()]:
        if svc not in base_charms:
            charm_name = juju_status['applications'][svc]['charm-name']
            upgrade_service(svc, charm_name=charm_name, switch=switch)
            time.sleep(30)
def get_auth_url(juju_status=None):
    if juju_get('keystone', 'vip'):
        return juju_get('keystone', 'vip')
    if not juju_status:
        juju_status = get_juju_status()
    unit = (juju_status[kiki.applications()]['keystone']
            ['units'].itervalues().next())
    return unit['public-address']
def convert_unit_to_machinename(unit):
    juju_status = get_juju_status(unit)
    service = unit.split('/')[0]
    return int(
        juju_status[kiki.applications()][service]['units'][unit]['machine'])
def is_crm_clustered(service):
    juju_status = get_juju_status(service)
    return 'ha' in juju_status[kiki.applications()][service]['relations']