brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [1/2] git commit: Move method to extract Docker port mappings to JcloudsUtil
Date Fri, 30 May 2014 15:25:11 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 18370fe2e -> baad13372


Move method to extract Docker port mappings to JcloudsUtil


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/c5601039
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/c5601039
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/c5601039

Branch: refs/heads/master
Commit: c5601039f3a5cb1aa762a3c235703a2bedf09395
Parents: 25b41f2
Author: Andrew Kennedy <andrew.kennedy@cloudsoftcorp.com>
Authored: Fri May 30 16:15:56 2014 +0100
Committer: Andrew Kennedy <andrew.kennedy@cloudsoftcorp.com>
Committed: Fri May 30 16:15:56 2014 +0100

----------------------------------------------------------------------
 .../location/jclouds/JcloudsLocation.java       | 65 +-------------------
 .../brooklyn/location/jclouds/JcloudsUtil.java  | 58 +++++++++++++++++
 2 files changed, 59 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c5601039/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
index e0c172c..3f658e5 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
@@ -29,11 +29,9 @@ import java.util.regex.Pattern;
 
 import javax.annotation.Nullable;
 
-import org.jclouds.ContextBuilder;
 import org.jclouds.abiquo.compute.options.AbiquoTemplateOptions;
 import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
 import org.jclouds.compute.ComputeService;
-import org.jclouds.compute.ComputeServiceContext;
 import org.jclouds.compute.RunNodesException;
 import org.jclouds.compute.config.AdminAccessConfiguration;
 import org.jclouds.compute.domain.ComputeMetadata;
@@ -49,14 +47,11 @@ import org.jclouds.compute.domain.TemplateBuilder;
 import org.jclouds.compute.domain.TemplateBuilderSpec;
 import org.jclouds.compute.functions.Sha512Crypt;
 import org.jclouds.compute.options.TemplateOptions;
-import org.jclouds.docker.DockerApi;
-import org.jclouds.docker.domain.Container;
 import org.jclouds.domain.Credentials;
 import org.jclouds.domain.LocationScope;
 import org.jclouds.domain.LoginCredentials;
 import org.jclouds.ec2.compute.options.EC2TemplateOptions;
 import org.jclouds.googlecomputeengine.compute.options.GoogleComputeEngineTemplateOptions;
-import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
 import org.jclouds.openstack.nova.v2_0.compute.options.NovaTemplateOptions;
 import org.jclouds.rest.AuthorizationException;
 import org.jclouds.scriptbuilder.domain.Statement;
@@ -67,7 +62,6 @@ import org.jclouds.scriptbuilder.statements.login.AdminAccess;
 import org.jclouds.scriptbuilder.statements.login.ReplaceShadowPasswordEntry;
 import org.jclouds.scriptbuilder.statements.ssh.AuthorizeRSAPublicKeys;
 import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
-import org.jclouds.sshj.config.SshjSshClientModule;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -145,7 +139,6 @@ import com.google.common.collect.Sets.SetView;
 import com.google.common.io.Files;
 import com.google.common.net.HostAndPort;
 import com.google.common.primitives.Ints;
-import com.google.inject.Module;
 
 /**
  * For provisioning and managing VMs in a particular provider/region, using jclouds.
@@ -643,7 +636,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation
im
             }
 
             if ("docker".equals(this.getProvider())) {
-                Map<Integer, Integer> portMappings = getPortMappingsForDocker(sshMachineLocation);
+                Map<Integer, Integer> portMappings = JcloudsUtil.dockerPortMappingsFor(this,
node.getId());
                 PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
                 if (portForwardManager != null) {
                     for(Integer containerPort : portMappings.keySet()) {
@@ -749,62 +742,6 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation
im
             }
             
             throw Exceptions.propagate(e);
-            
-        } finally {
-            //leave it open for reuse
-//            computeService.getContext().close();
-        }
-
-    }
-
-    private Map<Integer, Integer> getPortMappingsForDocker(JcloudsSshMachineLocation
machine) {
-        ComputeServiceContext context = null;
-        try {
-            context = ContextBuilder.newBuilder("docker")
-                    .endpoint(machine.getParent().getEndpoint())
-                    .credentials(getIdentity(), getCredential())
-                    .modules(ImmutableSet.<Module>of(new SLF4JLoggingModule(), new
SshjSshClientModule()))
-                    .build(ComputeServiceContext.class);
-            DockerApi api = context.unwrapApi(DockerApi.class);
-            String containerId = machine.getJcloudsId();
-            Container container = api.getRemoteApi().inspectContainer(containerId);
-            Map<Integer, Integer> portMappings = Maps.newLinkedHashMap();
-            Map<String, List<Map<String, String>>> ports = container.getNetworkSettings().getPorts();
-            LOG.debug("jclouds will use these ports {} (from {}) to provision {}",
-                    new Object[] {ports, machine, this});
-            for (Map.Entry<String, List<Map<String, String>>> entrySet
: ports.entrySet()) {
-                String containerPort = Iterables.get(Splitter.on("/").split(entrySet.getKey()),
0);
-                String hostPort = Iterables.getOnlyElement(Iterables.transform(entrySet.getValue(),
-                        new Function<Map<String, String>, String>() {
-                            @Override
-                            public String apply(Map<String, String> hostIpAndPort)
{
-                                return hostIpAndPort.get("HostPort");
-                            }
-                        }));
-                portMappings.put(Integer.parseInt(containerPort), Integer.parseInt(hostPort));
-            }
-            return portMappings;
-        } finally {
-            if (context != null) {
-                context.close();
-            }
-        }
-    }
-
-    private void mapSecurityGroupRuleToIpTables(ComputeService computeService, NodeMetadata
node,
-            LoginCredentials credentials, String networkInterface, Iterable<Integer>
ports) {
-        for (Integer port : ports) {
-            String insertIptableRule = IptablesCommands.insertIptablesRule(Chain.INPUT, networkInterface,

-                    Protocol.TCP, port, Policy.ACCEPT);
-            Statement statement = Statements.newStatementList(exec(insertIptableRule));
-            ExecResponse response = computeService.runScriptOnNode(node.getId(), statement,
-                    overrideLoginCredentials(credentials).runAsRoot(false));
-            if (response.getExitStatus() != 0) {
-                String msg = String.format("Cannot insert the iptables rule for port %d.
Error: %s", port,
-                        response.getError());
-                LOG.error(msg);
-                throw new RuntimeException(msg);
-            }
         }
     }
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c5601039/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
index fde3f82..0374be5 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
@@ -4,12 +4,14 @@ import static brooklyn.util.GroovyJavaMethods.truth;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
 import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
+import static org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials;
 import static org.jclouds.compute.util.ComputeServiceUtils.execHttpResponse;
 import static org.jclouds.scriptbuilder.domain.Statements.*;
 
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
@@ -27,6 +29,8 @@ import org.jclouds.compute.domain.NodeMetadata;
 import org.jclouds.compute.domain.OperatingSystem;
 import org.jclouds.compute.options.RunScriptOptions;
 import org.jclouds.compute.predicates.OperatingSystemPredicates;
+import org.jclouds.docker.DockerApi;
+import org.jclouds.docker.domain.Container;
 import org.jclouds.domain.LoginCredentials;
 import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
 import org.jclouds.ec2.compute.functions.WindowsLoginCredentialsFromEncryptedData;
@@ -46,8 +50,13 @@ import brooklyn.entity.basic.Entities;
 import brooklyn.location.jclouds.config.BrooklynStandardJcloudsGuiceModule;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
+import brooklyn.util.net.Protocol;
+import brooklyn.util.ssh.IptablesCommands;
+import brooklyn.util.ssh.IptablesCommands.Chain;
+import brooklyn.util.ssh.IptablesCommands.Policy;
 
 import com.google.common.base.Charsets;
+import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.base.Splitter;
@@ -337,4 +346,53 @@ public class JcloudsUtil implements JcloudsLocationConfig {
 
         return credentials.getPassword();
     }
+
+    public static Map<Integer, Integer> dockerPortMappingsFor(JcloudsLocation docker,
String containerId) {
+        ComputeServiceContext context = null;
+        try {
+            context = ContextBuilder.newBuilder("docker")
+                    .endpoint(docker.getEndpoint())
+                    .credentials("docker", "docker")
+                    .modules(ImmutableSet.<Module>of(new SLF4JLoggingModule(), new
SshjSshClientModule()))
+                    .build(ComputeServiceContext.class);
+            DockerApi api = context.unwrapApi(DockerApi.class);
+            Container container = api.getRemoteApi().inspectContainer(containerId);
+            Map<Integer, Integer> portMappings = Maps.newLinkedHashMap();
+            Map<String, List<Map<String, String>>> ports = container.getNetworkSettings().getPorts();
+            LOG.debug("Docker will forward these ports {}", ports);
+            for (Map.Entry<String, List<Map<String, String>>> entrySet
: ports.entrySet()) {
+                String containerPort = Iterables.get(Splitter.on("/").split(entrySet.getKey()),
0);
+                String hostPort = Iterables.getOnlyElement(Iterables.transform(entrySet.getValue(),
+                        new Function<Map<String, String>, String>() {
+                            @Override
+                            public String apply(Map<String, String> hostIpAndPort)
{
+                                return hostIpAndPort.get("HostPort");
+                            }
+                        }));
+                portMappings.put(Integer.parseInt(containerPort), Integer.parseInt(hostPort));
+            }
+            return portMappings;
+        } finally {
+            if (context != null) {
+                context.close();
+            }
+        }
+    }
+
+    public static void mapSecurityGroupRuleToIpTables(ComputeService computeService, NodeMetadata
node,
+            LoginCredentials credentials, String networkInterface, Iterable<Integer>
ports) {
+        for (Integer port : ports) {
+            String insertIptableRule = IptablesCommands.insertIptablesRule(Chain.INPUT, networkInterface,

+                    Protocol.TCP, port, Policy.ACCEPT);
+            Statement statement = Statements.newStatementList(exec(insertIptableRule));
+            ExecResponse response = computeService.runScriptOnNode(node.getId(), statement,
+                    overrideLoginCredentials(credentials).runAsRoot(false));
+            if (response.getExitStatus() != 0) {
+                String msg = String.format("Cannot insert the iptables rule for port %d.
Error: %s", port,
+                        response.getError());
+                LOG.error(msg);
+                throw new RuntimeException(msg);
+            }
+        }
+    }
 }


Mime
View raw message