diff --git a/bin/get_valid_custom_grains.py b/bin/get_valid_custom_grains.py index e71995a..4766df5 100755 --- a/bin/get_valid_custom_grains.py +++ b/bin/get_valid_custom_grains.py @@ -30,6 +30,26 @@ def get_virt_cluster_only_physical(): print('\n'.join(virt_cluster_only_physical)) +def get_all_valid_domains(country): + all_valid_domains = get_all_valid_localized_grains()[country]['domains'] + if type(all_valid_domains) == str: + # convert to list + all_valid_domains = [all_valid_domains] + print('\n'.join(all_valid_domains)) + + +def get_default_domain(country): + print(get_all_valid_localized_grains()[country]['default_domain']) + + +def get_default_virt_cluster(): + results = [] + all_valid_localized_grains = get_all_valid_localized_grains() + for country, items in all_valid_localized_grains.items(): + results.append('%s,%s,%s' % (country, items['city'], items['default_virt_cluster'])) + print('\n'.join(results)) + + def print_valid_localized_grains(): results = [] all_valid_localized_grains = get_all_valid_localized_grains() @@ -48,9 +68,18 @@ def print_valid_localized_grains(): if __name__ == "__main__": 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".') parser.add_argument('-p', action='store_true', help='Returns a list of physical machines that do not host any salt-managed VMs.') + parser.add_argument('-d', nargs=1, help='Returns a list of the valid domains of a location.') + parser.add_argument('--default-domain', nargs=1, help='Returns the default domain of a location.') + parser.add_argument('-v', action='store_true', help='Returns the valid custom grains, but only displaying once each country with their default virt_cluster.') args = parser.parse_args() if args.p: get_virt_cluster_only_physical() + elif args.d: + get_all_valid_domains(args.d[0]) + elif args.default_domain: + get_default_domain(args.default_domain[0]) + elif args.v: + get_default_virt_cluster() else: print_valid_localized_grains() diff --git a/bin/test_custom_grains.py b/bin/test_custom_grains.py index ecf0270..62e48f8 100755 --- a/bin/test_custom_grains.py +++ b/bin/test_custom_grains.py @@ -53,6 +53,8 @@ for sls in all_ids: try: valid_localized_grains = all_valid_localized_grains[mygrains['country']] + for ignored_key in ['domains', 'default_domain', 'default_virt_cluster']: + valid_localized_grains.pop(ignored_key, None) for key, valid_values in valid_localized_grains.items(): status = test_custom_grain(mygrains, sls, key, valid_values, status) except KeyError: diff --git a/bin/test_show_highstate.sh b/bin/test_show_highstate.sh index 1551dc2..1291a74 100755 --- a/bin/test_show_highstate.sh +++ b/bin/test_show_highstate.sh @@ -17,31 +17,54 @@ RUN_TEST="salt-call --local --retcode-passthrough state.show_highstate" ALL_VIRTUAL=( kvm ) -ALL_LOCATIONS=( $(bin/get_valid_custom_grains.py) ) PHYSICAL_ONLY_VIRT_CLUSTER=( $(bin/get_valid_custom_grains.py -p) ) write_grains() { - $SUDO sed -i -e "s/\(city:\).*/\1 $2/" -e "s/\(country:\).*/\1 $1/" -e "s/\(virt_cluster:\).*/\1 $3/" -e "s/\(virtual:\).*/\1 $4/" /etc/salt/grains - echo_INFO "Grains: city: $2, country: $1, virt_cluster: $3, virtual: $4" + $SUDO sed -i -e "s/\(city:\).*/\1 $2/" -e "s/\(country:\).*/\1 $1/" -e "s/\(domain:\).*/\1 $5/" -e "s/\(virt_cluster:\).*/\1 $3/" -e "s/\(virtual:\).*/\1 $4/" /etc/salt/grains + echo_INFO "Grains: city: $2, country: $1, domain: $5, virt_cluster: $3, virtual: $4" +} + +show_highstate() { + write_grains $country $city $virt_cluster $virtual $domain + $RUN_TEST > /dev/null + _STATUS=$(echo $?) + # We ignore exit code 2 as it means that an empty file is produced + # See https://github.com/saltstack/salt/issues/39172 + if [[ $_STATUS -eq 0 ]] || [[ $_STATUS -eq 2 ]]; then + echo_PASSED + else + echo_FAILED + STATUS=1 + fi + echo } +ALL_LOCATIONS=( $(bin/get_valid_custom_grains.py) ) for location in ${ALL_LOCATIONS[@]}; do + LOCATION=(${location//,/ }) + country=${LOCATION[0]} + city=${LOCATION[1]} + virt_cluster=${LOCATION[2]} + domain=$(bin/get_valid_custom_grains.py --default-domain $country) for virtual in ${ALL_VIRTUAL[@]}; do - if [[ $virtual == 'kvm' ]] && [[ ${PHYSICAL_ONLY_VIRT_CLUSTER[@]} =~ ${location##*,} ]]; then + if [[ $virtual == 'kvm' ]] && [[ ${PHYSICAL_ONLY_VIRT_CLUSTER[@]} =~ $virt_cluster ]]; then continue fi - write_grains ${location//,/ } ${virtual} - $RUN_TEST > /dev/null - _STATUS=$(echo $?) - # We ignore exit code 2 as it means that an empty file is produced - # See https://github.com/saltstack/salt/issues/39172 - if [[ $_STATUS -eq 0 ]] || [[ $_STATUS -eq 2 ]]; then - echo_PASSED - else - echo_FAILED - STATUS=1 - fi - echo + show_highstate + done +done + +ALL_LOCATIONS=( $(bin/get_valid_custom_grains.py -v) ) +for location in ${ALL_LOCATIONS[@]}; do + LOCATION=(${location//,/ }) + country=${LOCATION[0]} + city=${LOCATION[1]} + virt_cluster=${LOCATION[2]} + default_domain=$(bin/get_valid_custom_grains.py --default-domain $country) + virtual='kvm' + DOMAINS=( $(bin/get_valid_custom_grains.py -d $country) ) + for domain in ${DOMAINS[@]}; do + [[ $domain == $default_domain ]] || show_highstate done done diff --git a/pillar/valid_custom_grains.yaml b/pillar/valid_custom_grains.yaml index 1494c7b..9633f03 100644 --- a/pillar/valid_custom_grains.yaml +++ b/pillar/valid_custom_grains.yaml @@ -5,7 +5,13 @@ global: localized: de: city: nuremberg + default_domain: infra.opensuse.org + default_virt_cluster: atreju + domains: infra.opensuse.org virt_cluster: atreju us: city: provo + default_domain: infra.opensuse.org + default_virt_cluster: bryce + domains: infra.opensuse.org virt_cluster: bryce