2 * # ============LICENSE_START=======================================================
3 * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
4 * # ================================================================================
5 * # Licensed under the Apache License, Version 2.0 (the "License");
6 * # you may not use this file except in compliance with the License.
7 * # You may obtain a copy of the License at
9 * # http://www.apache.org/licenses/LICENSE-2.0
11 * # Unless required by applicable law or agreed to in writing, software
12 * # distributed under the License is distributed on an "AS IS" BASIS,
13 * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * # See the License for the specific language governing permissions and
15 * # limitations under the License.
16 * # ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
21 import lombok.extern.slf4j.Slf4j;
22 import org.springframework.stereotype.Component;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
31 * HelmClient implementation which uses helm command installed in the runtime environment.
32 * @author Dhrumin Desai
36 public class HelmClientImpl implements HelmClient {
39 * performs <code>helm lint</code> operation
40 * @param chartLocation helm chart location
43 public void lint(File chartLocation) {
44 ProcessBuilder builder = new ProcessBuilder();
45 builder.command("helm", "lint", chartLocation.getAbsolutePath());
46 runProcess(builder, "lint");
50 * performs <code>helm package</code> operation
51 * @param chartLocation helm chart location
52 * @param outputLocation location to store the generated helm package
53 * @return generated helm tgz file
56 public File packageChart(File chartLocation, String outputLocation) {
57 ProcessBuilder builder = new ProcessBuilder();
58 builder.directory(new File(System.getProperty("user.dir")));
59 builder.command("helm", "package", "-d", outputLocation, chartLocation.getAbsolutePath());
60 return runProcess(builder, "package");
63 private File runProcess(ProcessBuilder builder, String command) {
64 Process process = null;
65 String chartPath = "";
67 process = builder.start();
68 if(command.equals("lint")) {
69 printLintingProcessOutput(process);
72 chartPath = printPackagingProcessOutput(process);
74 assertExitCode(process);
75 } catch (IOException e) {
76 log.error(e.getMessage(), e);
77 throw new RuntimeException("Error occurred while running helm command.");
78 } catch (InterruptedException e) {
79 log.error(e.getMessage(), e);
80 Thread.currentThread().interrupt();
81 throw new RuntimeException("execution interrupted");
83 return new File(chartPath);
86 private void printLintingProcessOutput(Process process) throws IOException {
87 final InputStream inputStream = process.getInputStream();
88 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
89 reader.lines().forEach(log::info);
93 private String printPackagingProcessOutput(Process process) throws IOException {
94 String helmChartPath = "";
95 final InputStream inputStream = process.getInputStream();
96 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
98 while ((line = reader.readLine()) != null){
99 if (line.contains("Successfully packaged chart and saved it to: ")){
100 helmChartPath = line.split("Successfully packaged chart and saved it to: ")[1];
105 if(helmChartPath.isEmpty()){
106 throw new RuntimeException("Could not generate the chart.");
108 return helmChartPath;
111 private void assertExitCode(Process process) throws InterruptedException {
113 exitCode = process.waitFor();
116 throw new RuntimeException("Error occurred while running helm command.");