diff --git a/minimesos/build.gradle b/minimesos/build.gradle index b927c421..98a9022e 100644 --- a/minimesos/build.gradle +++ b/minimesos/build.gradle @@ -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' diff --git a/minimesos/src/main/java/com/containersol/minimesos/marathon/MarathonContainer.java b/minimesos/src/main/java/com/containersol/minimesos/marathon/MarathonContainer.java index 43b11c4d..78abbc19 100644 --- a/minimesos/src/main/java/com/containersol/minimesos/marathon/MarathonContainer.java +++ b/minimesos/src/main/java/com/containersol/minimesos/marathon/MarathonContainer.java @@ -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; @@ -106,6 +110,15 @@ 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 * @@ -113,9 +126,15 @@ protected CreateContainerCmd dockerCommand() { */ @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())); } /** @@ -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 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; } /**