| 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.cloudsigma.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Predicates.and; |
| 22 | import static com.google.common.base.Predicates.containsPattern; |
| 23 | import static com.google.common.base.Predicates.not; |
| 24 | import static com.google.common.base.Predicates.or; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
| 29 | |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.compute.domain.OsFamily; |
| 34 | import org.jclouds.compute.domain.OsFamilyVersion64Bit; |
| 35 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 36 | |
| 37 | import com.google.common.base.Function; |
| 38 | |
| 39 | /** |
| 40 | * Defaults to version null and 64bit, if the operating system is unrecognized and the pattern |
| 41 | * "32bit" isn't in the string. |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | * |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class ParseOsFamilyVersion64BitFromImageName implements Function<String, OsFamilyVersion64Bit> { |
| 48 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
| 49 | |
| 50 | @Inject |
| 51 | public ParseOsFamilyVersion64BitFromImageName(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 52 | this.osVersionMap = osVersionMap; |
| 53 | } |
| 54 | |
| 55 | // ex CentOS 5.5 Linux 64bit Preinstalled System with AppFirst Monitoring |
| 56 | public static final Pattern PATTERN = Pattern.compile("([^ ]+)[^0-9]([0-9.]+) .*"); |
| 57 | |
| 58 | @Override |
| 59 | public OsFamilyVersion64Bit apply(String input) { |
| 60 | boolean is64Bit = and(not(containsPattern("32bit")), |
| 61 | or(containsPattern("64bit"), not(containsPattern("Windows")))).apply(input); |
| 62 | if (input.contains("Windows")) { |
| 63 | String version = null; |
| 64 | Matcher matcher = Pattern.compile(".*(20[01][0-9] R[1-9]).*").matcher(input); |
| 65 | if (matcher.find()) { |
| 66 | version = matcher.group(1); |
| 67 | } else { |
| 68 | matcher = Pattern.compile(".*(20[01][0-9]).*").matcher(input); |
| 69 | if (matcher.find()) |
| 70 | version = matcher.group(1); |
| 71 | } |
| 72 | return new OsFamilyVersion64Bit(OsFamily.WINDOWS, osVersionMap.get(OsFamily.WINDOWS).get(version), is64Bit); |
| 73 | } else { |
| 74 | Matcher matcher = PATTERN.matcher(input); |
| 75 | if (matcher.find()) { |
| 76 | OsFamily fam = OsFamily.fromValue(matcher.group(1).toLowerCase()); |
| 77 | switch (fam) { |
| 78 | case UNRECOGNIZED: |
| 79 | return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit); |
| 80 | } |
| 81 | return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, matcher |
| 82 | .group(2), osVersionMap), is64Bit); |
| 83 | } else { |
| 84 | return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |