Helm-generator - seedcode delivery for helm chart generation tool from component...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / chartbuilder / HelmClientImpl.java
1 /*
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
8  * #
9  * #      http://www.apache.org/licenses/LICENSE-2.0
10  * #
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=========================================================
17  */
18
19 package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.springframework.stereotype.Component;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29
30 /**
31  * HelmClient implementation which uses helm command installed in the runtime environment.
32  * @author Dhrumin Desai
33  */
34 @Component
35 @Slf4j
36 public class HelmClientImpl implements HelmClient {
37
38     /**
39      * performs <code>helm lint</code> operation
40      * @param chartLocation helm chart location
41      */
42     @Override
43     public void lint(File chartLocation) {
44         ProcessBuilder builder = new ProcessBuilder();
45         builder.command("helm", "lint", chartLocation.getAbsolutePath());
46         runProcess(builder, "lint");
47     }
48
49     /**
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
54      */
55     @Override
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");
61     }
62
63     private File runProcess(ProcessBuilder builder, String command) {
64         Process process = null;
65         String chartPath = "";
66         try {
67             process = builder.start();
68             if(command.equals("lint")) {
69                 printLintingProcessOutput(process);
70             }
71             else {
72                 chartPath = printPackagingProcessOutput(process);
73             }
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");
82         }
83         return new File(chartPath);
84     }
85
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);
90         inputStream.close();
91     }
92
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));
97         String line;
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];
101             }
102             log.info(line);
103         }
104         inputStream.close();
105         if(helmChartPath.isEmpty()){
106             throw new RuntimeException("Could not generate the chart.");
107         }
108         return helmChartPath;
109     }
110
111     private void assertExitCode(Process process) throws InterruptedException {
112         int exitCode = 0;
113         exitCode = process.waitFor();
114         process.destroy();
115         if (exitCode != 0){
116             throw new RuntimeException("Error occurred while running helm command.");
117         }
118     }
119 }