Repository: aries-containers Updated Branches: refs/heads/master 8d1fcb5e0 -> f912dc052
Very initial Marathon binding implementation. Project: http://git-wip-us.apache.org/repos/asf/aries-containers/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-containers/commit/f912dc05 Tree: http://git-wip-us.apache.org/repos/asf/aries-containers/tree/f912dc05 Diff: http://git-wip-us.apache.org/repos/asf/aries-containers/diff/f912dc05 Branch: refs/heads/master Commit: f912dc0523c5af1a9982b7352f75e1d8afb2ef2d Parents: 8d1fcb5 Author: David Bosschaert <[email protected]> Authored: Fri May 26 14:13:26 2017 +0100 Committer: David Bosschaert <[email protected]> Committed: Fri May 26 14:13:26 2017 +0100 ---------------------------------------------------------------------- containers-marathon/pom.xml | 93 +++++++++++++ .../containers/marathon/impl/Activator.java | 43 ++++++ .../impl/MarathonConfigManagedService.java | 69 ++++++++++ .../marathon/impl/MarathonContainerFactory.java | 130 +++++++++++++++++++ .../containers/marathon/impl/ServiceImpl.java | 76 +++++++++++ pom.xml | 1 + 6 files changed, 412 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/containers-marathon/pom.xml ---------------------------------------------------------------------- diff --git a/containers-marathon/pom.xml b/containers-marathon/pom.xml new file mode 100644 index 0000000..19bb5c7 --- /dev/null +++ b/containers-marathon/pom.xml @@ -0,0 +1,93 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.aries.containers</groupId> + <artifactId>org.apache.aries.containers.parent</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../containers-parent</relativePath> + </parent> + + <artifactId>org.apache.aries.containers.marathon</artifactId> + <packaging>jar</packaging> + <name>Apache Aries Containers impl for use with Marathon</name> + + <dependencies> + <dependency> + <groupId>com.mesosphere</groupId> + <artifactId>marathon-client</artifactId> + <version>0.5.0</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.core</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.cmpn</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>org.apache.aries.containers.api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + + <!-- + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.utils</artifactId> + <version>1.10.1-SNAPSHOT</version> + </dependency> + --> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>biz.aQute.bnd</groupId> + <artifactId>bnd-maven-plugin</artifactId> + <version>3.3.0</version> + <executions> + <execution> + <goals> + <goal>bnd-process</goal> + </goals> + </execution> + </executions> + <configuration> + <bnd><![CDATA[ + Bundle-Activator: org.apache.aries.containers.marathon.impl.Activator + ]]></bnd> + </configuration> + </plugin> + </plugins> + </build> +</project> + http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/Activator.java ---------------------------------------------------------------------- diff --git a/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/Activator.java b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/Activator.java new file mode 100644 index 0000000..a6e68fa --- /dev/null +++ b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/Activator.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers.marathon.impl; + +import java.util.Dictionary; +import java.util.Hashtable; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.service.cm.ManagedService; + +public class Activator implements BundleActivator { + @Override + public void start(BundleContext context) throws Exception { + Dictionary<String, Object> props = new Hashtable<>(); + props.put(Constants.SERVICE_PID, "org.apache.aries.containers.marathon"); + context.registerService(ManagedService.class, + new MarathonConfigManagedService(context), props); + + } + + @Override + public void stop(BundleContext context) throws Exception { + // Nothing to do + } +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonConfigManagedService.java ---------------------------------------------------------------------- diff --git a/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonConfigManagedService.java b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonConfigManagedService.java new file mode 100644 index 0000000..e8e0f5f --- /dev/null +++ b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonConfigManagedService.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers.marathon.impl; + +import java.util.Dictionary; +import java.util.Hashtable; + +import org.apache.aries.containers.ContainerFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MarathonConfigManagedService implements ManagedService { + private static final Logger LOG = LoggerFactory.getLogger(MarathonConfigManagedService.class); + + private final BundleContext bundleContext; + volatile String marathonURL; + volatile ServiceRegistration<ContainerFactory> reg; + + MarathonConfigManagedService(BundleContext bc) { + bundleContext = bc; + } + + @Override + public void updated(Dictionary<String, ?> properties) throws ConfigurationException { + Object newURL = properties.get("marathon.url"); + if (!(newURL instanceof String)) { + LOG.error("marathon.url should be a String property {} - ignoring configuration", properties); + return; + } + + String marURL = (String) newURL; + marURL = marURL.trim(); + if (marURL.equals(marathonURL)) { + // Configuration didn't change + return; + } + + // The configuration has changed, unregister previous service + if (reg != null) + reg.unregister(); + + marathonURL = marURL; + ContainerFactory cf = new MarathonContainerFactory(marathonURL); + + Dictionary<String, Object> props = new Hashtable<>(); + props.put(ContainerFactory.BINDING, "marathon"); + reg = bundleContext.registerService(ContainerFactory.class, cf, props); + } +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonContainerFactory.java ---------------------------------------------------------------------- diff --git a/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonContainerFactory.java b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonContainerFactory.java new file mode 100644 index 0000000..c8129be --- /dev/null +++ b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonContainerFactory.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers.marathon.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import org.apache.aries.containers.ContainerFactory; +import org.apache.aries.containers.Service; +import org.apache.aries.containers.ServiceConfig; + +import mesosphere.marathon.client.Marathon; +import mesosphere.marathon.client.MarathonClient; +import mesosphere.marathon.client.model.v2.App; +import mesosphere.marathon.client.model.v2.Container; +import mesosphere.marathon.client.model.v2.Docker; +import mesosphere.marathon.client.model.v2.GetAppsResponse; +import mesosphere.marathon.client.model.v2.Port; + +public class MarathonContainerFactory implements ContainerFactory { + private static final String SERVICE_NAME = "org.apache.aries.containers.service.name"; + + private final Marathon marathonClient; + + public MarathonContainerFactory(String marathonURL) { + marathonClient = MarathonClient.getInstance(marathonURL); + } + + @Override + public Service getService(ServiceConfig config) throws Exception { + // TODO get existing service + + App app = new App(); + app.setId(config.getServiceName()); + app.setCpus(config.getRequestedCpuUnits()); + app.setMem(config.getRequestedMemory()); + app.setInstances(config.getRequestedInstances()); + app.setEnv(Collections.unmodifiableMap(config.getEnvVars())); + + StringBuilder cmd = new StringBuilder(); + if (config.getEntryPoint() != null) { + // TODO is this right? + cmd.append(config.getEntryPoint()); + } + + if (config.getCommandLine().length > 0) { + for (String c : config.getCommandLine()) { + if (cmd.length() > 0) + cmd.append(' '); + + if (c.contains(" ")) + c = "'" + c + "'"; + + cmd.append(c); + } + } + if (cmd.length() > 0) + app.setCmd(cmd.toString()); + + Docker docker = new Docker(); + docker.setImage(config.getContainerImage()); + docker.setNetwork("BRIDGE"); // TODO is this correct? + List<Port> ports = new ArrayList<>(); + for (int p : config.getContainerPorts()) { + Port port = new Port(); + port.setContainerPort(p); + ports.add(port); + } + docker.setPortMappings(ports); + + Container container = new Container(); + container.setType("DOCKER"); + container.setDocker(docker); + + app.setContainer(container); + app.addLabel(SERVICE_NAME, config.getServiceName()); + + App res = marathonClient.createApp(app); + + return createServiceFromApp(res, config); + } + + private Service createServiceFromApp(App app, ServiceConfig cfg) { + // TODO make this check more thorough + if (!cfg.getServiceName().equals(app.getLabels().get(SERVICE_NAME))) + throw new IllegalStateException("Application and configuration don't match"); + + ServiceImpl svc = new ServiceImpl(marathonClient, app, cfg); + return svc; + } + + @Override + public Set<String> listServices() throws Exception { + GetAppsResponse apps = marathonClient.getApps(); + return Collections.emptySet(); +// apps.getApps().stream().filter(a -> a).map(mapper) + + /* + return apps.getApps().stream(). + filter(a -> { + Map<String, String> labels = a.getLabels(); + if (labels != null) + return MARK_LABEL_VALUE.equals(a.getLabels().get(MARK_LABEL_KEY)); + else + return false; + }). + map(a -> marathonIdToGroupName(a.getId())). + collect(Collectors.toSet()); + */ + } + +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/ServiceImpl.java ---------------------------------------------------------------------- diff --git a/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/ServiceImpl.java b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/ServiceImpl.java new file mode 100644 index 0000000..d9ef851 --- /dev/null +++ b/containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/ServiceImpl.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers.marathon.impl; + +import java.util.Collections; +import java.util.List; + +import org.apache.aries.containers.Container; +import org.apache.aries.containers.Service; +import org.apache.aries.containers.ServiceConfig; + +import mesosphere.marathon.client.Marathon; +import mesosphere.marathon.client.model.v2.App; + +class ServiceImpl implements Service { + private final ServiceConfig configuration; + private final String marathonAppID; + private final Marathon marathonClient; + + public ServiceImpl(Marathon marathon, App app, ServiceConfig cfg) { + marathonClient = marathon; + marathonAppID = app.getId(); + configuration = cfg; + } + + @Override + public void destroy() { + marathonClient.deleteApp(marathonAppID); + } + + @Override + public int getActualInstanceCount() { + return marathonClient.getApp(marathonAppID).getApp().getInstances(); + } + + @Override + public ServiceConfig getConfiguration() { + return configuration; + } + + @Override + public List<Container> listContainers() { + // TODO Auto-generated method stub + return Collections.emptyList(); + } + + @Override + public void setInstanceCount(int count) { + App curApp = marathonClient.getApp(marathonAppID).getApp(); + curApp.setInstances(count); + marathonClient.updateApp(marathonAppID, curApp, true); + } + + @Override + public void refresh() { + // TODO Auto-generated method stub + + } + +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/f912dc05/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 881f9c4..2e08092 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ <module>containers-parent</module> <module>containers-api</module> <module>containers-docker-local</module> + <module>containers-marathon</module> </modules> </project>
