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

UCR Support #15

Merged
merged 10 commits into from
Aug 28, 2017
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.idea
target
.dcos-token
*.tmp
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -27,7 +29,16 @@
<version>3.2</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>net.iharder</groupId>
<artifactId>base64</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down Expand Up @@ -55,6 +66,11 @@
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>

<build>
Expand Down
87 changes: 57 additions & 30 deletions samples/spring-boot-sample/application.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
{
"id":"dcos-maven-spring-sample",
"cpus":1,
"mem":512,
"ports":[
0
{
"id": "/springboot-demo",
"cmd": "export JAVA_HOME=`pwd`/jre1.8.0_121 && export PATH=\\\"$JAVA_HOME/bin:$PATH\\\" && $JAVA_HOME/bin/java -jar <FILENAME>",
"instances": 1,
"cpus": 0.1,
"mem": 512,
"disk": 0,
"gpus": 0,
"constraints": [],
"fetch": [
{
"uri": "http://<NEXUS_URL>/repository/dcos/<FILENAME>",
"extract": true,
"executable": false,
"cache": false
},
{
"uri": "https://downloads.mesosphere.com/java/jre-8u121-linux-x64.tar.gz",
"extract": true,
"executable": false,
"cache": false
}
],
"instances":1,
"container":{
"type":"DOCKER",
"docker":{
"image":"unterstein/dcos-maven-spring-sample:latest",
"network":"BRIDGE",
"forcePullImage":true,
"portMappings":[
{
"hostPort":0,
"containerPort":8080,
"protocol":"tcp"
}
]
"storeUrls": [],
"backoffSeconds": 1,
"backoffFactor": 1.15,
"maxLaunchDelaySeconds": 3600,
"healthChecks": [
{
"gracePeriodSeconds": 300,
"intervalSeconds": 60,
"timeoutSeconds": 20,
"maxConsecutiveFailures": 3,
"portIndex": 0,
"path": "/",
"protocol": "MESOS_HTTP",
"delaySeconds": 15
}
],
"readinessChecks": [],
"dependencies": [],
"upgradeStrategy": {
"minimumHealthCapacity": 0,
"maximumOverCapacity": 0
},
"unreachableStrategy": {
"inactiveAfterSeconds": 300,
"expungeAfterSeconds": 600
},
"healthChecks":[
{
"protocol":"HTTP",
"path":"/",
"portIndex":0,
"timeoutSeconds":10,
"gracePeriodSeconds":10,
"intervalSeconds":2,
"maxConsecutiveFailures":10
"killSelection": "YOUNGEST_FIRST",
"acceptedResourceRoles": [
"slave_public"
],
"portDefinitions": [
{
"port": 8080,
"protocol": "tcp"
}
]
],
"requirePorts": true
}

36 changes: 4 additions & 32 deletions samples/spring-boot-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.dcos</groupId>
<artifactId>mvn-plugin-spring-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mvn-plugin-spring-sample</name>
Expand Down Expand Up @@ -37,47 +37,19 @@
</dependency>
</dependencies>

<pluginRepositories>
<pluginRepository>
<id>mesosphere-repo</id>
<name>Mesosphere Repo</name>
<url>http://downloads.mesosphere.io/maven</url>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<serverId>docker-hub</serverId>
<imageName>unterstein/dcos-maven-spring-sample</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<exposes>
<expose>8080</expose>
</exposes>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>dcos</groupId>
<artifactId>dcos-maven-plugin</artifactId>
<version>0.3</version>
<version>0.4-SNAPSHOT</version>
<configuration>
<dcosUrl>http://junterste-elasticl-792mfv4rl50j-59247683.us-west-2.elb.amazonaws.com/</dcosUrl>
<dcosUrl>https://joerg-a6u-elasticl-u301awdkkf3s-514314542.us-west-2.elb.amazonaws.com/</dcosUrl>
<nexusUrl>35.163.44.101:20684</nexusUrl>
<ignoreSslCertificate>true</ignoreSslCertificate>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MvnPluginSpringSampleApplication {

@RequestMapping("/")
public String hi() {
return "Hi!";
return "Hi OSS-Team and community!";
}

public static void main(String[] args) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/dcos/AbstractDcosMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.Map;
Expand All @@ -19,6 +20,22 @@ abstract class AbstractDcosMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}/app-definition.json", property = "legacyAppDefinition", required = true, readonly = true)
File legacyAppDefinitionFile;

// Required if you want to push to artifact store
@Parameter(defaultValue = "EMPTY", property = "nexusUrl", required = false)
String nexusUrl;

// Required if you want to push to artifact store
@Parameter(defaultValue = "dcos", property = "nexusRepositoryName", required = false)
String nexusRepositoryName;

// Required if you want to push to artifact store
@Parameter(defaultValue = "admin", property = "nexusUser", required = false)
String nexusUser;

// Required if you want to push to artifact store
@Parameter(defaultValue = "admin123", property = "nexusPassword", required = false)
String nexusPassword;

@Parameter(defaultValue = "${project.basedir}/.dcos-token", property = "dcosTokenFile", required = true)
File dcosTokenFile;

Expand All @@ -31,6 +48,9 @@ abstract class AbstractDcosMojo extends AbstractMojo {
@Parameter(defaultValue = "EMPTY", property = "deployable", required = true)
String deployable;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
MavenProject project;

void logConfiguration() {
Log log = getLog();
log.info("app definition: " + appDefinitionFile);
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/io/dcos/DcosDeployUCRMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.dcos;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
* Mojo to handle `mvn dcos:deployUCR`
*/
@Mojo(name = "deployUCR", defaultPhase = LifecyclePhase.DEPLOY)
public class DcosDeployUCRMojo extends AbstractDcosMojo {

public void execute() throws MojoExecutionException {
CloseableHttpClient client = null;
Path tmpPath = null;
try {
getLog().info("About to execute DC/OS deployUCR");
logConfiguration();
client = DcosPluginHelper.buildClient(ignoreSslCertificate);
Map<String, Object> marathonConfigurationJson = DcosPluginHelper.readJsonFileToMap(appDefinitionFile, legacyAppDefinitionFile);

// this is the main artifact file
File file = project.getArtifact().getFile();

// check if artifact exists
if (file == null) {
throw new RuntimeException("Artifact does not exist. You need to package first. Did you run `mvn package dcos:deployUCR`?");
}

tmpPath = File.createTempFile("ucr-deploy", "json").toPath();
Files.copy(appDefinitionFile.toPath(), tmpPath, REPLACE_EXISTING);

Path path = Paths.get(appDefinitionFile.getAbsolutePath());
Charset charset = StandardCharsets.UTF_8;

String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("<FILENAME>", file.getName());
content = content.replaceAll("<NEXUS_URL>", nexusUrl);

FileOutputStream fooStream = new FileOutputStream(appDefinitionFile, false);
fooStream.write(content.getBytes());
fooStream.close();

getLog().info(tmpPath.toString());

HttpPut put = new HttpPut(buildDcosUrl(marathonConfigurationJson.get("id"), marathonConfigurationJson) + "?force=true'");
put.setHeader("Authorization", "token=" + DcosPluginHelper.readToken(dcosTokenFile));
put.setHeader("Content-Type", "application/json");

put.setEntity(new FileEntity(appDefinitionFile));

CloseableHttpResponse response = client.execute(put);
getLog().info("Response from DC/OS [" + response.getStatusLine().getStatusCode() + "] " + IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
} catch (Exception e) {
getLog().error("Unable to perform deployment", e);
throw new RuntimeException(e);
} finally { // no try-with-resources in this language level -> compatibility
if (client != null) {
try {
client.close();
} catch (IOException e) {
getLog().warn("Unable to close the http client", e);
}
}
// clean up after deployment
if (tmpPath != null && tmpPath.toFile().exists()) {
try {
Files.delete(tmpPath);
} catch (IOException e) {
getLog().warn("Unable to delete temporary app definition", e);
}
}
}
}
}
Loading