Skip to content

Commit

Permalink
Merge pull request payara#5243 from avpinchuk/remote-deploy-draft-1
Browse files Browse the repository at this point in the history
FISH-1375 Improve remote archive deployment
  • Loading branch information
Alan authored and JamesHillyard committed Dec 23, 2021
1 parent 5f37754 commit c4398b2
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package fish.payara.micro.cmd.options;

import fish.payara.deployment.util.JavaArchiveUtils;

import java.io.File;
import java.text.MessageFormat;

Expand All @@ -64,19 +66,19 @@ boolean validate(String optionValue) throws ValidationException {
File deployment = new File(filePath);

if (!deployment.exists()) {
throw new ValidationException(MessageFormat.format(RuntimeOptions.commandlogstrings.getString("fileDoesNotExist"), filePath));
throw new ValidationException(MessageFormat.format(RuntimeOptions.commandlogstrings.getString("fileDoesNotExist"), deployment.getPath()));
}

if (!deployment.canRead()) {
throw new ValidationException(MessageFormat.format(RuntimeOptions.commandlogstrings.getString("fileNotReadable"), filePath));
throw new ValidationException(MessageFormat.format(RuntimeOptions.commandlogstrings.getString("fileNotReadable"), deployment.getPath()));
}

if (deployment.isFile() && !(filePath.endsWith(".rar") || filePath.endsWith(".jar") || filePath.endsWith(".war"))) {
throw new ValidationException(filePath + " is a not valid type of deployment archive");
if (deployment.isFile() && !JavaArchiveUtils.hasJavaArchiveExtension(deployment.getName(), false)) {
throw new ValidationException(deployment.getPath() + " is a not valid type of deployment archive");
}

if (deployment.isDirectory() && !new File(filePath, "WEB-INF").exists()) {
throw new ValidationException(filePath + " is a not valid exploded web archive");
if (deployment.isDirectory() && !JavaArchiveUtils.hasWebInf(deployment)) {
throw new ValidationException(deployment.getPath() + " is a not valid exploded web archive");
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package fish.payara.micro.cmd.options;

import fish.payara.deployment.util.JavaArchiveUtils;

import java.text.MessageFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -129,7 +131,7 @@ public RuntimeOptions(String[] args) throws ValidationException {
} catch (ValidationException ve) {
throw new ValidationException(arg + " " + ve.getMessage(), ve);
}
} else if (arg.endsWith(".war") || arg.endsWith(".rar") || arg.endsWith(".jar")) {
} else if (JavaArchiveUtils.hasJavaArchiveExtension(arg, false)) {
// we have a "raw" deployment
RUNTIME_OPTION.deploy.validate(arg);
options.add(new AbstractMap.SimpleImmutableEntry<>(RUNTIME_OPTION.deploy, arg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import com.sun.enterprise.glassfish.bootstrap.GlassFishImpl;
import com.sun.enterprise.server.logging.ODLLogFormatter;

import fish.payara.deployment.util.JavaArchiveUtils;
import fish.payara.deployment.util.URIUtils;
import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.CommandRunner;
import org.glassfish.embeddable.Deployer;
Expand Down Expand Up @@ -164,8 +166,7 @@ public class PayaraMicroImpl implements PayaraMicroBoot {
private boolean outputLauncher;
private File copyDirectory;
private Properties userSystemProperties;
private List<String> repositoryURLs;
private final String defaultMavenRepository = "https://repo.maven.apache.org/maven2/";
private final List<String> repositoryURIs;
private final short defaultHttpPort = 8080;
private final short defaultHttpsPort = 8181;
private final BootCommands preBootCommands;
Expand Down Expand Up @@ -211,7 +212,7 @@ public class PayaraMicroImpl implements PayaraMicroBoot {
* --help Shows this message and exits\n
* @throws BootstrapException If there is a problem booting the server
*/
public static void main(String args[]) throws Exception {
public static void main(String[] args) throws Exception {
create(args);
}

Expand Down Expand Up @@ -715,7 +716,7 @@ private void validateRuntimeOption(RUNTIME_OPTION option, String optionValue) {
public PayaraMicroImpl addRepoUrl(String... URLs) {
//if (runtime != null) {
checkNotRunning();
repositoryURLs.addAll(Arrays.asList(URLs));
repositoryURIs.addAll(Arrays.asList(URLs));
return this;
}

Expand Down Expand Up @@ -1107,11 +1108,10 @@ public void shutdown() throws BootstrapException {

private PayaraMicroImpl() {
// Initialise a random instance name
repositoryURLs = new LinkedList<>();
repositoryURIs = new LinkedList<>();
preBootCommands = new BootCommands();
postBootCommands = new BootCommands();
postDeployCommands = new BootCommands();
repositoryURLs.add(defaultMavenRepository);
addShutdownHook();
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ private void scanArgs(String[] args) {
enableHealthCheck = Boolean.parseBoolean(value);
break;
case additionalrepository:
repositoryURLs.add(value);
repositoryURIs.add(value);
break;
case outputuberjar:
uberJar = new File(value);
Expand Down Expand Up @@ -1375,7 +1375,7 @@ else if (requestTracing[0].matches("\\D+")) {
LOGGER.warning("Multiple --contextroot arguments only the last one will apply");
}
contextRoot = value;
if (contextRoot.equals("ROOT")) {
if (isRoot(contextRoot)) {
contextRoot = "/";
}
break;
Expand All @@ -1394,6 +1394,10 @@ else if (requestTracing[0].matches("\\D+")) {
}
}

private boolean isRoot(String context) {
return "ROOT".equals(context);
}

private void configureRequestTracingService() {
if (enableRequestTracing) {

Expand Down Expand Up @@ -1506,7 +1510,7 @@ private void processDeploymentOptions() throws GlassFishException {

deployments.put(deployment.getName(), deployment.toURI());

if (supportsContextRoot(deployment)) {
if (JavaArchiveUtils.hasContextRoot(deployment)) {
if (deploymentContext != null) {
addDeploymentContext(deployment.getName(), deploymentContext);
} else {
Expand All @@ -1515,13 +1519,13 @@ private void processDeploymentOptions() throws GlassFishException {
}
} else if (option == RUNTIME_OPTION.deploydir || option == RUNTIME_OPTION.deploymentdir) {
// Get all files in the directory, and sort them by file type
File[] deploymentEntries = deploymentRoot.listFiles();
File[] deploymentEntries = deploymentRoot.listFiles(file -> JavaArchiveUtils.hasJavaArchiveExtension(file.getName(), false));
Arrays.sort(deploymentEntries, new DeploymentComparator());

for (File deploymentEntry : deploymentEntries) {
if (deploymentEntry.isFile() && deploymentEntry.canRead() && hasJavaArchiveExtension(deploymentEntry.getName())) {
if (deploymentEntry.isFile() && deploymentEntry.canRead()) {
deployments.put(deploymentEntry.getName(), deploymentEntry.toURI());
if (supportsContextRoot(deploymentEntry)) {
if (JavaArchiveUtils.hasContextRoot(deploymentEntry)) {
contextRootAvailable = false;
}
}
Expand All @@ -1534,7 +1538,7 @@ private void processDeploymentOptions() throws GlassFishException {

deployments.put(artifactName, artifactURI);

if (artifactName.endsWith(".war")) {
if (JavaArchiveUtils.hasWebArchiveExtension(artifactName)) {
String deploymentContext = gavEntry.getKey();
if (contextRootAvailable) {
deploymentContext = contextRoot;
Expand All @@ -1551,7 +1555,7 @@ private void addDeploymentContext(String fileName, String deploymentContext) {
if (contextRoots == null) {
contextRoots = new Properties();
}
if ("ROOT".equals(deploymentContext)) {
if (isRoot(deploymentContext)) {
deploymentContext = "/";
}
contextRoots.put(fileName, deploymentContext);
Expand Down Expand Up @@ -1599,7 +1603,7 @@ private void deployAll() throws GlassFishException {

for (String entry : microInfEntries) {
File deployment = new File(entry);
String deploymentName = removeJavaArchiveExtension(deployment.getName());
String deploymentName = JavaArchiveUtils.removeJavaArchiveExtension(deployment.getName(), false);

List<String> deploymentParams = new ArrayList<>();
deploymentParams.add("--availabilityenabled=true");
Expand All @@ -1609,16 +1613,16 @@ private void deployAll() throws GlassFishException {
if (hotDeploy) {
deploymentParams.add("--hotDeploy=true");
}
if (deployment.getName().endsWith(".war")) {
if (JavaArchiveUtils.hasWebArchiveExtension(deployment.getName())) {
String deploymentContext;
if ("ROOT".equals(deploymentName)) {
if (isRoot(deploymentName)) {
deploymentContext = "/";
} else if (contextRoots != null && contextRoots.containsKey(deployment.getName())) {
deploymentContext = contextRoots.getProperty(deployment.getName());
} else {
deploymentContext = deploymentName;
}
if ("ROOT".equals(deploymentContext)) {
if (isRoot(deploymentContext)) {
deploymentContext = "/";
}
deploymentParams.add("--contextroot=" + deploymentContext);
Expand Down Expand Up @@ -1650,11 +1654,11 @@ private void deployAll() throws GlassFishException {
deploymentParams.add("--hotDeploy=true");
}
String deploymentContext = null;
if ("file".equalsIgnoreCase(deploymentURI.getScheme())) {
if (URIUtils.hasFileScheme(deploymentURI)) {
File deployment = new File(deploymentURI);
if (supportsContextRoot(deployment)) {
String deploymentName = deployment.isFile() ? removeJavaArchiveExtension(fileName) : fileName;
if ("ROOT".equals(deploymentName)) {
if (JavaArchiveUtils.hasContextRoot(deployment)) {
String deploymentName = deployment.isFile() ? JavaArchiveUtils.removeJavaArchiveExtension(fileName, false) : fileName;
if (isRoot(deploymentName)) {
deploymentContext = "/";
} else if (contextRoots != null && contextRoots.containsKey(fileName)) {
deploymentContext = contextRoots.getProperty(fileName);
Expand All @@ -1666,8 +1670,8 @@ private void deployAll() throws GlassFishException {
}
}
} else {
deploymentParams.add("--name=" + removeJavaArchiveExtension(fileName));
if (fileName.endsWith(".war")) {
deploymentParams.add("--name=" + JavaArchiveUtils.removeJavaArchiveExtension(fileName, false));
if (JavaArchiveUtils.hasWebArchiveExtension(fileName)) {
if (contextRoot != null) {
deploymentContext = contextRoot;
contextRoot = null;
Expand All @@ -1678,7 +1682,7 @@ private void deployAll() throws GlassFishException {
}

if (deploymentContext != null) {
if ("ROOT".equals(deploymentContext)) {
if (isRoot(deploymentContext)) {
deploymentContext = "/";
}
deploymentParams.add("--contextroot=" + deploymentContext);
Expand All @@ -1691,28 +1695,6 @@ private void deployAll() throws GlassFishException {
LOGGER.log(Level.INFO, "Deployed {0} archive(s)", deploymentCount);
}

private String removeJavaArchiveExtension(String fileName) {
if (hasJavaArchiveExtension(fileName)) {
return fileName.substring(0, fileName.length() - 4);
} else {
return fileName;
}
}

private static boolean hasJavaArchiveExtension(String filePath) {
if (filePath == null) {
return false;
}
return filePath.endsWith(".war") || filePath.endsWith(".jar") || filePath.endsWith(".rar");
}

private boolean supportsContextRoot(File archive) {
if (archive == null) {
return false;
}
return archive.isFile() ? archive.getName().endsWith(".war") : archive.isDirectory() && new File(archive.getPath(), "WEB-INF").exists();
}

private void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(
"GlassFish Shutdown Hook") {
Expand Down Expand Up @@ -2064,11 +2046,10 @@ void generateLogo() {
}

private Map.Entry<String, URI> getGAVURI(String gav) throws GlassFishException {
GAVConvertor gavConvertor = new GAVConvertor();
try {
Map.Entry<String, URL> artefactMapEntry = gavConvertor.getArtefactMapEntry(gav, repositoryURLs);
return new AbstractMap.SimpleImmutableEntry<>(artefactMapEntry.getKey(), artefactMapEntry.getValue().toURI());
} catch (MalformedURLException | URISyntaxException ex) {
Map.Entry<String, URI> artefactMapEntry = GAVConvertor.getArtefactMapEntry(gav, repositoryURIs);
return new AbstractMap.SimpleImmutableEntry<>(artefactMapEntry.getKey(), artefactMapEntry.getValue());
} catch (URISyntaxException ex) {
throw new GlassFishException(ex.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package fish.payara.micro.impl;

import com.sun.enterprise.util.io.FileUtils;
import fish.payara.deployment.util.JavaArchiveUtils;
import fish.payara.deployment.util.URIUtils;

import java.io.BufferedInputStream;
Expand All @@ -51,7 +53,6 @@
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -201,7 +202,7 @@ public void buildUberJar() {
// skip the entry as we will add later
} else {
JarEntry newEntry = new JarEntry(entry.getName());
if (entry.getName().endsWith(".jar") || entry.getName().endsWith(".rar")) {
if (JavaArchiveUtils.hasJavaArchiveExtension(entry.getName(), false)) {
newEntry.setMethod(JarEntry.STORED);
newEntry.setSize(entry.getSize());
newEntry.setCrc(entry.getCrc());
Expand All @@ -211,11 +212,7 @@ public void buildUberJar() {
}
jos.putNextEntry(newEntry);
try (InputStream is = getInputStream(jFile, entry)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
FileUtils.copy(is, jos, 0L);
}
}
jos.flush();
Expand Down Expand Up @@ -246,16 +243,8 @@ public void buildUberJar() {
JarEntry deploymentEntry = new JarEntry("MICRO-INF/deploy/" + deployment.getKey());
jos.putNextEntry(deploymentEntry);
URI deploymentURI = deployment.getValue();
if ("file".equalsIgnoreCase(deploymentURI.getScheme())) {
Files.copy(Paths.get(deploymentURI), jos);
} else {
try (InputStream is = URIUtils.openHttpConnection(deploymentURI).getInputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
}
try (InputStream is = URIUtils.openStream(deploymentURI)) {
FileUtils.copy(is, jos, 0L);
}
jos.flush();
jos.closeEntry();
Expand All @@ -265,11 +254,11 @@ public void buildUberJar() {
String basePath = copyDirectory.getCanonicalPath().replaceAll(copyDirectory + "$", "");
List<File> filesToCopy = fillFiles(copyDirectory);
for (File file : filesToCopy) {
JarEntry deploymentEntry = new JarEntry(file.getCanonicalPath().replace(basePath, ""));
jos.putNextEntry(deploymentEntry);
Files.copy(file.toPath(), jos);
jos.flush();
jos.closeEntry();
JarEntry deploymentEntry = new JarEntry(file.getCanonicalPath().replace(basePath, ""));
jos.putNextEntry(deploymentEntry);
Files.copy(file.toPath(), jos);
jos.flush();
jos.closeEntry();
}
}

Expand All @@ -296,7 +285,6 @@ public void buildUberJar() {
Files.copy(alternateHZConfigFile.toPath(), jos);
jos.flush();
jos.closeEntry();

}

if (domainDir != null) {
Expand Down Expand Up @@ -339,7 +327,7 @@ public void buildUberJar() {
if (applicationsDir.exists()){
for (File app : fillFiles(applicationsDir)){
String path = app.getPath();
if (path.endsWith(".war") || path.endsWith(".jar") || path.endsWith(".rar") || path.endsWith(".ear")){
if (JavaArchiveUtils.hasJavaArchiveExtension(path, false)){
JarEntry appEntry = new JarEntry("MICRO-INF/deploy/" + app.getName());
appEntry.setSize(app.length());
try (CheckedInputStream check = new CheckedInputStream(new FileInputStream(app), new CRC32());
Expand Down
Loading

0 comments on commit c4398b2

Please sign in to comment.