Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Add War download feature #68

Merged
merged 7 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions frontend/src/components/PackageGeneration/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ class editor extends React.Component {
componentDidMount() {
this.setState({code: localStorage.getItem("packageConfigYAML")})
this.setState({title: JSON.parse(localStorage.getItem("packageConfigJSON"))["bundle"]["title"]})
this.setState({description: JSON.parse(localStorage.getItem("packageConfigJSON"))["bundle"]["desc"]})
}
this.setState({description: JSON.parse(localStorage.getItem("packageConfigJSON"))["bundle"]["description"]})
}

downloadWarfile() {
var xhr = new XMLHttpRequest();
xhr.open("POST", 'http://localhost:8080/package/downloadWarPackage', true);
xhr.responseType = "blob";
xhr.onload = function () {
saveData(this.response, 'jenkins.war');
};
xhr.send(localStorage.getItem("packageConfigYAML"));
}

downloadPackagerConfig() {
var xhr = new XMLHttpRequest();
Expand All @@ -50,8 +60,6 @@ class editor extends React.Component {
xhr.send(localStorage.getItem("packageConfigYAML"));
}



render() {
const packageJSON = JSON.parse(localStorage.getItem("packageConfigJSON"))

Expand All @@ -72,8 +80,8 @@ class editor extends React.Component {
}}
/>
<div className="column">
<Button onClick = {this.downloadWarfile} style = {{backgroundColor:"#185ecc", fontSize:"25px"}} >Download War File </Button>
<Button onClick = { this.downloadPackagerConfig }style = {{backgroundColor:"#185ecc", fontSize:"25px", margin:"10px"}} >Download Packager Config </Button>
<Button style = {{backgroundColor:"#185ecc", fontSize:"25px"}} >Download War File </Button>
<Card style = {{ margin:"10px"}}>
<CardBody>
<CardTitle>Packager Details</CardTitle>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ModalExample extends React.Component {
var bundle = new Object()
bundle["artifactId"] = this.state.artifactID
bundle["title"] = this.state.title
bundle["desc"] = this.state.description
bundle["description"] = this.state.description
packagerInfo["bundle"] = bundle
// Add war object
packagerInfo["war"] = {jenkinsVersion: this.state.warVersion}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.org.jenkins.custom.jenkins.distribution.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.org.jenkins.custom.jenkins.distribution.service.services.PackagerDownloadService;
import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import java.util.Map;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.core.io.InputStreamResource;
Expand All @@ -15,16 +15,17 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.Yaml;

import static com.org.jenkins.custom.jenkins.distribution.service.generators.PackageConfigGenerator.generatePackageConfig;

@RestController
@CrossOrigin
@RequestMapping("/package")

public class PackagerController {

private Util util = new Util();
private static Util util = new Util();

private final static Logger LOGGER = Logger.getLogger(PackagerController.class.getName());

/**
Expand All @@ -51,6 +52,24 @@ public ResponseEntity<?> getPackageConfig(@RequestBody String postPayload) {
}
}

/**
* Takes the configuration in the browsers editor and generates and returns the
* war file.
*
* @param postPayload The content of the text editor in the browser
* @return a ResponseEntity instance with a body containing the generated jenkins.war package.
*/
@PostMapping (path = "/downloadWarPackage")
public ResponseEntity<?> downloadWAR(@RequestBody String postPayload) {
LOGGER.info("Request Received for downloading war file with configuration" + postPayload);
try {
return new PackagerDownloadService().downloadWAR(getWarVersion(), postPayload);
} catch (Exception e) {
LOGGER.severe(e.toString());
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}

/**
* Send the content of the browser's text editor to the client (a download).
*
Expand All @@ -71,4 +90,10 @@ public ResponseEntity<?> downloadPackageConfig(@RequestBody String postPayload)
}
}

private String getWarVersion() throws Exception {
Yaml yaml = new Yaml();
Map<String , Map<String,String>> yamlMaps = (Map<String, Map<String,String>>) yaml.load(util.readStringFromFile("packager-config.yml"));
JSONObject json = new JSONObject(new ObjectMapper().writeValueAsString(yamlMaps.get("war")));
return json.getJSONObject("source").get("version").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static JSONObject generateBundle(JSONObject bundle) {
bundleInfo.put("artifactId", bundle.getString("artifactId"));
bundleInfo.put("vendor", "Jenkins project");
bundleInfo.put("title", bundle.getString("title"));
bundleInfo.put("description", bundle.getString("desc"));
bundleInfo.put("description", bundle.getString("description"));
return bundleInfo;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package com.org.jenkins.custom.jenkins.distribution.service.generators;

import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import io.jenkins.tools.warpackager.lib.config.Config;
import io.jenkins.tools.warpackager.lib.impl.Builder;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

public class WarGenerator {

private static Util util = new Util();

private static final String PACKAGER_CONFIG_YAML = "packager-config.yml";
private final static Logger LOGGER = Logger.getLogger(WarGenerator.class.getName());
private static final String TEMP_PREFIX = "CDS";

public static void generateWAR(String versionName) {

public static File generateWAR(String versionName, String configuration) throws Exception {
LOGGER.info("Generating War File");
final Config cfg;
Path tempDirWithPrefix = null;
try {
tempDirWithPrefix = Files.createTempDirectory(TEMP_PREFIX);
final File configPath = util.getFileFromResources(PACKAGER_CONFIG_YAML);
cfg = Config.loadConfig(configPath);
Path tempDirWithPrefix = Files.createTempDirectory(TEMP_PREFIX);
File packagerConfigFile = File.createTempFile("packager-config", ".yml");
byte[] buf = configuration.getBytes();
Files.write(packagerConfigFile.toPath(), buf);
cfg = Config.loadConfig(packagerConfigFile);
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
cfg.buildSettings.setTmpDir(tempDirWithPrefix.toFile());
cfg.buildSettings.setVersion(versionName);
cfg.buildSettings.setInstallArtifacts(true);
new Builder(cfg).build();
} catch (Exception e) {
e.printStackTrace();
} finally {
util.cleanupTempDirectory(tempDirWithPrefix.toFile());
}
LOGGER.info("Cleaning up temporary directory");
packagerConfigFile.deleteOnExit();
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
return cfg.getOutputWar();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.org.jenkins.custom.jenkins.distribution.service.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.org.jenkins.custom.jenkins.distribution.service.generators.WarGenerator;
import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
Expand All @@ -19,11 +22,11 @@ public class PackagerDownloadService {
private final static Logger LOGGER = Logger.getLogger(PackagerDownloadService.class.getName());
private static Util util = new Util();

public ResponseEntity<Resource> downloadWAR(String versionName) throws Exception {
public ResponseEntity<Resource> downloadWAR(String versionName, String configuration) throws Exception {
File warFile = null;
String artifactId = getArtifactId();
try {
warFile = new File("/tmp/output/target/" + artifactId + "-" + versionName + ".war");
warFile = WarGenerator.generateWAR(versionName, configuration);
InputStreamResource resource = new InputStreamResource(new FileInputStream(warFile));
String headerValue = "attachment; filename=jenkins.war";
LOGGER.info("Returning War file");
Expand Down Expand Up @@ -56,8 +59,9 @@ private HttpHeaders returnHeaders(String headerValue) {
private String getArtifactId() throws Exception {
Yaml yaml = new Yaml();
Map<String , Map<String,String>> yamlMaps = (Map<String, Map<String,String>>) yaml.load(util.readStringFromFile("packager-config.yml"));
String artifactId = yamlMaps.get("bundle").get("artifactId");
return artifactId;
return yamlMaps.get("bundle").get("artifactId");
}


}

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/resources/packagerConfig/simpleConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"bundle": {
"artifactId" : "jenkins-all-latest",
"title" : "Jenkins WAR - all latest",
"desc" : "New Jenkins Version"
"description" : "New Jenkins Version"
},
"war" : {
"jenkinsVersion": "2.107.3"
Expand Down