Skip to content

Commit

Permalink
feat: transformed playbook.py into script instead of tdp-lib command
Browse files Browse the repository at this point in the history
  • Loading branch information
SteBaum committed Oct 30, 2023
1 parent abd9704 commit 494e8b9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
2 changes: 2 additions & 0 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2022 TOSIT.IO
# SPDX-License-Identifier: Apache-2.0
81 changes: 56 additions & 25 deletions tdp/cli/commands/playbooks.py → scripts/playbooks.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
# Copyright 2022 TOSIT.IO
# SPDX-License-Identifier: Apache-2.0

import argparse
import os
from pathlib import Path
from typing import Optional

import click
import networkx as nx

from tdp.cli.utils import collections
from tdp.core.collection import Collection
from tdp.core.collections import Collections
from tdp.core.dag import DEFAULT_SERVICE_PRIORITY, SERVICE_PRIORITY, Dag
from tdp.core.operation import Operation

TDP_COLLECTION_PATH = os.environ.get("TDP_COLLECTION_PATH")

# TODO: Transform this to a script as it is not really a command (see #346).
@click.command(
short_help="Generate meta playbooks in order to use a TDP like collection without tdp-lib"
)
@click.argument("services", required=False, nargs=-1)
@click.option(
"--output-dir",
type=Path,
help="Output dir where playbooks will be written",
required=False,
default=".",
)
@click.option(
"--for-collection",
type=str,
help="Only write operation from this collection",
required=False,
multiple=True,
)
@collections
def playbooks(services, output_dir, for_collection, collections):

def playbooks(
collections: Collections,
*,
services: Optional[list[str]] = None,
output_dir: str = "meta",
for_collection: Optional[list[str]] = None,
):
dag = Dag(collections)
# services DAG
dag_services = nx.DiGraph()
Expand All @@ -50,12 +41,11 @@ def playbooks(services, output_dir, for_collection, collections):
if not nx.is_directed_acyclic_graph(dag_services):
raise RuntimeError("dag_services is not a DAG")

def custom_key(node):
def custom_key(node: str):
operation_priority = SERVICE_PRIORITY.get(node, DEFAULT_SERVICE_PRIORITY)
return f"{operation_priority:02d}_{node}"

dag_services_order = nx.lexicographical_topological_sort(dag_services, custom_key)

if services:
services = set(services)

Expand Down Expand Up @@ -121,3 +111,44 @@ def write_copyright_licence_headers(fd):
)
else:
all_fd.write(f"# {operation.name}\n")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--collection",
action="append",
help="Folder path of the Ansible collection",
)
parser.add_argument(
"services",
metavar="service",
nargs="*",
help="Name of the services example hive hbase",
)
parser.add_argument(
"--output-dir", default=".", help="Output dir where playbooks will be written"
)
parser.add_argument(
"--for_collection",
action="append",
default=[],
help="Only write operation from this collection, specify the collection name",
)
args = parser.parse_args()
services = args.services
output_dir = args.output_dir
for_collection = args.for_collection
collections = Collections.from_collection_list(
[
Collection.from_path(col)
for col in args.collection or TDP_COLLECTION_PATH.split(":")
]
)

playbooks(
collections,
services=services,
output_dir=output_dir,
for_collection=for_collection,
)
2 changes: 0 additions & 2 deletions tdp/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from tdp.cli.commands.init import init
from tdp.cli.commands.operations import operations
from tdp.cli.commands.plan import plan
from tdp.cli.commands.playbooks import playbooks
from tdp.cli.commands.status import status
from tdp.cli.commands.validate import validate
from tdp.cli.commands.vars import vars
Expand Down Expand Up @@ -64,7 +63,6 @@ def main():
cli.add_command(init)
cli.add_command(operations)
cli.add_command(plan)
cli.add_command(playbooks)
cli.add_command(status)
cli.add_command(validate)
cli.add_command(vars)
Expand Down
15 changes: 0 additions & 15 deletions tdp/cli/commands/test_playbooks.py

This file was deleted.

0 comments on commit 494e8b9

Please sign in to comment.