7dfb056
From b36b33335c4872bc1bef5bcece33c3ea43de4ea5 Mon Sep 17 00:00:00 2001
7dfb056
From: Tomas Karasek <tomas.karasek@cern.ch>
7dfb056
Date: Wed, 27 Jun 2012 16:48:15 +0100
7dfb056
Subject: [PATCH] support package installation using 'yum'
7dfb056
7dfb056
---
7dfb056
 cloudinit/CloudConfig/__init__.py |   42 +++++++++++++++++++++++++++++++++---
7dfb056
 1 files changed, 38 insertions(+), 4 deletions(-)
7dfb056
7dfb056
diff --git a/cloudinit/CloudConfig/__init__.py b/cloudinit/CloudConfig/__init__.py
7dfb056
index a16bdde..6a7005a 100644
7dfb056
--- a/cloudinit/CloudConfig/__init__.py
7dfb056
+++ b/cloudinit/CloudConfig/__init__.py
7dfb056
@@ -27,6 +27,7 @@ import traceback
7dfb056
 import os
7dfb056
 import subprocess
7dfb056
 import time
7dfb056
+import platform
7dfb056
 
7dfb056
 per_instance = cloudinit.per_instance
7dfb056
 per_always = cloudinit.per_always
7dfb056
@@ -253,6 +254,27 @@ def run_per_instance(name, func, args, clear_on_fail=False):
7dfb056
         raise
7dfb056
 
7dfb056
 
7dfb056
+def get_package_manager():
7dfb056
+    if 'linux_distribution' in platform.__dict__:
7dfb056
+        distname, _, _ = platform.linux_distribution(
7dfb056
+            full_distribution_name=0)
7dfb056
+    else:
7dfb056
+        distname, _, _ = platform.dist()
7dfb056
+    yum_dists = ['redhat', 'fedora', 'centos']
7dfb056
+    apt_dists = ['debian', 'ubuntu']
7dfb056
+    if distname.lower() in yum_dists:
7dfb056
+        return 'yum'
7dfb056
+    elif distname.lower() in apt_dists:
7dfb056
+        return 'apt'
7dfb056
+    elif os.system('yum --help >/dev/null 2>&1'):
7dfb056
+        return 'yum'
7dfb056
+    elif os.system('apt-get --help >/dev/null 2>&1'):
7dfb056
+        return 'apt'
7dfb056
+
7dfb056
+
7dfb056
+_PACKAGE_MANAGER = get_package_manager()
7dfb056
+
7dfb056
+
7dfb056
 # apt_get top level command (install, update...), and args to pass it
7dfb056
 def apt_get(tlc, args=None):
7dfb056
     if args is None:
7dfb056
@@ -265,10 +287,22 @@ def apt_get(tlc, args=None):
7dfb056
     subprocess.check_call(cmd, env=e)
7dfb056
 
7dfb056
 
7dfb056
-def update_package_sources():
7dfb056
-    run_per_instance("update-sources", apt_get, ("update",))
7dfb056
+def yum(tlc, args=None):
7dfb056
+    if args is None:
7dfb056
+        args = []
7dfb056
+    cmd = ['yum', '-y', tlc]
7dfb056
+    cmd.extend(args)
7dfb056
+    subprocess.check_call(cmd)
7dfb056
 
7dfb056
 
7dfb056
 def install_packages(pkglist):
7dfb056
-    update_package_sources()
7dfb056
-    apt_get("install", pkglist)
7dfb056
+    if _PACKAGE_MANAGER == "yum":
7dfb056
+        run_per_instance("update-sources", yum, ("makecache",))
7dfb056
+        yum("install", pkglist)
7dfb056
+    elif _PACKAGE_MANAGER == "apt":
7dfb056
+        run_per_instance("update-sources", apt_get, ("update",))
7dfb056
+        apt_get("install", pkglist)
7dfb056
+    else:
7dfb056
+        raise Exception("Unknown distribution, unable to install packages %s" %
7dfb056
+            pkglist)
7dfb056
+
7dfb056
-- 
7dfb056
1.7.6.4
7dfb056