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