-
Notifications
You must be signed in to change notification settings - Fork 229
/
ToolInstallation.java
162 lines (139 loc) · 6.27 KB
/
ToolInstallation.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
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* 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 org.jenkinsci.test.acceptance.po;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import hudson.util.VersionNumber;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.jenkinsci.utils.process.CommandBuilder;
/**
* @author ogondza
* @see ToolInstallationPageObject
*/
public abstract class ToolInstallation extends PageAreaImpl {
public final Control name = control("name");
private final Control autoInstall = control("properties/hudson-tools-InstallSourceProperty");
public static void waitForUpdates(final Jenkins jenkins, final Class<? extends ToolInstallation> type) {
if (hasUpdatesFor(jenkins, type)) return;
jenkins.getPluginManager().checkForUpdates();
jenkins.waitFor()
.withMessage("tool installer metadata for %s has arrived", type.getAnnotation(ToolInstallationPageObject.class).installer())
.withTimeout(60, TimeUnit.SECONDS)
.until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return hasUpdatesFor(jenkins, type);
}
});
}
private static boolean hasUpdatesFor(final Jenkins jenkins, Class<? extends ToolInstallation> type) {
return Boolean.parseBoolean(jenkins.runScript(
"println DownloadService.Downloadable.get('%s').data != null",
type.getAnnotation(ToolInstallationPageObject.class).installer()
));
}
public static <T extends ToolInstallation> T addTool(Jenkins jenkins, Class<T> type) {
ConfigurablePageObject page = getPageObject(jenkins);
page.configure();
String name = type.getAnnotation(ToolInstallationPageObject.class).name();
page.clickButton("Add " + name);
page.elasticSleep(100);
String path = page.find(by.button("Delete " + name)).getAttribute("path");
String prefix = path.substring(0, path.length() - 18);
return page.newInstance(type, jenkins, prefix);
}
public static <T extends ToolInstallation> void installTool(Jenkins jenkins, Class<T> type, String name, String version) {
waitForUpdates(jenkins, type);
ConfigurablePageObject tools = getPageObject(jenkins);
tools.configure();
T maven = addTool(jenkins, type);
maven.name.set(name);
maven.installVersion(version);
tools.save();
}
public ToolInstallation(Jenkins jenkins, String path) {
super(getPageObject(jenkins), path);
}
protected static ConfigurablePageObject getPageObject(Jenkins jenkins) {
// TODO: change to jenkins.getVersion().isOlderThan(new VersionNumber("2")) after 2.0 is released.
// yeah, you could do a number of things here e.g. isNewerThan("1.1000") etc,
// but this is fine as it's temporary.
return jenkins.getVersion().toString().startsWith("1.")
? new JenkinsConfig(jenkins)
: new GlobalToolConfig(jenkins);
}
@Override
public ConfigurablePageObject getPage() {
return (ConfigurablePageObject) super.getPage();
}
public ToolInstallation installVersion(String version) {
autoInstall.check();
control("properties/hudson-tools-InstallSourceProperty/installers/id").select(version);
return this;
}
public ToolInstallation installedIn(String home) {
autoInstall.uncheck();
control("home").set(home);
return this;
}
protected String fakeHome(String binary, String homeEnvName) {
try {
final File home = File.createTempFile("toolhome", binary);
home.delete();
new File(home, "bin").mkdirs();
home.deleteOnExit();
if (SystemUtils.IS_OS_UNIX) {
final String path = new CommandBuilder("which", binary).popen().asText().trim();
final String code = String.format(
"#!/bin/sh\nexport %s=\nexec %s \"$@\"\n",
homeEnvName, path
);
final File command = new File(home, "bin/" + binary);
FileUtils.writeStringToFile(command, code);
command.setExecutable(true);
}
else {
String path = new CommandBuilder("where.exe", binary).popen().asText().trim();
// where will return all matches and we only want the first.
path = path.replaceAll("\r\n.*", "");
final String code = String.format("set %s=\r\ncall %s %%*\r\n",
homeEnvName, path
);
final File command = new File(home, "bin/" + binary + ".cmd");
FileUtils.writeStringToFile(command, code);
command.setExecutable(true);
}
return home.getAbsolutePath();
}
catch (IOException ex) {
throw new Error(ex);
}
catch (InterruptedException ex) {
throw new Error(ex);
}
}
}