Theo Chatzimichos 7cceb8
#!/usr/bin/python3
Theo Chatzimichos 7cceb8
Theo Chatzimichos 18791d
# For description and usage, see the argparse options at the end of the file
Theo Chatzimichos 18791d
Theo Chatzimichos 18791d
import argparse
Theo Chatzimichos 7cceb8
import yaml
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
def get_valid_custom_grains():
Theo Chatzimichos 7cceb8
    with open('pillar/valid_custom_grains.yaml', 'r') as f:
Theo Chatzimichos 7cceb8
        VALID_CUSTOM_GRAINS = yaml.load(f)
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
    return VALID_CUSTOM_GRAINS
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
def get_valid_global_grains():
Theo Chatzimichos 7cceb8
    return get_valid_custom_grains()['global']
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
def get_all_valid_localized_grains():
Theo Chatzimichos 7cceb8
    return get_valid_custom_grains()['localized']
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
Theo Chatzimichos 18791d
def get_virt_cluster_only_physical():
Theo Chatzimichos 18791d
    virt_cluster_only_physical = ''
Theo Chatzimichos 18791d
    try:
Theo Chatzimichos 18791d
        virt_cluster_only_physical = get_valid_custom_grains()['virt_cluster_only_physical']
Theo Chatzimichos 18791d
    except KeyError:
Theo Chatzimichos 18791d
        pass
Theo Chatzimichos 18791d
    print('\n'.join(virt_cluster_only_physical))
Theo Chatzimichos 18791d
Theo Chatzimichos 18791d
Theo Chatzimichos 12b8f9
def get_all_valid_domains(country):
Theo Chatzimichos 12b8f9
    all_valid_domains = get_all_valid_localized_grains()[country]['domains']
Theo Chatzimichos 12b8f9
    if type(all_valid_domains) == str:
Theo Chatzimichos 12b8f9
        # convert to list
Theo Chatzimichos 12b8f9
        all_valid_domains = [all_valid_domains]
Theo Chatzimichos 12b8f9
    print('\n'.join(all_valid_domains))
Theo Chatzimichos 12b8f9
Theo Chatzimichos 12b8f9
Theo Chatzimichos 12b8f9
def get_default_domain(country):
Theo Chatzimichos 12b8f9
    print(get_all_valid_localized_grains()[country]['default_domain'])
Theo Chatzimichos 12b8f9
Theo Chatzimichos 12b8f9
Theo Chatzimichos 12b8f9
def get_default_virt_cluster():
Theo Chatzimichos 12b8f9
    results = []
Theo Chatzimichos 12b8f9
    all_valid_localized_grains = get_all_valid_localized_grains()
Theo Chatzimichos 12b8f9
    for country, items in all_valid_localized_grains.items():
Theo Chatzimichos 12b8f9
        results.append('%s,%s,%s' % (country, items['city'], items['default_virt_cluster']))
Theo Chatzimichos 12b8f9
    print('\n'.join(results))
Theo Chatzimichos 12b8f9
Theo Chatzimichos 12b8f9
Theo Chatzimichos 7cceb8
def print_valid_localized_grains():
Theo Chatzimichos 7cceb8
    results = []
Theo Chatzimichos 7cceb8
    all_valid_localized_grains = get_all_valid_localized_grains()
Theo Chatzimichos 7cceb8
    for country, items in all_valid_localized_grains.items():
Theo Chatzimichos afdd48
        if type(items['virt_cluster']) == str:
Theo Chatzimichos 707d79
            # convert to list
Theo Chatzimichos 707d79
            items['virt_cluster'] = [ items['virt_cluster'] ]
Theo Chatzimichos 707d79
        if type(items['virt_cluster']) == list:
Theo Chatzimichos afdd48
            for virt_cluster in items['virt_cluster']:
Theo Chatzimichos afdd48
                results.append('%s,%s,%s' % (country, items['city'], virt_cluster))
Theo Chatzimichos 707d79
        else:
Theo Chatzimichos 707d79
            raise Exception('virt_cluster "%s" is not a string or a list' % items['virt_cluster'])
Theo Chatzimichos 7cceb8
    print('\n'.join(results))
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
Theo Chatzimichos 7cceb8
if __name__ == "__main__":
Theo Chatzimichos 18791d
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description='Loads the pillar/valid_custom_grains.py and returns a list of valid custom grains in the form of "country,city,virt_cluster".')
Theo Chatzimichos 18791d
    parser.add_argument('-p', action='store_true', help='Returns a list of physical machines that do not host any salt-managed VMs.')
Theo Chatzimichos 12b8f9
    parser.add_argument('-d', nargs=1, help='Returns a list of the valid domains of a location.')
Theo Chatzimichos 12b8f9
    parser.add_argument('--default-domain', nargs=1, help='Returns the default domain of a location.')
Theo Chatzimichos 12b8f9
    parser.add_argument('-v', action='store_true', help='Returns the valid custom grains, but only displaying once each country with their default virt_cluster.')
Theo Chatzimichos 18791d
    args = parser.parse_args()
Theo Chatzimichos 18791d
Theo Chatzimichos 18791d
    if args.p:
Theo Chatzimichos 18791d
        get_virt_cluster_only_physical()
Theo Chatzimichos 12b8f9
    elif args.d:
Theo Chatzimichos 12b8f9
        get_all_valid_domains(args.d[0])
Theo Chatzimichos 12b8f9
    elif args.default_domain:
Theo Chatzimichos 12b8f9
        get_default_domain(args.default_domain[0])
Theo Chatzimichos 12b8f9
    elif args.v:
Theo Chatzimichos 12b8f9
        get_default_virt_cluster()
Theo Chatzimichos 18791d
    else:
Theo Chatzimichos 18791d
        print_valid_localized_grains()