| 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.compute.predicates; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import org.jclouds.compute.domain.OperatingSystem; |
| 24 | |
| 25 | import com.google.common.base.Predicate; |
| 26 | import com.google.common.base.Predicates; |
| 27 | import com.google.common.collect.Sets; |
| 28 | |
| 29 | /** |
| 30 | * Container for operating system filters (predicates). |
| 31 | * |
| 32 | * This class has static methods that create customized predicates to use with |
| 33 | * {@link org.jclouds.compute.ComputeService}. |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public class OperatingSystemPredicates { |
| 38 | /** |
| 39 | * evaluates true if the OperatingSystem is unix like |
| 40 | * |
| 41 | */ |
| 42 | public static Predicate<OperatingSystem> isUnix() { |
| 43 | return new Predicate<OperatingSystem>() { |
| 44 | @Override |
| 45 | public boolean apply(OperatingSystem os) { |
| 46 | if (os.getFamily() != null) { |
| 47 | switch (os.getFamily()) { |
| 48 | case WINDOWS: |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | for (String toMatch : searchStrings(os)) |
| 53 | if (toMatch != null && toMatch.toLowerCase().indexOf("windows") != -1) |
| 54 | return false; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return "isUnix()"; |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * evaluates true if the OperatingSystem supports the apt installer |
| 67 | * |
| 68 | */ |
| 69 | public static Predicate<OperatingSystem> supportsApt() { |
| 70 | return new Predicate<OperatingSystem>() { |
| 71 | @Override |
| 72 | public boolean apply(OperatingSystem os) { |
| 73 | if (os.getFamily() != null) { |
| 74 | switch (os.getFamily()) { |
| 75 | case DEBIAN: |
| 76 | case UBUNTU: |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | for (String toMatch : searchStrings(os)) |
| 81 | if (toMatch != null && toMatch.toLowerCase().indexOf("ubuntu") != -1 |
| 82 | || toMatch.toLowerCase().indexOf("debian") != -1) |
| 83 | return true; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return "supportsApt()"; |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * evaluates true if the OperatingSystem supports the yum installer |
| 96 | * |
| 97 | */ |
| 98 | public static Predicate<OperatingSystem> supportsYum() { |
| 99 | return new Predicate<OperatingSystem>() { |
| 100 | @Override |
| 101 | public boolean apply(OperatingSystem os) { |
| 102 | if (os.getFamily() != null) { |
| 103 | switch (os.getFamily()) { |
| 104 | case CENTOS: |
| 105 | case AMZN_LINUX: |
| 106 | case FEDORA: |
| 107 | case RHEL: |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | for (String toMatch : searchStrings(os)) |
| 113 | if (toMatch.toLowerCase().indexOf("centos") != -1 || toMatch.toLowerCase().indexOf("rhel") != -1 |
| 114 | || toMatch.toLowerCase().replace(" ", "").indexOf("redhate") != -1 |
| 115 | || toMatch.toLowerCase().indexOf("fedora") != -1) |
| 116 | return true; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return "supportsYum()"; |
| 123 | } |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * evaluates true if the OperatingSystem supports the zypper installer |
| 129 | * |
| 130 | */ |
| 131 | public static Predicate<OperatingSystem> supportsZypper() { |
| 132 | return new Predicate<OperatingSystem>() { |
| 133 | @Override |
| 134 | public boolean apply(OperatingSystem os) { |
| 135 | if (os.getFamily() != null) { |
| 136 | switch (os.getFamily()) { |
| 137 | case SUSE: |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | for (String toMatch : searchStrings(os)) |
| 142 | if (toMatch != null && toMatch.toLowerCase().indexOf("suse") != -1) |
| 143 | return true; |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public String toString() { |
| 149 | return "supportsZypper()"; |
| 150 | } |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * return everything. |
| 156 | */ |
| 157 | public static Predicate<OperatingSystem> any() { |
| 158 | return Predicates.<OperatingSystem> alwaysTrue(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * return true if this is a 64bit os. |
| 163 | */ |
| 164 | public static Predicate<OperatingSystem> is64Bit() { |
| 165 | return new Predicate<OperatingSystem>() { |
| 166 | @Override |
| 167 | public boolean apply(OperatingSystem os) { |
| 168 | return os.is64Bit(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public String toString() { |
| 173 | return "is64Bit()"; |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | static Iterable<String> searchStrings(OperatingSystem os) { |
| 179 | Set<String> search = Sets.newLinkedHashSet(); |
| 180 | if (os.getName() != null) |
| 181 | search.add(os.getName()); |
| 182 | if (os.getDescription() != null) |
| 183 | search.add(os.getDescription()); |
| 184 | return search; |
| 185 | } |
| 186 | |
| 187 | } |