/
StartTSBuilder.java
263 lines (231 loc) · 8.81 KB
/
StartTSBuilder.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Copyright (c) 2015-2016 TraceTronic GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of TraceTronic GmbH nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package de.tracetronic.jenkins.plugins.ecutest.tool;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.TaskListener;
import hudson.model.Run;
import hudson.util.FormValidation;
import java.io.IOException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import de.tracetronic.jenkins.plugins.ecutest.log.TTConsoleLogger;
import de.tracetronic.jenkins.plugins.ecutest.tool.client.TSClient;
import de.tracetronic.jenkins.plugins.ecutest.tool.installation.AbstractToolInstallation;
import de.tracetronic.jenkins.plugins.ecutest.tool.installation.ETInstallation;
import de.tracetronic.jenkins.plugins.ecutest.util.EnvUtil;
import de.tracetronic.jenkins.plugins.ecutest.util.ProcessUtil;
/**
* Builder providing the start up of the Tool-Server.
*
* @author Christian Pönisch <christian.poenisch@tracetronic.de>
*/
public class StartTSBuilder extends AbstractToolBuilder {
/**
* Defines the default timeout to start up the Tool-Server.
*/
public static final int DEFAULT_TIMEOUT = 120;
/**
* Defines the default port used for TCP communication with the Tool-Server.
*/
public static final int DEFAULT_TCP_PORT = 5017;
@CheckForNull
private String toolLibsIni;
@Nonnull
private String tcpPort = String.valueOf(DEFAULT_TCP_PORT);
/**
* Instantiates a new {@link StartTSBuilder}.
*
* @param toolName
* the tool name identifying the {@link ETInstallation} to be used
*/
@DataBoundConstructor
public StartTSBuilder(@Nonnull final String toolName) {
super(toolName);
}
/**
* Instantiates a new {@link StartTSBuilder}.
*
* @param toolName
* the tool name identifying the {@link ETInstallation} to be used
* @param timeout
* the timeout
* @param toolLibsIni
* the alternative ToolLibs.ini path
* @param tcpPort
* the alternative TCP port
* @deprecated since 1.11 use {@link #StartTSBuilder(String)}
*/
@Deprecated
public StartTSBuilder(final String toolName, final String timeout, final String toolLibsIni,
final String tcpPort) {
super(toolName, StringUtils.defaultIfEmpty(timeout, String.valueOf(DEFAULT_TIMEOUT)));
this.toolLibsIni = StringUtils.trimToEmpty(toolLibsIni);
this.tcpPort = StringUtils.defaultIfEmpty(tcpPort, String.valueOf(DEFAULT_TCP_PORT));
}
/**
* @return the default timeout
*/
@Override
public int getDefaultTimeout() {
return DEFAULT_TIMEOUT;
}
/**
* @return the default TCP port
*/
public int getDefaultTcpPort() {
return DEFAULT_TCP_PORT;
}
/**
* @return the ToolLibs.ini path
*/
@Nonnull
public String getToolLibsIni() {
return StringUtils.trimToEmpty(toolLibsIni);
}
/**
* @return the TCP port
*/
@Nonnull
public String getTcpPort() {
return tcpPort;
}
/**
* @param toolLibsIni
* the ToolLibs.ini path
*/
@DataBoundSetter
public void setToolLibsIni(@CheckForNull final String toolLibsIni) {
this.toolLibsIni = Util.fixNull(toolLibsIni);
}
/**
* @param tcpPort
* the TCP port
*/
@DataBoundSetter
public void setTcpPort(@Nonnull final String tcpPort) {
this.tcpPort = tcpPort;
}
@Override
public void perform(final Run<?, ?> run, final FilePath workspace, final Launcher launcher,
final TaskListener listener) throws InterruptedException, IOException {
// Check OS running this build
if (!ProcessUtil.checkOS(launcher, listener)) {
return;
}
// Initialize logger
final TTConsoleLogger logger = new TTConsoleLogger(listener);
// Expand build parameters
final EnvVars buildEnvVars = run.getEnvironment(listener);
final int expandedTimeout = Integer.parseInt(EnvUtil.expandEnvVar(getTimeout(), buildEnvVars,
String.valueOf(DEFAULT_TIMEOUT)));
final int expandedTcpPort = Integer.parseInt(EnvUtil.expandEnvVar(getTcpPort(), buildEnvVars,
String.valueOf(DEFAULT_TCP_PORT)));
final String expandedToolLibs = buildEnvVars.expand(getToolLibsIni());
final FilePath expandedTLPath = new FilePath(launcher.getChannel(), expandedToolLibs);
// Check ToolLibs.ini path
if (!expandedTLPath.exists()) {
logger.logError(String.format("ToolLibs.ini path at %s does not exist!",
expandedTLPath.getRemote()));
return;
}
// Get selected ECU-TEST installation
final AbstractToolInstallation installation = configureToolInstallation(listener, run.getEnvironment(listener));
// Start selected Tool-Server from inside of ECU-TEST installation
if (installation instanceof ETInstallation) {
final String tsName = "Tool-Server";
logger.logInfo(String.format("Starting %s...", tsName));
final String installPath = ((ETInstallation) installation).getTSExecutable(launcher);
final TSClient tsClient = new TSClient(getToolName(), installPath, expandedTimeout,
expandedToolLibs, expandedTcpPort);
if (tsClient.start(true, launcher, listener)) {
logger.logInfo(String.format("%s started successfully.", tsName));
} else {
logger.logError(String.format("Starting %s failed!", tsName));
}
} else {
logger.logError("Selected ECU-TEST installation is not configured for this node!");
}
}
/**
* DescriptorImpl for {@link StartTSBuilder}.
*/
@Extension(ordinal = 1004)
public static final class DescriptorImpl extends AbstractToolDescriptor {
/**
* Instantiates a new {@link DescriptorImpl}.
*/
public DescriptorImpl() {
super(StartTSBuilder.class);
load();
}
@Override
public int getDefaultTimeout() {
return DEFAULT_TIMEOUT;
}
/**
* @return the default TCP port
*/
public int getDefaultTcpPort() {
return DEFAULT_TCP_PORT;
}
/**
* Validates the ToolLibs.ini path.
*
* @param value
* the ToolLibs.ini path
* @return the form validation
*/
public FormValidation doCheckToolLibsIni(@QueryParameter final String value) {
return toolValidator.validateToolLibsIni(value);
}
/**
* Validates the TCP port.
*
* @param value
* the TCP port
* @return the form validation
*/
public FormValidation doCheckTcpPort(@QueryParameter final String value) {
return toolValidator.validateTcpPort(value);
}
@Override
public String getDisplayName() {
return Messages.StartTSBuilder_DisplayName();
}
}
}