stop removing whoopsie as it is no longer necessary (create_policy_ubuntu), code cleaning
authorPaul Tonelli <paul.tonelli@logilab.fr>
Fri, 01 Mar 2013 11:07:00 +0100
changeset 12 8f6330ef64ec
parent 11 729b32f1b0f0
child 13 5a3437449dc8
stop removing whoopsie as it is no longer necessary (create_policy_ubuntu), code cleaning
_modules/openstack_client.py
_modules/vm_create.py
--- a/_modules/openstack_client.py	Fri Mar 01 09:27:29 2013 +0100
+++ b/_modules/openstack_client.py	Fri Mar 01 11:07:00 2013 +0100
@@ -147,9 +147,9 @@
             if item.nk.servers.list()[0].human_id == human_id]
     if len(matching_instances) == 1:
         machine = matching_instances[0]
-        if (image_id!=None):
+        if image_id != None:
             assert machine.image['id'] == image_d
-        if flavor!=None
+        if flavor != None:
             assert nt_ks.flavors.get(machine.flavor['id']) == image_d
         if machine.status == 'PAUSED':
             machine.unpause()
--- a/_modules/vm_create.py	Fri Mar 01 09:27:29 2013 +0100
+++ b/_modules/vm_create.py	Fri Mar 01 11:07:00 2013 +0100
@@ -3,6 +3,8 @@
 translation of a bash script to create a flavored virtual machine
 '''
 
+
+
 PROXY_ADDRESS = 'http://proxy.logilab.priv:3142/'
 DEFAULT_PATH = '/mnt'
 APT_PROXY_FILENAME = '/etc/apt/apt.conf.d/01proxy'
@@ -10,33 +12,39 @@
 POLICY_CONTENT =  'exit 101'
 
 from os import listdir, remove, stat, environ
+import logging
+
+log = logging.getLogger(__name__)
+
 def _get_images(path):
     '''
     find .img files in path
     '''
     images = [element for element in listdir(path) 
-            if element.split('.')[-1] == 'img']
+            if element.endswith('img')]
     return images
 
 def _create_apt_proxy(file_path=APT_PROXY_FILENAME,
         proxy=PROXY_ADDRESS, revert=False):
     if revert:
-        print __salt__['file.remove'](file_path)
+        return __salt__['file.remove'](file_path):
     else:
         proxy_file = open(file_path,'w')
         proxy_file.write("Acquire::http::Proxy \"" + proxy + "\";\n")
         proxy_file.close()
+        return True
 
 def _create_policy_debian(file_path=POLICY_FILENAME,
         content=POLICY_CONTENT, revert=False):
     if revert:
-        print __salt__['file.remove'](file_path)
+        return __salt__['file.remove'](file_path)
     else:
         policy_file = open(file_path,'w')
         policy_file.write('#!/bin/sh\n')
         policy_file.write(content + '\n')
         policy_file.close()
         __salt__['file.check_perms'](file_path, {}, 'root', 'root', '755')
+        return True
 
 def _create_policy_ubuntu(image_path, revert=False):
     if revert:
@@ -58,8 +66,8 @@
       
         salt '*' vm_create.umount_images /mnt
     '''
-    mounts = sorted([mount for mount in __salt__['mount.active']() 
-        if mount.find(path) != -1], reverse = True)
+    mounts = sorted([mount for mount in __salt__['mount.active']()
+        if path in mount], reverse = True)
     for mount in mounts:
         print 'umounting ' + mount
         print __salt__['mount.umount'](mount)
@@ -124,7 +132,7 @@
       
         salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
     '''
-    print __salt__['file.makedirs'](path + '/') 
+    log.info(__salt__['file.makedirs'](path + '/'))
     assert archive_name.split('.')[-1] == 'gz'
     remove_images(path)
     assert len(_get_images(path)) == 0
@@ -150,24 +158,19 @@
     In order to be executed in chroot, you should use is with 
     create_proxy=True unless you have /run mounted
 
-    ***WARNING***
-    it also deletes whoopsie which crashed while 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:
+    if bool(create_proxy):
         _create_apt_proxy(path + '/' + image_dirname 
             + '/etc/apt/apt.conf.d/01proxy')
-    if create_policy:
+    if bool(create_policy):
         _create_policy_ubuntu(path + '/' + image_dirname)
     print __salt__['file.remove'](path + '/' + image_dirname + "/etc/resolv.conf")
     print __salt__['cmd.run']('cp /etc/resolv.conf ' + path + '/' + image_dirname 
             + '/etc/')
-    print _chroot_exec(path + "/" + image_dirname, 'apt-get -f -y remove whoopsie')
     print _chroot_exec(path + "/" + image_dirname, 'apt-get -f -y update && apt-get upgrade')
     print _chroot_exec(path + "/" + image_dirname, 'apt-get -f -y install salt-minion')
     return files_to_delete
@@ -193,7 +196,7 @@
     for filetype in ('vmlinuz','initrd'):
         myfiles = [myfile for myfile in listdir(path + '/' + image_dirname 
             + '/boot/') 
-                if myfile.find(filetype) != -1]
+                if filetype in myfile]
         assert len(myfiles) == 1
         myfile = myfiles[0]
         __salt__['cmd.run']('cp ' + path + '/' + image_dirname + '/boot/'
@@ -202,7 +205,7 @@
     return tuple(return_files)
 
 def create_flavor(archive_name, flavor_filename, path=DEFAULT_PATH,
-        upload_to_glance=True):
+        upload_to_glance=None):
     '''
     complete function to create the new flavor from a virtual machine
 
@@ -215,10 +218,10 @@
     flavor_image(path, image_dirname, flavor_filename)
     kernel_name, ramdisk_name = get_initrd_kernel(path, image_dirname)
     clean_images(path,image_dirname)
-    umount_images(path)
-    resize_and_check(path, image_name)
-    if upload_to_glance:
-        print 'will now upload to glance server'
+    #umount_images(path)
+    #resize_and_check(path, image_name)
+    if bool(upload_to_glance):
+        log.info('will now upload to glance server')
         id_kernel = __salt__['openstack_client.upload_to_glance'](
                 path, kernel_name, 'aki', '', image_dirname)
         id_ramdisk = __salt__['openstack_client.upload_to_glance'](
@@ -227,9 +230,9 @@
                 path, image_name, 'ami', 'kernel_id='
                 + id_kernel + ' ramdisk_id=' + id_ramdisk)
     else:
-        print 'Done, no upload to do'
+        log.info('Not uploading to glance')
     #clean
     print "kernel name " + kernel_name
     print "ramdisk name " + ramdisk_name
-    revert_all(path, (kernel_name, ramdisk_name))
+    #revert_all(path, (kernel_name, ramdisk_name))
     return True