-
Notifications
You must be signed in to change notification settings - Fork 25
/
tca_cli.py
135 lines (105 loc) · 4.52 KB
/
tca_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import sys
# sys.path.append('/app')
import argparse
import json
import time
from service.standardization import Standardization
from service.assessment import Assessment
from service.planning import Plan
from service.infer_tech import InferTech
from service.clustering import Clustering
# input argument parser
def parser():
parser = argparse.ArgumentParser(description="TCA command line: standardize applications entities and recommend container images")
parser.add_argument("-input_json", dest="input_json", type=str, help="input json", required=False)
parser.add_argument("-input_string", dest="input_string", type=str, help="input string", required=False)
parser.add_argument("-operation", dest="operation", type=str, default="standardize", help="operation to perform: standardize (default) | containerize | clustering | all | standardize+containerize | standardize+clustering | containerize+clustering ")
parser.add_argument("-output_json", dest="output_json", type=str, help="output json", required=False)
parser.add_argument("-catalog", dest="catalog", type=str, help="catalog", default="ibmcloud", required=False)
return parser.parse_args()
# main
def main():
# parse inputs
args = parser()
input_json = args.input_json
operation = args.operation
output_json = args.output_json
#input_string = "[{\"application_name\": \"app1\", \"application_description\": \"my application\", \"technology_summary\": \"rhel,db2,java\"}]"
input_string = args.input_string
if input_string != None:
input_string = "[{" + input_string + "}]";
if input_json == None and input_string == None:
print("Provide -input_json or -input_string")
exit()
# load input
if (input_json != None):
with open(input_json) as fin:
app_data = json.load(fin)
#print(app_data)
elif (input_string != None):
app_data = json.loads(input_string)
# print(type(app_data))
oplist = []
if operation == 'all':
oplist.append('standardize')
oplist.append('containerize')
oplist.append('clustering')
elif operation == 'standardize+containerize':
oplist.append('standardize')
oplist.append('containerize')
elif operation == 'standardize+clustering':
oplist.append('standardize')
oplist.append('clustering')
elif operation == 'containerize+clustering':
oplist.append('containerize')
oplist.append('clustering')
else:
oplist.append(operation)
result_list = {}
# start_time = time.time()
for op in oplist:
result_data = ''
# entity standardization
print("Invoking TCA for op: ", op)
if op == 'standardize':
standardizer = Standardization()
result_data = standardizer.app_standardizer(app_data)
assessment = Assessment()
result_data = assessment.app_validation(result_data)
result_data = assessment.output_to_ui_assessment(result_data)
elif op == 'containerize':
plan = Plan(catalog="ibmcloud")
inferTech = InferTech()
result_data = plan.ui_to_input_assessment(app_data)
result_data = inferTech.infer_missing_tech(result_data)
#print(json.dumps(result_data, indent=4))
result_data = plan.validate_app(result_data)
result_data = plan.map_to_docker(result_data, 'ibmcloud')
result_data = plan.output_to_ui_planning(result_data)
elif op == 'clustering':
cluster = Clustering()
result_data = cluster.output_to_ui_clustering(app_data)
else:
print('invalid operation')
result_list[op] = result_data
if 'standardize' in result_list:
app_data = result_list['standardize']
# save json output
result_data_final = ''
if len(oplist) > 1:
result_data_final = result_list
else:
result_data_final = result_list[operation]
if result_data_final != '':
if output_json != None:
with open(output_json, "w") as fot:
json.dump(result_data_final, fot, indent=4)
else:
if operation == "standardize+containerize":
result_data_final_str = "^^^" + json.dumps(result_data_final) + "$$$"
print(result_data_final_str)
# end_time = time.time()
# tot_time = end_time - start_time
# print("Total time: ", tot_time)
if __name__ == "__main__":
main()