clean code
authorPaul Tonelli <paul.tonelli@logilab.fr>
Thu, 21 Mar 2013 14:35:02 +0100
changeset 4 9b1b64ddeebd
parent 3 6cca0ba6876f
child 5 430383a302c7
clean code
_modules/openstack_client.py
_modules/vm_create.py
--- a/_modules/openstack_client.py	Tue Feb 26 18:44:37 2013 +0100
+++ b/_modules/openstack_client.py	Thu Mar 21 14:35:02 2013 +0100
@@ -6,21 +6,40 @@
 from os import environ
 from glance import client as glance_client
 
-def set_vars():
+def _set_credentials(id_elements=None, force_update=False):
     '''
     really ugly, but works
     until credentials expire
     '''
-    environ['OS_AUTH_URL'] = 'http://control.example.com:5000/v2.0'
-    environ['OS_TENANT_ID'] = '00000000000000000000000000000000'
-    environ['OS_TENANT_NAME'] = 'tenant'
-    environ['OS_USERNAME'] = 'username'
-    environ['OS_PASSWORD'] = 'password'
+    if id_elements == None:
+        id_elements={
+                'OS_AUTH_URL': 'http://control.example.com:5000/v2.0',
+                'OS_TENANT_ID': '00000000000000000000000000000000',
+                'OS_TENANT_NAME':'tenant',
+                'OS_USERNAME': 'username',
+                'OS_PASSWORD': 'password'
+                }
+        for key in id_elements:
+            if force_update or key not in environ:
+                environ[key] = id_elements[key]
+    return True
+
+def delete_image(id_image):
+     '''
+    delete image from glance 
+
+    CLI Example::
+      
+        salt '*'  openstack_client.delete_image 00000000-0000-0000-0000-000000000000
+    '''
+    print __salt__['cmd.run']("glance delete " + id_image)
+
 
 def get_running_instances():
+    _set_credentials()
     data = __salt__['nova.list']()
-    print data
-    set_vars()
+    for i in data:
+        print str(i) + " len : " + str(len(i))
     gc = glance_client.get_client(host='0.0.0.0')
     print gc.get_images()
     return True
@@ -32,3 +51,19 @@
         if i.find("keystone") != -1:
             print i + " : " + pillars_val[i]
     return True
+
+def upload_to_glance(path, filename, disk_format, other_params, 
+        image_name=None):
+    _set_credentials()
+    if image_name == None:
+        image_name = filename.replace(".img","")
+    #glance upload initrd, kernel and get ids
+    print "sending "+ disk_format + " to glance"
+    output_add = __salt__['cmd.run']("glance add is_public=false disk_format=" 
+            + disk_format + " container_format=" + disk_format + " name=\""
+            + image_name + "\" " + other_params + " < " + path + "/" + filename)
+    id_image = output_add.split("ID: ")[1].split('\n')[0]
+    print "Uploaded " + disk_format + " with id : " + id_image
+    return id_image
+
+
--- a/_modules/vm_create.py	Tue Feb 26 18:44:37 2013 +0100
+++ b/_modules/vm_create.py	Thu Mar 21 14:35:02 2013 +0100
@@ -3,6 +3,9 @@
 translation of a bash script to create a flavored virtual machine
 '''
 
+PROXY_ADDRESS = "http://proxy.logilab.priv:3142"
+DEFAULT_PATH = "/mnt"
+
 from os import listdir, remove, stat, environ
 def _get_images(path):
     '''
@@ -12,21 +15,21 @@
             if element.split(".")[-1] == "img"]
     return images
 
-def _credentials_defined():
-    '''
-    check if glance credentials have been set in env
-    '''
-    for keyword in environ:
-        if keyword.find('OS_TENANT_NAME') !=1:
-            return True
-    return False
-
-
 def _create_apt_proxy(path="/etc/apt/apt.conf.d/01proxy",
-        proxy="http://proxy.logilab.priv:3142"):
+        proxy=PROXY_ADDRESS):
     proxy_file = open(path,'w')
     proxy_file.write(proxy)
     proxy_file.close()
+    return path
+
+def _create_policy_script(path="/usr/sbin/policy-rc.d",
+        content="exit 101"):
+    policy_file = open(path,'w')
+    policy_file.write("#!/bin/sh")
+    policy_file.write(content)
+    __salt__['file.check_perms'](path, {}, 'root', 'root', '755')
+    policy_file.close()
+    return path
 
 def _chroot_exec(chroot_path, command):
     return __salt__['cmd.run']("chroot " + chroot_path + " " + command)
@@ -34,6 +37,10 @@
 def umount_images(path):
     '''
     umounts all images in path
+
+    CLI Example::
+      
+        salt '*' vm_create.umount_images /mnt
     '''
     mounts = sorted([mount for mount in __salt__['mount.active']() 
         if mount.find(path) != -1], reverse = True)
@@ -42,13 +49,24 @@
         print __salt__['mount.umount'](mount)
 
 def remove_images(path):
+    '''
+    delete all images in path
+
+    CLI Example::
+      
+        salt '*' vm_create.remove_images /mnt
+    '''
     for image in _get_images(path):
         print "removing " + path + "/" + image
         remove(path + "/"+ image)
 
-def revert(path="/mnt", files_to_delete=()):
+def revert(path=DEFAULT_PATH, files_to_delete=()):
     '''
     umounts and delete all images in path
+
+    CLI Example::
+      
+        salt '*' vm_create.revert /mnt
     '''
     umount_images(path)
     remove_images(path)
@@ -59,6 +77,11 @@
     '''
     resize partition and filesystem whenever possible (size in Mb) 
     and check the integrity (for ext* fs) 
+
+    CLI Example::
+      
+        salt '*' vm_create.resize_and_check /mnt raring_image.img 4
+        salt '*' vm_create.resize_and_check /mnt raring_image.img
     '''
     current_size = stat(path + "/" + image_name).st_size/pow(1024, 2)
     if new_size > current_size:
@@ -72,15 +95,19 @@
     print __salt__['cmd.run']("resize2fs -f " + minimize + path + "/" 
             + image_name)
 
-def mount_image(archive_name, path, new_size = 4):
+def mount_image(path, archive_name, new_size = 4):
     '''
-    unrar, resize and mount image in path
+    unrar, resize, mount image in path, add /proc and console
+
+    CLI Example::
+      
+        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
     '''
     print __salt__['file.makedirs'](path + "/") 
     assert archive_name.split('.')[-1] == "gz"
     remove_images(path)
     assert len(_get_images(path)) == 0
-    print __salt__['cmd.run']("tar xzf " + archive_name + " --wildcards -C " 
+    print __salt__['cmd.run']("tar xzf " + path + "/" + archive_name + " --wildcards -C " 
             + path + """ "*.img" """)
     images = _get_images(path)
     assert len(images) == 1
@@ -96,12 +123,29 @@
             "none", fstype="devpts")
     return image_dirname, image_name
 
-def update_image(path, image_dirname):
-    _create_apt_proxy(path + "/" + image_dirname 
-            + "/etc/apt/apt.conf.d/01proxy")
+def update_image(path, image_dirname, create_proxy=True, create_policy=True):
+    '''
+    update image using apt-get. Doing so, it creates a 
+
+    ***WARNING***
+    it also deletes whoopsie which crashed when being updated 
+    return files added which must be deletes afterwards
+
+    CLI Example::
+      
+        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
+    '''
+    files_to_delete = []
+    if create_proxy:
+        files_to_delete.append(_create_apt_proxy(path + "/" + image_dirname 
+            + "/etc/apt/apt.conf.d/01proxy"))
+    if create_policy:
+    files_to_delete.append(_create_policy_script(path + "/" + image_dirname 
+        + "/usr/bin/policy-rc.d"))
     _chroot_exec(image_dirname, "apt-get remove whoopsie")
     _chroot_exec(image_dirname, "apt-get update && apt-get upgrade")
     _chroot_exec(image_dirname, "apt-get install salt-minion")
+    return files_to_delete
 
 def flavor_image(path, image_dirname, flavor_filename):
     '''
@@ -110,23 +154,14 @@
     '''
     pass
 
-def upload_to_glance(path, filename, disk_format, other_params, 
-        image_name=None):
-    if image_name == None:
-        image_name = filename.replace(".img","")
-    #glance upload initrd, kernel and get ids
-    print "sending "+ disk_format + " to glance"
-    output_add = __salt__['cmd.run']("glance add is_public=false disk_format=" 
-            + disk_format + " container_format=" + disk_format + " name=\""
-            + image_name + "\" " + other_params + " < " + path + "/" + filename)
-    id_image = output_add.split("ID: ")[1].split('\n')[0]
-    print "Uploaded " + disk_format + " with id : " + id_image
-    return id_image
+def get_initrd_kernel(path, image_dirname):
+    '''
+    copy the kernel and ramdisk from a mounted image to path
 
-def delete_image(id_image):
-    print __salt__['cmd.run']("glance delete " + id_image)
-
-def get_initrd_kernel(path, image_dirname):
+    CLI Example::
+      
+        salt '*' vm_create.get_initrd_kernel /mnt raring_image
+    '''
     return_files = []
     for filetype in ("vmlinuz","initrd"):
         myfiles = [myfile for myfile in listdir(path + "/" + image_dirname 
@@ -139,24 +174,28 @@
         return_files.append(myfile)
     return tuple(return_files)
 
-def create_flavor(archive_name, flavor_filename, path="/mnt"):
+def create_flavor(archive_name, flavor_filename, path=DEFAULT_PATH, upload_to_glance=True):
     '''
     complete function to create the new flavor
+
+    CLI Example::
+      
+        salt '*' vm_create.create_flavor raring_image.tar.gz vanilla
     '''
-    image_dirname, image_name =  mount_image(archive_name, path)
+    image_dirname, image_name =  mount_image(path, archive_name)
     update_image(path, image_dirname)
     flavor_image(path, image_dirname, flavor_filename)
     kernel_name, ramdisk_name = get_initrd_kernel(path, image_dirname)
     umount_images(image_dirname)
     resize_and_check(path, image_name)
-    if _credentials_defined():
+    if upload_to_glance:
         print "will now upload to glance server"
-        id_kernel = upload_to_glance(path, kernel_name, "aki", "", image_dirname)
-        id_ramdisk = upload_to_glance(path, ramdisk_name, "ari", "", image_dirname)
-        id_image = upload_to_glance(path, image_name, "ami", "kernel_id="
+        id_kernel = __salt__["openstack_client.upload_to_glance"](path, kernel_name, "aki", "", image_dirname)
+        id_ramdisk = __salt__["openstack_client.upload_to_glance"](path, ramdisk_name, "ari", "", image_dirname)
+        id_image = __salt__["openstack_client.upload_to_glance"](path, image_name, "ami", "kernel_id="
                 + id_kernel + " ramdisk_id=" + id_ramdisk)
     else:
-        print "cannot upload, no credentials available"
+        print "Done, no upload to do"
     #clean
     revert(path, (kernel_name, ramdisk_name))
     return True