| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 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.Singleton; |
| 31 | |
| 32 | import org.jclouds.compute.domain.ImageBuilder; |
| 33 | import org.jclouds.compute.domain.OperatingSystem; |
| 34 | import org.jclouds.compute.domain.OsFamily; |
| 35 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 36 | import org.jclouds.ec2.compute.strategy.ReviseParsedImage; |
| 37 | import org.jclouds.logging.Logger; |
| 38 | |
| 39 | /** |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class AWSEC2ReviseParsedImage implements ReviseParsedImage { |
| 44 | |
| 45 | // 137112412989/amzn-ami-0.9.7-beta.i386-ebs |
| 46 | // 137112412989/amzn-ami-0.9.7-beta.x86_64-ebs |
| 47 | // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.x86_64.manifest.xml |
| 48 | // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.i386.manifest.xml |
| 49 | public static final Pattern AMZN_PATTERN = Pattern |
| 50 | .compile(".*/(amzn-hvm-|amzn-)?ami-(.*)\\.(i386|x86_64)(-ebs|\\.manifest.xml)?"); |
| 51 | |
| 52 | // amazon/EC2 CentOS 5.4 HVM AMI |
| 53 | public static final Pattern AMAZON_PATTERN = Pattern.compile("amazon/EC2 ([^ ]+) ([^ ]+).*"); |
| 54 | |
| 55 | public static final Pattern CANONICAL_PATTERN = Pattern.compile(".*/([^-]*)-([^-]*)-.*-(.*)(\\.manifest.xml)?"); |
| 56 | |
| 57 | // ex rightscale-us-east/CentOS_5.4_x64_v4.4.10.manifest.xml |
| 58 | public static final Pattern RIGHTSCALE_PATTERN = Pattern |
| 59 | .compile("[^/]*/([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?"); |
| 60 | |
| 61 | // ex 411009282317/RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha |
| 62 | // 411009282317/RightImage_Windows_2008_x64_v5.5.5 |
| 63 | public static final Pattern RIGHTIMAGE_PATTERN = Pattern |
| 64 | .compile("[^/]*/RightImage[_ ]([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?"); |
| 65 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
| 66 | |
| 67 | @Resource |
| 68 | protected Logger logger = Logger.NULL; |
| 69 | |
| 70 | @Inject |
| 71 | public AWSEC2ReviseParsedImage(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 72 | this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void reviseParsedImage(org.jclouds.ec2.domain.Image from, ImageBuilder builder, OsFamily family, |
| 77 | OperatingSystem.Builder osBuilder) { |
| 78 | try { |
| 79 | Matcher matcher = getMatcherAndFind(from.getImageLocation()); |
| 80 | if (matcher.pattern() == AMZN_PATTERN) { |
| 81 | osBuilder.family(OsFamily.AMZN_LINUX); |
| 82 | osBuilder.version(matcher.group(2)); |
| 83 | builder.version(matcher.group(2)); |
| 84 | } else if (matcher.pattern() == AMAZON_PATTERN) { |
| 85 | family = OsFamily.fromValue(matcher.group(1)); |
| 86 | osBuilder.family(family); |
| 87 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2), |
| 88 | osVersionMap)); |
| 89 | } else { |
| 90 | family = OsFamily.fromValue(matcher.group(1)); |
| 91 | osBuilder.family(family); |
| 92 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2), |
| 93 | osVersionMap)); |
| 94 | builder.version(matcher.group(3).replace(".manifest.xml", "")); |
| 95 | } |
| 96 | } catch (IllegalArgumentException e) { |
| 97 | logger.debug("<< didn't match os(%s)", from.getImageLocation()); |
| 98 | } catch (NoSuchElementException e) { |
| 99 | logger.trace("<< didn't match at all(%s)", from.getImageLocation()); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * |
| 105 | * @throws NoSuchElementException |
| 106 | * if no configured matcher matches the manifest. |
| 107 | */ |
| 108 | private Matcher getMatcherAndFind(String manifest) { |
| 109 | for (Pattern pattern : new Pattern[] { AMZN_PATTERN, AMAZON_PATTERN, CANONICAL_PATTERN, RIGHTIMAGE_PATTERN, |
| 110 | RIGHTSCALE_PATTERN }) { |
| 111 | Matcher matcher = pattern.matcher(manifest); |
| 112 | if (matcher.find()) |
| 113 | return matcher; |
| 114 | } |
| 115 | throw new NoSuchElementException(manifest); |
| 116 | } |
| 117 | } |