diff --git a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/BaseExtension.java b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/BaseExtension.java similarity index 92% rename from rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/BaseExtension.java rename to rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/BaseExtension.java index 1de24615..5c671cf6 100644 --- a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/BaseExtension.java +++ b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/BaseExtension.java @@ -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; diff --git a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/EnterpriseApi.java b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/EnterpriseApi.java index 55ff2a0f..006e9725 100644 --- a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/EnterpriseApi.java +++ b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/EnterpriseApi.java @@ -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.*; @@ -14,4 +15,13 @@ public interface EnterpriseApi { @Headers("Accept: application/json") @POST("enterprise/license") Call storeLicense(@Body RequestBody contents, @Query("license_agreement") boolean agree); + + + @Headers("Accept: application/json") + @POST("enterprise/cluster/executions/enable") + Call executionModeEnable(@Query("uuid") String uuid); + + @Headers("Accept: application/json") + @POST("enterprise/cluster/executions/disable") + Call executionModeDisable(@Query("uuid") String uuid); } diff --git a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/model/EnterpriseModeResponse.java b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/model/EnterpriseModeResponse.java new file mode 100644 index 00000000..c92ffb0c --- /dev/null +++ b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/api/model/EnterpriseModeResponse.java @@ -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 map = new HashMap<>(); + map.put("status", status); + map.put("mode", getExecutionMode().toString()); + map.put("uuid", uuid); + return map; + } +} diff --git a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/cluster/Mode.java b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/cluster/Mode.java new file mode 100644 index 00000000..8ad86b3e --- /dev/null +++ b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/cluster/Mode.java @@ -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> 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()); + } + +} diff --git a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/License.java b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/License.java index dae1fc4a..8c6d1757 100644 --- a/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/License.java +++ b/rd-cli-enterprise/src/main/java/org/rundeck/client/tool/commands/enterprise/license/License.java @@ -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;