| 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.epc.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 EucalyptusPartnerCloudReviseParsedImage implements ReviseParsedImage { |
| 46 | |
| 47 | // debian-6.0-x86_64/debian.6-0.x86-64.img.manifest.xml |
| 48 | public static final Pattern PATTERN = Pattern.compile("^([^-]+)-([^-]+)-.*"); |
| 49 | public static final Pattern WINDOWS = Pattern.compile("^windows-([^/]+)/.*"); |
| 50 | |
| 51 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
| 52 | |
| 53 | @Resource |
| 54 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 55 | protected Logger logger = Logger.NULL; |
| 56 | |
| 57 | @Inject |
| 58 | public EucalyptusPartnerCloudReviseParsedImage(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 59 | this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap"); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void reviseParsedImage(org.jclouds.ec2.domain.Image from, ImageBuilder builder, OsFamily family, |
| 64 | OperatingSystem.Builder osBuilder) { |
| 65 | try { |
| 66 | if (from.getImageLocation().startsWith("windows")) { |
| 67 | family = OsFamily.WINDOWS; |
| 68 | osBuilder.family(family); |
| 69 | Matcher matcher = WINDOWS.matcher(from.getImageLocation()); |
| 70 | if (matcher.find()) { |
| 71 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(1).replace( |
| 72 | '-', ' ').replace('s', 'S'), osVersionMap)); |
| 73 | } |
| 74 | } else { |
| 75 | Matcher matcher = PATTERN.matcher(from.getImageLocation()); |
| 76 | if (matcher.find()) { |
| 77 | family = OsFamily.fromValue(matcher.group(1)); |
| 78 | osBuilder.family(family); |
| 79 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2), |
| 80 | osVersionMap)); |
| 81 | } |
| 82 | } |
| 83 | } catch (IllegalArgumentException e) { |
| 84 | logger.debug("<< didn't match os(%s)", from.getImageLocation()); |
| 85 | } catch (NoSuchElementException e) { |
| 86 | logger.debug("<< didn't match at all(%s)", from.getImageLocation()); |
| 87 | } |
| 88 | } |
| 89 | } |