Skip to content

Commit 494e8b9

Browse files
author
SteBaum
committed
feat: transformed playbook.py into script instead of tdp-lib command
1 parent abd9704 commit 494e8b9

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

scripts/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright 2022 TOSIT.IO
2+
# SPDX-License-Identifier: Apache-2.0

tdp/cli/commands/playbooks.py scripts/playbooks.py

+56-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
11
# Copyright 2022 TOSIT.IO
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import argparse
5+
import os
46
from pathlib import Path
7+
from typing import Optional
58

6-
import click
79
import networkx as nx
810

9-
from tdp.cli.utils import collections
11+
from tdp.core.collection import Collection
12+
from tdp.core.collections import Collections
1013
from tdp.core.dag import DEFAULT_SERVICE_PRIORITY, SERVICE_PRIORITY, Dag
1114
from tdp.core.operation import Operation
1215

16+
TDP_COLLECTION_PATH = os.environ.get("TDP_COLLECTION_PATH")
1317

14-
# TODO: Transform this to a script as it is not really a command (see #346).
15-
@click.command(
16-
short_help="Generate meta playbooks in order to use a TDP like collection without tdp-lib"
17-
)
18-
@click.argument("services", required=False, nargs=-1)
19-
@click.option(
20-
"--output-dir",
21-
type=Path,
22-
help="Output dir where playbooks will be written",
23-
required=False,
24-
default=".",
25-
)
26-
@click.option(
27-
"--for-collection",
28-
type=str,
29-
help="Only write operation from this collection",
30-
required=False,
31-
multiple=True,
32-
)
33-
@collections
34-
def playbooks(services, output_dir, for_collection, collections):
18+
19+
def playbooks(
20+
collections: Collections,
21+
*,
22+
services: Optional[list[str]] = None,
23+
output_dir: str = "meta",
24+
for_collection: Optional[list[str]] = None,
25+
):
3526
dag = Dag(collections)
3627
# services DAG
3728
dag_services = nx.DiGraph()
@@ -50,12 +41,11 @@ def playbooks(services, output_dir, for_collection, collections):
5041
if not nx.is_directed_acyclic_graph(dag_services):
5142
raise RuntimeError("dag_services is not a DAG")
5243

53-
def custom_key(node):
44+
def custom_key(node: str):
5445
operation_priority = SERVICE_PRIORITY.get(node, DEFAULT_SERVICE_PRIORITY)
5546
return f"{operation_priority:02d}_{node}"
5647

5748
dag_services_order = nx.lexicographical_topological_sort(dag_services, custom_key)
58-
5949
if services:
6050
services = set(services)
6151

@@ -121,3 +111,44 @@ def write_copyright_licence_headers(fd):
121111
)
122112
else:
123113
all_fd.write(f"# {operation.name}\n")
114+
115+
116+
if __name__ == "__main__":
117+
parser = argparse.ArgumentParser()
118+
parser.add_argument(
119+
"--collection",
120+
action="append",
121+
help="Folder path of the Ansible collection",
122+
)
123+
parser.add_argument(
124+
"services",
125+
metavar="service",
126+
nargs="*",
127+
help="Name of the services example hive hbase",
128+
)
129+
parser.add_argument(
130+
"--output-dir", default=".", help="Output dir where playbooks will be written"
131+
)
132+
parser.add_argument(
133+
"--for_collection",
134+
action="append",
135+
default=[],
136+
help="Only write operation from this collection, specify the collection name",
137+
)
138+
args = parser.parse_args()
139+
services = args.services
140+
output_dir = args.output_dir
141+
for_collection = args.for_collection
142+
collections = Collections.from_collection_list(
143+
[
144+
Collection.from_path(col)
145+
for col in args.collection or TDP_COLLECTION_PATH.split(":")
146+
]
147+
)
148+
149+
playbooks(
150+
collections,
151+
services=services,
152+
output_dir=output_dir,
153+
for_collection=for_collection,
154+
)

tdp/cli/__main__.py

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from tdp.cli.commands.init import init
1616
from tdp.cli.commands.operations import operations
1717
from tdp.cli.commands.plan import plan
18-
from tdp.cli.commands.playbooks import playbooks
1918
from tdp.cli.commands.status import status
2019
from tdp.cli.commands.validate import validate
2120
from tdp.cli.commands.vars import vars
@@ -64,7 +63,6 @@ def main():
6463
cli.add_command(init)
6564
cli.add_command(operations)
6665
cli.add_command(plan)
67-
cli.add_command(playbooks)
6866
cli.add_command(status)
6967
cli.add_command(validate)
7068
cli.add_command(vars)

tdp/cli/commands/test_playbooks.py

-15
This file was deleted.

0 commit comments

Comments
 (0)