[Erp5-report] r35071 rafael - /erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri May 7 00:19:27 CEST 2010


Author: rafael
Date: Fri May  7 00:19:27 2010
New Revision: 35071

URL: http://svn.erp5.org?rev=35071&view=rev
Log:
Refactor Recipe to provide more flexibility for configure kvm and support -drive arguments.

Modified:
    erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/__init__.py
    erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/kvm.py

Modified: erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/__init__.py
URL: http://svn.erp5.org/erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/__init__.py?rev=35071&r1=35070&r2=35071&view=diff
==============================================================================
--- erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/__init__.py [utf8] (original)
+++ erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/__init__.py [utf8] Fri May  7 00:19:27 2010
@@ -20,6 +20,7 @@
 import zc.buildout.easy_install
 import zc.buildout.download
 import zc.recipe.egg
+import subprocess
 
 class Recipe(object):
     
@@ -40,60 +41,78 @@
     options.setdefault('kvm_run_directory',
                        buildout['buildout']['var-directory'])
 
-    options.setdefault("url", '')
+    # File is preferred, if defined image will be ignored.
+    options.setdefault("image", '')
+    options.setdefault("file", '')
+
+    options.setdefault("image_type", "drive")
+
     options.setdefault("md5sum", '')
     options.setdefault("gzip", 'true')
     options.setdefault("gzip_md5", '')
 
-    # XXX Informations to connect to instance
-    # this should be used to create the commands to 
-    # Manipulate it.
-    options.setdefault("ssh_port", "") # XXX
-    options.setdefault("ssh_hostname", "") # XXX
-    options.setdefault("ssh_key_path", "") # XXX
-    options.setdefault("ssh_user", "") # XXX
+    options.setdefault("kvm_vnc", "")
+    options.setdefault("kvm_graphic", "")
+    options.setdefault("kvm_redir", "")
+    options.setdefault("kvm_net", "")
 
   def install(self):
     options = self.options
     location = options['location']
     if not os.path.exists(location):
       os.mkdir(location)
-    download_file, is_temp = self.download()
-    image = self.copy(download_file)
-    ssh_port = options.get("ssh_port")
-    ssh_hostname = options.get("ssh_hostname")
-    ssh_key_path = options.get("ssh_key_path")
-    ssh_user = options.get("ssh_user")
-    md5_image = self.options.get("md5_image", None)
+
+    if options.get('file') in ["", None ]:
+      download_file, is_temp = self.download()
+      image = self.copy(download_file)
+    else:
+      image = options.get('file')
+
+    md5_image = options.get("md5_image", None)
     assert zc.buildout.download.check_md5sum(image, md5_image) == True
 
+    image_type = options.get("image_type")
+    if image_type == "drive":
+      kvm_drive = "-drive file=" + ','.join([image, "if=virtio", "cache=none", "boot=on"])
+    else:
+      kvm_drive = "-%s %s" % (image_type, image)
+
+    kvm_redir = options.get('kvm_redir').split()
     requirements, ws = self.egg.working_set(['erp5.recipe.kvm'])
-    script_name = "%s-sendfile" % self.name
-    scripts = zc.buildout.easy_install.scripts(
-        [(script_name,'erp5.recipe.kvm.ssh', 'put')],
-        ws, options['executable'], options.get("kvm_bin_directory"),
-        arguments = ("\n        '%s', '%s',"
-                     "\n        '%s', '%s', "
-                     "\n        sys.argv[1], sys.argv[2]" % (ssh_user, ssh_hostname,
-                                                             ssh_port, ssh_key_path)))
-    
-    script_name = "%s-getfile" % self.name
-    scripts = zc.buildout.easy_install.scripts(
-        [(script_name,'erp5.recipe.kvm.ssh', 'get')],
-        ws, options['executable'], options.get("kvm_bin_directory"),
-        arguments = ("\n        '%s', '%s',"
-                     "\n        '%s', '%s', "
-                     "\n        sys.argv[1], sys.argv[2]" % (ssh_user, ssh_hostname,
-                                                             ssh_port, ssh_key_path)))
+    if options.get("ssh_port", "") not in ["", None ]:
+      ssh_port = options.get("ssh_port")
+      ssh_hostname = options.get("ssh_hostname", "localhost")
+      ssh_key_path = options.get("ssh_key_path")
+      ssh_user = options.get("ssh_user", "root")
 
-    script_name = "%s-runscript" % self.name
-    scripts = zc.buildout.easy_install.scripts(
-        [(script_name,'erp5.recipe.kvm.ssh', 'run')],
-        ws, options['executable'], options.get("kvm_bin_directory"),
-        arguments = ("\n        '%s', '%s',"
-                     "\n        '%s', '%s', "
-                     "\n        sys.argv[1]" % (ssh_user, ssh_hostname,
-                                                ssh_port, ssh_key_path)))
+      kvm_redir.append(" tcp:%s::22 " % ssh_port)
+
+      script_name = "%s-sendfile" % self.name
+      scripts = zc.buildout.easy_install.scripts(
+          [(script_name,'erp5.recipe.kvm.ssh', 'put')],
+          ws, options['executable'], options.get("kvm_bin_directory"),
+          arguments = ("\n        '%s', '%s',"
+                       "\n        '%s', '%s', "
+                       "\n        sys.argv[1], sys.argv[2]" % (ssh_user, ssh_hostname,
+                                                               ssh_port, ssh_key_path)))
+      
+      script_name = "%s-getfile" % self.name
+      scripts = zc.buildout.easy_install.scripts(
+          [(script_name,'erp5.recipe.kvm.ssh', 'get')],
+          ws, options['executable'], options.get("kvm_bin_directory"),
+          arguments = ("\n        '%s', '%s',"
+                       "\n        '%s', '%s', "
+                       "\n        sys.argv[1], sys.argv[2]" % (ssh_user, ssh_hostname,
+                                                               ssh_port, ssh_key_path)))
+  
+      script_name = "%s-runscript" % self.name
+      scripts = zc.buildout.easy_install.scripts(
+          [(script_name,'erp5.recipe.kvm.ssh', 'run')],
+          ws, options['executable'], options.get("kvm_bin_directory"),
+          arguments = ("\n        '%s', '%s',"
+                       "\n        '%s', '%s', "
+                       "\n        sys.argv[1]" % (ssh_user, ssh_hostname,
+                                                  ssh_port, ssh_key_path)))
     script_name = "%s-ctl" % self.name
     # XXX The followed code requires some improvements
     snapshot = "False"
@@ -101,19 +120,25 @@
     if kvm_snapshot == 'true': 
       snapshot = "True"
 
-    kvm_vnc = options.get("kvm_vnc", "")
-    kvm_graphic = options.get("kvm_graphic", "")
-
     kvm_pid_file = "%s/%s.pid" % (options.get('kvm_run_directory'), self.name)
     scripts = zc.buildout.easy_install.scripts(
         [(script_name,'erp5.recipe.kvm.kvm', 'ctl')],
         ws, options['executable'], options.get("kvm_bin_directory"),
-        arguments = ("\n        '%s', '%s',      "
-                     "\n        '%s' , '%s',     "
-                     "\n        %s , '%s', '%s', "
-                     "\n        sys.argv[1]" % (image, ssh_port,
-                                                kvm_pid_file, self.name,
-                                                snapshot, kvm_vnc, kvm_graphic)))
+        arguments = ("\n        '%s', "
+                     "\n        '%s', "
+                     "\n        %s, "
+                     "\n        %s, "
+                     "\n         %s , "
+                     "\n        '%s', "
+                     "\n        '%s', "
+                     "\n        sys.argv[1]" % (
+                                   kvm_drive, 
+                                   kvm_pid_file,
+                                   options.get('kvm_net').split(),
+                                   kvm_redir,
+                                   snapshot, 
+                                   options.get("kvm_vnc"), 
+                                   options.get("kvm_graphic"))))
     # XXX Improvements until here
     return [] # instance related recipe, it can be disaster to allow buildout
               # to remove instances
@@ -121,7 +146,7 @@
   def download(self):
     """Download image file
     """
-    url = self.options['url']
+    url = self.options['image']
     namespace = self.options['recipe']
     download = zc.buildout.download.Download(self.buildout['buildout'],
                                              namespace=namespace,
@@ -139,18 +164,17 @@
       return image
 
     if self.options["gzip"].lower() == "true":
-      self._gzip(download_file, image)
+      self._gunzip(download_file, image)
       return image
 
     os.copy(download_file, image)
     return image
     
-  def _gzip(self, download_file, image):
-    """ Uncompress image into in case it is compressed
+  def _gunzip(self, download_file, image):
+    """ Use gunzip command to uncompress image.
     """
-    # XXX USE Subprocess.
-    code = os.system('gunzip -cd %s > %s' % (download_file, 
-                                                   image))
+    command = 'gunzip -cd %s > %s' % (download_file, image)
+    code = subprocess.call(command, shell=True)
     assert code == 0
     
   update = install

Modified: erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/kvm.py
URL: http://svn.erp5.org/erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/kvm.py?rev=35071&r1=35070&r2=35071&view=diff
==============================================================================
--- erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/kvm.py [utf8] (original)
+++ erp5/trunk/utils/erp5.recipe.kvm/src/erp5/recipe/kvm/kvm.py [utf8] Fri May  7 00:19:27 2010
@@ -2,44 +2,46 @@
 import os
 import signal
 
-BASE_COMMAND = "kvm -net nic,model=ne2k_pci -redir tcp:%s::22 -k fr -net user,hostname=%s -m 1024 %s -daemonize -pidfile %s %s "
+USE_DRIVE = 1
+
+BASE_COMMAND = "kvm %s %s -k fr -m 1024 %s -daemonize -pidfile %s %s "
 
 class Kvm:
-  def __init__(self, image, port, pid_file, hostname, 
-		     snapshot=True, vnc=None, graphic=None):
+  def __init__(self, drive, pid_file, net, redir, snapshot=True, vnc=None, graphic=None):
     """ Handle the KVM command for a certain image, and 
         start ssh in a certain port.
 
 	image: Image Path
     """
     # XXX Make drive configuration configurable
-    #self.drive = "-drive file=" + ','.join([image, "if=virtio", "cache=none", "boot=on"])
-    self.drive = "-hda %s" % (image)
-    self.ssh_port = port
+    self.drive = drive
+    self.net = ''.join([" -net %s " % n for n in net ])
+    self.redir = ''.join([" -redir %s " % r for r in redir])
+
     self.kvm_pid_file = pid_file
-    self.kvm_hostname = hostname
-    self.kvm_extra_args = ""
+    self.kvm_args = ""
     if vnc not in [None, ""]:
-      self.kvm_extra_args += " -vnc %s " % vnc
+      self.kvm_args += " -vnc %s " % vnc
 
     if graphic not in [None, ""]:
-      self.kvm_extra_args += " -vga %s " % (graphic)
+      self.kvm_args += " -vga %s " % (graphic)
     else:
-      self.kvm_extra_args += " -nographic "
+      self.kvm_args += " -nographic "
 
     if snapshot:
-      self.kvm_extra_args += " -snapshot "
+      self.kvm_args += " -snapshot "
 
 
   def start(self):
     """ Start Kvm in background."""
-    command = BASE_COMMAND % ( self.ssh_port,
-                               self.kvm_hostname,
+    command = BASE_COMMAND % ( self.net,
+                               self.redir,
                                self.drive,
                                self.kvm_pid_file, 
-                               self.kvm_extra_args)
+                               self.kvm_args)
     print command
-    subprocess.call(command, shell=True)
+    return_code = subprocess.call(command, shell=True)
+    assert return_code == 0
 
   def stop(self):
     """ Stop Kvm in the server """
@@ -53,7 +55,8 @@
     os.kill(pid, signal.SIGKILL)
 
   def status(self):
-    """ Check Kvm status """
+    """ Check if Kvm process is running.
+    """
     try:
       pid = int(open(self.kvm_pid_file, 'r').read().strip())
     except:
@@ -67,15 +70,20 @@
     return True
 
   def restart(self):
+    """ Stop and starts the KVM 
+    """
     self.stop()
     self.start()
 
-def ctl(image, port, pid_file, hostname, snapshot, vnc, graphic, command):
+def ctl(drive, pid_file, net, redir, 
+        snapshot, vnc, graphic, command):
   """ Control KVM Command
   """
-  handler = Kvm(image, port, pid_file, hostname, snapshot, vnc, graphic)
+  handler = Kvm(drive, pid_file, net, redir, snapshot, vnc, graphic)
   command = getattr(handler, command, None)
   if command is not None:
-    return command()
- 
+    print command()
+    return
+
+  print "ERROR!"
   return "ERROR!"




More information about the Erp5-report mailing list