Skip to content

Commit

Permalink
Merge pull request #116 from rundeck/rundeck-cli-115
Browse files Browse the repository at this point in the history
Add execution mode control to rd system
  • Loading branch information
gschueler authored Aug 24, 2017
2 parents 7df2d49 + 63e628f commit 58cfecc
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ repositories {
maven { url "https://jitpack.io" }
}

ext.toolbeltVersion = "0.1.18"
ext.toolbeltVersion = "0.1.19"
ext.toolbeltGroup = "com${toolbeltVersion.contains('SNAPSHOT')?'':'.github'}.simplifyops.cli-toolbelt"

dependencies {
Expand Down
12 changes: 12 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ View system information

acls - Manage System ACLs
info - Print system information and stats
mode - Manage Execution Mode

### system acls

Expand All @@ -239,6 +240,17 @@ Manage System ACLs

Print system information and stats.

### system mode

Manage Execution Mode.


Available commands:

active - Set execution mode Active
info - Show execution mode
passive - Set execution mode Passive

## tokens

Create, and manage tokens
Expand Down
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 58cfecc

Please sign in to comment.