Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 7745ed4

Browse files
authored
Merge pull request #4 from xlgao-zju/master
add code to download Hypercli and config credentials
2 parents 27ce6f0 + 9c6ec50 commit 7745ed4

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

src/main/java/hyperbuildstep/hyperbuildstepplugin/HyperBuilder.java

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package hyperbuildstep.hyperbuildstepplugin;
2+
23
import hudson.Launcher;
34
import hudson.Extension;
45
import hudson.FilePath;
@@ -16,8 +17,9 @@
1617
import org.kohsuke.stapler.QueryParameter;
1718

1819
import javax.servlet.ServletException;
19-
import java.io.IOException;
20-
import java.io.ByteArrayOutputStream;
20+
import java.io.*;
21+
import java.net.URL;
22+
import java.net.URLConnection;
2123

2224
/**
2325
* Sample {@link Builder}.
@@ -133,6 +135,122 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
133135
public String getDisplayName() {
134136
return "Execute shell in Hyper_";
135137
}
138+
139+
//save credential
140+
public FormValidation doSaveCredential(@QueryParameter("accessId") final String accessId,
141+
@QueryParameter("secretKey") final String secretKey) throws IOException, ServletException {
142+
try {
143+
String jsonStr = "{\"clouds\": {" +
144+
"\"tcp://us-west-1.hyper.sh:443\": {" +
145+
"\"accesskey\": " + "\"" + accessId + "\"," +
146+
"\"secretkey\": " + "\"" + secretKey + "\"" +
147+
"}" +
148+
"}" +
149+
"}";
150+
BufferedWriter writer = null;
151+
String configPath;
152+
String jenkinsHome = System.getenv("HUDSON_HOME");
153+
154+
if (jenkinsHome == null) {
155+
String home = System.getenv("HOME");
156+
configPath = home + "/.hyper/config.json";
157+
File hyperPath = new File(home + "/.hyper");
158+
try {
159+
if (!hyperPath.exists()) {
160+
hyperPath.mkdir();
161+
}
162+
} catch (SecurityException e) {
163+
e.printStackTrace();
164+
}
165+
} else {
166+
File hyperPath = new File(jenkinsHome +"/.hyper");
167+
try {
168+
if (!hyperPath.exists()) {
169+
hyperPath.mkdir();
170+
}
171+
} catch (SecurityException e) {
172+
e.printStackTrace();
173+
}
174+
configPath = jenkinsHome + "/.hyper/config.json";
175+
}
176+
177+
178+
File config = new File(configPath);
179+
if(!config.exists()){
180+
try {
181+
config.createNewFile();
182+
} catch (IOException e) {
183+
e.printStackTrace();
184+
}
185+
}
186+
187+
try {
188+
writer = new BufferedWriter(new FileWriter(config));
189+
writer.write(jsonStr);
190+
} catch (IOException e) {
191+
e.printStackTrace();
192+
}finally {
193+
try {
194+
if(writer != null){
195+
writer.close();
196+
}
197+
} catch (IOException e) {
198+
e.printStackTrace();
199+
}
200+
}
201+
202+
return FormValidation.ok("Credentials saved!");
203+
} catch (Exception e) {
204+
return FormValidation.error("Saving credentials error : "+e.getMessage());
205+
}
206+
}
207+
208+
//download Hypercli
209+
public FormValidation doDownloadHypercli() throws IOException, ServletException {
210+
try {
211+
String urlPath = "https://hyper-install.s3.amazonaws.com/hyper";
212+
String hyperCliPath;
213+
URL url = new URL(urlPath);
214+
URLConnection connection = url.openConnection();
215+
InputStream in = connection.getInputStream();
216+
217+
String jenkinsHome = System.getenv("HUDSON_HOME");
218+
219+
if (jenkinsHome == null) {
220+
hyperCliPath = System.getenv("HOME") + "/hyper";
221+
} else {
222+
File hyperPath = new File(jenkinsHome +"/bin");
223+
try {
224+
if (!hyperPath.exists()) {
225+
hyperPath.mkdir();
226+
}
227+
} catch (SecurityException e) {
228+
e.printStackTrace();
229+
}
230+
hyperCliPath = jenkinsHome + "/bin/hyper";
231+
}
232+
233+
FileOutputStream os = new FileOutputStream(hyperCliPath);
234+
byte[] buffer = new byte[4 * 1024];
235+
int read;
236+
while ((read = in.read(buffer)) > 0) {
237+
os.write(buffer, 0, read);
238+
}
239+
os.close();
240+
in.close();
241+
242+
try {
243+
String command = "chmod +x " + hyperCliPath;
244+
Runtime runtime = Runtime.getRuntime();
245+
runtime.exec(command);
246+
} catch (IOException e) {
247+
e.printStackTrace();
248+
}
249+
return FormValidation.ok("Hypercli downloaded!");
250+
} catch (Exception e) {
251+
return FormValidation.error("Downloading Hypercli error : "+e.getMessage());
252+
}
253+
}
136254
}
137255
}
138256

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
3+
<!--
4+
This Jelly script is used to produce the global configuration option.
5+
6+
Jenkins uses a set of tag libraries to provide uniformity in forms.
7+
To determine where this tag is defined, first check the namespace URI,
8+
and then look under $JENKINS/views/. For example, <f:section> is defined
9+
in $JENKINS/views/lib/form/section.jelly.
10+
11+
It's also often useful to just check other similar scripts to see what
12+
tags they use. Views are always organized according to its owner class,
13+
so it should be straightforward to find them.
14+
-->
15+
<f:section title="Hyper Install And Config">
16+
17+
<f:entry title="${%Access Key ID}">
18+
<f:textbox field="accessId" />
19+
</f:entry>
20+
<f:entry title="${%Secret Access Key}">
21+
<f:textbox field="secretKey" />
22+
</f:entry>
23+
<f:validateButton
24+
title="${%Save credential}" progress="${%saving...}"
25+
method="saveCredential" with="secretKey,accessId" />
26+
<f:validateButton
27+
title="${%Download Hypercli}" progress="${%Downloading...}"
28+
method="downloadHypercli"/>
29+
</f:section>
30+
</j:jelly>

src/main/resources/index.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
This view is used to render the installed plugins page.
44
-->
55
<div>
6-
This plugin is a sample to explain how to write a Jenkins plugin.
6+
This plugin allows to add various docker commands to your job as build steps, download Hypercli and config credentials.
77
</div>

0 commit comments

Comments
 (0)