From e5327039e79f1a9a05ba3dba02227cf5b37142d8 Mon Sep 17 00:00:00 2001
From: Theo Chatzimichos <tampakrap@opensuse.org>
Date: Nov 10 2017 17:52:41 +0000
Subject: split the test_roles script into two scripts:


- bin/get_roles.py: collects all roles and returns them in a python list
  (so that it can be imported from another python script), in a yaml
  array or to a plain list (parsable by bash)
- bin/test_roles.py: imports the get_roles script and tests the
  unassigned roles as before

---

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b78cfb2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+bin/__pycache__
diff --git a/bin/get_roles.py b/bin/get_roles.py
new file mode 100755
index 0000000..3534fc7
--- /dev/null
+++ b/bin/get_roles.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python3
+
+# For description and usage, see the argparse options at the end of the file
+
+import argparse
+import yaml
+import os
+
+
+def get_roles(with_base=False):
+    roles = []
+    if with_base:
+        roles.append('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:
+                roles.append(item)
+
+    roles = sorted(set(roles))
+    return roles
+
+
+def print_roles():
+    parser = argparse.ArgumentParser('Collects all the roles that are assigned to a minion, and returns them as a python array, a yaml list or a plain list (parsable by bash)')
+    parser.add_argument('-p', '--python', action='store_true', help='Prints the roles as a python array')
+    parser.add_argument('-y', '--yaml', action='store_true', help='Prints the roles as a yaml array')
+    parser.add_argument('--with-base', action='store_true', help='Include the base role at the results')
+    args = parser.parse_args()
+
+    roles = get_roles(with_base=args.with_base)
+    if args.python:
+        print(roles)
+    elif args.yaml:
+        print('roles:')
+        for role in roles:
+            print('  - %s' % role)
+    else:
+        print(' '.join(roles))
+
+
+if __name__ == "__main__":
+    print_roles()
diff --git a/bin/test_roles.py b/bin/test_roles.py
index aef27f5..d899178 100755
--- a/bin/test_roles.py
+++ b/bin/test_roles.py
@@ -1,32 +1,21 @@
 #!/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
+# Checks if the declared {salt,pillar}/role/*.sls files actually match an
+# assigned role to a minion
 
-import yaml
 import os
 import sys
+from get_roles import get_roles
 
-all_roles = ['base']
 status = 0
 
-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))
+roles = get_roles(with_base=True)
 
 for directory in ['salt', 'pillar']:
     for sls in os.listdir('%s/role' % directory):
         if sls.endswith('.sls'):
             with open('%s/role/%s' % (directory, sls)) as f:
-                if sls.split('.sls')[0] not in all_roles:
+                if sls.split('.sls')[0] not in roles:
                     print ('%s/role/%s not in roles' % (directory, sls))
                     status = 1