Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for the /safeExit method to enable safe shutdown using the API #351

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = io.github.cdancy
version = 1.0.2
version = 1.1.0

artifactoryURL = http://127.0.0.1:8080/artifactory
artifactoryUser = admin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ public static SystemInfo create(String hudsonVersion, String jenkinsVersion, Str
hudsonVersion, jenkinsVersion, jenkinsSession,
instanceIdentity, sshEndpoint, server);
}

}
8 changes: 8 additions & 0 deletions src/main/java/com/cdancy/jenkins/rest/features/SystemApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,13 @@ public interface SystemApi {
@Consumes(MediaType.TEXT_HTML)
@POST
RequestStatus cancelQuietDown();

@Named("system:safe-exit")
@Path("safeExit")
@Fallback(JenkinsFallbacks.RequestStatusOnError.class)
@ResponseParser(RequestStatusParser.class)
@Consumes(MediaType.TEXT_HTML)
@POST
RequestStatus safeExit();

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/
@Singleton
public class SystemInfoFromJenkinsHeaders implements Function<HttpResponse, SystemInfo> {

private static final String DEFAULT_EMPTY_HEADER_STRING = "information not available";


@Override
public SystemInfo apply(HttpResponse response) {
Expand All @@ -41,10 +44,15 @@ public SystemInfo apply(HttpResponse response) {
if (statusCode >= 200 && statusCode < 400) {
return SystemInfo.create(response.getFirstHeaderOrNull("X-Hudson"), response.getFirstHeaderOrNull("X-Jenkins"),
response.getFirstHeaderOrNull("X-Jenkins-Session"),
response.getFirstHeaderOrNull("X-Instance-Identity"), response.getFirstHeaderOrNull("X-SSH-Endpoint"),
response.getFirstHeaderOrNull("Server"), null);
response.getFirstHeaderOrNull("X-Instance-Identity"), getFirstHeaderOrDefault(response, "X-SSH-Endpoint"),
getFirstHeaderOrDefault(response, "Server"), null);
} else {
throw new RuntimeException(response.getStatusLine());
}
}

private String getFirstHeaderOrDefault(HttpResponse response, String header) {
String headerFromResponse = response.getFirstHeaderOrNull(header);
return (headerFromResponse != null) ? headerFromResponse : DEFAULT_EMPTY_HEADER_STRING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,21 @@ public void testCancelQuietDownOnAuthException() throws Exception {
server.shutdown();
}
}

public void testSafeExit() throws Exception {
MockWebServer server = mockWebServer();

server.enqueue(new MockResponse().setResponseCode(200));
JenkinsApi jenkinsApi = api(server.url("/").url());
SystemApi api = jenkinsApi.systemApi();
try {
RequestStatus success = api.safeExit();
assertNotNull(success);
assertTrue(success.value());
assertSentAccept(server, "POST", "/safeExit", MediaType.TEXT_HTML);
} finally {
jenkinsApi.close();
server.shutdown();
}
}
}
Loading