From 9bc750e92525eb1233f5794f2dd72c79a6f4ee37 Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Thu, 18 May 2023 12:49:44 +0100 Subject: [PATCH 1/2] Upgrade installation-manager SPI to 1.0.1.Final --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e30ee26a3..92487ddd4 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ 3.8.9.Final 1.6.0.Final 6.4.2.Final - 1.0.0.Final + 1.0.1.Final 5.3.0 2.0.7 2.0 From fc0a7e34da6df69d70a6165ce3724f76851133f1 Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Thu, 18 May 2023 09:59:21 +0100 Subject: [PATCH 2/2] Add option to generate PS1 script via the SPI --- .../CreateSnapshotTest.java | 39 +++++++++++++++++++ .../prospero/cli/spi/CliProviderImpl.java | 21 +++++++--- .../spi/ProsperoInstallationManager.java | 20 ++++++++-- .../prospero/spi/internal/CliProvider.java | 5 ++- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/integration-tests/src/test/java/org/wildfly/prospero/it/installationmanager/CreateSnapshotTest.java b/integration-tests/src/test/java/org/wildfly/prospero/it/installationmanager/CreateSnapshotTest.java index 2f5febb0e..9a8e431df 100644 --- a/integration-tests/src/test/java/org/wildfly/prospero/it/installationmanager/CreateSnapshotTest.java +++ b/integration-tests/src/test/java/org/wildfly/prospero/it/installationmanager/CreateSnapshotTest.java @@ -22,6 +22,7 @@ import org.junit.Test; import org.wildfly.installationmanager.MavenOptions; import org.wildfly.installationmanager.spi.InstallationManager; +import org.wildfly.installationmanager.spi.OsShell; import org.wildfly.prospero.api.InstallationMetadata; import org.wildfly.prospero.api.ProvisioningDefinition; import org.wildfly.prospero.cli.DistributionInfo; @@ -96,4 +97,42 @@ public void generateApplyCommand() throws Exception { final String command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo")); assertEquals(expected.toString(), command); } + + @Test + public void generateApplyCommandWithOsShell() throws Exception { + final Path channelsFile = MetadataTestUtils.prepareChannel(CHANNEL_BASE_CORE_19); + final StringBuffer expected = new StringBuffer("\"") + .append(outputPath.resolve("bin").resolve(DistributionInfo.DIST_NAME)); + + expected.append("."); + + expected.append("\""); + expected.append(" "); + expected.append(CliConstants.Commands.UPDATE).append(" ").append(CliConstants.Commands.APPLY); + expected.append(" "); + expected.append(CliConstants.DIR).append(" \"").append(outputPath.toAbsolutePath()).append("\""); + expected.append(" "); + expected.append(CliConstants.CANDIDATE_DIR).append(" \"").append(Path.of("foo").toAbsolutePath()).append("\""); + expected.append(" "); + expected.append(CliConstants.YES); + + final ProvisioningDefinition provisioningDefinition = defaultWfCoreDefinition() + .setChannelCoordinates(channelsFile.toString()) + .build(); + + installation.provision(provisioningDefinition.toProvisioningConfig(), + provisioningDefinition.resolveChannels(CHANNELS_RESOLVER_FACTORY)); + + + final ProsperoInstallationManager manager = (ProsperoInstallationManager) new ProsperoInstallationManagerFactory().create(outputPath, new MavenOptions(MavenSessionManager.LOCAL_MAVEN_REPO, false)); + + String command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.Linux); + assertEquals(expected.toString().replace("", "sh"), command); + + command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.WindowsBash); + assertEquals(expected.toString().replace("", "bat"), command); + + command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.WindowsPowerShell); + assertEquals(expected.toString().replace("", "ps1"), command); + } } diff --git a/prospero-cli/src/main/java/org/wildfly/prospero/cli/spi/CliProviderImpl.java b/prospero-cli/src/main/java/org/wildfly/prospero/cli/spi/CliProviderImpl.java index 3c5e94466..268eba16d 100644 --- a/prospero-cli/src/main/java/org/wildfly/prospero/cli/spi/CliProviderImpl.java +++ b/prospero-cli/src/main/java/org/wildfly/prospero/cli/spi/CliProviderImpl.java @@ -17,23 +17,34 @@ package org.wildfly.prospero.cli.spi; +import org.wildfly.installationmanager.spi.OsShell; import org.wildfly.prospero.spi.internal.CliProvider; import org.wildfly.prospero.cli.DistributionInfo; import org.wildfly.prospero.cli.commands.CliConstants; import java.nio.file.Path; -import java.util.Locale; /** * implementation of {@link CliProvider} */ public class CliProviderImpl implements CliProvider { - private boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"); - @Override - public String getScriptName() { - final String suffix = isWindows ?".bat":".sh"; + public String getScriptName(OsShell shell) { + final String suffix; + switch (shell) { + case Linux: + suffix = ".sh"; + break; + case WindowsBash: + suffix = ".bat"; + break; + case WindowsPowerShell: + suffix = ".ps1"; + break; + default: + throw new IllegalArgumentException(String.format("The requested shell %s is not supported", shell)); + } return DistributionInfo.DIST_NAME + suffix; } diff --git a/prospero-common/src/main/java/org/wildfly/prospero/spi/ProsperoInstallationManager.java b/prospero-common/src/main/java/org/wildfly/prospero/spi/ProsperoInstallationManager.java index c783ac037..815995f30 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/spi/ProsperoInstallationManager.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/spi/ProsperoInstallationManager.java @@ -13,6 +13,7 @@ import org.wildfly.installationmanager.OperationNotAvailableException; import org.wildfly.installationmanager.Repository; import org.wildfly.installationmanager.spi.InstallationManager; +import org.wildfly.installationmanager.spi.OsShell; import org.wildfly.prospero.ProsperoLogger; import org.wildfly.prospero.actions.InstallationExportAction; import org.wildfly.prospero.actions.InstallationHistoryAction; @@ -34,6 +35,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.Optional; import java.util.ServiceLoader; @@ -181,24 +183,36 @@ public Path createSnapshot(Path targetPath) throws Exception { @Override public String generateApplyUpdateCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException { + final boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"); + return this.generateApplyUpdateCommand(scriptHome, candidatePath, isWindows?OsShell.WindowsBash:OsShell.Linux); + } + + @Override + public String generateApplyRevertCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException { + final boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"); + return this.generateApplyRevertCommand(scriptHome, candidatePath, isWindows?OsShell.WindowsBash:OsShell.Linux); + } + + @Override + public String generateApplyUpdateCommand(Path scriptHome, Path candidatePath, OsShell shell) throws OperationNotAvailableException { final Optional cliProviderLoader = ServiceLoader.load(CliProvider.class).findFirst(); if (cliProviderLoader.isEmpty()) { throw new OperationNotAvailableException("Installation manager does not support CLI operations."); } final CliProvider cliProvider = cliProviderLoader.get(); - return escape(scriptHome.resolve(cliProvider.getScriptName())) + " " + cliProvider.getApplyUpdateCommand(installationDir, candidatePath); + return escape(scriptHome.resolve(cliProvider.getScriptName(shell))) + " " + cliProvider.getApplyUpdateCommand(installationDir, candidatePath); } @Override - public String generateApplyRevertCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException { + public String generateApplyRevertCommand(Path scriptHome, Path candidatePath, OsShell shell) throws OperationNotAvailableException { final Optional cliProviderLoader = ServiceLoader.load(CliProvider.class).findFirst(); if (cliProviderLoader.isEmpty()) { throw new OperationNotAvailableException("Installation manager does not support CLI operations."); } final CliProvider cliProvider = cliProviderLoader.get(); - return escape(scriptHome.resolve(cliProvider.getScriptName())) + " " + cliProvider.getApplyRevertCommand(installationDir, candidatePath); + return escape(scriptHome.resolve(cliProvider.getScriptName(shell))) + " " + cliProvider.getApplyRevertCommand(installationDir, candidatePath); } private String escape(Path absolutePath) { diff --git a/prospero-common/src/main/java/org/wildfly/prospero/spi/internal/CliProvider.java b/prospero-common/src/main/java/org/wildfly/prospero/spi/internal/CliProvider.java index 0a58ca476..d29d6ac75 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/spi/internal/CliProvider.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/spi/internal/CliProvider.java @@ -17,6 +17,8 @@ package org.wildfly.prospero.spi.internal; +import org.wildfly.installationmanager.spi.OsShell; + import java.nio.file.Path; /** @@ -25,9 +27,10 @@ public interface CliProvider { /** * returns the script name used to run Prospero. The returned script is OS-specific. + * @param shell * @return */ - String getScriptName(); + String getScriptName(OsShell shell); /** * generates command used to apply an update candidate in {@code candidatePath} into {@code installationPath}