1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | * contributor license agreements. See the NOTICE file distributed with |
4 | * this work for additional information regarding copyright ownership. |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | * (the "License"); you may not use this file except in compliance with |
7 | * the License. You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | package org.jclouds.glesys.compute.functions; |
18 | |
19 | import static com.google.common.base.Predicates.containsPattern; |
20 | |
21 | import java.util.Map; |
22 | import java.util.regex.Matcher; |
23 | import java.util.regex.Pattern; |
24 | |
25 | import javax.inject.Inject; |
26 | import javax.inject.Singleton; |
27 | |
28 | import org.jclouds.compute.domain.OsFamily; |
29 | import org.jclouds.compute.domain.OsFamilyVersion64Bit; |
30 | import org.jclouds.compute.util.ComputeServiceUtils; |
31 | |
32 | import com.google.common.base.Function; |
33 | |
34 | /** |
35 | * Defaults to version null and 64bit, if the operating system is unrecognized |
36 | * and the pattern "32bit" isn't in the string. |
37 | * |
38 | * @author Adrian Cole |
39 | * |
40 | */ |
41 | @Singleton |
42 | public class ParseOsFamilyVersion64BitFromImageName implements Function<String, OsFamilyVersion64Bit> { |
43 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
44 | |
45 | @Inject |
46 | public ParseOsFamilyVersion64BitFromImageName(Map<OsFamily, Map<String, String>> osVersionMap) { |
47 | this.osVersionMap = osVersionMap; |
48 | } |
49 | |
50 | // ex Debian 6.0 64-bit |
51 | // ex. Ubuntu 10.04 LTS 32-bit |
52 | public static final Pattern OSFAMILY_VERSION = Pattern |
53 | .compile("([^ ]+).*[ -]([0-9.]+)( LTS)?( [36][24]-bit)?( x[68][46])?$"); |
54 | public static final Pattern OSFAMILY = Pattern.compile("([^ ]+).*$"); |
55 | |
56 | @Override |
57 | public OsFamilyVersion64Bit apply(String input) { |
58 | boolean is64Bit = containsPattern("64").apply(input); |
59 | String version = ""; |
60 | |
61 | if (input.indexOf("Windows") != -1) { |
62 | Matcher matcher = Pattern.compile(".*(20[01][0-9] R[1-9]).*").matcher(input); |
63 | if (matcher.find()) { |
64 | version = matcher.group(1); |
65 | } else { |
66 | matcher = Pattern.compile(".*(20[01][0-9]).*").matcher(input); |
67 | if (matcher.find()) |
68 | version = matcher.group(1); |
69 | } |
70 | return new OsFamilyVersion64Bit(OsFamily.WINDOWS, osVersionMap.get(OsFamily.WINDOWS).get(version), is64Bit); |
71 | } |
72 | Matcher osFamilyVersionMatcher = OSFAMILY_VERSION.matcher(input); |
73 | if (osFamilyVersionMatcher.find()) { |
74 | OsFamily fam = OsFamily.fromValue(osFamilyVersionMatcher.group(1).toLowerCase()); |
75 | if (fam == OsFamily.UNRECOGNIZED ) { |
76 | return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, version, is64Bit); |
77 | } |
78 | return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, |
79 | osFamilyVersionMatcher.group(2), osVersionMap), is64Bit); |
80 | } else { |
81 | Matcher osFamilyMatcher = OSFAMILY.matcher(input); |
82 | OsFamily fam = OsFamily.UNRECOGNIZED; |
83 | if (osFamilyMatcher.find()) { |
84 | fam = OsFamily.fromValue(osFamilyMatcher.group(1).toLowerCase()); |
85 | } |
86 | return new OsFamilyVersion64Bit(fam, version, is64Bit); |
87 | } |
88 | |
89 | } |
90 | } |