| 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.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 | // ex. Centos-5.6-20110917 pub |
| 57 | public static final Pattern PATTERN = Pattern.compile("([^ -]+)[^0-9]([0-9.]+)[ -].*"); |
| 58 | |
| 59 | @Override |
| 60 | public OsFamilyVersion64Bit apply(String input) { |
| 61 | boolean is64Bit = and(not(containsPattern("32bit")), |
| 62 | or(containsPattern("64bit"), not(containsPattern("Windows")))).apply(input); |
| 63 | if (input.contains("Windows")) { |
| 64 | String version = null; |
| 65 | Matcher matcher = Pattern.compile(".*(20[01][0-9] R[1-9]).*").matcher(input); |
| 66 | if (matcher.find()) { |
| 67 | version = matcher.group(1); |
| 68 | } else { |
| 69 | matcher = Pattern.compile(".*(20[01][0-9]).*").matcher(input); |
| 70 | if (matcher.find()) |
| 71 | version = matcher.group(1); |
| 72 | } |
| 73 | return new OsFamilyVersion64Bit(OsFamily.WINDOWS, osVersionMap.get(OsFamily.WINDOWS).get(version), is64Bit); |
| 74 | } else { |
| 75 | Matcher matcher = PATTERN.matcher(input); |
| 76 | if (matcher.find()) { |
| 77 | OsFamily fam = OsFamily.fromValue(matcher.group(1).toLowerCase()); |
| 78 | switch (fam) { |
| 79 | case UNRECOGNIZED: |
| 80 | return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit); |
| 81 | } |
| 82 | return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, matcher |
| 83 | .group(2), osVersionMap), is64Bit); |
| 84 | } else { |
| 85 | return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |