Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #518 from kislotniq/master
Browse files Browse the repository at this point in the history
#438 replaced marathon client code with external depdendency
  • Loading branch information
frankscholten authored Nov 16, 2016
2 parents d83877c + 91d7e01 commit 84a483c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
2 changes: 2 additions & 0 deletions minimesos/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dependencies {
compile 'com.jayway.awaitility:awaitility:1.6.3'
compile 'com.mashape.unirest:unirest-java:1.4.8'
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.mesosphere:marathon-client:0.3.0'
compile 'com.google.code.gson:gson-parent:2.8.0'

compile 'ch.qos.logback:logback-core:1.1.3'
compile 'ch.qos.logback:logback-classic:1.1.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import mesosphere.marathon.client.model.v2.App;
import mesosphere.marathon.client.MarathonClient;
import mesosphere.marathon.client.utils.MarathonException;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -106,16 +110,31 @@ protected CreateContainerCmd dockerCommand() {
.withPortBindings(portBindings);
}

/**
* Returns a Marathon endpoint
*
* @return String endpoint
*/
private String getMarathonEndpoint() {
return getServiceUrl().toString();
}

/**
* Deploys a Marathon app by JSON string
*
* @param marathonJson JSON string
*/
@Override
public void deployApp(String marathonJson) {
String marathonEndpoint = getServiceUrl().toString();
HttpRequestWithBody httpRequest = Unirest.post(marathonEndpoint + END_POINT_EXT);
deployOrUpdateApp(marathonJson, httpRequest, HttpStatus.SC_CREATED);
mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());

try {
marathon.createApp(constructApp(marathonJson));
} catch (MarathonException e) {
throw new MinimesosException("Marathon did not accept the app, error: " + e.toString());
}

LOGGER.debug(String.format("Installing an app on marathon %s", getMarathonEndpoint()));
}

/**
Expand All @@ -125,38 +144,30 @@ public void deployApp(String marathonJson) {
*/
@Override
public void updateApp(String marathonJson) {
String marathonEndpoint = getServiceUrl().toString();
JSONObject marathonObject = new JSONObject(marathonJson);
String id = marathonObject.getString("id");
HttpRequestWithBody httpRequest = Unirest.put(marathonEndpoint + END_POINT_EXT + "/" + id);
deployOrUpdateApp(marathonJson, httpRequest, HttpStatus.SC_OK);
mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());

try {
App app = constructApp(marathonJson);
boolean force = true;
marathon.updateApp(app.getId(), app, force);
} catch (MarathonException e) {
throw new MinimesosException("Marathon could not update the app, error: " + e.toString());
}

LOGGER.debug(String.format("Installing an app on marathon %s", getMarathonEndpoint()));
}

/**
* Deploys or updates a Marathon app by JSON string
* Return App given a JSON string
*
* @param marathonJson JSON string
* @param httpRequest The HTTP request to perform
* @param httpResponseSuccessStatusCode The HTTP status code to expect from the response of a successful operation
* @param appJson JSON string
* @return App object
*/
private void deployOrUpdateApp(String marathonJson, HttpRequestWithBody httpRequest, int httpResponseSuccessStatusCode) {
String marathonEndpoint = getServiceUrl().toString();
String tokenisedJson = replaceTokens(marathonJson);
try {
byte[] app = tokenisedJson.getBytes(Charset.forName("UTF-8"));
HttpResponse<JsonNode> response = httpRequest.header(HEADER_ACCEPT, APPLICATION_JSON).body(app).asJson();
JSONObject deployResponse = response.getBody().getObject();
if (response.getStatus() == httpResponseSuccessStatusCode) {
LOGGER.debug(deployResponse.toString());
} else {
throw new MinimesosException("Marathon did not accept the app: " + deployResponse);
}
} catch (UnirestException e) {
String msg = "Could not deploy app on Marathon at " + marathonEndpoint + " => " + e.getMessage();
LOGGER.error(msg);
throw new MinimesosException(msg, e);
}
LOGGER.debug(String.format("Installing an app on marathon %s", marathonEndpoint));
private App constructApp(String appJson) {
Gson gson = new Gson();
App appObject = gson.fromJson(replaceTokens(appJson), App.class);

return appObject;
}

/**
Expand Down

0 comments on commit 84a483c

Please sign in to comment.