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.slicehost.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 SlicehostImageToOperatingSystem implements Function<org.jclouds.slicehost.domain.Image, OperatingSystem> { |
44 | public static final Pattern DEFAULT_PATTERN = Pattern.compile("(([^ ]*) ([0-9.]+) ?.*)"); |
45 | // Windows Server 2008 R2 x64 |
46 | public static final Pattern WINDOWS_PATTERN = Pattern.compile("Windows (.*) (x[86][64])"); |
47 | |
48 | @Resource |
49 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
50 | protected Logger logger = Logger.NULL; |
51 | |
52 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
53 | |
54 | @Inject |
55 | public SlicehostImageToOperatingSystem(Map<OsFamily, Map<String, String>> osVersionMap) { |
56 | this.osVersionMap = osVersionMap; |
57 | } |
58 | |
59 | public OperatingSystem apply(final org.jclouds.slicehost.domain.Image from) { |
60 | OsFamily osFamily = null; |
61 | String osName = null; |
62 | String osArch = null; |
63 | String osVersion = null; |
64 | String osDescription = from.getName(); |
65 | boolean is64Bit = !from.getName().endsWith("32-bit"); |
66 | if (from.getName().indexOf("Red Hat EL") != -1) { |
67 | osFamily = OsFamily.RHEL; |
68 | } else if (from.getName().indexOf("Oracle EL") != -1) { |
69 | osFamily = OsFamily.OEL; |
70 | } else if (from.getName().indexOf("Windows") != -1) { |
71 | osFamily = OsFamily.WINDOWS; |
72 | Matcher matcher = WINDOWS_PATTERN.matcher(from.getName()); |
73 | if (matcher.find()) { |
74 | osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap); |
75 | is64Bit = matcher.group(2).equals("x64"); |
76 | } |
77 | } else { |
78 | Matcher matcher = DEFAULT_PATTERN.matcher(from.getName()); |
79 | if (matcher.find()) { |
80 | try { |
81 | osFamily = OsFamily.fromValue(matcher.group(2).toLowerCase()); |
82 | } catch (IllegalArgumentException e) { |
83 | logger.debug("<< didn't match os(%s)", matcher.group(2)); |
84 | } |
85 | osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap); |
86 | } |
87 | } |
88 | return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit); |
89 | } |
90 | } |