1
1
# Copyright 2022 TOSIT.IO
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
+ import argparse
5
+ import os
4
6
from pathlib import Path
7
+ from typing import Optional
5
8
6
- import click
7
9
import networkx as nx
8
10
9
- from tdp .cli .utils import collections
11
+ from tdp .core .collection import Collection
12
+ from tdp .core .collections import Collections
10
13
from tdp .core .dag import DEFAULT_SERVICE_PRIORITY , SERVICE_PRIORITY , Dag
11
14
from tdp .core .operation import Operation
12
15
16
+ TDP_COLLECTION_PATH = os .environ .get ("TDP_COLLECTION_PATH" )
13
17
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
+ ):
35
26
dag = Dag (collections )
36
27
# services DAG
37
28
dag_services = nx .DiGraph ()
@@ -50,12 +41,11 @@ def playbooks(services, output_dir, for_collection, collections):
50
41
if not nx .is_directed_acyclic_graph (dag_services ):
51
42
raise RuntimeError ("dag_services is not a DAG" )
52
43
53
- def custom_key (node ):
44
+ def custom_key (node : str ):
54
45
operation_priority = SERVICE_PRIORITY .get (node , DEFAULT_SERVICE_PRIORITY )
55
46
return f"{ operation_priority :02d} _{ node } "
56
47
57
48
dag_services_order = nx .lexicographical_topological_sort (dag_services , custom_key )
58
-
59
49
if services :
60
50
services = set (services )
61
51
@@ -121,3 +111,44 @@ def write_copyright_licence_headers(fd):
121
111
)
122
112
else :
123
113
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
+ )
0 commit comments