From be147b8b09aa9ee596e32146cad3d97d3fafee51 Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@ubuntu.com>
Date: Fri, 10 Sep 2010 17:12:41 -0400
Subject: set grub-pc values based on user input or automatically

LP: #623609
---
 cloudinit/CloudConfig/cc_grub_dpkg.py | 77 +++++++++++++++++++++++++++++++++++
 cloudinit/__init__.py                 |  1 +
 doc/examples/cloud-config.txt         |  1 +
 3 files changed, 79 insertions(+)
 create mode 100644 cloudinit/CloudConfig/cc_grub_dpkg.py

diff --git a/cloudinit/CloudConfig/cc_grub_dpkg.py b/cloudinit/CloudConfig/cc_grub_dpkg.py
new file mode 100644
index 00000000..72538c79
--- /dev/null
+++ b/cloudinit/CloudConfig/cc_grub_dpkg.py
@@ -0,0 +1,77 @@
+# vi: ts=4 expandtab
+#
+#    Copyright (C) 2009-2010 Canonical Ltd.
+#
+#    Author: Scott Moser <scott.moser@canonical.com>
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License version 3, as
+#    published by the Free Software Foundation.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import cloudinit.util as util
+import subprocess
+import traceback
+
+shcmd="""
+unset idevs idevs_empty
+if [ -n "${1}" ]; then
+    idevs=${1}
+    [ "$1" = "None" ] && idevs=""
+fi
+if [ -n "${2}" ]; then
+    idevs_empty=${2}
+    [ "$2" = "None" ] && idevs_empty=""
+fi
+
+f_idevs=""
+f_idevs_empty=""
+if [ -b /dev/sda1 -a ! -e /dev/sda ]; then
+  f_idevs=""
+  f_idevs_empty=true
+else
+  f_idevs=/dev/sda
+  for dev in /dev/sda /dev/vda /dev/sda1 /dev/vda1; do
+      [ -b "${dev}" ] && f_idevs=${dev} && break;
+  done
+  f_idevs_empty=false
+fi
+
+idevs=${idevs-${f_idevs}}
+idevs_empty=${idevs_empty-${f_idevs_empty}}
+
+printf "%s\t%s\t%s\t%s\n%s\t%s\t%s\t%s\n" \
+  grub-pc grub-pc/install_devices string "${idevs}" \
+  grub-pc grub-pc/install_devices_empty boolean "${idevs_empty}" |
+  debconf-set-selections
+"""
+
+def handle(name,cfg,cloud,log,args):
+    
+    idevs=""
+    idevs_empty=""
+
+    if "grub-dpkg" in cfg:
+        idevs=util.get_cfg_option_str(cfg["grub-dpkg"],
+            "grub-pc/install_devices","")
+        idevs_empty=util.get_cfg_option_str(cfg["grub-dpkg"],
+            "grub-pc/install_devices_empty","")
+
+    cmd = [ "/bin/sh", "-c", shcmd, 'grub-dpkg', idevs, idevs_empty ]
+
+    log.debug("invoking grub-dpkg with '%s','%s'" % (idevs,idevs_empty))
+
+    try:
+        subprocess.check_call(cmd)
+    except subprocess.CalledProcessError as e:
+        log.debug(traceback.format_exc(e))
+        raise Exception("Cmd returned %s: %s" % ( e.returncode, cmd))
+    except OSError as e:
+        log.debug(traceback.format_exc(e))
+        raise Exception("Cmd failed to execute: %s" % ( cmd ))
diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py
index 830bc17b..6a59a23f 100644
--- a/cloudinit/__init__.py
+++ b/cloudinit/__init__.py
@@ -41,6 +41,7 @@ cloud_config_modules:
  - mounts
  - ssh-import-id
  - ssh
+ - grub-dpkg
  - apt-update-upgrade
  - puppet
  - updates-check
diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt
index 5525099b..3a46a481 100644
--- a/doc/examples/cloud-config.txt
+++ b/doc/examples/cloud-config.txt
@@ -206,6 +206,7 @@ cloud_config_modules:
  - mounts
  - ssh-import-id
  - ssh
+ - grub-dpkg
  - [ apt-update-upgrade, always ]
  - puppet
  - updates-check
-- 
cgit v1.2.3