Skip to content

Commit

Permalink
Fix #115 add rd system mode info/active/passive
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Aug 15, 2017
1 parent 0e9b0da commit 87f9892
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/rundeck/client/api/RundeckApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,17 @@ Call<Void> deleteSystemAclPolicy(
@GET("system/info")
Call<SystemInfo> systemInfo();


@Headers("Accept: application/json")
@POST("system/executions/enable")
Call<SystemMode> executionModeEnable();

@Headers("Accept: application/json")
@POST("system/executions/disable")
Call<SystemMode> executionModeDisable();



//scheduler

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/rundeck/client/api/model/ExecutionMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.rundeck.client.api.model;

/**
* @author greg
* @since 8/15/17
*/
public enum ExecutionMode {
active,
passive
}
17 changes: 17 additions & 0 deletions src/main/java/org/rundeck/client/api/model/SystemMode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/rundeck/client/tool/commands/RDSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,8 +42,9 @@ public RDSystem(final RdApp client) {

@Override
public List<Object> getSubCommands() {
return Collections.singletonList(
new ACLs(this)
return Arrays.asList(
new ACLs(this),
new Mode(this)
);
}

Expand Down
106 changes: 106 additions & 0 deletions src/main/java/org/rundeck/client/tool/commands/system/Mode.java
Original file line number Diff line number Diff line change
@@ -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<RundeckApi, Call<SystemMode>> operation,
final Client<RundeckApi> 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());
}

}

0 comments on commit 87f9892

Please sign in to comment.