From 87f989272fdb4ae982a3d85c4e7ba448a87ec5d8 Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Tue, 15 Aug 2017 08:47:12 -0700 Subject: [PATCH] Fix #115 add `rd system mode info/active/passive` --- .../org/rundeck/client/api/RundeckApi.java | 11 ++ .../client/api/model/ExecutionMode.java | 10 ++ .../rundeck/client/api/model/SystemMode.java | 17 +++ .../client/tool/commands/RDSystem.java | 7 +- .../client/tool/commands/system/Mode.java | 106 ++++++++++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/rundeck/client/api/model/ExecutionMode.java create mode 100644 src/main/java/org/rundeck/client/api/model/SystemMode.java create mode 100644 src/main/java/org/rundeck/client/tool/commands/system/Mode.java diff --git a/src/main/java/org/rundeck/client/api/RundeckApi.java b/src/main/java/org/rundeck/client/api/RundeckApi.java index 3d631611..c5efc464 100644 --- a/src/main/java/org/rundeck/client/api/RundeckApi.java +++ b/src/main/java/org/rundeck/client/api/RundeckApi.java @@ -685,6 +685,17 @@ Call deleteSystemAclPolicy( @GET("system/info") Call systemInfo(); + + @Headers("Accept: application/json") + @POST("system/executions/enable") + Call executionModeEnable(); + + @Headers("Accept: application/json") + @POST("system/executions/disable") + Call executionModeDisable(); + + + //scheduler /** diff --git a/src/main/java/org/rundeck/client/api/model/ExecutionMode.java b/src/main/java/org/rundeck/client/api/model/ExecutionMode.java new file mode 100644 index 00000000..ef4624dd --- /dev/null +++ b/src/main/java/org/rundeck/client/api/model/ExecutionMode.java @@ -0,0 +1,10 @@ +package org.rundeck.client.api.model; + +/** + * @author greg + * @since 8/15/17 + */ +public enum ExecutionMode { + active, + passive +} diff --git a/src/main/java/org/rundeck/client/api/model/SystemMode.java b/src/main/java/org/rundeck/client/api/model/SystemMode.java new file mode 100644 index 00000000..aee55fba --- /dev/null +++ b/src/main/java/org/rundeck/client/api/model/SystemMode.java @@ -0,0 +1,17 @@ +package org.rundeck.client.api.model; + +/** + * @author greg + * @since 8/14/17 + */ +public class SystemMode { + private ExecutionMode executionMode; + + public ExecutionMode getExecutionMode() { + return executionMode; + } + + public void setExecutionMode(ExecutionMode executionMode) { + this.executionMode = executionMode; + } +} diff --git a/src/main/java/org/rundeck/client/tool/commands/RDSystem.java b/src/main/java/org/rundeck/client/tool/commands/RDSystem.java index 0ea7d90a..0b7fe588 100644 --- a/src/main/java/org/rundeck/client/tool/commands/RDSystem.java +++ b/src/main/java/org/rundeck/client/tool/commands/RDSystem.java @@ -24,8 +24,10 @@ import org.rundeck.client.api.model.SystemInfo; import org.rundeck.client.tool.RdApp; import org.rundeck.client.tool.commands.system.ACLs; +import org.rundeck.client.tool.commands.system.Mode; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -40,8 +42,9 @@ public RDSystem(final RdApp client) { @Override public List getSubCommands() { - return Collections.singletonList( - new ACLs(this) + return Arrays.asList( + new ACLs(this), + new Mode(this) ); } diff --git a/src/main/java/org/rundeck/client/tool/commands/system/Mode.java b/src/main/java/org/rundeck/client/tool/commands/system/Mode.java new file mode 100644 index 00000000..7872f6d2 --- /dev/null +++ b/src/main/java/org/rundeck/client/tool/commands/system/Mode.java @@ -0,0 +1,106 @@ +package org.rundeck.client.tool.commands.system; + +import com.lexicalscope.jewel.cli.CommandLineInterface; +import com.lexicalscope.jewel.cli.Option; +import com.simplifyops.toolbelt.Command; +import com.simplifyops.toolbelt.CommandOutput; +import com.simplifyops.toolbelt.InputError; +import org.rundeck.client.api.RundeckApi; +import org.rundeck.client.api.model.ExecutionMode; +import org.rundeck.client.api.model.SystemInfo; +import org.rundeck.client.api.model.SystemMode; +import org.rundeck.client.tool.RdApp; +import org.rundeck.client.tool.commands.AppCommand; +import org.rundeck.client.tool.options.QuietOption; +import org.rundeck.client.util.Client; +import retrofit2.Call; + +import java.io.IOException; +import java.util.function.Function; + +/** + * @author greg + * @since 8/14/17 + */ + +@Command(description = "Manage Execution Mode") +public class Mode extends AppCommand { + public Mode(final RdApp rdApp) { + super(rdApp); + } + + @CommandLineInterface(application = "info") interface ModeInfo { + + @Option(shortName = "A", + longName = "testactive", + description = "Test whether the execution mode is active: fail if not") + boolean isTestActive(); + + @Option(shortName = "P", + longName = "testpassive", + description = "Test whether the execution mode is passive: fail if not") + boolean isTestPassive(); + } + + @Command(description = + "Show execution mode\n" + + "When --testactive or --testpassive are used, the exit code will be 0 if the test is successful, 1 otherwise.") + public boolean info(ModeInfo opts, CommandOutput output) throws IOException, InputError { + if (opts.isTestPassive() && opts.isTestActive()) { + throw new InputError("--testactive and --testpassive cannot be combined"); + } + SystemInfo systemInfo = apiCall(RundeckApi::systemInfo); + Object executionMode = systemInfo.system.getExecutions().get("executionMode"); + boolean modeIsActive = "active".equals(executionMode); + boolean testpass = true; + String message = "Execution Mode is currently:"; + + if (opts.isTestActive() && !modeIsActive || opts.isTestPassive() && modeIsActive) { + testpass = false; + output.warning(message); + } else { + output.info(message); + } + output.output(executionMode); + return testpass; + } + + @CommandLineInterface(application = "active") interface ModeActive extends QuietOption { + + } + + @Command(description = "Set execution mode Active") + public boolean active(ModeActive opts, CommandOutput output) throws IOException, InputError { + return changeMode(opts, output, ExecutionMode.active, RundeckApi::executionModeEnable, getClient()); + } + + @CommandLineInterface(application = "passive") interface ModePassive extends QuietOption { + + } + + @Command(description = "Set execution mode Passive") + public boolean passive(ModePassive opts, CommandOutput output) throws IOException, InputError { + return changeMode(opts, output, ExecutionMode.passive, RundeckApi::executionModeDisable, getClient()); + } + + static boolean changeMode( + final QuietOption opts, + final CommandOutput output, + final ExecutionMode expected, + final Function> operation, + final Client client + ) + throws InputError, IOException + { + if (!opts.isQuiet()) { + output.info(String.format("Setting execution mode to %s...", expected)); + } + SystemMode mode = apiCall(client, operation); + if (!opts.isQuiet()) { + output.info("Execution Mode is now:"); + output.output(mode.getExecutionMode()); + } + return expected.equals(mode.getExecutionMode()); + } + +}