-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCommand.java
More file actions
199 lines (173 loc) · 8.12 KB
/
Command.java
File metadata and controls
199 lines (173 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package io.github.inseefrlab.helmwrapper.utils;
import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.exec.InvalidExitValueException;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
import org.zeroturnaround.exec.listener.ProcessListener;
/** Executeur */
public class Command {
private static final Pattern safeToConcatenate =
Pattern.compile(
"^[ ]*[a-z0-9]([-a-z0-9 ]*[a-z0-9 ])?(\\.[a-z0-9 ]([-a-z0-9 ]*[a-z0-9 ])?)*[ ]*$");
private static final Logger LOGGER = LoggerFactory.getLogger(Command.class);
private static ProcessExecutor getProcessExecutor(OutputStream errorOutputStream) {
ProcessExecutor processExecutor = new ProcessExecutor();
if (errorOutputStream != null) {
processExecutor.redirectError(errorOutputStream);
}
processExecutor.readOutput(true);
processExecutor.addListener(
new ProcessListener() {
@Override
public void afterStart(Process process, ProcessExecutor executor) {
process.info().commandLine().ifPresent(cli -> LOGGER.info(cli));
super.afterStart(process, executor);
}
});
return processExecutor;
}
public static ProcessResultWithError executeAndGetResponseAsJson(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(
addConfigToCommand(command, helmConfiguration) + " --output json")
.execute();
return new ProcessResultWithError(processResult, errorStream);
}
public static ProcessResultWithError executeAndGetResponseAsJson(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return executeAndGetResponseAsJson(null, command);
}
public static ProcessResultWithError executeAndGetResponseAsRaw(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.execute();
return new ProcessResultWithError(processResult, errorStream);
}
public static ProcessResultWithError executeAndGetResponseAsRaw(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return executeAndGetResponseAsRaw(null, command);
}
public static ProcessResultWithError execute(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.execute();
return new ProcessResultWithError(processResult, errorStream);
}
public static ProcessResultWithError execute(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return execute(null, command);
}
public static class ProcessResultWithError {
private ProcessResult processResult;
private String error = null;
public ProcessResultWithError() {}
public ProcessResultWithError(ProcessResult processResult, ByteArrayOutputStream boas) {
this.processResult = processResult;
if (boas != null) {
error = boas.toString(Charset.defaultCharset());
}
}
public ProcessResult getProcessResult() {
return processResult;
}
public String getError() {
return error;
}
}
private static Map<String, String> getEnv(HelmConfiguration helmConfiguration) {
Map<String, String> env = new HashMap<>();
if (System.getProperty("http.proxyHost") != null) {
env.put(
"HTTP_PROXY",
System.getProperty("http.proxyHost")
+ (System.getProperty("http.proxyPort") != null
? ":" + System.getProperty("http.proxyPort")
: ""));
}
if (System.getProperty("https.proxyHost") != null) {
env.put(
"HTTPS_PROXY",
System.getProperty("https.proxyHost")
+ (System.getProperty("https.proxyPort") != null
? ":" + System.getProperty("https.proxyPort")
: ""));
}
if (System.getProperty("no_proxy") != null) {
env.put("NO_PROXY", System.getProperty("no_proxy"));
}
return env;
}
private static String addConfigToCommand(String command, HelmConfiguration helmConfiguration) {
if (helmConfiguration == null) {
return command;
}
String newCommand = command;
newCommand = newCommand.concat(" ");
if (helmConfiguration.getAsKubeUser() != null) {
newCommand =
newCommand.concat(" --kube-as-user " + helmConfiguration.getAsKubeUser() + " ");
}
String kubeConfig = null;
if (StringUtils.isNotEmpty(helmConfiguration.getApiserverUrl())) {
newCommand =
newCommand
.concat(" --kube-apiserver=" + helmConfiguration.getApiserverUrl())
.concat(" ");
// Kubeconfig should be set to /dev/null to prevent mixing user provided configuration
// with pre-existing local kubeconfig (most likely re-using a cluster certificate from
// another cluster)
kubeConfig = "/dev/null";
}
if (StringUtils.isNotEmpty(helmConfiguration.getKubeToken())) {
newCommand =
newCommand
.concat(" --kube-token=" + helmConfiguration.getKubeToken())
.concat(" ");
kubeConfig = "/dev/null";
}
if (StringUtils.isNotEmpty(helmConfiguration.getKubeConfig())) {
kubeConfig = helmConfiguration.getKubeConfig();
}
if (kubeConfig != null) {
newCommand = newCommand.concat(" --kubeconfig=" + kubeConfig).concat(" ");
}
return newCommand;
}
public static void safeConcat(StringBuilder currentCommand, String toConcat)
throws IllegalArgumentException {
if (!safeToConcatenate.matcher(toConcat).matches()) {
throw new IllegalArgumentException(
"Illegal character found while building helm command, refusing to concatenate "
+ toConcat
+ " to "
+ currentCommand.toString());
}
currentCommand.append(toConcat);
}
}