Skip to content

Commit

Permalink
add loader for playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jctanner committed Oct 14, 2024
1 parent 569f3c0 commit 748b365
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions galaxy_importer/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ContentCategory(enum.Enum):


class ContentType(enum.Enum):
PLAYBOOK = "playbook"
ROLE = "role"
MODULE = "module"
MODULE_UTILS = "module_utils"
Expand All @@ -110,6 +111,7 @@ class ContentType(enum.Enum):
@property
def category(self):
return {
ContentType.PLAYBOOK: ContentCategory.PLAYBOOK,
ContentType.ROLE: ContentCategory.ROLE,
ContentType.MODULE: ContentCategory.MODULE,
ContentType.MODULE_UTILS: ContentCategory.PLUGIN,
Expand Down
16 changes: 15 additions & 1 deletion galaxy_importer/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def find_contents(self, path, logger=None):

def _find_content(self):
for content_type, directory, func in self._content_type_dirs():
print(f'ctype:{content_type} dir:{directory} func:{func}')
content_path = os.path.join(self.path, directory)
if not os.path.exists(content_path):
continue
Expand All @@ -73,6 +74,17 @@ def _find_plugins(self, content_type, content_dir):
rel_path = os.path.relpath(file_path, self.path)
yield Result(content_type, rel_path)

def _find_playbooks(self, content_type, content_dir):
for root, dirs, filenames in os.walk(content_dir):
if root != content_dir:
continue
for filename in filenames:
_, extension = os.path.splitext(filename)
if extension and extension.lower() in ['.yml', '.yaml']:
file_path = os.path.join(root, filename)
rel_path = os.path.relpath(file_path, self.path)
yield Result(content_type, rel_path)

def _find_roles(self, content_type, content_dir):
"""Find all dirs inside roles dir where contents match a role."""

Expand All @@ -97,7 +109,9 @@ def recurse_role_dir(path):

def _content_type_dirs(self):
for content_type in constants.ContentType:
if content_type == constants.ContentType.ROLE:
if content_type == constants.ContentType.PLAYBOOK:
yield content_type, "playbooks", self._find_playbooks
elif content_type == constants.ContentType.ROLE:
yield content_type, "roles", self._find_roles
elif content_type == constants.ContentType.MODULE:
yield content_type, "plugins/modules", self._find_plugins
Expand Down
2 changes: 1 addition & 1 deletion galaxy_importer/loaders/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def load(self):
).load()

self.content_objs = list(self._load_contents())

self.contents = self._build_contents_blob()

self.docs_blob = self._build_docs_blob()
self.requires_ansible = file_parser.RuntimeFileParser(self.path).get_requires_ansible()
self._check_ee_yml_dep_files()
Expand Down
27 changes: 26 additions & 1 deletion galaxy_importer/loaders/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ def _make_path_name(rel_path, name):
return name


class PlaybookLoader(ContentLoader):

def load(self):
self._log_loading()

return schema.Content(
name=self.path_name,
content_type=self.content_type,
)

@staticmethod
def _make_name(rel_path):
return os.path.basename(rel_path)

@staticmethod
def _make_path_name(rel_path, name):
dirname_parts = Path(os.path.dirname(rel_path)).parts[1:]
return ".".join(list(dirname_parts) + [name])

def _validate_name(self):
return True


class RoleLoader(ContentLoader):
def load(self):
self._log_loading()
Expand Down Expand Up @@ -233,7 +256,9 @@ def _find_metadata_file_path(root, rel_path):


def get_loader_cls(content_type):
if content_type.category == constants.ContentCategory.ROLE:
if content_type.category == constants.ContentCategory.PLAYBOOK:
return PlaybookLoader
elif content_type.category == constants.ContentCategory.ROLE:
return RoleLoader
elif content_type.category in [
constants.ContentCategory.PLUGIN,
Expand Down

0 comments on commit 748b365

Please sign in to comment.