From aa384d234be61a84c3b982f6ff3fe34f6fd55b65 Mon Sep 17 00:00:00 2001 From: Theo Chatzimichos Date: Nov 08 2017 20:54:03 +0000 Subject: Add script to test if the roles we define are assigned the script collects all the assigned roles of all the minions, and checks if the declared {salt,pillar}/role/*.sls files actually match an assigned role to a minion --- diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9d893aa..543e51b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,15 @@ stages: + - test - deploy +test_roles_job: + stage: test + before_script: + - zypper -qn in --no-recommends python3 python3-PyYAML + script: bin/test_roles.py + tags: + - docker + deploy_job: stage: deploy script: salt-call event.fire_master update salt/fileserver/gitfs/update diff --git a/bin/test_roles.py b/bin/test_roles.py new file mode 100755 index 0000000..f6b8c2d --- /dev/null +++ b/bin/test_roles.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +# Collects all the assigned roles of all the minions, and checks if the +# declared {salt,pillar}/role/*.sls files actually match a minion-assigned role + +import yaml +import os +import sys + +all_roles = ['base'] + +for sls in os.listdir('pillar/id'): + with open("pillar/id/%s" % sls) as f: + try: + _roles = yaml.load(f)['grains']['roles'] + except KeyError: + continue + + for item in _roles: + all_roles.append(item) + +all_roles = sorted(set(all_roles)) + +for directory in ['salt', 'pillar']: + for sls in os.listdir('%s/role' % directory): + if sls.endswith('.sls'): + with open('salt/role/%s' % sls) as f: + if sls.split('.sls')[0] not in all_roles: + print ('%s/role/%s not in roles' % (directory, sls)) + status = 1 + +sys.exit(status)