From 4d3affcfc7ec91185ff17dfc878bdb1ffd0d6667 Mon Sep 17 00:00:00 2001 From: Jean Francois Denise Date: Thu, 28 Mar 2024 18:03:20 +0100 Subject: [PATCH] Introduce a cli-script option for openshift --- .../wildfly/glow/cli/commands/Constants.java | 3 ++ .../glow/cli/commands/ScanCommand.java | 7 +++- .../main/resources/UsageMessages.properties | 1 + .../openshift/api/OpenShiftSupport.java | 42 ++++++++++++++----- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/cli/src/main/java/org/wildfly/glow/cli/commands/Constants.java b/cli/src/main/java/org/wildfly/glow/cli/commands/Constants.java index 073e5ebf..0219e4cc 100644 --- a/cli/src/main/java/org/wildfly/glow/cli/commands/Constants.java +++ b/cli/src/main/java/org/wildfly/glow/cli/commands/Constants.java @@ -58,6 +58,9 @@ public interface Constants { String HELP_OPTION = "--help"; String HELP_OPTION_SHORT = "-h"; + String CLI_SCRIPT_OPTION = "--cli-script"; + String CLI_SCRIPT_OPTION_SHORT = "-cs"; + String CLI_SCRIPT_OPTION_LABEL = ""; String INIT_SCRIPT_OPTION = "--init-script"; String INIT_SCRIPT_OPTION_SHORT = "-is"; String INIT_SCRIPT_OPTION_LABEL = ""; diff --git a/cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java b/cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java index 9fcbbacb..c0c3f9cb 100644 --- a/cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java +++ b/cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java @@ -125,6 +125,9 @@ public Stability convert(String value) throws Exception { @CommandLine.Option(names = {Constants.INIT_SCRIPT_OPTION_SHORT, Constants.INIT_SCRIPT_OPTION}, paramLabel = Constants.INIT_SCRIPT_OPTION_LABEL) Optional initScriptFile; + @CommandLine.Option(names = {Constants.CLI_SCRIPT_OPTION_SHORT, Constants.CLI_SCRIPT_OPTION}, paramLabel = Constants.CLI_SCRIPT_OPTION_LABEL) + Optional cliScriptFile; + @CommandLine.Option(names = Constants.DISABLE_DEPLOYERS, split = ",", paramLabel = Constants.ADD_ONS_OPTION_LABEL) Set disableDeployers = new LinkedHashSet<>(); @@ -405,7 +408,9 @@ public Integer call() throws Exception { haProfile.orElse(false), extraEnv, disableDeployers, - initScriptFile.orElse(null), new OpenShiftConfiguration.Builder().build()); + initScriptFile.orElse(null), + cliScriptFile.orElse(null), + new OpenShiftConfiguration.Builder().build()); print("@|bold \nOpenshift build and deploy DONE.|@"); } else { if (content.getDockerImageName() != null) { diff --git a/cli/src/main/resources/UsageMessages.properties b/cli/src/main/resources/UsageMessages.properties index b5f28635..159f8092 100644 --- a/cli/src/main/resources/UsageMessages.properties +++ b/cli/src/main/resources/UsageMessages.properties @@ -39,6 +39,7 @@ config-stability-level = Specify a stability to be used when provisioning the se package-stability-level = Specify a stability to be used when provisioning server packages. The stability is also used to identify server packages that would be not provisioned by the specified stability. The stability is by default the minimum stability of each Galleon feature-packs. The stability can be @|fg(yellow) default|@, @|fg(yellow) community|@, @|fg(yellow) preview|@, @|fg(yellow) experimental|@. env-file = The path to a file that contains environment variables (in the form env=value) to be passed to the OpenShift deployment. Can only be used with @|fg(yellow) OPENSHIFT|@ kind of provisioning. init-script = The path to a script that contains commands (JBoss CLI, add-user, ...) to fine tune the server on OpenShift deployment. Can only be used with @|fg(yellow) OPENSHIFT|@ kind of provisioning. +cli-script = The path to a CLI script file that only contains CLI commands in order to fine tune the server on OpenShift deployment. Can only be used with @|fg(yellow) OPENSHIFT|@ kind of provisioning. disable-deployers = A comma separated list of deployer names to disable. To retrieve all the deployer names call the @|fg(yellow) show-configuration|@ operation. To disable them all, use @|fg(yellow) ALL|@ value. Can only be used with @|fg(yellow) OPENSHIFT|@ kind of provisioning. properties = A space separated list of Java system properties. When multiple system properties are set, the list must be enclosed in double quotes. For example: "-Dfoo=bar -DmyProp" diff --git a/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/OpenShiftSupport.java b/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/OpenShiftSupport.java index bb7d81b4..aa3f10e0 100644 --- a/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/OpenShiftSupport.java +++ b/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/OpenShiftSupport.java @@ -185,7 +185,9 @@ public static void deploy(GlowMessageWriter writer, boolean ha, Map extraEnv, Set disabledDeployers, - Path initScript, OpenShiftConfiguration config) throws Exception { + Path initScript, + Path cliScript, + OpenShiftConfiguration config) throws Exception { Map actualEnv = new TreeMap<>(); OpenShiftClient osClient = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class); writer.info("\nConnected to OpenShift cluster"); @@ -242,7 +244,7 @@ public static void deploy(GlowMessageWriter writer, } } - createBuild(writer, target, osClient, appName, initScript, config); + createBuild(writer, target, osClient, appName, initScript, cliScript, config); actualEnv.put("APPLICATION_ROUTE_HOST", host); actualEnv.putAll(extraEnv); if (!actualEnv.isEmpty()) { @@ -265,16 +267,35 @@ private static void createBuild(GlowMessageWriter writer, OpenShiftClient osClient, String name, Path initScript, + Path cliScript, OpenShiftConfiguration config) throws Exception { String serverImageName = doServerImageBuild(writer, target, osClient, config); - doAppImageBuild(serverImageName, writer, target, osClient, name, initScript, config); + doAppImageBuild(serverImageName, writer, target, osClient, name, initScript, cliScript, config); } - private static void packageInitScript(Path initScript, Path target) throws Exception { - Path extensions = target.resolve("extensions"); - Files.createDirectories(extensions); - Path postconfigure = extensions.resolve("postconfigure.sh"); - Files.copy(initScript, postconfigure); + private static boolean packageInitScript(Path initScript, Path cliScript, Path target) throws Exception { + if (initScript != null || cliScript != null) { + Path extensions = target.resolve("extensions"); + Files.createDirectories(extensions); + StringBuilder initExecution = new StringBuilder(); + initExecution.append("#!/bin/bash").append("\n"); + if (initScript != null) { + initExecution.append("echo \"Calling initialization script\"").append("\n"); + Path init = extensions.resolve("init-script.sh"); + Files.copy(initScript, init); + initExecution.append("sh $JBOSS_HOME/extensions/init-script.sh").append("\n"); + } + if (cliScript != null) { + initExecution.append("echo \"Calling CLI script\"").append("\n"); + Path cli = extensions.resolve("cli-script.cli"); + Files.copy(cliScript, cli); + initExecution.append("cat $JBOSS_HOME/extensions/cli-script.cli >> \"${CLI_SCRIPT_FILE}\""); + } + Path postconfigure = extensions.resolve("postconfigure.sh"); + Files.write(postconfigure, initExecution.toString().getBytes()); + return true; + } + return false; } private static boolean isDisabled(String name, Set disabledDeployers) { @@ -386,6 +407,7 @@ private static void doAppImageBuild(String serverImageName, OpenShiftClient osClient, String name, Path initScript, + Path cliScript, OpenShiftConfiguration config) throws Exception { // Now step 2 // From the server image, do a docker build, copy the server and copy in it the deployments and init file. @@ -395,9 +417,7 @@ private static void doAppImageBuild(String serverImageName, dockerFileBuilder.append("FROM " + config.getRuntimeImage() + "\n"); dockerFileBuilder.append("COPY --chown=jboss:root /server $JBOSS_HOME\n"); dockerFileBuilder.append("COPY --chown=jboss:root deployments/* $JBOSS_HOME/standalone/deployments\n"); - - if (initScript != null) { - packageInitScript(initScript, stepTwo); + if (packageInitScript(initScript, cliScript, stepTwo)) { dockerFileBuilder.append("COPY --chown=jboss:root extensions $JBOSS_HOME/extensions\n"); dockerFileBuilder.append("RUN chmod ug+rwx $JBOSS_HOME/extensions/postconfigure.sh\n"); }