| 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.cloudservers.compute.functions; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.regex.Matcher; |
| 23 | import java.util.regex.Pattern; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.OperatingSystem; |
| 31 | import org.jclouds.compute.domain.OsFamily; |
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 33 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 34 | import org.jclouds.logging.Logger; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class CloudServersImageToOperatingSystem implements |
| 44 | Function<org.jclouds.cloudservers.domain.Image, OperatingSystem> { |
| 45 | public static final Pattern DEFAULT_PATTERN = Pattern.compile("(([^ ]*) ([0-9.]+) ?.*)"); |
| 46 | // Windows Server 2008 R2 x64 |
| 47 | public static final Pattern WINDOWS_PATTERN = Pattern.compile("Windows (.*) (x[86][64])"); |
| 48 | |
| 49 | @Resource |
| 50 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 51 | protected Logger logger = Logger.NULL; |
| 52 | |
| 53 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
| 54 | |
| 55 | @Inject |
| 56 | public CloudServersImageToOperatingSystem(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 57 | this.osVersionMap = osVersionMap; |
| 58 | } |
| 59 | |
| 60 | public OperatingSystem apply(final org.jclouds.cloudservers.domain.Image from) { |
| 61 | OsFamily osFamily = null; |
| 62 | String osName = null; |
| 63 | String osArch = null; |
| 64 | String osVersion = null; |
| 65 | String osDescription = from.getName(); |
| 66 | boolean is64Bit = true; |
| 67 | if (from.getName().indexOf("Red Hat EL") != -1) { |
| 68 | osFamily = OsFamily.RHEL; |
| 69 | } else if (from.getName().indexOf("Oracle EL") != -1) { |
| 70 | osFamily = OsFamily.OEL; |
| 71 | } else if (from.getName().indexOf("Windows") != -1) { |
| 72 | osFamily = OsFamily.WINDOWS; |
| 73 | Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); |
| 74 | if (matcher.find()) { |
| 75 | osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); |
| 76 | is64Bit = matcher.group(2).equals("x64"); |
| 77 | } |
| 78 | } else { |
| 79 | Matcher matcher = DEFAULT_PATTERN.matcher(from.getName()); |
| 80 | if (matcher.find()) { |
| 81 | try { |
| 82 | osFamily = OsFamily.fromValue(matcher.group(2).toLowerCase()); |
| 83 | } catch (IllegalArgumentException e) { |
| 84 | logger.debug("<< didn't match os(%s)", matcher.group(2)); |
| 85 | } |
| 86 | osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); |
| 87 | } |
| 88 | } |
| 89 | return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit); |
| 90 | } |
| 91 | } |