X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=xapp_onboarder%2Fxapp_onboarder%2Fhelm_controller%2FxApp_builder.py;h=da9f34aa255765302d2e979dee87a8a0893b33b2;hb=a9b4e2aca7fdaae20d576805e136a17342f4c625;hp=9506a46c390d672627d114c6b2238fa65962e333;hpb=3b56acc14a2308e853612e22738cd44687288149;p=it%2Fdev.git diff --git a/xapp_onboarder/xapp_onboarder/helm_controller/xApp_builder.py b/xapp_onboarder/xapp_onboarder/helm_controller/xApp_builder.py index 9506a46..da9f34a 100644 --- a/xapp_onboarder/xapp_onboarder/helm_controller/xApp_builder.py +++ b/xapp_onboarder/xapp_onboarder/helm_controller/xApp_builder.py @@ -67,10 +67,19 @@ class xApp(): shutil.rmtree(self.chart_workspace_path) os.makedirs(self.chart_workspace_path) shutil.copytree(resource_filename( 'xapp_onboarder', 'resources/xapp-std'), self.chart_workspace_path + '/' + self.chart_name) - self.helm_client_path = settings.CHART_WORKSPACE_PATH + '/helm' self.setup_helm() def setup_helm(self): + self.helm_client_path = 'helm' + try: + process = subprocess.run([self.helm_client_path], stdout=PIPE, stderr=PIPE, check=True) + + except Exception as err: + print(err) + self.download_helm() + self.helm_client_path = settings.CHART_WORKSPACE_PATH + '/helm' + + def download_helm(self): if not os.path.isfile(settings.CHART_WORKSPACE_PATH + '/helm'): log.info("Helm client missing. Trying to download it.") helm_file_name = "helm-v{}-{}-amd64.tar.gz".format(settings.HELM_VERSION, platform.system().lower()) @@ -113,14 +122,13 @@ class xApp(): for item in current_node.keys(): if type(current_node.get(item)) is not dict: - current_node[item] = '{{ '+ helm_value_path +'.'+ item + ' | toJson }}' + current_node[item] = '{{ index '+ helm_value_path +' "'+ item + '" | toJson }}' else: new_node_list = node_list.copy() new_node_list.append(item) self.recursive_convert_config_file(new_node_list) - def append_config_to_config_map(self): with open(self.chart_workspace_path + '/' + self.chart_name + '/templates/appconfig.yaml', 'a') as outputfile: self.recursive_convert_config_file() @@ -128,6 +136,7 @@ class xApp(): indented_config_text = indent(config_file_json_text, 4) indented_config_text = re.sub(r"\"{{", '{{', indented_config_text) indented_config_text = re.sub(r"}}\"", '}}', indented_config_text) + indented_config_text = re.sub(r"\\", '', indented_config_text) outputfile.write(" config-file.json: |\n") outputfile.write(indented_config_text) outputfile.write("\n schema.json: |\n") @@ -136,13 +145,27 @@ class xApp(): outputfile.write(indented_schema_text) +# This is a work around for the bronze release to be backward compatible to the previous xapp standard helm template + def write_config_and_schema(self): + os.makedirs(self.chart_workspace_path + '/' + self.chart_name + '/descriptors') + os.makedirs(self.chart_workspace_path + '/' + self.chart_name + '/config') + with open(self.chart_workspace_path + '/' + self.chart_name + '/descriptors/schema.json', 'w') as outfile: + json.dump(self.schema_file, outfile) + with open(self.chart_workspace_path + '/' + self.chart_name + '/config/config-file.json', 'w') as outfile: + json.dump(self.config_file, outfile) + + + def add_probes_to_deployment(self): with open(self.chart_workspace_path + '/' + self.chart_name + '/templates/deployment.yaml', 'a') as outputfile: for probes in ['readinessProbe', 'livenessProbe']: if self.configmap_config_json_file.get(probes): probe_definition = self.configmap_config_json_file.get(probes) - probe_definition_yaml = yaml.dump(probe_definition) + probe_definition_yaml = yaml.dump(probe_definition, width=1000) + + print(probe_definition_yaml) + indented_probe_definition_yaml = indent(probe_definition_yaml, 12) indented_probe_definition_yaml = re.sub(r" \| toJson", '', indented_probe_definition_yaml) indented_probe_definition_yaml = re.sub(r"'", '', indented_probe_definition_yaml) @@ -150,46 +173,11 @@ class xApp(): outputfile.write(indented_probe_definition_yaml) - def append_config_to_values_yaml(self): with open(self.chart_workspace_path + '/' + self.chart_name + '/values.yaml', 'a') as outputfile: yaml.dump(self.config_file, outputfile, default_flow_style=False) - def append_env_to_config_map(self): - with open(self.chart_workspace_path + '/' + self.chart_name + '/templates/appenv.yaml', 'a') as outputfile: - append = {} - if settings.DBAAS_MASTER_NAME: - master_name = settings.DBAAS_MASTER_NAME - service_host = settings.DBAAS_SERVICE_HOST - sentinel_port = settings.DBAAS_SERVICE_SENTINEL_PORT - if not service_host: - raise xAppError( - "Internal failure. Cannot find environment variable 'DBAAS_SERVICE_HOST'. (Caused by: Misconfiguration of temp deployment)", 500) - if not sentinel_port: - raise xAppError( - "Internal failure. Cannot find environment variable 'DBAAS_SERVICE_SENTINEL_PORT'. (Caused by: Misconfiguration of temp deployment)", 500) - - append['DBAAS_MASTER_NAME'] = master_name - append['DBAAS_SERVICE_HOST'] = service_host - append['DBAAS_SERVICE_SENTINEL_PORT'] = sentinel_port - elif settings.DBAAS_SERVICE_HOST: - service_host = settings.DBAAS_SERVICE_HOST - service_port = settings.DBAAS_SERVICE_PORT - if not service_port: - raise xAppError( - "Internal failure. Cannot find environment variable 'DBAAS_SERVICE_PORT'. (Caused by: Misconfiguration of temp deployment)", 500) - append['DBAAS_SERVICE_HOST'] = service_host - append['DBAAS_SERVICE_PORT'] = service_port - else: - raise xAppError( - "Internal failure. Cannot find environment variable 'DBAAS_SERVICE_HOST' or 'DBAAS_MASTER_NAME'. (Caused by: Misconfiguration of temp deployment)", - 500) - output_yaml = yaml.dump(append) - indented_output_yaml = indent(output_yaml, 2) - outputfile.write(indented_output_yaml) - - def change_chart_name_version(self): with open(self.chart_workspace_path + '/' + self.chart_name + '/Chart.yaml', 'r') as inputfile: self.chart_yaml = yaml.load(inputfile, Loader=yaml.FullLoader) @@ -214,14 +202,19 @@ class xApp(): err.stderr.decode("utf-8") + "\n" + err.stdout.decode("utf-8") + ")", 400) def package_chart(self): + self.write_config_and_schema() self.append_config_to_config_map() self.append_config_to_values_yaml() - self.append_env_to_config_map() self.add_probes_to_deployment() self.change_chart_name_version() self.helm_lint() try: - process = subprocess.run([self.helm_client_path, "package", self.chart_workspace_path + "/" + self.chart_name, "-d" + + if settings.HELM_VERSION.startswith("3") == True: + process = subprocess.run([self.helm_client_path, "package", self.chart_workspace_path + "/" + self.chart_name, "-d" + ,self.chart_workspace_path], stdout=PIPE, stderr=PIPE, check=True) + else: + process = subprocess.run([self.helm_client_path, "package", self.chart_workspace_path + "/" + self.chart_name, "-d" ,self.chart_workspace_path,"--save=false"], stdout=PIPE, stderr=PIPE, check=True) except OSError as err: