Skip to content

Commit

Permalink
Merge pull request #380 from rundeck/enh/cluster-exec-mode-toggle
Browse files Browse the repository at this point in the history
API41: cluster exec mode toggle
  • Loading branch information
gschueler authored Mar 30, 2022
2 parents 64565bf + 4e31faf commit 40a0067
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.rundeck.client.tool.commands.enterprise.license;
package org.rundeck.client.tool.commands.enterprise;

import org.rundeck.client.tool.commands.enterprise.api.EnterpriseApi;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.rundeck.client.tool.commands.enterprise.api;

import okhttp3.RequestBody;
import org.rundeck.client.tool.commands.enterprise.api.model.EnterpriseModeResponse;
import org.rundeck.client.tool.commands.enterprise.api.model.LicenseResponse;
import org.rundeck.client.tool.commands.enterprise.api.model.LicenseStoreResponse;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.*;

Expand All @@ -14,4 +15,13 @@ public interface EnterpriseApi {
@Headers("Accept: application/json")
@POST("enterprise/license")
Call<LicenseStoreResponse> storeLicense(@Body RequestBody contents, @Query("license_agreement") boolean agree);


@Headers("Accept: application/json")
@POST("enterprise/cluster/executions/enable")
Call<EnterpriseModeResponse> executionModeEnable(@Query("uuid") String uuid);

@Headers("Accept: application/json")
@POST("enterprise/cluster/executions/disable")
Call<EnterpriseModeResponse> executionModeDisable(@Query("uuid") String uuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.rundeck.client.tool.commands.enterprise.api.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.rundeck.client.api.model.SystemMode;
import org.rundeck.client.util.DataOutput;

import java.util.HashMap;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class EnterpriseModeResponse extends SystemMode implements DataOutput {
String status;
String uuid;

@Override
public Map<?, ?> asMap() {
HashMap<String, String> map = new HashMap<>();
map.put("status", status);
map.put("mode", getExecutionMode().toString());
map.put("uuid", uuid);
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.rundeck.client.tool.commands.enterprise.cluster;

import com.lexicalscope.jewel.cli.CommandLineInterface;
import com.lexicalscope.jewel.cli.Option;
import org.rundeck.client.api.model.ExecutionMode;
import org.rundeck.client.tool.InputError;
import org.rundeck.client.tool.commands.enterprise.BaseExtension;
import org.rundeck.client.tool.commands.enterprise.api.EnterpriseApi;
import org.rundeck.client.tool.commands.enterprise.api.model.EnterpriseModeResponse;
import org.rundeck.client.tool.extension.RdTool;
import org.rundeck.toolbelt.Command;
import org.rundeck.toolbelt.CommandOutput;
import org.rundeck.toolbelt.SubCommand;
import retrofit2.Call;

import java.io.IOException;
import java.util.function.BiFunction;

@Command()
@SubCommand(path = {"cluster"}, descriptions = {"Manage Rundeck Enterprise Cluster"})
public class Mode extends BaseExtension {

interface QuietOption {
@Option(shortName = "q", longName = "quiet", description = "Reduce output.")
boolean isQuiet();
}

interface UuidOption {
@Option(shortName = "u", longName = "uuid", description = "Cluster member UUID")
String getUuid();
}

interface BaseOption extends QuietOption, UuidOption {
}

@CommandLineInterface(application = "active")
interface ModeActive extends BaseOption {

}

@Command(description = "Set cluster member execution mode Active")
public boolean active(ModeActive opts, CommandOutput output) throws IOException, InputError {
return changeMode(opts, output, ExecutionMode.active, EnterpriseApi::executionModeEnable);
}

@CommandLineInterface(application = "passive")
interface ModePassive extends BaseOption {

}

@Command(description = "Set cluster member execution mode Passive")
public boolean passive(ModePassive opts, CommandOutput output) throws IOException, InputError {
return changeMode(opts, output, ExecutionMode.passive, EnterpriseApi::executionModeDisable);
}

boolean changeMode(
final BaseOption opts,
final CommandOutput output,
final ExecutionMode expected,
final BiFunction<EnterpriseApi, String, Call<EnterpriseModeResponse>> operation
)
throws InputError, IOException {
RdTool.apiVersionCheck("change cluster member execution mode", 41, getClient().getApiVersion());

if (!opts.isQuiet()) {
output.info(String.format("Setting Execution Mode to %s for cluster member %s...", expected, opts.getUuid()));
}

EnterpriseModeResponse mode = getClient().apiCall((e) -> operation.apply(e, opts.getUuid()));

if (!opts.isQuiet()) {
output.info(String.format("Execution Mode change is %s for cluster member %s:", mode.getStatus(), opts.getUuid()));
output.output(mode.getExecutionMode());
}

return expected.equals(mode.getExecutionMode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lexicalscope.jewel.cli.CommandLineInterface;
import com.lexicalscope.jewel.cli.Option;
import org.rundeck.client.tool.commands.enterprise.BaseExtension;
import org.rundeck.client.tool.commands.enterprise.api.EnterpriseApi;
import org.rundeck.client.tool.commands.enterprise.api.model.LicenseResponse;
import org.rundeck.client.tool.commands.enterprise.api.model.LicenseStoreResponse;
Expand Down

0 comments on commit 40a0067

Please sign in to comment.