Commits

Albert Hopkins committed 83b6786

Miscellaneous changes:

* Fixed bug introduced in colors backward-compatibility change

* startup() tries to find the correct kvm executable. Some systems use
"kvm" while others use qemu-kvm. This is normally only a problem when
migrating a project from on distro to another.

* Added reboot() function to fabfile

* Put "from custom import *" at the *end* of the fabfile (to allow
overriding functions provided by hemp

* Smarter argument parsing. I used argparse which is apparently only
avaliable in Python 2.7+

* Added --version argument to hemp.py

* Better exception handling in hemp.py

Comments (0)

Files changed (3)

         def __getattr__(self, name):
             return self.anycolor
     import fabric
-    fabric.colors = _Colors()
+    colors = _Colors()
+    fabric.colors = colors
 import libvirt
 import os
 import time
 
-# import custom fabfile
-from custom import *
-
 # we prefix this with "_" so that fabric does not think it's a fab function
 class _Cloud(object):
     """
 You first need to create them, e.g. use "fab create".""")
        return
 
+    possible_emulators = ['qemu-kvm', 'kvm', 'qemu']
     template_file = cloud.settings.TEMPLATE
 
     # override default template file for role-specific one, if one exists
             template_file = filename
             break
 
+    for possible_emulator in possible_emulators:
+        emulator = cloud.helpers.which(possible_emulator)
+        if emulator:
+            break
+    else:
+        print colors.red('Warning, could not locate the kvm executable',
+                bold=True)
+        emulator = '/usr/bin/qemu-kvm'
+
     xml = open(template_file).read() % {
             'name': env.host,
             'dirname': DIRNAME,
+            'emulator': emulator,
             }
 
+
     conn.createXML(xml, 0)
     if cloud.settings.STAGGER > 0:
         time.sleep(cloud.settings.STAGGER)
     run('halt')
 
 
+def reboot():
+    """Reboot the virtual machines
+    This does *not* remove the persitent-net udev rule
+    """
+    run('/sbin/reboot')
+
+
 def destroy():
     """Destory virtual machines"""
 
 # we dont' want libvirt errors poluting stderr. fix
 def _handler(context, error):
     pass
+libvirt.registerErrorHandler(_handler, None)
 
-libvirt.registerErrorHandler(_handler, None)
+# import custom fabfile
+from custom import *
+
     shutil.copy(tmpfile.name, state_file)
     tmpfile.close()
 
+
+def which(progname, path=None, test=None):
+    """Like the 'which' command, but works on any type of file/dir, not
+    just executables.  path is an interable of directories
+
+    Keyword arguments:
+    path -- a list/tuple of directories to search (defaults to system PATH)
+    test -- a test function which possible candidates will be passed as an
+            argument.  function should return True if the arguement should be
+            returned, else False.
+    """
+
+    import os
+
+    path = path or os.environ['PATH'].split(':')
+    for directory in path:
+        full_path = os.path.join( directory, progname )
+        if os.path.exists(full_path):
+            if test and not test(full_path):
+                return None
+            return full_path
+    return None
 #!/usr/bin/env python
 """Make fabric out of hemp!"""
 
+import argparse
 import os
 from os import path
 import shutil
 
 import settings
 
-__version__ = '0.4.1'
+__version__ = '0.4.2'
 __revision__ = '$Revision$'
 
 DIRS = ('images',)
         # try to make compatible with older versions of qemu than the source
         os_type.set('machine', 'pc')
 
+    # change <emulator>
+    emulator = domtree.find('devices/emulator')
+    if emulator is not None:
+        emulator.text = '%(emulator)s'
+
     # return resulting xml
     return ElementTree.ElementTree(domtree)
 
 def main():
     "Main program entry point"
 
-    basevm = sys.argv[1]
+    parser = argparse.ArgumentParser(description='Make fabric from hemp!')
+    parser.add_argument(
+            'basevm',
+            type=str,
+            help='Name of the domain to base the project on'
+    )
+    parser.add_argument(
+            'destdir',
+            help='Destination directory for project.',
+            type=str,
+            nargs='?',
+            default=None,
+    )
+    parser.add_argument(
+            '--version',
+            action='version',
+            version='%(prog)s ' + '%s (%s)' % (__version__,
+                __revision__.strip('$ ')),
+            help='Print version and exit.'
+    )
+    args = parser.parse_args()
+
+    basevm = args.basevm
+    basedir = args.destdir or basevm
 
     try:
-        basedir = sys.argv[2]
-    except IndexError:
-        # default to name of the basevm (in the current directory)
-        basedir = basevm
+        conn = libvirt.open(settings.LIBVIRT_URI)
+        dom = conn.lookupByName(basevm)
+    except libvirt.libvirtError as e:
+        sys.exit(1)
 
-    conn = libvirt.open(settings.LIBVIRT_URI)
-    dom = conn.lookupByName(basevm)
     xml = dom.XMLDesc(0)
     domtree = ElementTree.XML(xml)