-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathCommandInterpreter.java
172 lines (155 loc) · 6.96 KB
/
CommandInterpreter.java
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
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.tasks;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.Util;
import hudson.EnvVars;
import hudson.Functions;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Node;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.remoting.ChannelClosedException;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
/**
* Common part between {@link Shell} and {@link BatchFile}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class CommandInterpreter extends Builder {
/**
* Command to execute. The format depends on the actual {@link CommandInterpreter} implementation.
*/
protected final String command;
public CommandInterpreter(String command) {
this.command = command;
}
public final String getCommand() {
return command;
}
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
return perform(build,launcher,(TaskListener)listener);
}
/**
* Determines whether a non-zero exit code from the process should change the build
* status to {@link Result#UNSTABLE} instead of default {@link Result#FAILURE}.
*
* Changing to {@link Result#UNSTABLE} does not abort the build, next steps are continued.
*
* @since 2.26
*/
protected boolean isErrorlevelForUnstableBuild(int exitCode) {
return false;
}
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, TaskListener listener) throws InterruptedException {
FilePath ws = build.getWorkspace();
if (ws == null) {
Node node = build.getBuiltOn();
if (node == null) {
throw new NullPointerException("no such build node: " + build.getBuiltOnStr());
}
throw new NullPointerException("no workspace from node " + node + " which is computer " + node.toComputer() + " and has channel " + node.getChannel());
}
FilePath script=null;
int r = -1;
try {
try {
script = createScriptFile(ws);
} catch (IOException e) {
Util.displayIOException(e,listener);
Functions.printStackTrace(e, listener.fatalError(Messages.CommandInterpreter_UnableToProduceScript()));
return false;
}
try {
EnvVars envVars = build.getEnvironment(listener);
// on Windows environment variables are converted to all upper case,
// but no such conversions are done on Unix, so to make this cross-platform,
// convert variables to all upper cases.
for(Map.Entry<String,String> e : build.getBuildVariables().entrySet())
envVars.put(e.getKey(),e.getValue());
r = join(launcher.launch().cmds(buildCommandLine(script)).envs(envVars).stdout(listener).pwd(ws).start());
if(isErrorlevelForUnstableBuild(r)) {
build.setResult(Result.UNSTABLE);
r = 0;
}
} catch (IOException e) {
Util.displayIOException(e, listener);
Functions.printStackTrace(e, listener.fatalError(Messages.CommandInterpreter_CommandFailed()));
}
return r==0;
} finally {
try {
if(script!=null)
script.delete();
} catch (IOException e) {
if (r==-1 && e.getCause() instanceof ChannelClosedException) {
// JENKINS-5073
// r==-1 only when the execution of the command resulted in IOException,
// and we've already reported that error. A common error there is channel
// losing a connection, and in that case we don't want to confuse users
// by reporting the 2nd problem. Technically the 1st exception may not be
// a channel closed error, but that's rare enough, and JENKINS-5073 is common enough
// that this suppressing of the error would be justified
LOGGER.log(Level.FINE, "Script deletion failed", e);
} else {
Util.displayIOException(e,listener);
Functions.printStackTrace(e, listener.fatalError(Messages.CommandInterpreter_UnableToDelete(script)));
}
} catch (Exception e) {
Functions.printStackTrace(e, listener.fatalError(Messages.CommandInterpreter_UnableToDelete(script)));
}
}
}
/**
* Reports the exit code from the process.
*
* This allows subtypes to treat the exit code differently (for example by treating non-zero exit code
* as if it's zero, or to set the status to {@link Result#UNSTABLE}). Any non-zero exit code will cause
* the build step to fail. Use {@link #isErrorlevelForUnstableBuild(int exitCode)} to redefine the default
* behaviour.
*
* @since 1.549
*/
protected int join(Proc p) throws IOException, InterruptedException {
return p.join();
}
/**
* Creates a script file in a temporary name in the specified directory.
*/
public FilePath createScriptFile(@Nonnull FilePath dir) throws IOException, InterruptedException {
return dir.createTextTempFile("jenkins", getFileExtension(), getContents(), false);
}
public abstract String[] buildCommandLine(FilePath script);
protected abstract String getContents();
protected abstract String getFileExtension();
private static final Logger LOGGER = Logger.getLogger(CommandInterpreter.class.getName());
}