|
1 | 1 | package hyperbuildstep.hyperbuildstepplugin;
|
| 2 | + |
2 | 3 | import hudson.Launcher;
|
3 | 4 | import hudson.Extension;
|
4 | 5 | import hudson.FilePath;
|
|
16 | 17 | import org.kohsuke.stapler.QueryParameter;
|
17 | 18 |
|
18 | 19 | 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; |
21 | 23 |
|
22 | 24 | /**
|
23 | 25 | * Sample {@link Builder}.
|
@@ -133,6 +135,122 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
|
133 | 135 | public String getDisplayName() {
|
134 | 136 | return "Execute shell in Hyper_";
|
135 | 137 | }
|
| 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 | + } |
136 | 254 | }
|
137 | 255 | }
|
138 | 256 |
|
0 commit comments