Skip to content

Commit

Permalink
Merge pull request #129 from rundeck/issue/128
Browse files Browse the repository at this point in the history
fix #128 api calls not auto downgrading
  • Loading branch information
gschueler authored Oct 23, 2017
2 parents 5fa067a + be7c02a commit 0b4f257
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 97 deletions.
18 changes: 8 additions & 10 deletions src/main/java/org/rundeck/client/tool/commands/Adhoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.rundeck.client.tool.options.AdhocBaseOptions;
import org.rundeck.client.util.Quoting;
import org.rundeck.client.util.Util;
import retrofit2.Call;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -58,7 +57,7 @@ public Adhoc(final RdApp client) {

@Command(isSolo = true, isDefault = true)
public boolean adhoc(AdhocOptions options, CommandOutput output) throws IOException, InputError {
Call<AdhocResponse> adhocResponseCall;
AdhocResponse adhocResponse;

String project = projectOrEnv(options);
if (options.isScriptFile() || options.isStdin()) {
Expand Down Expand Up @@ -90,7 +89,7 @@ public boolean adhoc(AdhocOptions options, CommandOutput output) throws IOExcept
filename = "script.sh";
}

adhocResponseCall = getClient().getService().runScript(
adhocResponse = apiCall(api -> api.runScript(
project,
MultipartBody.Part.createFormData("scriptFile", filename, scriptFileBody),
options.getThreadcount(),
Expand All @@ -100,9 +99,9 @@ public boolean adhoc(AdhocOptions options, CommandOutput output) throws IOExcept
false,
null,
options.getFilter()
);
));
} else if (options.isUrl()) {
adhocResponseCall = getClient().getService().runUrl(
adhocResponse = apiCall(api -> api.runUrl(
project,
options.getUrl(),
options.getThreadcount(),
Expand All @@ -112,21 +111,20 @@ public boolean adhoc(AdhocOptions options, CommandOutput output) throws IOExcept
false,
null,
options.getFilter()
);
));
} else if (options.getCommandString() != null && options.getCommandString().size() > 0) {
//command
adhocResponseCall = getClient().getService().runCommand(
adhocResponse = apiCall(api -> api.runCommand(
project,
Quoting.joinStringQuoted(options.getCommandString()),
options.getThreadcount(),
options.isKeepgoing(),
options.getFilter()
);
));
} else {
throw new InputError("-s, -u, or -- command string, was expected");
}

AdhocResponse adhocResponse = getClient().checkError(adhocResponseCall);

Execution execution = apiCall(api -> api.getExecution(adhocResponse.execution.getId()));
if (options.isFollow()) {
Expand All @@ -138,7 +136,7 @@ public boolean adhoc(AdhocOptions options, CommandOutput output) throws IOExcept
Executions.outputExecutionList(options, output, Collections.singletonList(execution), getAppConfig());
}

return Executions.maybeFollow(getClient(), options, adhocResponse.execution.getId(), output);
return Executions.maybeFollow(getRdApp(), options, adhocResponse.execution.getId(), output);
}

}
45 changes: 45 additions & 0 deletions src/main/java/org/rundeck/client/tool/commands/AppCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.rundeck.client.util.Client;
import org.rundeck.client.util.ServiceClient;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.function.Function;
Expand Down Expand Up @@ -61,6 +62,21 @@ public <T> T apiCall(Function<RundeckApi, Call<T>> func) throws InputError, IOEx
return apiCallDowngradable(rdApp, func);
}

/**
* Perform a downgradable API call
*
* @param func function
* @param <T> result type
*
* @return result
*
* @throws InputError on error
* @throws IOException on error
*/
public <T> Response<T> apiResponse(Function<RundeckApi, Call<T>> func) throws InputError, IOException {
return apiResponseDowngradable(rdApp, func);
}

/**
* Perform API call with a client
*
Expand Down Expand Up @@ -110,6 +126,35 @@ public static <T> T apiCallDowngradable(
return rdApp.getClient(downgrade.getSupportedVersion()).apiCall(func);
}
}
/**
* Perform a downgradable api call
*
* @param rdApp app
* @param func function
* @param <T> result type
*
* @return result
*
* @throws InputError on error
* @throws IOException on error
*/
public static <T> Response<T> apiResponseDowngradable(
final RdApp rdApp,
final Function<RundeckApi, Call<T>> func
)
throws InputError, IOException
{
try {
return rdApp.getClient().apiResponseDowngradable(func);
} catch (Client.UnsupportedVersionDowngrade downgrade) {
//downgrade to supported version and try again
rdApp.versionDowngradeWarning(
downgrade.getRequestedVersion(),
downgrade.getSupportedVersion()
);
return rdApp.getClient(downgrade.getSupportedVersion()).apiResponse(func);
}
}

/**
* @param options project options
Expand Down
43 changes: 21 additions & 22 deletions src/main/java/org/rundeck/client/tool/commands/Executions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.rundeck.client.tool.options.*;
import org.rundeck.client.util.Format;
import org.rundeck.client.util.ServiceClient;
import retrofit2.Call;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -95,8 +94,8 @@ public boolean follow(Follow options, CommandOutput out) throws IOException, Inp

int max = 500;

Call<ExecOutput> output = startFollowOutput(
getClient(),
ExecOutput output = startFollowOutput(
getRdApp(),
max,
options.isRestart(),
options.getId(),
Expand All @@ -119,21 +118,21 @@ public boolean follow(Follow options, CommandOutput out) throws IOException, Inp
}


public static Call<ExecOutput> startFollowOutput(
final ServiceClient<RundeckApi> serviceClient,
public static ExecOutput startFollowOutput(
final RdApp rdApp,
final long max,
final boolean restart,
final String id,
final long tail,
final boolean compacted
)
) throws IOException, InputError
{

Call<ExecOutput> out;
ExecOutput out;
if (restart) {
out = serviceClient.getService().getOutput(id, 0L, 0L, max, compacted);
out = apiCallDowngradable(rdApp, api -> api.getOutput(id, 0L, 0L, max, compacted));
} else {
out = serviceClient.getService().getOutput(id, tail);
out = apiCallDowngradable(rdApp, api -> api.getOutput(id, tail));
}
return out;
}
Expand All @@ -154,7 +153,7 @@ public static Call<ExecOutput> startFollowOutput(
*/
public static boolean followOutput(
final ServiceClient<RundeckApi> serviceClient,
final Call<ExecOutput> output,
final ExecOutput output,
final boolean progress,
final boolean quiet,
final String id,
Expand Down Expand Up @@ -195,7 +194,7 @@ public static boolean followOutput(
*/
public static boolean followOutput(
final ServiceClient<RundeckApi> serviceClient,
final Call<ExecOutput> output,
final ExecOutput output,
final String id,
long max,
final boolean compacted,
Expand All @@ -205,23 +204,23 @@ public static boolean followOutput(
{
boolean done = false;
String status = null;
Call<ExecOutput> callOutput = output;
ExecOutput execOutput = output;
while (!done) {
ExecOutput execOutput = serviceClient.checkError(callOutput);
receiver.accept(execOutput.decompactEntries());
status = execOutput.execState;
done = execOutput.execCompleted && execOutput.completed;
if (!done) {
if (!waitFunc.getAsBoolean()){
break;
}
callOutput = serviceClient.getService().getOutput(
final ExecOutput passOutput = execOutput;
execOutput = serviceClient.apiCall(api -> api.getOutput(
id,
execOutput.offset,
execOutput.lastModified,
passOutput.offset,
passOutput.lastModified,
max,
compacted
);
));
}
}
return "succeeded".equals(status);
Expand Down Expand Up @@ -526,25 +525,25 @@ public boolean deletebulk(BulkDeleteCmd options, CommandOutput out) throws IOExc
}

public static boolean maybeFollow(
final ServiceClient<RundeckApi> serviceClient,
final RdApp rdApp,
final FollowOptions options,
final String id,
CommandOutput output
) throws IOException
) throws IOException, InputError
{
if (!options.isFollow()) {
return true;
}
Call<ExecOutput> execOutputCall = startFollowOutput(
serviceClient,
ExecOutput execOutputCall = startFollowOutput(
rdApp,
500,
true,
id,
0,
false
);
return followOutput(
serviceClient,
rdApp.getClient(),
execOutputCall,
options.isProgress(),
options.isQuiet(),
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/org/rundeck/client/tool/commands/Jobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,23 @@ public void list(ListOpts options, CommandOutput output) throws IOException, Inp
String project = projectOrEnv(options);
if (options.isFile()) {
//write response to file instead of parsing it
Call<ResponseBody> responseCall;
ResponseBody body;
if (options.isIdlist()) {
responseCall = getClient().getService().exportJobs(
body = apiCall(api -> api.exportJobs(
project,
options.getIdlist(),
options.getFormat()
);
));
} else {
responseCall = getClient().getService().exportJobs(
body = apiCall(api -> api.exportJobs(
project,
options.getJob(),
options.getGroup(),
options.getJobExact(),
options.getGroupExact(),
options.getFormat()
);
));
}
ResponseBody body = getClient().checkError(responseCall);
if ((!"yaml".equals(options.getFormat()) ||
!ServiceClient.hasAnyMediaType(body, Client.MEDIA_TYPE_YAML, Client.MEDIA_TYPE_TEXT_YAML)) &&
!ServiceClient.hasAnyMediaType(body, Client.MEDIA_TYPE_XML, Client.MEDIA_TYPE_TEXT_XML)) {
Expand All @@ -238,19 +237,18 @@ public void list(ListOpts options, CommandOutput output) throws IOException, Inp
}
}
} else {
Call<List<JobItem>> listCall;
List<JobItem> body;
if (options.isIdlist()) {
listCall = getClient().getService().listJobs(project, options.getIdlist());
body = apiCall(api -> api.listJobs(project, options.getIdlist()));
} else {
listCall = getClient().getService().listJobs(
body = apiCall(api -> api.listJobs(
project,
options.getJob(),
options.getGroup(),
options.getJobExact(),
options.getGroupExact()
);
));
}
List<JobItem> body = getClient().checkError(listCall);
if (!options.isOutputFormat()) {
output.info(String.format("%d Jobs in project %s%n", body.size(), project));
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/rundeck/client/tool/commands/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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.Execution;
import org.rundeck.client.api.model.JobFileUploadResult;
import org.rundeck.client.api.model.JobItem;
Expand All @@ -30,8 +29,6 @@
import org.rundeck.client.tool.options.RunBaseOptions;
import org.rundeck.client.util.Format;
import org.rundeck.client.util.Quoting;
import org.rundeck.client.util.ServiceClient;
import retrofit2.Call;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -64,7 +61,7 @@ public boolean run(RunBaseOptions options, CommandOutput out) throws IOException
if (null == jobId) {
return false;
}
Call<Execution> executionListCall;
Execution execution;
Date runat = null;
if (getClient().getApiVersion() >= 18) {
JobRun request = new JobRun();
Expand Down Expand Up @@ -154,17 +151,16 @@ public boolean run(RunBaseOptions options, CommandOutput out) throws IOException
Format.date(runat, "yyyy-MM-dd'T'HH:mm:ssXX")
));
}
executionListCall = getClient().getService().runJob(jobId, request);
execution = apiCall(api -> api.runJob(jobId, request));
} else {
executionListCall = getClient().getService().runJob(
execution = apiCall(api -> api.runJob(
jobId,
Quoting.joinStringQuoted(options.getCommandString()),
options.getLoglevel(),
options.getFilter(),
options.getUser()
);
));
}
Execution execution = getClient().checkError(executionListCall);
String started = runat != null ? "scheduled" : "started";
out.info(String.format("Execution %s: %s%n", started, execution.toBasicString()));

Expand All @@ -182,7 +178,7 @@ public boolean run(RunBaseOptions options, CommandOutput out) throws IOException
}
out.info("Started.");
}
return Executions.maybeFollow(getClient(), options, execution.getId(), out);
return Executions.maybeFollow(getRdApp(), options, execution.getId(), out);
}

/**
Expand Down
Loading

0 comments on commit 0b4f257

Please sign in to comment.