org.jclouds.compute.domain.Image Java Examples

The following examples show how to use org.jclouds.compute.domain.Image. 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: AWSEC2ComputeServiceContextModuleTest.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacheLoaderDoesNotReloadAfterAuthorizationException() throws Exception {
   AWSEC2ComputeServiceContextModule module = new AWSEC2ComputeServiceContextModule() {
      public Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(RegionAndIdToImage delegate) {
         return super.provideRegionAndNameToImageSupplierCacheLoader(delegate);
      }
   };
   
   RegionAndName regionAndName = new RegionAndName("myregion", "myname");
   AuthorizationException authException = new AuthorizationException();
   
   RegionAndIdToImage mockRegionAndIdToImage = createMock(RegionAndIdToImage.class);
   expect(mockRegionAndIdToImage.load(regionAndName)).andThrow(authException).once();
   replay(mockRegionAndIdToImage);
   
   CacheLoader<RegionAndName, Image> cacheLoader = module.provideRegionAndNameToImageSupplierCacheLoader(mockRegionAndIdToImage).get();

   for (int i = 0; i < 2; i++) {
      try {
         Image image = cacheLoader.load(regionAndName);
         fail("Expected Authorization exception, but got " + image);
      } catch (AuthorizationException e) {
         // success
      }
   }
}
 
Example #2
Source File: JcloudsSshMachineLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected Optional<Image> getOptionalImage() {
  if (_image == null) {
      if (imageId == null) {
          _image = Optional.absent(); // can happen with JcloudsLocation.resumeMachine() usage
      } else {
          try {
              ComputeService computeService = getComputeServiceOrNull();
              if (computeService == null) {
                  if (LOG.isDebugEnabled()) LOG.debug("Cannot get image (with id {}) for {}, because cannot get compute-service from parent {}", new Object[] {imageId, this, getParent()});
                  _image = Optional.absent();
              } else {
                  _image = Optional.fromNullable(computeService.getImage(imageId));
              }
          } catch (Exception e) {
              Exceptions.propagateIfFatal(e);
              if (LOG.isDebugEnabled()) LOG.debug("Problem getting image for " + this + ", image id " + imageId + " (continuing)", e);
              _image = Optional.absent();
          }
      }
  }
  return _image;
}
 
Example #3
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * If the ImageChooser is a string, then try instantiating a class with that name (in the same
 * way as we do for {@link #getCloudMachineNamer(ConfigBag)}, for example). Otherwise, assume
 * that convention TypeCoercions will work.
 */
@SuppressWarnings("unchecked")
protected Function<Iterable<? extends Image>, Image> getImageChooser(ComputeService computeService, ConfigBag config) {
    Function<Iterable<? extends Image>, Image> chooser;
    Object rawVal = config.getStringKey(JcloudsLocationConfig.IMAGE_CHOOSER.getName());
    if (rawVal instanceof String && Strings.isNonBlank((String)rawVal)) {
        // Configured with a string: it could be a class that we need to instantiate
        Class<?> clazz;
        try {
            clazz = new ClassLoaderUtils(this.getClass(), getManagementContext()).loadClass((String)rawVal);
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Could not load configured ImageChooser " + rawVal, e);
        }
        Maybe<?> instance = Reflections.invokeConstructorFromArgs(clazz);
        if (!instance.isPresent()) {
            throw new IllegalStateException("Failed to create ImageChooser "+rawVal+" for location "+this);
        } else if (!(instance.get() instanceof Function)) {
            throw new IllegalStateException("Failed to create ImageChooser "+rawVal+" for location "+this+"; expected type Function but got "+instance.get().getClass());
        } else {
            chooser = (Function<Iterable<? extends Image>, Image>) instance.get();
        }
    } else {
        chooser = config.get(JcloudsLocationConfig.IMAGE_CHOOSER);
    }
    return BrooklynImageChooser.cloneFor(chooser, computeService, config);
}
 
Example #4
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected double punishmentForOldOsVersions(Image img, OsFamily family, double minVersion) {
    OperatingSystem os = img.getOperatingSystem();
    if (os!=null && family.equals(os.getFamily())) {
        String v = os.getVersion();
        if (v!=null) {
            try {
                double vd = Double.parseDouble(v);
                // punish older versions, with a -log function (so 0.5 version behind is -log(1.5)=-0.5 and 2 versions behind is -log(3)=-1.2  
                if (vd < minVersion) return -Math.log(1+(minVersion - vd));
            } catch (Exception e) {
                /* ignore unparseable versions */
            }
        }
    }
    return 0;
}
 
Example #5
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected double punishmentForDeprecation(Image img) {
    // google deprecation strategy
    //        userMetadata={deprecatedState=DEPRECATED}}
    String deprecated = img.getUserMetadata().get("deprecatedState");
    if (deprecated!=null) {
        if ("deprecated".equalsIgnoreCase(deprecated))
            return -30;
        if ("obsolete".equalsIgnoreCase(deprecated))
            return -40;
        log.warn("Unrecognised 'deprecatedState' value '"+deprecated+"' when scoring "+img+"; ignoring that metadata");
    }
    
    // common strategies
    if (imageNameContainsWordCaseInsensitive(img, "deprecated")) return -30;
    if (imageNameContainsWordCaseInsensitive(img, "alpha")) return -10;
    if (imageNameContainsWordCaseInsensitive(img, "beta")) return -5;
    if (imageNameContainsWordCaseInsensitive(img, "testing")) return -5;
    if (imageNameContainsWordCaseInsensitive(img, "rc")) return -3;

    // no indication this is deprecated
    return 0;
}
 
Example #6
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** @deprecated since 0.7.0; kept for persisted state backwards compatibility */
@Deprecated
@SuppressWarnings("unused")
private static Ordering<Image> orderingWithDefaultsDeprecated(final Ordering<Image> primaryOrdering) {
    return new Ordering<Image>() {
        @Override
        public int compare(Image left, Image right) {
            return ComparisonChain.start()
                .compare(left, right, primaryOrdering)
                // fall back to default strategy otherwise, except preferring *non*-null values
                // TODO suggest to use NaturalOrderComparator (so 10>9) then order by:
                // 1) `name.replaceAll("([^0-9]+)", " ")` 
                // 2) shortest non-empty name
                // 3) version (NaturalOrderComparator, prefer last)
                // 4) name (NaturalOrderComparator, prefer last)
                // 5) other fields (NaturalOrderComparator, prefer last)
                .compare(left.getName(), right.getName(), Ordering.<String> natural().nullsFirst())
                .compare(left.getVersion(), right.getVersion(), Ordering.<String> natural().nullsFirst())
                .compare(left.getDescription(), right.getDescription(), Ordering.<String> natural().nullsFirst())
                .compare(left.getOperatingSystem().getName(), right.getOperatingSystem().getName(), Ordering.<String> natural().nullsFirst())
                .compare(left.getOperatingSystem().getVersion(), right.getOperatingSystem().getVersion(), Ordering.<String> natural().nullsFirst())
                .compare(left.getOperatingSystem().getDescription(), right.getOperatingSystem().getDescription(), Ordering.<String> natural().nullsFirst())
                .compare(left.getOperatingSystem().getArch(), right.getOperatingSystem().getArch(), Ordering.<String> natural().nullsFirst()).result();
        }
    };
}
 
Example #7
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public ImageChooserFromOrdering cloneFor(ComputeService service) {
    if (Iterables.tryFind(orderings, Predicates.instanceOf(ComputeServiceAwareChooser.class)).isPresent()) {
        List<Ordering<? super Image>> clonedOrderings = Lists.newArrayList();
        for (Ordering<? super Image> ordering : orderings) {
            if (ordering instanceof ComputeServiceAwareChooser) {
                clonedOrderings.add(BrooklynImageChooser.cloneFor(ordering, service));
            } else {
                clonedOrderings.add(ordering);
            }
        }
        return new ImageChooserFromOrdering(clonedOrderings);
    } else {
        return this;
    }
}
 
Example #8
Source File: BrooklynImageChooser.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public ImageChooserFromOrdering cloneFor(ConfigBag config) {
    if (Iterables.tryFind(orderings, Predicates.instanceOf(ConfigAwareChooser.class)).isPresent()) {
        List<Ordering<? super Image>> clonedOrderings = Lists.newArrayList();
        for (Ordering<? super Image> ordering : orderings) {
            if (ordering instanceof ConfigAwareChooser) {
                clonedOrderings.add(BrooklynImageChooser.cloneFor(ordering, config));
            } else {
                clonedOrderings.add(ordering);
            }
        }
        return new ImageChooserFromOrdering(clonedOrderings);
    } else {
        return this;
    }
}
 
Example #9
Source File: JcloudsStubTemplateBuilder.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected Image getImage() {
    switch (providerName) {
    case "aws-ec2" :
    case "ec2" :
        return new ImageBuilder().providerId("ebs-image-provider").name("image")
                .id(regionName+"/bogus-image").location(jcloudsDomainLocation)
                .userMetadata(ImmutableMap.of("rootDeviceType", RootDeviceType.EBS.value()))
                .operatingSystem(new OperatingSystem(OsFamily.UBUNTU, null, "1.0", VirtualizationType.PARAVIRTUAL.value(), "ubuntu", true))
                .description("description").version("1.0").defaultCredentials(LoginCredentials.builder().user("root").build())
                .status(Image.Status.AVAILABLE)
                .build();
    case "google-compute-engine" :
        return new ImageBuilder().providerId("gce-image-provider").name("image")
                .id(regionName+"/bogus-image").location(jcloudsDomainLocation)
                .operatingSystem(new OperatingSystem(OsFamily.UBUNTU, null, "1.0", VirtualizationType.PARAVIRTUAL.value(), "ubuntu", true))
                .description("description").version("1.0").defaultCredentials(LoginCredentials.builder().user("root").build())
                .status(Image.Status.AVAILABLE)
                .build();
    default:
        throw new UnsupportedOperationException("Unsupported stubbed Image for provider "+providerName);
    }
}
 
Example #10
Source File: JCloudsComputeIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testListImages() throws Exception {
    CamelContext camelctx = getCamelContext();
    ProducerTemplate template = camelctx.createProducerTemplate();

    MockEndpoint result = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedMessageCount(1);
    template.sendBodyAndHeader("direct:start", null, JcloudsConstants.OPERATION, JcloudsConstants.LIST_IMAGES);
    result.assertIsSatisfied();

    List<Exchange> exchanges = result.getExchanges();
    if (exchanges != null && !exchanges.isEmpty()) {
        for (Exchange exchange : exchanges) {
            Set<?> images = exchange.getIn().getBody(Set.class);
            Assert.assertTrue(images.size() > 0);
            for (Object obj : images) {
                Assert.assertTrue(obj instanceof Image);
            }
        }
    }
}
 
Example #11
Source File: CreateServer.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
/**
 * This method uses the generic ComputeService.listImages() to find the image.
 *
 * @return An Ubuntu 14.04 Image
 */
private Image getImage() {
   System.out.format("  Images%n");

   Set<? extends Image> images = computeService.listImages();
   Image result = null;

   for (Image image: images) {
      System.out.format("    %s%n", image);
      if (image.getOperatingSystem().getName().startsWith("Ubuntu 14.04 LTS")) {
         result = image;
      }
   }

   if (result == null) {
      System.err.println("Image with Ubuntu 14.04 operating system not found. Using first image found.%n");
      result = images.iterator().next();
   }

   return result;
}
 
Example #12
Source File: CloudExplorerSupport.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
protected void doCall(JcloudsLocation loc, String indent) throws Exception {

    ComputeService computeService = loc.getComputeService();
    ConfigBag setup = loc.config().getBag();
    
    JcloudsLocationCustomizer customizersDelegate = LocationCustomizerDelegate.newInstance(loc.getManagementContext(), setup);
    Template template = loc.buildTemplate(computeService, setup, customizersDelegate);
    Image image = template.getImage();
    Hardware hardware = template.getHardware();
    org.jclouds.domain.Location location = template.getLocation();
    TemplateOptions options = template.getOptions();
    stdout.println(indent+"Default template {");
    stdout.println(indent+"\tImage: "+image);
    stdout.println(indent+"\tHardware: "+hardware);
    stdout.println(indent+"\tLocation: "+location);
    stdout.println(indent+"\tOptions: "+options);
    stdout.println(indent+"}");
}
 
Example #13
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void assertCentos(Image image, String expectedVersion) {
    // Expect owner RightScale (i.e. "RightImage")
    assertEquals(image.getUserMetadata().get("owner"), "411009282317", "image="+image);
    assertTrue(image.getName().toLowerCase().contains("centos"), "image="+image);
    assertTrue(image.getName().contains(expectedVersion), "image="+image);
    assertEquals(image.getOperatingSystem().getFamily(), OsFamily.CENTOS, "image="+image);
    assertEquals(image.getOperatingSystem().getVersion(), expectedVersion, "image="+image);
}
 
Example #14
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Live", "Live-sanity"})
public void testDefault() throws Exception {
    obtainMachine(ImmutableMap.of());
    Image image = template.getImage();

    LOG.info("default="+image);
    assertCentos(image, "7.0");
}
 
Example #15
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Live", "Live-sanity"})
public void testUbuntu() throws Exception {
    obtainMachine(ImmutableMap.of(JcloudsLocation.OS_FAMILY, "ubuntu"));
    Image image = template.getImage();

    LOG.info("ubuntu="+image);
    assertUbuntu(image, "14.04");
}
 
Example #16
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Live", "Live-sanity"})
public void testCentos() throws Exception {
    obtainMachine(ImmutableMap.of(JcloudsLocation.OS_FAMILY, "centos"));
    Image image = template.getImage();
    
    LOG.info("centos="+image);
    assertCentos(image, "7.0");
    
}
 
Example #17
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Live", "Live-sanity"})
public void testCentos7() throws Exception {
    Thread.sleep(10*1000);
    obtainMachine(ImmutableMap.of(JcloudsLocation.OS_FAMILY, "centos", JcloudsLocation.OS_VERSION_REGEX, "7"));
    Image image = template.getImage();
    
    LOG.info("centos_7="+image);
    assertCentos(image, "7.0");
}
 
Example #18
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Live", "Live-sanity"})
public void testCentos6() throws Exception {
    obtainMachine(ImmutableMap.of(JcloudsLocation.OS_FAMILY, "centos", JcloudsLocation.OS_VERSION_REGEX, "6"));
    Image image = template.getImage();
    
    LOG.info("centos_6="+image);
    assertCentos(image, "6.6");
}
 
Example #19
Source File: JCloudsConnector.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Search for an image with that name
 * @param name name if the image
 * @return an Image
 */
public Image checkIfImageExist(String name){
    for(Image i : listOfImages()){
        if(i.getName().equals(name))
            return i;
    }
    return null;
}
 
Example #20
Source File: JcloudsAwsImageChoiceStubbedLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Broken", "Live", "Live-sanity"})
public void testUbuntu16() throws Exception {
    obtainMachine(ImmutableMap.of(JcloudsLocation.OS_FAMILY, "ubuntu", JcloudsLocation.OS_VERSION_REGEX, "16.*"));
    Image image = template.getImage();

    LOG.info("ubuntu_16="+image);
    assertUbuntu(image, "16.04");
}
 
Example #21
Source File: JcloudsStubTemplateBuilder.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public TemplateBuilder createTemplateBuilder() {
    final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(getImage()));
    ImmutableMap<RegionAndName, Image> imageMap = (ImmutableMap<RegionAndName, Image>) ImagesToRegionAndIdMap.imagesToMap(images.get());
    Supplier<LoadingCache<RegionAndName, ? extends Image>> imageCache = Suppliers.<LoadingCache<RegionAndName, ? extends Image>> ofInstance(
            CacheBuilder.newBuilder().<RegionAndName, Image>build(CacheLoader.from(Functions.forMap(imageMap))));
    return newTemplateBuilder(images, imageCache);
}
 
Example #22
Source File: DockerJcloudsLocationLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Live", "Live-sanity"})
public void testExplicitCredentialsNotOverwritten() throws Exception {
    loc = newDockerLocation(ImmutableMap.<String, Object>of());
    JcloudsSshMachineLocation machine = newDockerMachine(loc, MutableMap.of(
            JcloudsLocationConfig.LOGIN_USER, "myuser",
            JcloudsLocationConfig.LOGIN_USER_PASSWORD, "mypassword"));
    Image image = getOptionalImage(machine).get();
    assertEquals(image.getDescription(), "brooklyncentral/centos:7");
    assertEquals(templateOptions.getLoginUser(), "myuser");
    assertEquals(templateOptions.getLoginPassword(), "mypassword");
    assertEquals(templateOptions.getLoginPassword(), "mypassword");
    assertEnvNotContainsKey(templateOptions, "BROOKLYN_ROOT_PASSWORD");
}
 
Example #23
Source File: DockerJcloudsLocationLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Live", "Live-sanity"})
public void testExplicitImageIdNotOverwritten() throws Exception {
    // TODO This id will likely change sometimes; once CCS-29 is done, then use an image name.
    // Assumes we have executed:
    //     docker run ubuntu /bin/echo 'Hello world'
    // which will have downloaded the ubuntu image with the given id.
    String imageId = "sha256:2fa927b5cdd31cdec0027ff4f45ef4343795c7a2d19a9af4f32425132a222330";
    loc = newDockerLocation(ImmutableMap.<String, Object>of());
    JcloudsSshMachineLocation machine = newDockerMachine(loc, MutableMap.of(
            JcloudsLocation.IMAGE_ID, imageId,
            JcloudsLocation.TEMPLATE_OPTIONS, ImmutableMap.of(
                    "entrypoint", ImmutableList.of("/bin/sleep", "1000"))));
    Image image = getOptionalImage(machine).get();
    assertEquals(image.getId(), imageId);
}
 
Example #24
Source File: DockerIaas.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
@Override
public void buildTemplate() {
    IaasProvider iaasProvider = getIaasProvider();
    ComputeService computeService = iaasProvider.getComputeService();
    Set<? extends Image> images = computeService.listImages();
    Image image = findImage(images, iaasProvider.getImage());
    if (image == null) {
        throw new CloudControllerException(String.format("Docker image not found: %s", iaasProvider.getImage()));
    }
    Template template = computeService.templateBuilder().fromImage(image).build();
    iaasProvider.setTemplate(template);
}
 
Example #25
Source File: DockerJcloudsLocationLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void assertMachineSshableSecureAndFromImage(JcloudsSshMachineLocation machine, String expectedImageDescription) throws Exception {
    Image image = getOptionalImage(machine).get();
    assertEquals(image.getDescription(), expectedImageDescription);
    assertEquals(templateOptions.getLoginUser(), "root");
    assertEnvContainsKeyValue(templateOptions, "BROOKLYN_ROOT_PASSWORD", templateOptions.getLoginPassword());
    assertPasswordIsSecure(templateOptions.getLoginPassword());

    assertTrue(machine.isSshable(), "machine=" + machine);
}
 
Example #26
Source File: JcloudsLocationResolverTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testJcloudsImageChooserConfiguredFromBrooklynProperties() {
    brooklynProperties.put("brooklyn.location.named.myloc", "jclouds:aws-ec2");
    brooklynProperties.put("brooklyn.location.named.myloc.imageChooser", JcloudsLocationResolverTest.class.getName()+"$"+MyFunction.class.getSimpleName());
    brooklynProperties.put("brooklyn.location.jclouds.openstack-nova.foo", "bar");
    JcloudsLocation loc = resolve("myloc");
    
    // Test relies on the computeService not being used! Not great, but good enough.
    Function<Iterable<? extends Image>, Image> chooser = loc.getImageChooser((ComputeService)null, loc.config().getLocalBag());
    assertTrue(chooser instanceof MyFunction, "chooser="+chooser);
}
 
Example #27
Source File: DockerJcloudsLocationLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Optional<Image> getOptionalImage(JcloudsSshMachineLocation machine) throws Exception {
    Method method = machine.getClass().getDeclaredMethod("getOptionalImage");
    method.setAccessible(true);
    Optional<Image> result = (Optional<Image>) method.invoke(machine);
    return checkNotNull(result, "null must not be returned by getOptionalImage, for %s", machine);
}
 
Example #28
Source File: CloudExplorerSupport.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
protected void doCall(ComputeService computeService, String indent) throws Exception {
    Set<? extends Image> images = computeService.listImages();
    stdout.println(indent+"Images {");
    for (Image image : images) {
        stdout.println(indent+"\t"+image);
    }
    stdout.println(indent+"}");
}
 
Example #29
Source File: CloudExplorerSupport.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
protected void doCall(ComputeService computeService, String indent) throws Exception {

    for (String imageId : names) {
        Image image = computeService.getImage(imageId);
        if (image == null) {
            return;
        }
        stdout.println(indent+"Image "+imageId+" {");
        stdout.println(indent+"\t"+image);
        stdout.println(indent+"}");
    }
}
 
Example #30
Source File: DockerIaas.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private Image findImage(Set<? extends Image> images, String name) {
    for (Image image : images) {
        if (image.getDescription().contains(name))
            return image;
    }
    return null;
}