[KEYCLOAK-19426] - Support for auto-build when starting the server

This commit is contained in:
Pedro Igor 2021-10-01 13:40:46 -03:00
parent b2fd05f571
commit 4e6e125ede
4 changed files with 35 additions and 28 deletions

View file

@ -29,7 +29,6 @@ DEBUG_MODE="${DEBUG:-false}"
DEBUG_PORT="${DEBUG_PORT:-8787}" DEBUG_PORT="${DEBUG_PORT:-8787}"
CONFIG_ARGS=${CONFIG_ARGS:-""} CONFIG_ARGS=${CONFIG_ARGS:-""}
IS_DEV_MODE="false"
while [ "$#" -gt 0 ] while [ "$#" -gt 0 ]
do do
@ -47,10 +46,10 @@ do
;; ;;
*) *)
if [[ $1 = --* || ! $1 =~ ^-.* ]]; then if [[ $1 = --* || ! $1 =~ ^-.* ]]; then
if [ "$1" = "start-dev" ]; then
IS_DEV_MODE=true
fi
CONFIG_ARGS="$CONFIG_ARGS $1" CONFIG_ARGS="$CONFIG_ARGS $1"
if [[ "$1" = "start-dev" ]]; then
CONFIG_ARGS="$CONFIG_ARGS --auto-build"
fi
else else
SERVER_OPTS="$SERVER_OPTS $1" SERVER_OPTS="$SERVER_OPTS $1"
fi fi
@ -82,8 +81,8 @@ CLASSPATH_OPTS="$DIRNAME/../lib/quarkus-run.jar"
JAVA_RUN_OPTS="$JAVA_OPTS $SERVER_OPTS -cp $CLASSPATH_OPTS io.quarkus.bootstrap.runner.QuarkusEntryPoint ${CONFIG_ARGS#?}" JAVA_RUN_OPTS="$JAVA_OPTS $SERVER_OPTS -cp $CLASSPATH_OPTS io.quarkus.bootstrap.runner.QuarkusEntryPoint ${CONFIG_ARGS#?}"
if [ "$IS_DEV_MODE" = "true" ]; then if [[ $CONFIG_ARGS = *"--auto-build"* ]]; then
java -Dkc.dev.rebuild=true $JAVA_RUN_OPTS java -Dkc.config.rebuild-and-exit=true $JAVA_RUN_OPTS
fi fi
exec java $JAVA_RUN_OPTS exec java ${JAVA_RUN_OPTS}

View file

@ -19,7 +19,6 @@ package org.keycloak.cli;
import static org.keycloak.cli.MainCommand.BUILD_COMMAND; import static org.keycloak.cli.MainCommand.BUILD_COMMAND;
import static org.keycloak.cli.MainCommand.START_COMMAND; import static org.keycloak.cli.MainCommand.START_COMMAND;
import static org.keycloak.cli.MainCommand.isStartDevCommand;
import static org.keycloak.cli.Picocli.createCommandLine; import static org.keycloak.cli.Picocli.createCommandLine;
import static org.keycloak.cli.Picocli.error; import static org.keycloak.cli.Picocli.error;
import static org.keycloak.cli.Picocli.getCliArgs; import static org.keycloak.cli.Picocli.getCliArgs;
@ -98,23 +97,12 @@ public class KeycloakMain {
} }
private static void parseAndRun(List<String> cliArgs) { private static void parseAndRun(List<String> cliArgs) {
CommandLine cmd = createCommandLine(); CommandLine cmd = createCommandLine(cliArgs);
try { try {
CommandLine.ParseResult result = cmd.parseArgs(cliArgs.toArray(new String[0])); CommandLine.ParseResult result = cmd.parseArgs(cliArgs.toArray(new String[0]));
if (result.hasSubcommand()) { if (!result.hasSubcommand() && !result.isUsageHelpRequested() && !result.isVersionHelpRequested()) {
if (isStartDevCommand(result.subcommand().commandSpec())) {
String profile = Environment.getProfile();
if (profile == null) {
// force the server image to be set with the dev profile
Environment.forceDevProfile();
}
runReAugmentationIfNeeded(cliArgs, cmd);
}
} else if ((!result.isUsageHelpRequested() && !result.isVersionHelpRequested())) {
// if no command was set, the start command becomes the default // if no command was set, the start command becomes the default
cliArgs.add(0, START_COMMAND); cliArgs.add(0, START_COMMAND);
} }
@ -131,6 +119,8 @@ public class KeycloakMain {
System.exit(cmd.getCommandSpec().exitCodeOnExecutionException()); System.exit(cmd.getCommandSpec().exitCodeOnExecutionException());
} }
runReAugmentationIfNeeded(cliArgs, cmd);
int exitCode = cmd.execute(cliArgs.toArray(new String[0])); int exitCode = cmd.execute(cliArgs.toArray(new String[0]));
if (!isDevMode()) { if (!isDevMode()) {
@ -139,11 +129,14 @@ public class KeycloakMain {
} }
private static void runReAugmentationIfNeeded(List<String> cliArgs, CommandLine cmd) { private static void runReAugmentationIfNeeded(List<String> cliArgs, CommandLine cmd) {
if (Boolean.getBoolean("kc.dev.rebuild")) { if (cliArgs.contains("--auto-build")) {
if (requiresReAugmentation(cliArgs)) { if (requiresReAugmentation(cliArgs)) {
runReAugmentation(cliArgs, cmd); runReAugmentation(cliArgs, cmd);
} }
System.exit(cmd.getCommandSpec().exitCodeOnSuccess());
if (Boolean.getBoolean("kc.config.rebuild-and-exit")) {
System.exit(cmd.getCommandSpec().exitCodeOnSuccess());
}
} }
} }
@ -166,12 +159,22 @@ public class KeycloakMain {
} }
private static void runReAugmentation(List<String> cliArgs, CommandLine cmd) { private static void runReAugmentation(List<String> cliArgs, CommandLine cmd) {
if (MainCommand.START_DEV_COMMAND.equals(cliArgs.get(0))) {
String profile = Environment.getProfile();
if (profile == null) {
// force the server image to be set with the dev profile
Environment.forceDevProfile();
}
}
List<String> configArgsList = new ArrayList<>(cliArgs); List<String> configArgsList = new ArrayList<>(cliArgs);
if (!configArgsList.get(0).startsWith("--")) { if (!configArgsList.get(0).startsWith("--")) {
configArgsList.remove(0); configArgsList.remove(0);
} }
configArgsList.remove("--auto-build");
configArgsList.add(0, BUILD_COMMAND); configArgsList.add(0, BUILD_COMMAND);
cmd.execute(configArgsList.toArray(new String[0])); cmd.execute(configArgsList.toArray(new String[0]));

View file

@ -53,6 +53,7 @@ public class MainCommand {
static final String START_DEV_COMMAND = "start-dev"; static final String START_DEV_COMMAND = "start-dev";
static final String START_COMMAND = "start"; static final String START_COMMAND = "start";
static final String BUILD_COMMAND = "build"; static final String BUILD_COMMAND = "build";
public static final String AUTO_BUILD_OPTION = "--auto-build";
public static boolean isStartDevCommand(CommandSpec commandSpec) { public static boolean isStartDevCommand(CommandSpec commandSpec) {
return START_DEV_COMMAND.equals(commandSpec.name()); return START_DEV_COMMAND.equals(commandSpec.name());
@ -117,7 +118,8 @@ public class MainCommand {
mixinStandardHelpOptions = true, mixinStandardHelpOptions = true,
optionListHeading = "%nOptions%n", optionListHeading = "%nOptions%n",
parameterListHeading = "Available Commands%n") parameterListHeading = "Available Commands%n")
public void startDev(@Option(names = "--verbose", description = "Print out more details when running this command.", required = false) Boolean verbose) { public void startDev(@Option(names = "--verbose", description = "Print out more details when running this command.", required = false) Boolean verbose,
@Option(names = AUTO_BUILD_OPTION, description = "Automatically detects whether the server configuration changed and a new server image must be built prior to starting the server. This option provides an alternative to manually running the '" + BUILD_COMMAND + "' prior to starting the server. Use this configuration carefully in production as it might impact the startup time.", required = false) Boolean autoConfig) {
Environment.forceDevProfile(); Environment.forceDevProfile();
CommandLine cmd = spec.commandLine(); CommandLine cmd = spec.commandLine();
@ -173,7 +175,8 @@ public class MainCommand {
@Option(names = "--show-config", arity = "0..1", @Option(names = "--show-config", arity = "0..1",
description = "Print out the configuration options when starting the server.", description = "Print out the configuration options when starting the server.",
fallbackValue = "show-config") String showConfig, fallbackValue = "show-config") String showConfig,
@Option(names = "--verbose", description = "Print out more details when running this command.", required = false) Boolean verbose) { @Option(names = "--verbose", description = "Print out more details when running this command.", required = false) Boolean verbose,
@Option(names = AUTO_BUILD_OPTION, description = "Automatically detects whether the server configuration changed and a new server image must be built prior to starting the server. This option provides an alternative to manually running the '" + BUILD_COMMAND + "' prior to starting the server. Use this configuration carefully in production as it might impact the startup time.", required = false) Boolean autoConfig) {
if ("show-config".equals(showConfig)) { if ("show-config".equals(showConfig)) {
System.setProperty("kc.show.config.runtime", Boolean.TRUE.toString()); System.setProperty("kc.show.config.runtime", Boolean.TRUE.toString());
System.setProperty("kc.show.config", "all"); System.setProperty("kc.show.config", "all");
@ -213,6 +216,6 @@ public class MainCommand {
} }
setProfile(Environment.IMPORT_EXPORT_MODE); setProfile(Environment.IMPORT_EXPORT_MODE);
start(null, verbose); start(null, verbose, false);
} }
} }

View file

@ -43,11 +43,13 @@ final class Picocli {
private static final Logger logger = Logger.getLogger(Picocli.class); private static final Logger logger = Logger.getLogger(Picocli.class);
static CommandLine createCommandLine() { static CommandLine createCommandLine(List<String> cliArgs) {
CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.forAnnotatedObject(new MainCommand()) CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.forAnnotatedObject(new MainCommand())
.name(Environment.getCommand()); .name(Environment.getCommand());
addOption(spec, START_COMMAND, false); boolean addBuildOptionsToStartCommand = cliArgs.contains(MainCommand.AUTO_BUILD_OPTION);
addOption(spec, START_COMMAND, addBuildOptionsToStartCommand);
addOption(spec, START_DEV_COMMAND, true); addOption(spec, START_DEV_COMMAND, true);
addOption(spec, BUILD_COMMAND, true); addOption(spec, BUILD_COMMAND, true);