@Test public void testRHEL() { String name = "Red Hat EL"; Image imageToConvert = Image.builder().id("id-" + name).name(name).build(); ImageToOperatingSystem converter = new ImageToOperatingSystem( new HashMap<OsFamily, Map<String, String>>()); OperatingSystem convertedOs = converter.apply(imageToConvert); assertEquals(convertedOs.getName(), imageToConvert.getName()); assertEquals(convertedOs.getFamily(), OsFamily.RHEL); assertEquals(convertedOs.getDescription(), imageToConvert.getName()); assertEquals(convertedOs.getVersion(), null); assertEquals(convertedOs.getArch(), null); assertTrue(convertedOs.is64Bit()); }
public OperatingSystem apply(final Image from) { OsFamily osFamily = null; String osVersion = null; String imageName = MoreObjects.firstNonNull(from.getName(), "unspecified"); boolean is64Bit = true; if (imageName.indexOf("Windows") != -1) { osFamily = OsFamily.WINDOWS; Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); if (matcher.find()) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); is64Bit = matcher.group(2).equals("x64"); } } else { if (imageName.contains("Red Hat")) { osFamily = OsFamily.RHEL; } else if (imageName.contains("Oracle")) { osFamily = OsFamily.OEL; } else { final Iterable<String> imageNameParts = Splitter.on(CharMatcher.WHITESPACE).trimResults().split( imageName.toLowerCase()); try { osFamily = find(Arrays.asList(OsFamily.values()), new Predicate<OsFamily>() { @Override public boolean apply(OsFamily osFamily) { return any(imageNameParts, equalTo(osFamily.name().toLowerCase())); } }); } catch (NoSuchElementException e) { String ubuntuVersion = startsWithUbuntuVersion(imageNameParts); if (ubuntuVersion != null) { osFamily = OsFamily.UBUNTU; osVersion = ubuntuVersion; } else { logger.trace("could not parse operating system family for image(%s): %s", from.getId(), imageNameParts); osFamily = OsFamily.UNRECOGNIZED; } } } Matcher matcher = DEFAULT_PATTERN.matcher(imageName); if (matcher.find() && matcher.groupCount() >= 3) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); } } return new OperatingSystem(osFamily, imageName, osVersion, null, imageName, is64Bit); }
ImageToOperatingSystem implements Function<Image, OperatingSystem> { public OperatingSystem apply(final Image from) { OsFamily osFamily = null; String osVersion = null; String imageName = MoreObjects.firstNonNull(from.getName(), "unspecified"); boolean is64Bit = true; if (imageName.indexOf("Windows") != -1) { osFamily = OsFamily.WINDOWS; Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); if (matcher.find()) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); is64Bit = matcher.group(2).equals("x64"); } } else { if (imageName.contains("Red Hat")) { osFamily = OsFamily.RHEL; } else if (imageName.contains("Oracle")) { osFamily = OsFamily.OEL; } else { final Iterable<String> imageNameParts = Splitter.on(CharMatcher.WHITESPACE).trimResults().split( imageName.toLowerCase()); try { osFamily = find(Arrays.asList(OsFamily.values()), new Predicate<OsFamily>() { @Override public boolean apply(OsFamily osFamily) { return any(imageNameParts, equalTo(osFamily.name().toLowerCase())); } }); } catch (NoSuchElementException e) { String ubuntuVersion = startsWithUbuntuVersion(imageNameParts); if (ubuntuVersion != null) { osFamily = OsFamily.UBUNTU; osVersion = ubuntuVersion; } else { logger.trace("could not parse operating system family for image(%s): %s", from.getId(), imageNameParts); osFamily = OsFamily.UNRECOGNIZED; } } } Matcher matcher = DEFAULT_PATTERN.matcher(imageName); if (matcher.find() && matcher.groupCount() >= 3) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); } } return new OperatingSystem(osFamily, imageName, osVersion, null, imageName, is64Bit); } }
ImageToOperatingSystem implements Function<Image, OperatingSystem> { public OperatingSystem apply(final Image from) { OsFamily osFamily = null; String osVersion = null; String imageName = MoreObjects.firstNonNull(from.getName(), "unspecified"); boolean is64Bit = true; if (imageName.indexOf("Windows") != -1) { osFamily = OsFamily.WINDOWS; Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); if (matcher.find()) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); is64Bit = matcher.group(2).equals("x64"); } } else { if (imageName.contains("Red Hat")) { osFamily = OsFamily.RHEL; } else if (imageName.contains("Oracle")) { osFamily = OsFamily.OEL; } else { final Iterable<String> imageNameParts = Splitter.on(CharMatcher.WHITESPACE).trimResults().split( imageName.toLowerCase()); try { osFamily = find(Arrays.asList(OsFamily.values()), new Predicate<OsFamily>() { @Override public boolean apply(OsFamily osFamily) { return any(imageNameParts, equalTo(osFamily.name().toLowerCase())); } }); } catch (NoSuchElementException e) { String ubuntuVersion = startsWithUbuntuVersion(imageNameParts); if (ubuntuVersion != null) { osFamily = OsFamily.UBUNTU; osVersion = ubuntuVersion; } else { logger.trace("could not parse operating system family for image(%s): %s", from.getId(), imageNameParts); osFamily = OsFamily.UNRECOGNIZED; } } } Matcher matcher = DEFAULT_PATTERN.matcher(imageName); if (matcher.find() && matcher.groupCount() >= 3) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); } } return new OperatingSystem(osFamily, imageName, osVersion, null, imageName, is64Bit); } @Inject  ImageToOperatingSystem(Map<OsFamily, Map<String, String>> osVersionMap); }
ImageToOperatingSystem implements Function<Image, OperatingSystem> { public OperatingSystem apply(final Image from) { OsFamily osFamily = null; String osVersion = null; String imageName = MoreObjects.firstNonNull(from.getName(), "unspecified"); boolean is64Bit = true; if (imageName.indexOf("Windows") != -1) { osFamily = OsFamily.WINDOWS; Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); if (matcher.find()) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); is64Bit = matcher.group(2).equals("x64"); } } else { if (imageName.contains("Red Hat")) { osFamily = OsFamily.RHEL; } else if (imageName.contains("Oracle")) { osFamily = OsFamily.OEL; } else { final Iterable<String> imageNameParts = Splitter.on(CharMatcher.WHITESPACE).trimResults().split( imageName.toLowerCase()); try { osFamily = find(Arrays.asList(OsFamily.values()), new Predicate<OsFamily>() { @Override public boolean apply(OsFamily osFamily) { return any(imageNameParts, equalTo(osFamily.name().toLowerCase())); } }); } catch (NoSuchElementException e) { String ubuntuVersion = startsWithUbuntuVersion(imageNameParts); if (ubuntuVersion != null) { osFamily = OsFamily.UBUNTU; osVersion = ubuntuVersion; } else { logger.trace("could not parse operating system family for image(%s): %s", from.getId(), imageNameParts); osFamily = OsFamily.UNRECOGNIZED; } } } Matcher matcher = DEFAULT_PATTERN.matcher(imageName); if (matcher.find() && matcher.groupCount() >= 3) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); } } return new OperatingSystem(osFamily, imageName, osVersion, null, imageName, is64Bit); } @Inject  ImageToOperatingSystem(Map<OsFamily, Map<String, String>> osVersionMap); OperatingSystem apply(final Image from); }
ImageToOperatingSystem implements Function<Image, OperatingSystem> { public OperatingSystem apply(final Image from) { OsFamily osFamily = null; String osVersion = null; String imageName = MoreObjects.firstNonNull(from.getName(), "unspecified"); boolean is64Bit = true; if (imageName.indexOf("Windows") != -1) { osFamily = OsFamily.WINDOWS; Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); if (matcher.find()) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); is64Bit = matcher.group(2).equals("x64"); } } else { if (imageName.contains("Red Hat")) { osFamily = OsFamily.RHEL; } else if (imageName.contains("Oracle")) { osFamily = OsFamily.OEL; } else { final Iterable<String> imageNameParts = Splitter.on(CharMatcher.WHITESPACE).trimResults().split( imageName.toLowerCase()); try { osFamily = find(Arrays.asList(OsFamily.values()), new Predicate<OsFamily>() { @Override public boolean apply(OsFamily osFamily) { return any(imageNameParts, equalTo(osFamily.name().toLowerCase())); } }); } catch (NoSuchElementException e) { String ubuntuVersion = startsWithUbuntuVersion(imageNameParts); if (ubuntuVersion != null) { osFamily = OsFamily.UBUNTU; osVersion = ubuntuVersion; } else { logger.trace("could not parse operating system family for image(%s): %s", from.getId(), imageNameParts); osFamily = OsFamily.UNRECOGNIZED; } } } Matcher matcher = DEFAULT_PATTERN.matcher(imageName); if (matcher.find() && matcher.groupCount() >= 3) { osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); } } return new OperatingSystem(osFamily, imageName, osVersion, null, imageName, is64Bit); } @Inject  ImageToOperatingSystem(Map<OsFamily, Map<String, String>> osVersionMap); OperatingSystem apply(final Image from); static final Pattern DEFAULT_PATTERN; static final Pattern WINDOWS_PATTERN; }