1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

Merge pull request #6922 from AlanCoding/dir_crawling

Detailed tweaking of inventory and playbook listings
This commit is contained in:
Alan Rominger 2017-07-12 12:10:55 -04:00 committed by GitHub
commit 0c3eac6aa0
2 changed files with 21 additions and 17 deletions

View File

@ -25,7 +25,7 @@ from awx.main.models.notifications import (
from awx.main.models.unified_jobs import * # noqa from awx.main.models.unified_jobs import * # noqa
from awx.main.models.mixins import ResourceMixin from awx.main.models.mixins import ResourceMixin
from awx.main.utils import update_scm_url from awx.main.utils import update_scm_url
from awx.main.utils.ansible import could_be_inventory, could_be_playbook from awx.main.utils.ansible import skip_directory, could_be_inventory, could_be_playbook
from awx.main.fields import ImplicitRoleField from awx.main.fields import ImplicitRoleField
from awx.main.models.rbac import ( from awx.main.models.rbac import (
ROLE_SINGLETON_SYSTEM_ADMINISTRATOR, ROLE_SINGLETON_SYSTEM_ADMINISTRATOR,
@ -180,6 +180,8 @@ class ProjectOptions(models.Model):
project_path = self.get_project_path() project_path = self.get_project_path()
if project_path: if project_path:
for dirpath, dirnames, filenames in os.walk(smart_str(project_path)): for dirpath, dirnames, filenames in os.walk(smart_str(project_path)):
if skip_directory(dirpath):
continue
for filename in filenames: for filename in filenames:
playbook = could_be_playbook(project_path, dirpath, filename) playbook = could_be_playbook(project_path, dirpath, filename)
if playbook is not None: if playbook is not None:
@ -195,7 +197,7 @@ class ProjectOptions(models.Model):
# Cap the number of results, because it could include lots # Cap the number of results, because it could include lots
max_inventory_listing = 50 max_inventory_listing = 50
for dirpath, dirnames, filenames in os.walk(smart_str(project_path)): for dirpath, dirnames, filenames in os.walk(smart_str(project_path)):
if dirpath.startswith('.'): if skip_directory(dirpath):
continue continue
for filename in filenames: for filename in filenames:
inv_path = could_be_inventory(project_path, dirpath, filename) inv_path = could_be_inventory(project_path, dirpath, filename)

View File

@ -10,15 +10,15 @@ from itertools import islice
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
__all__ = ['could_be_playbook', 'could_be_inventory'] __all__ = ['skip_directory', 'could_be_playbook', 'could_be_inventory']
valid_playbook_re = re.compile(r'^\s*?-?\s*?(?:hosts|include):\s*?.*?$') valid_playbook_re = re.compile(r'^\s*?-?\s*?(?:hosts|include):\s*?.*?$')
valid_inventory_re = re.compile(r'^[a-zA-Z0-9_.=\[\]]') valid_inventory_re = re.compile(r'^[a-zA-Z0-9_.=\[\]]')
def _skip_directory(rel_path): def skip_directory(relative_directory_path):
path_elements = rel_path.split(os.sep) path_elements = relative_directory_path.split(os.sep)
# Exclude files in a roles subdirectory. # Exclude files in a roles subdirectory.
if 'roles' in path_elements: if 'roles' in path_elements:
return True return True
@ -29,6 +29,9 @@ def _skip_directory(rel_path):
# Do not include dot files or dirs # Do not include dot files or dirs
if element.startswith('.'): if element.startswith('.'):
return True return True
# Exclude anything inside of group or host vars directories
if 'group_vars' in path_elements or 'host_vars' in path_elements:
return True
return False return False
@ -52,18 +55,20 @@ def could_be_playbook(project_path, dir_path, filename):
return None return None
if not matched: if not matched:
return None return None
playbook = os.path.relpath(playbook_path, smart_str(project_path)) return os.path.relpath(playbook_path, smart_str(project_path))
if _skip_directory(playbook):
return None
return playbook
def could_be_inventory(project_path, dir_path, filename): def could_be_inventory(project_path, dir_path, filename):
suspected_ext = os.path.splitext(filename)[-1] # Decisions based exclusively on filename
# Allow for files with no extension, or with extensions in a certain set
if '.' in suspected_ext and suspected_ext not in ['.yml', '.yaml', '.ini']:
return None
inventory_path = os.path.join(dir_path, filename) inventory_path = os.path.join(dir_path, filename)
suspected_ext = os.path.splitext(filename)[-1]
if suspected_ext in ['.yml', '.yaml', '.ini']:
# Files with any of these extensions are always included
return os.path.relpath(inventory_path, smart_str(project_path))
elif '.' in suspected_ext:
# If not using those extensions, inventory must have _no_ extension
return None
# Filter files that do not use a character set consistent with # Filter files that do not use a character set consistent with
# Ansible inventory mainly # Ansible inventory mainly
try: try:
@ -74,7 +79,4 @@ def could_be_inventory(project_path, dir_path, filename):
return None return None
except IOError: except IOError:
return None return None
inventory = os.path.relpath(inventory_path, smart_str(project_path)) return os.path.relpath(inventory_path, smart_str(project_path))
if _skip_directory(inventory):
return None
return inventory