Skip to content

Commit

Permalink
Introduce a cli-script option for openshift
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Francois Denise committed Mar 28, 2024
1 parent 0e7db91 commit 4d3affc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<CLI script file path>";
String INIT_SCRIPT_OPTION = "--init-script";
String INIT_SCRIPT_OPTION_SHORT = "-is";
String INIT_SCRIPT_OPTION_LABEL = "<init script file path>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> initScriptFile;

@CommandLine.Option(names = {Constants.CLI_SCRIPT_OPTION_SHORT, Constants.CLI_SCRIPT_OPTION}, paramLabel = Constants.CLI_SCRIPT_OPTION_LABEL)
Optional<Path> cliScriptFile;

@CommandLine.Option(names = Constants.DISABLE_DEPLOYERS, split = ",", paramLabel = Constants.ADD_ONS_OPTION_LABEL)
Set<String> disableDeployers = new LinkedHashSet<>();

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions cli/src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ public static void deploy(GlowMessageWriter writer,
boolean ha,
Map<String, String> extraEnv,
Set<String> disabledDeployers,
Path initScript, OpenShiftConfiguration config) throws Exception {
Path initScript,
Path cliScript,
OpenShiftConfiguration config) throws Exception {
Map<String, String> actualEnv = new TreeMap<>();
OpenShiftClient osClient = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);
writer.info("\nConnected to OpenShift cluster");
Expand Down Expand Up @@ -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()) {
Expand All @@ -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<String> disabledDeployers) {
Expand Down Expand Up @@ -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.
Expand All @@ -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");
}
Expand Down

0 comments on commit 4d3affc

Please sign in to comment.