org.jclouds.compute.ComputeServiceContext Java Examples
The following examples show how to use
org.jclouds.compute.ComputeServiceContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JCloudsConnector.java From cloudml with GNU Lesser General Public License v3.0 | 6 votes |
public JCloudsConnector(String provider,String login,String secretKey){ journal.log(Level.INFO, ">> Connecting to "+provider+" ..."); Properties overrides = new Properties(); if(provider.equals("aws-ec2")){ // choose only amazon images that are ebs-backed //overrides.setProperty(AWSEC2Constants.PROPERTY_EC2_AMI_OWNERS,"107378836295"); overrides.setProperty(AWSEC2Constants.PROPERTY_EC2_AMI_QUERY, "owner-id=137112412989,107378836295,099720109477;state=available;image-type=machine;root-device-type=ebs"); } overrides.setProperty(PROPERTY_CONNECTION_TIMEOUT, 0 + ""); overrides.setProperty(PROPERTY_SO_TIMEOUT, 0 + ""); overrides.setProperty(PROPERTY_REQUEST_TIMEOUT, 0 + ""); overrides.setProperty(PROPERTY_RETRY_DELAY_START, 0 + ""); Iterable<Module> modules = ImmutableSet.<Module> of( new SshjSshClientModule(), new NullLoggingModule()); ContextBuilder builder = ContextBuilder.newBuilder(provider).credentials(login, secretKey).modules(modules).overrides(overrides); journal.log(Level.INFO, ">> Authenticating ..."); computeContext=builder.buildView(ComputeServiceContext.class); ec2api=builder.buildApi(EC2Api.class); //loadBalancerCtx=builder.buildView(LoadBalancerServiceContext.class); compute=computeContext.getComputeService(); this.provider = provider; }
Example #2
Source File: EC2Iaas.java From attic-stratos with Apache License 2.0 | 6 votes |
@Override public void deleteVolume(String volumeId) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo); if (region == null) { log.fatal("Cannot delete the volume [id]: " + volumeId + " of the [region] : " + region + " of Iaas : " + iaasInfo); return; } ElasticBlockStoreApi blockStoreApi = context.unwrapApi(AWSEC2Api.class).getElasticBlockStoreApiForRegion(region) .get(); blockStoreApi.deleteVolumeInRegion(region, volumeId); log.info("Deletion of Volume [id]: " + volumeId + " was successful. [region] : " + region + " of Iaas : " + iaasInfo); }
Example #3
Source File: NovaNetworkingApi.java From attic-stratos with Apache License 2.0 | 6 votes |
@Override public void releaseAddress(String ip) { ComputeServiceContext context = iaasProvider.getComputeService().getContext(); String region = ComputeServiceBuilderUtil.extractRegion(iaasProvider); NovaApi novaApi = context.unwrapApi(NovaApi.class); FloatingIPApi floatingIPApi = novaApi.getFloatingIPExtensionForZone(region).get(); for (FloatingIP floatingIP : floatingIPApi.list()) { if (floatingIP.getIp().equals(ip)) { floatingIPApi.delete(floatingIP.getId()); break; } } }
Example #4
Source File: CloudStackIaas.java From attic-stratos with Apache License 2.0 | 6 votes |
@Override public boolean isValidZone(String region, String zone) throws InvalidZoneException { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class); ListZonesOptions listZonesOptions = new ListZonesOptions(); listZonesOptions.available(true); Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions); for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) { if (configuredZone.getName().equalsIgnoreCase(zone)) { return true; } } String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType(); log.error(msg); throw new InvalidZoneException(msg); }
Example #5
Source File: CloudStackIaas.java From attic-stratos with Apache License 2.0 | 6 votes |
@Override public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class); SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName); if (sshKeyPair != null) { iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class) .keyPair(sshKeyPair.getName()); log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName()); return true; } log.error("Key-pair is unable to create"); return false; }
Example #6
Source File: OpenstackIaas.java From attic-stratos with Apache License 2.0 | 6 votes |
@Override public synchronized boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) { IaasProvider iaasInfo = getIaasProvider(); String openstackNovaMsg = " Openstack-nova. Region: " + region + " - Name: "; ComputeServiceContext context = iaasInfo.getComputeService().getContext(); NovaApi novaApi = context.unwrapApi(NovaApi.class); KeyPairApi api = novaApi.getKeyPairExtensionForZone(region).get(); KeyPair keyPair = api.createWithPublicKey(keyPairName, publicKey); if (keyPair != null) { iaasInfo.getTemplate().getOptions().as(NovaTemplateOptions.class).keyPairName(keyPair.getName()); log.info(SUCCESSFUL_LOG_LINE + openstackNovaMsg + keyPair.getName()); return true; } log.error(FAILED_LOG_LINE + openstackNovaMsg); return false; }
Example #7
Source File: ComputeServiceBuilderUtil.java From attic-stratos with Apache License 2.0 | 6 votes |
public static ComputeService buildDefaultComputeService(IaasProvider iaasProvider) { Properties properties = new Properties(); // load properties for (Map.Entry<String, String> entry : iaasProvider.getProperties().entrySet()) { properties.put(entry.getKey(), entry.getValue()); } // set modules Iterable<Module> modules = ImmutableSet.<Module>of(new SshjSshClientModule(), new SLF4JLoggingModule(), new EnterpriseConfigurationModule()); // build context ContextBuilder builder = ContextBuilder.newBuilder(iaasProvider.getProvider()) .credentials(iaasProvider.getIdentity(), iaasProvider.getCredential()).modules(modules) .overrides(properties); return builder.buildView(ComputeServiceContext.class).getComputeService(); }
Example #8
Source File: Utils.java From jclouds-examples with Apache License 2.0 | 6 votes |
public static ComputeServiceContext getComputeApiFromCarinaDirectory(String path) throws IOException { // docker.ps1 contains the endpoint String endpoint = "https://" + Files.readFirstLine(new File(joinPath(path, "docker.ps1")), Charset.forName("UTF-8")).split("=")[1].replace("\"", "").substring(6); // enable logging and sshj Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule()); Properties overrides = new Properties(); // disable certificate checking for Carina overrides.setProperty("jclouds.trust-all-certs", "true"); return ContextBuilder.newBuilder("docker") .credentials(joinPath(path, "cert.pem"), joinPath(path, "key.pem")) .modules(modules) .overrides(overrides) .endpoint(endpoint) .buildView(ComputeServiceContext.class); }
Example #9
Source File: CloudServersPublish.java From jclouds-examples with Apache License 2.0 | 6 votes |
public CloudServersPublish(List<String> args) { String username = args.get(0); String apiKey = args.get(1); numServers = args.size() == 3 ? Integer.valueOf(args.get(2)) : 1; Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule()); // These properties control how often jclouds polls for a status update Properties overrides = new Properties(); overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS); overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS); ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .overrides(overrides) .modules(modules) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); }
Example #10
Source File: CreateServerWithKeyPair.java From jclouds-examples with Apache License 2.0 | 6 votes |
public CreateServerWithKeyPair(String username, String apiKey) { Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule()); // These properties control how often jclouds polls for a status update Properties overrides = new Properties(); overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS); overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS); ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .overrides(overrides) .modules(modules) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); novaApi = context.unwrapApi(NovaApi.class); }
Example #11
Source File: MainApp.java From jclouds-examples with Apache License 2.0 | 6 votes |
private static ComputeService initComputeService(final String provider, final String identity, final String credential) { // example of specific properties, in this case optimizing image list to // only amazon supplied Properties properties = new Properties(); long scriptTimeout = TimeUnit.MILLISECONDS.convert(20, TimeUnit.MINUTES); properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + ""); // example of injecting a ssh implementation Iterable<Module> modules = ImmutableSet.<Module>of(new SshjSshClientModule(), new SLF4JLoggingModule(), new EnterpriseConfigurationModule()); ContextBuilder builder = ContextBuilder.newBuilder(provider).credentials(identity, credential).modules(modules) .overrides(properties); System.out.printf(">> initializing %s%n", builder.getApiMetadata()); return builder.buildView(ComputeServiceContext.class).getComputeService(); }
Example #12
Source File: CreateVolumeAndAttach.java From jclouds-examples with Apache License 2.0 | 6 votes |
public CreateVolumeAndAttach(String username, String apiKey) { // The provider configures jclouds To use the Rackspace Cloud (US) // To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudservers-uk" String provider = System.getProperty("provider.cs", "rackspace-cloudservers-us"); // These properties control how often jclouds polls for a status udpate Properties overrides = new Properties(); overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS); overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS); Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule()); ComputeServiceContext context = ContextBuilder.newBuilder(provider) .credentials(username, apiKey) .modules(modules) .overrides(overrides) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); novaApi = context.unwrapApi(NovaApi.class); volumeAttachmentApi = novaApi.getVolumeAttachmentApi(REGION).get(); cinderApi = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildApi(CinderApi.class); volumeApi = cinderApi.getVolumeApi(REGION); }
Example #13
Source File: DetachVolume.java From jclouds-examples with Apache License 2.0 | 6 votes |
public DetachVolume(String username, String apiKey) { // The provider configures jclouds To use the Rackspace Cloud (US) // To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudservers-uk" String provider = System.getProperty("provider.cs", "rackspace-cloudservers-us"); Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule()); ComputeServiceContext context = ContextBuilder.newBuilder(provider) .credentials(username, apiKey) .modules(modules) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); NovaApi novaApi = context.unwrapApi(NovaApi.class); serverApi = novaApi.getServerApi(REGION); volumeAttachmentApi = novaApi.getVolumeAttachmentApi(REGION).get(); cinderApi = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildApi(CinderApi.class); volumeApi = cinderApi.getVolumeApi(REGION); }
Example #14
Source File: EC2Iaas.java From attic-stratos with Apache License 2.0 | 5 votes |
@Override public synchronized void releaseAddress(String ip) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); ElasticIPAddressApi elasticIPAddressApi = context.unwrapApi(AWSEC2Api.class).getElasticIPAddressApi().get(); String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo); elasticIPAddressApi.disassociateAddressInRegion(region, ip); elasticIPAddressApi.releaseAddressInRegion(region, ip); }
Example #15
Source File: EC2Iaas.java From attic-stratos with Apache License 2.0 | 5 votes |
@Override public void detachVolume(String instanceId, String volumeId) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo); if (region == null) { log.fatal("Cannot detach the volume [id]: " + volumeId + " from the instance [id]: " + instanceId + " of the [region] : " + region + " of Iaas : " + iaasInfo); return; } ElasticBlockStoreApi blockStoreApi = context.unwrapApi(AWSEC2Api.class).getElasticBlockStoreApiForRegion(region) .get(); Set<Volume> volumeDescriptions = blockStoreApi.describeVolumesInRegion(region, volumeId); Iterator<Volume> it = volumeDescriptions.iterator(); while (it.hasNext()) { Volume.Status status = it.next().getStatus(); if (status == Volume.Status.AVAILABLE) { log.warn(String.format("Volume %s is already in AVAILABLE state. Volume seems to be detached somehow", volumeId)); return; } } blockStoreApi .detachVolumeInRegion(region, volumeId, true, DetachVolumeOptions.Builder.fromInstance(instanceId)); log.info("Detachment of Volume [id]: " + volumeId + " from instance [id]: " + instanceId + " was successful. [region] : " + region + " of Iaas : " + iaasInfo); }
Example #16
Source File: NovaContext.java From karamel with Apache License 2.0 | 5 votes |
public NovaContext(NovaCredentials credentials, ContextBuilder builder) { this.novaCredentials = credentials; ComputeServiceContext context = builder.credentials(credentials.getAccountName(),credentials.getAccountPass()) .endpoint(credentials.getEndpoint()) .buildView(ComputeServiceContext.class); this.computeService = context.getComputeService(); this.novaApi = computeService.getContext().unwrapApi(NovaApi.class); this.securityGroupApi = novaApi.getSecurityGroupApi(credentials.getRegion()).get(); this.keyPairApi = novaApi.getKeyPairApi(credentials.getRegion()).get(); }
Example #17
Source File: OpenStackConnector.java From cloudml with GNU Lesser General Public License v3.0 | 5 votes |
public OpenStackConnector(String endPoint,String provider,String login,String secretKey){ this.endpoint=endPoint; journal.log(Level.INFO, ">> Connecting to "+provider+" ..."); Iterable<Module> modules = ImmutableSet.<Module> of( new SshjSshClientModule(), new NullLoggingModule()); ContextBuilder builder = ContextBuilder.newBuilder(provider) .endpoint(endPoint) .credentials(login, secretKey) .modules(modules); journal.log(Level.INFO, ">> Authenticating ..."); computeContext=builder.buildView(ComputeServiceContext.class); novaComputeService= computeContext.getComputeService(); serverApi=builder.buildApi(NovaApi.class); }
Example #18
Source File: MainApp.java From jclouds-examples with Apache License 2.0 | 5 votes |
private static ComputeService initComputeService(String provider, String identity, String credential) { // example of specific properties, in this case optimizing image list to // only amazon supplied Properties properties = new Properties(); properties.setProperty(PROPERTY_EC2_AMI_QUERY, "owner-id=137112412989;state=available;image-type=machine"); properties.setProperty(PROPERTY_EC2_CC_AMI_QUERY, ""); long scriptTimeout = TimeUnit.MILLISECONDS.convert(20, TimeUnit.MINUTES); properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + ""); // set oauth endpoint property if set in system property String oAuthEndpoint = System.getProperty(PROPERTY_OAUTH_ENDPOINT); if (oAuthEndpoint != null) { properties.setProperty(PROPERTY_OAUTH_ENDPOINT, oAuthEndpoint); } // example of injecting a ssh implementation Iterable<Module> modules = ImmutableSet.<Module> of( new SshjSshClientModule(), new SLF4JLoggingModule(), new EnterpriseConfigurationModule()); ContextBuilder builder = ContextBuilder.newBuilder(provider) .credentials(identity, credential) .modules(modules) .overrides(properties); System.out.printf(">> initializing %s%n", builder.getApiMetadata()); return builder.buildView(ComputeServiceContext.class).getComputeService(); }
Example #19
Source File: OpenstackIaas.java From attic-stratos with Apache License 2.0 | 5 votes |
private boolean waitForStatus(String volumeId, Volume.Status expectedStatus, int timeoutInMins) throws TimeoutException { int timeout = 1000 * 60 * timeoutInMins; long timout = System.currentTimeMillis() + timeout; IaasProvider iaasInfo = getIaasProvider(); String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); NovaApi novaApi = context.unwrapApi(NovaApi.class); VolumeApi volumeApi = novaApi.getVolumeExtensionForZone(region).get(); Volume.Status volumeStatus = this.getVolumeStatus(volumeApi, volumeId); while (volumeStatus != expectedStatus) { try { if (log.isDebugEnabled()) { log.debug(String.format("Volume %s is still NOT in %s. Current State=%s", volumeId, expectedStatus, volumeStatus)); } if (volumeStatus == Volume.Status.ERROR) { log.error("Volume " + volumeId + " is in state ERROR"); return false; } Thread.sleep(1000); volumeStatus = this.getVolumeStatus(volumeApi, volumeId); if (System.currentTimeMillis() > timout) { throw new TimeoutException(); } } catch (InterruptedException e) { // Ignoring the exception } } if (log.isDebugEnabled()) { log.debug(String.format("Volume %s status became %s", volumeId, expectedStatus)); } return true; }
Example #20
Source File: OpenstackIaas.java From attic-stratos with Apache License 2.0 | 5 votes |
@Override public void detachVolume(String instanceId, String volumeId) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo); //NovaApi novaApi = context.unwrapApi(NovaApi.class); //VolumeApi api = novaApi.getVolumeExtensionForZone(region).get(); if (region == null) { log.fatal(String.format( "Cannot detach the volume [id]: %s from the instance [id]: %s. Extracted region is null for Iaas " + ": %s", volumeId, instanceId, iaasInfo)); return; } if (log.isDebugEnabled()) { log.debug(String.format("Starting to detach volume %s from the instance %s", volumeId, instanceId)); } NovaApi novaApi = context.unwrapApi(NovaApi.class); VolumeAttachmentApi attachmentApiapi = novaApi.getVolumeAttachmentExtensionForZone(region).get(); VolumeApi volumeApi = novaApi.getVolumeExtensionForZone(region).get(); if (attachmentApiapi.detachVolumeFromServer(volumeId, removeRegionPrefix(instanceId))) { log.info(String.format( "Detachment of Volume [id]: %s from instance [id]: %s was successful. [region] : %s of Iaas : %s", volumeId, instanceId, region, iaasInfo)); } else { log.error(String.format( "Detachment of Volume [id]: %s from instance [id]: %s was unsuccessful. [region] : %s [volume " + "Status] : %s", volumeId, instanceId, region, getVolumeStatus(volumeApi, volumeId))); } }
Example #21
Source File: GCEIaas.java From attic-stratos with Apache License 2.0 | 5 votes |
private GoogleComputeEngineApi getGCEApi() { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); GoogleComputeEngineApi api = context.unwrapApi(GoogleComputeEngineApi.class); return api; }
Example #22
Source File: CreateComputeContainer.java From jclouds-examples with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException, RunNodesException { // Get a context with docker that offers the portable ComputeService api ComputeServiceContext client = Utils.getComputeApiFromCarinaDirectory(args[0]); // Carina does not allow privileged mode containers DockerTemplateOptions dto = new DockerTemplateOptions(); dto.privileged(false); // Use a known sshd image for demonstration purposes: sickp/apline-sshd Template template = client.getComputeService() .templateBuilder() .options(dto) .imageNameMatches("sickp/alpine-sshd") .build(); // Run a couple nodes accessible via group container Set<? extends NodeMetadata> nodes = client.getComputeService().createNodesInGroup("jcloudscontainertest", 2, template); // Show the nodes for(NodeMetadata node : nodes) { System.out.println("Node: " + node.getName()); } // Cleanup client.getComputeService().destroyNodesMatching(runningInGroup("jcloudscontainertest")); // Release resources client.close(); }
Example #23
Source File: DeleteServer.java From jclouds-examples with Apache License 2.0 | 5 votes |
public DeleteServer(String username, String apiKey) { // These properties control how often jclouds polls for a status udpate Properties overrides = new Properties(); overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS); overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS); ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .overrides(overrides) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); }
Example #24
Source File: CreateServer.java From jclouds-examples with Apache License 2.0 | 5 votes |
public CreateServer(String username, String apiKey) { // These properties control how often jclouds polls for a status update Properties overrides = new Properties(); overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS); overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS); ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .overrides(overrides) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); }
Example #25
Source File: ListServersWithFiltering.java From jclouds-examples with Apache License 2.0 | 5 votes |
public ListServersWithFiltering(String username, String apiKey) { ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); }
Example #26
Source File: ServerMetadata.java From jclouds-examples with Apache License 2.0 | 5 votes |
public ServerMetadata(String username, String apiKey) { ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildView(ComputeServiceContext.class); computeService = context.getComputeService(); NovaApi novaApi = context.unwrapApi(NovaApi.class); serverApi = novaApi.getServerApi(REGION); }
Example #27
Source File: CloudStackIaas.java From attic-stratos with Apache License 2.0 | 5 votes |
@Override public void releaseAddress(String ip) { IaasProvider iaasInfo = getIaasProvider(); ComputeServiceContext context = iaasInfo.getComputeService().getContext(); CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class); cloudStackApi.getAddressApi().disassociateIPAddress(ip); }
Example #28
Source File: JcloudsRenamesRebindTest.java From brooklyn-server with Apache License 2.0 | 5 votes |
private void assertComputeServiceType(JcloudsLocation loc, String expectedType) { // TODO Would be nice to do this more explicitly, rather than relying on toString. // But this is good enough. ComputeService computeService = loc.getComputeService(); ComputeServiceContext context = computeService.getContext(); assertTrue(context.toString().contains("id="+expectedType), "computeService="+computeService+"; context="+computeService.getContext()); }
Example #29
Source File: JcloudsUtil.java From brooklyn-server with Apache License 2.0 | 5 votes |
public static Map<Integer, Integer> dockerPortMappingsFor(JcloudsLocation docker, String containerId) { ComputeServiceContext context = null; try { Properties properties = new Properties(); properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, Boolean.toString(true)); properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, Boolean.toString(true)); context = ContextBuilder.newBuilder("docker") .endpoint(docker.getEndpoint()) .credentials(docker.getIdentity(), docker.getCredential()) .overrides(properties) .modules(ImmutableSet.<Module>of(new SLF4JLoggingModule(), new SshjSshClientModule())) .build(ComputeServiceContext.class); DockerApi api = context.unwrapApi(DockerApi.class); Container container = api.getContainerApi().inspectContainer(containerId); Map<Integer, Integer> portMappings = Maps.newLinkedHashMap(); Map<String, List<Map<String, String>>> ports = container.networkSettings().ports(); if (ports == null) ports = ImmutableMap.<String, List<Map<String,String>>>of(); 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(); } } }
Example #30
Source File: JCloudsHandler.java From roboconf-platform with Apache License 2.0 | 5 votes |
/** * Creates a JCloud context. * @param targetProperties the target properties * @return a non-null object * @throws TargetException if the target properties are invalid */ ComputeService jcloudContext( Map<String,String> targetProperties ) throws TargetException { validate( targetProperties ); ComputeServiceContext context = ContextBuilder .newBuilder( targetProperties.get( PROVIDER_ID )) .endpoint( targetProperties.get( ENDPOINT )) .credentials( targetProperties.get( IDENTITY ), targetProperties.get( CREDENTIAL )) .buildView( ComputeServiceContext.class ); return context.getComputeService(); }