| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.NoSuchElementException; |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.compute.domain.ImageBuilder; |
| 34 | import org.jclouds.compute.domain.OperatingSystem; |
| 35 | import org.jclouds.compute.domain.OsFamily; |
| 36 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 37 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 38 | import org.jclouds.ec2.compute.strategy.ReviseParsedImage; |
| 39 | import org.jclouds.logging.Logger; |
| 40 | |
| 41 | /** |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class AWSEC2ReviseParsedImage implements ReviseParsedImage { |
| 46 | |
| 47 | // 137112412989/amzn-ami-0.9.7-beta.i386-ebs |
| 48 | // 137112412989/amzn-ami-0.9.7-beta.x86_64-ebs |
| 49 | // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.x86_64.manifest.xml |
| 50 | // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.i386.manifest.xml |
| 51 | public static final Pattern AMZN_PATTERN = Pattern |
| 52 | .compile(".*/(amzn-hvm-|amzn-)?ami-(.*)\\.(i386|x86_64)(-ebs|\\.manifest.xml)?"); |
| 53 | |
| 54 | // amazon/EC2 CentOS 5.4 HVM AMI |
| 55 | public static final Pattern AMAZON_PATTERN = Pattern.compile("amazon/EC2 ([^ ]+) ([^ ]+).*"); |
| 56 | |
| 57 | public static final Pattern CANONICAL_PATTERN = Pattern.compile(".*/([^-]*)-([^-]*)-.*-(.*)(\\.manifest.xml)?"); |
| 58 | |
| 59 | // ex rightscale-us-east/CentOS_5.4_x64_v4.4.10.manifest.xml |
| 60 | public static final Pattern RIGHTSCALE_PATTERN = Pattern |
| 61 | .compile("[^/]*/([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?"); |
| 62 | |
| 63 | // ex 411009282317/RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha |
| 64 | // 411009282317/RightImage_Windows_2008_x64_v5.5.5 |
| 65 | public static final Pattern RIGHTIMAGE_PATTERN = Pattern |
| 66 | .compile("[^/]*/RightImage[_ ]([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?"); |
| 67 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
| 68 | |
| 69 | @Resource |
| 70 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 71 | protected Logger logger = Logger.NULL; |
| 72 | |
| 73 | @Inject |
| 74 | public AWSEC2ReviseParsedImage(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 75 | this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap"); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void reviseParsedImage(org.jclouds.ec2.domain.Image from, ImageBuilder builder, OsFamily family, |
| 80 | OperatingSystem.Builder osBuilder) { |
| 81 | try { |
| 82 | Matcher matcher = getMatcherAndFind(from.getImageLocation()); |
| 83 | if (matcher.pattern() == AMZN_PATTERN) { |
| 84 | osBuilder.family(OsFamily.AMZN_LINUX); |
| 85 | osBuilder.version(matcher.group(2)); |
| 86 | builder.version(matcher.group(2)); |
| 87 | } else if (matcher.pattern() == AMAZON_PATTERN) { |
| 88 | family = OsFamily.fromValue(matcher.group(1)); |
| 89 | osBuilder.family(family); |
| 90 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2), |
| 91 | osVersionMap)); |
| 92 | } else { |
| 93 | family = OsFamily.fromValue(matcher.group(1)); |
| 94 | osBuilder.family(family); |
| 95 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2), |
| 96 | osVersionMap)); |
| 97 | builder.version(matcher.group(3).replace(".manifest.xml", "")); |
| 98 | } |
| 99 | } catch (IllegalArgumentException e) { |
| 100 | logger.debug("<< didn't match os(%s)", from.getImageLocation()); |
| 101 | } catch (NoSuchElementException e) { |
| 102 | logger.trace("<< didn't match at all(%s)", from.getImageLocation()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | * @throws NoSuchElementException |
| 109 | * if no configured matcher matches the manifest. |
| 110 | */ |
| 111 | private Matcher getMatcherAndFind(String manifest) { |
| 112 | for (Pattern pattern : new Pattern[] { AMZN_PATTERN, AMAZON_PATTERN, CANONICAL_PATTERN, RIGHTIMAGE_PATTERN, |
| 113 | RIGHTSCALE_PATTERN }) { |
| 114 | Matcher matcher = pattern.matcher(manifest); |
| 115 | if (matcher.find()) |
| 116 | return matcher; |
| 117 | } |
| 118 | throw new NoSuchElementException(manifest); |
| 119 | } |
| 120 | } |