Skip to content

Commit

Permalink
[KEYCLOAK-19426] - Support for auto-build when starting the server
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor committed Oct 4, 2021
1 parent b2fd05f commit 4e6e125
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
13 changes: 6 additions & 7 deletions distribution/server-x-dist/src/main/content/bin/kc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ DEBUG_MODE="${DEBUG:-false}"
DEBUG_PORT="${DEBUG_PORT:-8787}"

CONFIG_ARGS=${CONFIG_ARGS:-""}
IS_DEV_MODE="false"

while [ "$#" -gt 0 ]
do
Expand All @@ -47,10 +46,10 @@ do
;;
*)
if [[ $1 = --* || ! $1 =~ ^-.* ]]; then
if [ "$1" = "start-dev" ]; then
IS_DEV_MODE=true
fi
CONFIG_ARGS="$CONFIG_ARGS $1"
if [[ "$1" = "start-dev" ]]; then
CONFIG_ARGS="$CONFIG_ARGS --auto-build"
fi
else
SERVER_OPTS="$SERVER_OPTS $1"
fi
Expand Down Expand Up @@ -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#?}"

if [ "$IS_DEV_MODE" = "true" ]; then
java -Dkc.dev.rebuild=true $JAVA_RUN_OPTS
if [[ $CONFIG_ARGS = *"--auto-build"* ]]; then
java -Dkc.config.rebuild-and-exit=true $JAVA_RUN_OPTS
fi

exec java $JAVA_RUN_OPTS
exec java ${JAVA_RUN_OPTS}
35 changes: 19 additions & 16 deletions quarkus/runtime/src/main/java/org/keycloak/cli/KeycloakMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.keycloak.cli.MainCommand.BUILD_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.error;
import static org.keycloak.cli.Picocli.getCliArgs;
Expand Down Expand Up @@ -98,23 +97,12 @@ private static void start(List<String> cliArgs, PrintWriter errorWriter) {
}

private static void parseAndRun(List<String> cliArgs) {
CommandLine cmd = createCommandLine();
CommandLine cmd = createCommandLine(cliArgs);

try {
CommandLine.ParseResult result = cmd.parseArgs(cliArgs.toArray(new String[0]));

if (result.hasSubcommand()) {
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 (!result.hasSubcommand() && !result.isUsageHelpRequested() && !result.isVersionHelpRequested()) {
// if no command was set, the start command becomes the default
cliArgs.add(0, START_COMMAND);
}
Expand All @@ -131,6 +119,8 @@ private static void parseAndRun(List<String> cliArgs) {
System.exit(cmd.getCommandSpec().exitCodeOnExecutionException());
}

runReAugmentationIfNeeded(cliArgs, cmd);

int exitCode = cmd.execute(cliArgs.toArray(new String[0]));

if (!isDevMode()) {
Expand All @@ -139,11 +129,14 @@ private static void parseAndRun(List<String> cliArgs) {
}

private static void runReAugmentationIfNeeded(List<String> cliArgs, CommandLine cmd) {
if (Boolean.getBoolean("kc.dev.rebuild")) {
if (cliArgs.contains("--auto-build")) {
if (requiresReAugmentation(cliArgs)) {
runReAugmentation(cliArgs, cmd);
}
System.exit(cmd.getCommandSpec().exitCodeOnSuccess());

if (Boolean.getBoolean("kc.config.rebuild-and-exit")) {
System.exit(cmd.getCommandSpec().exitCodeOnSuccess());
}
}
}

Expand All @@ -166,12 +159,22 @@ private static boolean requiresReAugmentation(List<String> cliArgs) {
}

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);

if (!configArgsList.get(0).startsWith("--")) {
configArgsList.remove(0);
}

configArgsList.remove("--auto-build");
configArgsList.add(0, BUILD_COMMAND);

cmd.execute(configArgsList.toArray(new String[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MainCommand {
static final String START_DEV_COMMAND = "start-dev";
static final String START_COMMAND = "start";
static final String BUILD_COMMAND = "build";
public static final String AUTO_BUILD_OPTION = "--auto-build";

public static boolean isStartDevCommand(CommandSpec commandSpec) {
return START_DEV_COMMAND.equals(commandSpec.name());
Expand Down Expand Up @@ -117,7 +118,8 @@ private void beforeReaugmentationOnWindows() throws Exception {
mixinStandardHelpOptions = true,
optionListHeading = "%nOptions%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();
CommandLine cmd = spec.commandLine();

Expand Down Expand Up @@ -173,7 +175,8 @@ public void start(
@Option(names = "--show-config", arity = "0..1",
description = "Print out the configuration options when starting the server.",
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)) {
System.setProperty("kc.show.config.runtime", Boolean.TRUE.toString());
System.setProperty("kc.show.config", "all");
Expand Down Expand Up @@ -213,6 +216,6 @@ private void runImportExport(String action, String toDir, String toFile, String
}

setProfile(Environment.IMPORT_EXPORT_MODE);
start(null, verbose);
start(null, verbose, false);
}
}
6 changes: 4 additions & 2 deletions quarkus/runtime/src/main/java/org/keycloak/cli/Picocli.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ final class Picocli {

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())
.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, BUILD_COMMAND, true);

Expand Down

0 comments on commit 4e6e125

Please sign in to comment.