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 2 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
Expand All @@ -10,6 +13,7 @@
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
Expand All @@ -18,6 +22,8 @@

public class PackagerController {

private static Util util = new Util();

private final static Logger LOGGER = Logger.getLogger(PackagerController.class.getName());
/*
* Usage : GET: api/plugin/getPluginList
Expand All @@ -35,15 +41,25 @@ public ResponseEntity<?> getPackageConfig(@RequestBody String postPayload) {
}
}

/**
* @param postPayload Configuration for the war file
* @return War file
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
*/
@PostMapping (path = "/downloadWarPackage")
public ResponseEntity<?> downloadPackageConfig(@RequestBody String postPayload) {
LOGGER.info("Request Received for downloading war file with configuration" + postPayload);
public ResponseEntity<?> downloadWAR(@RequestBody String postPayload) {
LOGGER.info("Request Received for downloading war file with configuration");
try {
return new PackagerDownloadService().downloadWAR("0.1", postPayload);
return new PackagerDownloadService().downloadWAR(getWarVersion(), postPayload);
} catch (Exception e) {
LOGGER.severe(e.toString());
e.printStackTrace();
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}

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
@@ -1,9 +1,7 @@
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;
Expand All @@ -12,15 +10,13 @@
public class WarGenerator {

private final static Logger LOGGER = Logger.getLogger(WarGenerator.class.getName());
private static Util util = new Util();
private static final String TEMP_PREFIX = "CDS";

public static void generateWAR(String versionName, String configuration) {

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);
Path tempDirWithPrefix = Files.createTempDirectory(TEMP_PREFIX);
File packagerConfigFile = File.createTempFile("packager-config", ".yml");
byte[] buf = configuration.getBytes();
Files.write(packagerConfigFile.toPath(), buf);
Expand All @@ -31,10 +27,7 @@ public static void generateWAR(String versionName, String configuration) {
new Builder(cfg).build();
LOGGER.info("Cleaning up temporary directory");
packagerConfigFile.deleteOnExit();
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
e.printStackTrace();
} finally {
util.cleanupTempDirectory(tempDirWithPrefix.toFile());
}
return cfg.getOutputWar();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +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 @@ -24,8 +26,7 @@ public ResponseEntity<Resource> downloadWAR(String versionName, String configura
File warFile = null;
String artifactId = getArtifactId();
try {
WarGenerator.generateWAR(versionName, configuration);
warFile = new File("/tmp/output/target/" + artifactId + "-" + versionName + ".war");
warFile = WarGenerator.generateWAR(versionName, configuration, artifactId);
InputStreamResource resource = new InputStreamResource(new FileInputStream(warFile));
String headerValue = "attachment; filename=jenkins.war";
LOGGER.info("Returning War file");
Expand Down Expand Up @@ -55,12 +56,12 @@ private HttpHeaders returnHeaders(String headerValue) {
return headers;
}


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");
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class WarGeneratorTest {
@Test
public void testWarGenerationSucceeds() {
try {
WarGenerator.generateWAR("1.0", util.readStringFromFile("packager-config.yml") );
WarGenerator.generateWAR("1.0", util.readStringFromFile("packager-config.yml"), "jenkins-all-latest" );
} catch (Exception e) {
fail("Should not have thrown any exception");
fail("Should not have thrown any exception" + e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/packagerConfig/simpleConfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ casc:
dir: "casc.yml"
plugins:
- groupId: "org.jenkins-ci.plugins"
artifactId: "AnchorChain"
artifactId: "dotnet-as-script"
source:
version: "1.0"
version: "1.0.2"
- groupId: "org.jenkins-ci.plugins"
artifactId: "ApicaLoadtest"
source:
Expand Down