|
@@ -17,86 +17,140 @@
|
|
|
*/
|
|
|
package org.apache.ambari.server.state.stack;
|
|
|
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.lang.reflect.Type;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Map.Entry;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
+
|
|
|
+import com.google.inject.Singleton;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
|
|
+import org.apache.ambari.server.configuration.Configuration;
|
|
|
+
|
|
|
/**
|
|
|
* Class that encapsulates OS family logic
|
|
|
*/
|
|
|
+@Singleton
|
|
|
public class OsFamily {
|
|
|
+ private final String os_pattern = "([^\\d]*)([\\d]*)";
|
|
|
+ private final String OS_DISTRO = "distro";
|
|
|
+ private final String OS_VERSION = "versions";
|
|
|
+ private final String LOAD_CONFIG_MSG = "Could not load OS family definition from %s file";
|
|
|
+ private final String FILE_NAME = "os_family.json";
|
|
|
+ private final Logger LOG = LoggerFactory.getLogger(OsFamily.class);
|
|
|
|
|
|
- private static final String FILE_NAME = "os_family.json";
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger(OsFamily.class);
|
|
|
-
|
|
|
- private static Map<String, Set<String>> osMap = new HashMap<String, Set<String>>();
|
|
|
-
|
|
|
- static {
|
|
|
- try {
|
|
|
- InputStream inputStream = OsFamily.class.getClassLoader().getResourceAsStream(FILE_NAME);
|
|
|
- if (null == inputStream)
|
|
|
- inputStream = ClassLoader.getSystemResourceAsStream(FILE_NAME);
|
|
|
-
|
|
|
- Type type = new TypeToken<Map<String, Set<String>>>(){}.getType();
|
|
|
- Gson gson = new Gson();
|
|
|
- osMap = gson.fromJson(new InputStreamReader(inputStream), type);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- LOG.error("Could not load OS family definition, using defaults");
|
|
|
- osMap.put("centos5", new HashSet<String>());
|
|
|
- osMap.put("centos6", new HashSet<String>());
|
|
|
- osMap.put("suse11", new HashSet<String>());
|
|
|
- osMap.put("ubuntu12", new HashSet<String>());
|
|
|
-
|
|
|
- Collections.addAll(osMap.get("centos5"), "centos5", "redhat5", "oraclelinux5", "rhel5");
|
|
|
- Collections.addAll(osMap.get("centos6"), "centos6", "redhat6", "oraclelinux6", "rhel6");
|
|
|
- Collections.addAll(osMap.get("suse11"), "suse11", "sles11", "opensuse11");
|
|
|
- Collections.addAll(osMap.get("ubuntu12"), "ubuntu12");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ private Map<String, Map<String, Set<String>>> osMap = null;
|
|
|
|
|
|
/**
|
|
|
- * Gets the array of compatible OS types
|
|
|
- * @param os the os
|
|
|
- * @return all types that are compatible with the supplied type
|
|
|
+ * Initialize object
|
|
|
+ * @param conf Configuration instance
|
|
|
*/
|
|
|
- static Set<String> findTypes(String os) {
|
|
|
- for (Entry<String, Set<String>> entry : osMap.entrySet()) {
|
|
|
- for (String type : entry.getValue()) {
|
|
|
- if (type.equals(os))
|
|
|
- return Collections.unmodifiableSet(entry.getValue());
|
|
|
- }
|
|
|
+ public OsFamily(Configuration conf){
|
|
|
+ init(conf.getSharedResourcesDirPath());
|
|
|
}
|
|
|
- return Collections.emptySet();
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * Finds the family for the specific OS
|
|
|
- * @param os the OS
|
|
|
- * @return the family, or <code>null</code> if not defined
|
|
|
+ * Initialize object
|
|
|
+ * @param properties list of properties
|
|
|
*/
|
|
|
- public static String find(String os) {
|
|
|
- for (Entry<String, Set<String>> entry : osMap.entrySet()) {
|
|
|
- for (String type : entry.getValue()) {
|
|
|
- if (type.equals(os))
|
|
|
- return entry.getKey();
|
|
|
+ public OsFamily(Properties properties){
|
|
|
+ init(properties.getProperty(Configuration.SHARED_RESOURCES_DIR_KEY));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init(String SharedResourcesPath){
|
|
|
+ try {
|
|
|
+ File f = new File(SharedResourcesPath, FILE_NAME);
|
|
|
+ if (!f.exists()) throw new Exception();
|
|
|
+ FileInputStream inputStream = new FileInputStream(f);
|
|
|
+
|
|
|
+ Type type = new TypeToken<Map<String, Map<String, Set<String>>>>() {}.getType();
|
|
|
+ Gson gson = new Gson();
|
|
|
+ osMap = gson.fromJson(new InputStreamReader(inputStream), type);
|
|
|
+ inputStream.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error(String.format(LOAD_CONFIG_MSG, new File(SharedResourcesPath, FILE_NAME).toString()));
|
|
|
+ throw new RuntimeException(LOAD_CONFIG_MSG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Separate os name from os major version
|
|
|
+ * @param os the os
|
|
|
+ * @return separated os name and os version
|
|
|
+ */
|
|
|
+ private Map<String,String> parse_os(String os){
|
|
|
+ Map<String,String> pos = new HashMap<String,String>();
|
|
|
+
|
|
|
+ Pattern r = Pattern.compile(os_pattern);
|
|
|
+ Matcher m = r.matcher(os);
|
|
|
+
|
|
|
+ if (m.find()){
|
|
|
+ pos.put(OS_DISTRO, m.group(1));
|
|
|
+ pos.put(OS_VERSION, m.group(2));
|
|
|
+ } else {
|
|
|
+ pos.put(OS_DISTRO, os);
|
|
|
+ pos.put(OS_VERSION, "");
|
|
|
+ }
|
|
|
+ return pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Gets the array of compatible OS types
|
|
|
+ * @param os the os
|
|
|
+ * @return all types that are compatible with the supplied type
|
|
|
+ */
|
|
|
+ public Set<String> findTypes(String os) {
|
|
|
+ Map<String,String> pos = parse_os(os);
|
|
|
+ for ( String family : osMap.keySet()) {
|
|
|
+ Map<String, Set<String>> fam = osMap.get(family);
|
|
|
+ if (fam.get(OS_DISTRO).contains(pos.get(OS_DISTRO)) && fam.get(OS_VERSION).contains(pos.get(OS_VERSION))){
|
|
|
+ Set<String> data=new HashSet<String>();
|
|
|
+ for (String item: fam.get(OS_DISTRO)) data.add(item + pos.get(OS_VERSION));
|
|
|
+ return Collections.unmodifiableSet(data);
|
|
|
+ }
|
|
|
}
|
|
|
+ return Collections.emptySet();
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
|
|
|
-}
|
|
|
+ /**
|
|
|
+ * Finds the family for the specific OS
|
|
|
+ * @param os the OS
|
|
|
+ * @return the family, or <code>null</code> if not defined
|
|
|
+ */
|
|
|
+ public String find(String os) {
|
|
|
+ Map<String,String> pos = parse_os(os);
|
|
|
+ for ( String family : osMap.keySet()) {
|
|
|
+ Map<String, Set<String>> fam = osMap.get(family);
|
|
|
+ if (fam.get(OS_DISTRO).contains(pos.get(OS_DISTRO)) && fam.get(OS_VERSION).contains(pos.get(OS_VERSION))){
|
|
|
+ return family + pos.get(OS_VERSION);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Form list of all supported os types
|
|
|
+ * @return one dimension list with os types
|
|
|
+ */
|
|
|
+ public Set<String> os_list(){
|
|
|
+ Set<String> r= new HashSet<String>();
|
|
|
+ for ( String family : osMap.keySet()) {
|
|
|
+ Map<String, Set<String>> fam = osMap.get(family);
|
|
|
+ for (String version: fam.get(OS_VERSION)){
|
|
|
+ Set<String> data=new HashSet<String>();
|
|
|
+ for (String item: fam.get(OS_DISTRO)) data.add(item + version);
|
|
|
+ r.addAll(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+}
|