From 4ae2d85301d04961d0894dc42e9843ea2da6c744 Mon Sep 17 00:00:00 2001
From: Robert Andrew Rankin <andrew@eiknet.com>
Date: Wed, 23 Feb 2011 13:37:57 -0500
Subject: [PATCH] Trac Ticket #510: Modified 'cobbler buildiso' to use /var/cache/cobbler/buildiso by default.  Added a /etc/cobbler/settings value of 'buildisodir' to make it setable by the end user.  --tempdir will still overwrite either setting on the command line.

Trac Ticket #509: Added 'self' as a argument to self.on_done() to keep the web inteface from stating that buildiso failed.
---
 cobbler/action_buildiso.py |   42 ++++++++++++++++++++++--------------------
 cobbler/api.py             |    4 ++--
 cobbler/cli.py             |    2 +-
 cobbler/remote.py          |    4 ++--
 cobbler/settings.py        |    1 +
 5 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/cobbler/action_buildiso.py b/cobbler/action_buildiso.py
index 0c4bd95..e12d63b 100644
--- a/cobbler/action_buildiso.py
+++ b/cobbler/action_buildiso.py
@@ -102,7 +102,7 @@ class BuildIso:
                   self.logger.info("skipping Xen distro: %s" % dist.name)
                   continue
               distname = self.make_shorter(dist.name)
-              # tempdir/isolinux/$distro/vmlinuz, initrd.img
+              # buildisodir/isolinux/$distro/vmlinuz, initrd.img
               # FIXME: this will likely crash on non-Linux breeds
               f1 = os.path.join(isolinuxdir, "%s.krn" % distname)
               f2 = os.path.join(isolinuxdir, "%s.img" % distname)
@@ -123,7 +123,7 @@ class BuildIso:
                  if dist.name.find("-xen") != -1:
                     continue
                  distname = self.make_shorter(dist.name)
-                 # tempdir/isolinux/$distro/vmlinuz, initrd.img
+                 # buildisodir/isolinux/$distro/vmlinuz, initrd.img
                  # FIXME: this will likely crash on non-Linux breeds
                  shutil.copyfile(dist.kernel, os.path.join(isolinuxdir, "%s.krn" % distname))
                  shutil.copyfile(dist.initrd, os.path.join(isolinuxdir, "%s.img" % distname))
@@ -293,7 +293,7 @@ class BuildIso:
                 utils.die(self.logger," Error, no installation source found. When building a standalone ISO, you must specify a --source if the distro install tree is not hosted locally")
 
         self.logger.info("copying kernels and initrds for standalone distro")
-        # tempdir/isolinux/$distro/vmlinuz, initrd.img
+        # buildisodir/isolinux/$distro/vmlinuz, initrd.img
         # FIXME: this will likely crash on non-Linux breeds
         f1 = os.path.join(isolinuxdir, "vmlinuz")
         f2 = os.path.join(isolinuxdir, "initrd.img")
@@ -352,7 +352,9 @@ class BuildIso:
         return
 
 
-    def run(self,iso=None,tempdir=None,profiles=None,systems=None,distro=None,standalone=None,source=None,exclude_dns=None):
+    def run(self,iso=None,buildisodir=None,profiles=None,systems=None,distro=None,standalone=None,source=None,exclude_dns=None):
+
+        self.settings = self.config.settings()
 
         # the distro option is for stand-alone builds only
         if not standalone and distro is not None:
@@ -371,28 +373,28 @@ class BuildIso:
         if iso is None:
             iso = "kickstart.iso"
 
-        if tempdir is None:
-            tempdir = os.path.join(os.getcwd(), "buildiso")
+        if buildisodir is None:
+            buildisodir = self.settings.buildisodir
         else:
-            if not os.path.isdir(tempdir):
+            if not os.path.isdir(buildisodir):
                 utils.die(self.logger,"The --tempdir specified is not a directory")
 
-            (tempdir_head,tempdir_tail) = os.path.split(os.path.normpath(tempdir))
-            if tempdir_tail != "buildiso":
-                tempdir = os.path.join(tempdir, "buildiso")
+            (buildisodir_head,buildisodir_tail) = os.path.split(os.path.normpath(buildisodir))
+            if buildisodir_tail != "buildiso":
+                buildisodir = os.path.join(buildisodir, "buildiso")
 
-        self.logger.info("using/creating tempdir: %s" % tempdir)
-        if not os.path.exists(tempdir):
-            os.makedirs(tempdir)
+        self.logger.info("using/creating buildisodir: %s" % buildisodir)
+        if not os.path.exists(buildisodir):
+            os.makedirs(buildisodir)
         else:
-            shutil.rmtree(tempdir)
-            os.makedirs(tempdir)
+            shutil.rmtree(buildisodir)
+            os.makedirs(buildisodir)
 
-        # if base of tempdir does not exist, fail
+        # if base of buildisodir does not exist, fail
         # create all profiles unless filtered by "profiles"
 
-        imagesdir = os.path.join(tempdir, "images")
-        isolinuxdir = os.path.join(tempdir, "isolinux")
+        imagesdir = os.path.join(buildisodir, "images")
+        isolinuxdir = os.path.join(buildisodir, "isolinux")
 
         self.logger.info("building tree for isolinux")
         if not os.path.exists(imagesdir):
@@ -427,14 +429,14 @@ class BuildIso:
         # removed --quiet
         cmd = "mkisofs -o %s -r -b isolinux/isolinux.bin -c isolinux/boot.cat" % iso
         cmd = cmd + " -no-emul-boot -boot-load-size 4"
-        cmd = cmd + " -boot-info-table -V Cobbler\ Install -R -J -T %s" % tempdir
+        cmd = cmd + " -boot-info-table -V Cobbler\ Install -R -J -T %s" % buildisodir
 
         rc = utils.subprocess_call(self.logger, cmd, shell=True)
         if rc != 0:
             utils.die(self.logger,"mkisofs failed")
 
         self.logger.info("ISO build complete")
-        self.logger.info("You may wish to delete: %s" % tempdir)
+        self.logger.info("You may wish to delete: %s" % buildisodir)
         self.logger.info("The output file is: %s" % iso)
 
         return True
diff --git a/cobbler/api.py b/cobbler/api.py
index 2d7c4cf..276319b 100644
--- a/cobbler/api.py
+++ b/cobbler/api.py
@@ -751,10 +751,10 @@ class BootAPI:
 
     # ==========================================================================
 
-    def build_iso(self,iso=None,profiles=None,systems=None,tempdir=None,distro=None,standalone=None,source=None, exclude_dns=None, logger=None):
+    def build_iso(self,iso=None,profiles=None,systems=None,buildisodir=None,distro=None,standalone=None,source=None, exclude_dns=None, logger=None):
         builder = action_buildiso.BuildIso(self._config, logger=logger)
         return builder.run(
-           iso=iso, profiles=profiles, systems=systems, tempdir=tempdir, distro=distro, standalone=standalone, source=source, exclude_dns=exclude_dns
+           iso=iso, profiles=profiles, systems=systems, buildisodir=buildisodir, distro=distro, standalone=standalone, source=source, exclude_dns=exclude_dns
         )
 
     # ==========================================================================
diff --git a/cobbler/cli.py b/cobbler/cli.py
index 33344c2..0926fcd 100755
--- a/cobbler/cli.py
+++ b/cobbler/cli.py
@@ -316,7 +316,7 @@ class BootCLI:
             self.parser.add_option("--iso",      dest="iso",  default=defaultiso, help="(OPTIONAL) output ISO to this path")
             self.parser.add_option("--profiles", dest="profiles", help="(OPTIONAL) use these profiles only")
             self.parser.add_option("--systems",  dest="systems",  help="(OPTIONAL) use these systems only")
-            self.parser.add_option("--tempdir",  dest="tempdir",  help="(OPTIONAL) working directory")
+            self.parser.add_option("--tempdir",  dest="buildisodir",  help="(OPTIONAL) working directory")
             self.parser.add_option("--distro",   dest="distro",   help="(OPTIONAL) used with --standalone to create a distro-based ISO including all associated profiles/systems")
             self.parser.add_option("--standalone", dest="standalone", action="store_true", help="(OPTIONAL) creates a standalone ISO with all required distro files on it")
             self.parser.add_option("--source",   dest="source",   help="(OPTIONAL) used with --standalone to specify a source for the distribution files")
diff --git a/cobbler/remote.py b/cobbler/remote.py
index eae98cd..5c84c1b 100644
--- a/cobbler/remote.py
+++ b/cobbler/remote.py
@@ -94,7 +94,7 @@ class CobblerThread(Thread):
         try:
             rc = self._run(self)
             self.remote._set_task_state(self,self.event_id,EVENT_COMPLETE)
-            self.on_done()
+            self.on_done(self)
             return rc
         except:
             utils.log_exc(self.logger)
@@ -147,7 +147,7 @@ class CobblerXMLRPCInterface:
                 self.options.get("iso","/var/www/cobbler/pub/generated.iso"),
                 self.options.get("profiles",None),
                 self.options.get("systems",None),
-                self.options.get("tempdir",None),
+                self.options.get("buildisodir",None),
                 self.options.get("distro",None),
                 self.options.get("standalone",False),
                 self.options.get("source",None),
diff --git a/cobbler/settings.py b/cobbler/settings.py
index 5dce138..b92c05c 100644
--- a/cobbler/settings.py
+++ b/cobbler/settings.py
@@ -97,6 +97,7 @@ DEFAULTS = {
     "template_remote_kickstarts"  : 0,
     "virt_auto_boot"              : 0,
     "webdir"                      : "/var/www/cobbler",
+    "buildisodir"                 : "/var/cache/cobbler/buildiso",
     "xmlrpc_port"                 : 25151,
     "yum_post_install_mirror"     : 1,
     "createrepo_flags"            : "-c cache -s sha",
-- 
1.5.5.6

