1
1
import json
2
+ from typing import List
2
3
3
4
from jina .orchestrate .flow .base import Flow
4
5
from jina .orchestrate .deployments import Deployment
7
8
from jina .schemas import get_full_schema
8
9
from jina_cli .export import api_to_dict
9
10
10
-
11
11
def export_kubernetes (args ):
12
12
"""Export to k8s yaml files
13
13
14
14
:param args: args from CLI
15
15
"""
16
16
from jina .jaml import JAMLCompatible
17
17
18
- obj = JAMLCompatible .load_config (args .config_path )
18
+ try :
19
+ obj = JAMLCompatible .load_config (args .config_path )
20
+ except Exception as e :
21
+ default_logger .error (f"Failed to load config: { e } " )
22
+ raise
19
23
20
24
if isinstance (obj , (Flow , Deployment )):
21
- obj .to_kubernetes_yaml (
22
- output_base_path = args .outpath , k8s_namespace = args .k8s_namespace
23
- )
25
+ try :
26
+ obj .to_kubernetes_yaml (
27
+ output_base_path = args .outpath , k8s_namespace = args .k8s_namespace
28
+ )
29
+ default_logger .info (f'Kubernetes YAML exported to { args .outpath } ' )
30
+ except Exception as e :
31
+ default_logger .error (f"Failed to export to Kubernetes YAML: { e } " )
32
+ raise
24
33
else :
25
34
raise NotImplementedError (
26
35
f'Object of class { obj .__class__ .__name__ } cannot be exported to Kubernetes'
27
36
)
28
37
29
-
30
38
def export_docker_compose (args ):
31
39
"""Export to Docker compose yaml files
32
40
33
41
:param args: args from CLI
34
42
"""
35
-
36
43
from jina .jaml import JAMLCompatible
37
44
38
- obj = JAMLCompatible .load_config (args .config_path )
45
+ try :
46
+ obj = JAMLCompatible .load_config (args .config_path )
47
+ except Exception as e :
48
+ default_logger .error (f"Failed to load config: { e } " )
49
+ raise
39
50
40
51
if isinstance (obj , (Flow , Deployment )):
41
- obj .to_docker_compose_yaml (
42
- output_path = args .outpath , network_name = args .network_name
43
- )
52
+ try :
53
+ obj .to_docker_compose_yaml (
54
+ output_path = args .outpath , network_name = args .network_name
55
+ )
56
+ default_logger .info (f'Docker Compose YAML exported to { args .outpath } ' )
57
+ except Exception as e :
58
+ default_logger .error (f"Failed to export to Docker Compose YAML: { e } " )
59
+ raise
44
60
else :
45
61
raise NotImplementedError (
46
62
f'Object of class { obj .__class__ .__name__ } cannot be exported to Docker Compose'
47
63
)
48
64
49
-
50
65
def export_flowchart (args ):
51
66
"""Export to flowchart file
52
67
53
68
:param args: args from CLI
54
69
"""
55
- Flow .load_config (args .config_path ).plot (
56
- args .outpath , vertical_layout = args .vertical_layout
57
- )
58
-
70
+ try :
71
+ flow = Flow .load_config (args .config_path )
72
+ flow .plot (args .outpath , vertical_layout = args .vertical_layout )
73
+ default_logger .info (f'Flowchart exported to { args .outpath } ' )
74
+ except Exception as e :
75
+ default_logger .error (f"Failed to export flowchart: { e } " )
76
+ raise
59
77
60
78
def export_schema (args ):
61
79
"""Export to JSON Schemas
@@ -64,26 +82,28 @@ def export_schema(args):
64
82
"""
65
83
from jina import __version__
66
84
85
+ def dump_data (dump_api : dict , paths : List [str ], extension : str ):
86
+ for path in paths :
87
+ f_name = (path % __version__ ) if '%s' in path else path
88
+ try :
89
+ with open (f_name , 'w' , encoding = 'utf-8' ) as fp :
90
+ if extension == 'yaml' :
91
+ JAML .dump (dump_api , fp )
92
+ elif extension == 'json' :
93
+ json .dump (dump_api , fp , sort_keys = True )
94
+ default_logger .info (f'API is exported to { f_name } ' )
95
+ except Exception as e :
96
+ default_logger .error (f"Failed to export schema to { f_name } : { e } " )
97
+ raise
98
+
67
99
if args .yaml_path :
68
100
dump_api = api_to_dict ()
69
- for yp in args .yaml_path :
70
- f_name = (yp % __version__ ) if '%s' in yp else yp
71
- with open (f_name , 'w' , encoding = 'utf-8' ) as fp :
72
- JAML .dump (dump_api , fp )
73
- default_logger .info (f'API is exported to { f_name } ' )
101
+ dump_data (dump_api , args .yaml_path , 'yaml' )
74
102
75
103
if args .json_path :
76
104
dump_api = api_to_dict ()
77
- for jp in args .json_path :
78
- f_name = (jp % __version__ ) if '%s' in jp else jp
79
- with open (f_name , 'w' , encoding = 'utf-8' ) as fp :
80
- json .dump (dump_api , fp , sort_keys = True )
81
- default_logger .info (f'API is exported to { f_name } ' )
105
+ dump_data (dump_api , args .json_path , 'json' )
82
106
83
107
if args .schema_path :
84
108
dump_api = get_full_schema ()
85
- for jp in args .schema_path :
86
- f_name = (jp % __version__ ) if '%s' in jp else jp
87
- with open (f_name , 'w' , encoding = 'utf-8' ) as fp :
88
- json .dump (dump_api , fp , sort_keys = True )
89
- default_logger .info (f'API is exported to { f_name } ' )
109
+ dump_data (dump_api , args .schema_path , 'json' )
0 commit comments