Distribute python package with script wrapper
[it/dev.git] / xapp_onboarder / xapp_onboarder / server / cli.py
1 ################################################################################
2 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
3 #                                                                              #
4 #   Licensed under the Apache License, Version 2.0 (the "License");            #
5 #   you may not use this file except in compliance with the License.           #
6 #   You may obtain a copy of the License at                                    #
7 #                                                                              #
8 #       http://www.apache.org/licenses/LICENSE-2.0                             #
9 #                                                                              #
10 #   Unless required by applicable law or agreed to in writing, software        #
11 #   distributed under the License is distributed on an "AS IS" BASIS,          #
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13 #   See the License for the specific language governing permissions and        #
14 #   limitations under the License.                                             #
15 ################################################################################
16
17
18 import fire
19 import json
20 import os
21 import pkg_resources
22 import logging.config
23
24 from xapp_onboarder.api.charts import get_charts_list, download_chart_package, download_values_yaml
25 from xapp_onboarder.repo_manager.repo_manager import repo_manager
26 from xapp_onboarder.api.onboard import onboard, download_config_and_schema_and_onboard
27
28 log = logging.getLogger(__name__)
29
30
31 class cli():
32     """This is the cli tool for xapp_onboarder."""
33
34     def get_charts_list(self, xapp_chart_name=''):
35         """Get the list of all onboarded xApps. To show all version of an xApp, use --xapp_chart_name to
36         specify the name."""
37         message, status = get_charts_list(xapp_chart_name=xapp_chart_name)
38
39         return json.dumps(message, indent=4, sort_keys=True)
40
41     def download_helm_chart(self, xapp_chart_name, version, output_path="."):
42         """Download the helm chart package of an xApp. Specify xApp name with --xapp_chart_name, version with
43         --version. Optionally use --output_path to specify the path to save the file."""
44         content, status = download_chart_package(xapp_chart_name=xapp_chart_name, version=version)
45
46         if status != 200:
47             return json.dumps(content, indent=4, sort_keys=True)
48
49         try:
50             if os.path.isabs(output_path):
51                 path = output_path + '/' + xapp_chart_name + '-' + version + '.tgz'
52             else:
53                 path = os.getcwd() + '/' + output_path + '/' + xapp_chart_name + '-' + version + '.tgz'
54
55             if not os.path.exists(os.path.dirname(path)):
56                 os.makedirs(os.path.dirname(path))
57
58             with open(path, 'wb') as outputfile:
59                 outputfile.write(content)
60         except Exception as err:
61             return err
62         return {"status": "OK"}
63
64     def download_values_yaml(self, xapp_chart_name, version, output_path="."):
65         """Download the values.yaml file that can be used to override parameters at runtime. Specify xApp name with
66         --xapp_chart_name, version with --version. Optionally use --output_path to specify the path to save the file.
67         """
68         content, status = download_values_yaml(xapp_chart_name=xapp_chart_name, version=version)
69
70         if status != 200:
71             return json.dumps(content, indent=4, sort_keys=True)
72
73         try:
74             if os.path.isabs(output_path):
75                 path = output_path + '/values.yaml'
76             else:
77                 path = os.getcwd() + '/' + output_path + '/values.yaml'
78
79             if not os.path.exists(os.path.dirname(path)):
80                 os.makedirs(os.path.dirname(path))
81
82             with open(path, 'wb') as outputfile:
83                 outputfile.write(content)
84         except Exception as err:
85             return err
86         return {"status": "OK"}
87
88     def health(self):
89         """Health check. If xapp onboarder is not ready, it return false."""
90         return repo_manager.is_repo_ready()
91
92     def onboard(self, config_file_path, shcema_file_path):
93         """Onboard an xApp with local descriptor files. Use --config_file_path to specify the path to
94         config-file.json file, --shcema_file_path to specify the path to schema.json file. """
95         try:
96             with open(config_file_path, 'r') as inputfile:
97                 config_file = json.load(inputfile)
98
99             with open(shcema_file_path, 'r') as inputfile:
100                 schema_file = json.load(inputfile)
101
102         except Exception as err:
103             return err
104
105         message, status = onboard(config_file, schema_file)
106         return json.dumps(message, indent=4, sort_keys=True)
107
108     def download_and_onboard(self, config_file_url, schema_file_url):
109         """Onboard an xApp with URLs pointing to the xApp descriptor files. Use --config_file_url to specify the
110         config-file.json URL, --schema_file_url to specify the schema.json URL. """
111         message, status = download_config_and_schema_and_onboard(config_file_url, schema_file_url)
112         return json.dumps(message, indent=4, sort_keys=True)
113
114 def run():
115     logger_config = pkg_resources.resource_filename("xapp_onboarder", 'logging.conf')
116     logging.config.fileConfig(logger_config)
117     fire.Fire(cli(), name='xapp_onboarder')
118
119 if __name__ == "__main__":
120
121     run()