code cleaning : move protected functions to end and remove unnecessary ones
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 05 Mar 2013 15:16:27 +0100
changeset 13 5a3437449dc8
parent 12 8f6330ef64ec
child 14 6c75dfac2047
code cleaning : move protected functions to end and remove unnecessary ones
_modules/vm_create.py
--- a/_modules/vm_create.py	Fri Mar 01 11:07:00 2013 +0100
+++ b/_modules/vm_create.py	Tue Mar 05 15:16:27 2013 +0100
@@ -10,24 +10,210 @@
 APT_PROXY_FILENAME = '/etc/apt/apt.conf.d/01proxy'
 POLICY_FILENAME = '/usr/sbin/policy-rc.d'
 POLICY_CONTENT =  'exit 101'
+DEFAULT_VARIATION_NAME = 'init-client.sls'
 
 from os import listdir, remove, stat, environ
+import salt.utils
 import logging
 
 log = logging.getLogger(__name__)
 
+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 path in mount], reverse = True)
+    for mount in mounts:
+        print 'umounting ' + mount
+        print __salt__['mount.umount'](mount)
+
+def clean_images(path, image_dirname):
+    _create_apt_proxy(path + '/' + image_dirname
+            + '/etc/apt/apt.conf.d/01proxy', revert=True)
+    _create_policy_ubuntu(path + '/' + image_dirname, revert=True)
+
+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_all(path, files_to_delete=()):
+    '''
+    umounts and delete all images in path
+
+    CLI Example::
+
+        salt '*' vm_create.revert /mnt
+    '''
+    umount_images(path)
+    remove_images(path)
+    for file_to_delete in files_to_delete:
+        remove(path + '/' + file_to_delete)
+
+def resize_and_check(path, image_name, new_size=0):
+    '''
+    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:
+        log.info('increasing size of ' + path + '/' + image_name + ' to ' + str(new_size) + 'Gb')
+        log.debug(__salt__['cmd.run_all']('dd if=/dev/zero bs=1024k count='
+                + str(new_size - current_size) + ' >> ' + path + '/'
+                + image_name))
+        minimize = ''
+    else:
+        minimize = '-M '
+    log.info(__salt__['cmd.run_all']('e2fsck -n -f ' + path + '/' + image_name))
+    log.info(__salt__['cmd.run_all']('resize2fs -f ' + minimize + path + '/'
+            + image_name))
+
+def mount_image(path, archive_name, new_size = 4):
+    '''
+    unrar, resize, mount image in path, add /proc and console
+
+    CLI Example::
+
+        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
+    '''
+    log.info(__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 ' + path + '/' + archive_name
+            + ' --wildcards -C ' + path + ' "*.img" ')
+    images = _get_images(path)
+    assert len(images) == 1
+    image_name = images[0]
+    resize_and_check(path, image_name, new_size * 1024)
+    image_dirname = image_name.replace('.img','')
+    log.info(__salt__['file.makedirs'](path + '/' + image_dirname + '/'))
+    log.info(__salt__['mount.mount'](path + '/' + image_dirname, path + '/'
+            + image_name, opts='loop'))
+    log.info(__salt__['mount.mount'](path + '/' + image_dirname + '/proc', 'none',
+            fstype='proc'))
+    log.info(__salt__['mount.mount'](path + '/' + image_dirname + '/dev/pts',
+            'none', fstype='devpts'))
+    return image_dirname, image_name
+
+def update_image(chroot_dirname, create_proxy=True, create_policy=True):
+    '''
+    update image using apt-get. Doing so, it creates a few files.
+    In order to be executed in chroot, you should use is with
+    create_proxy=True unless you have /run mounted
+
+    CLI Example::
+
+        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
+    '''
+    files_to_delete = []
+    if bool(create_proxy):
+        _create_apt_proxy(chroot_dirname
+            + '/etc/apt/apt.conf.d/01proxy')
+    if bool(create_policy):
+        _create_policy_ubuntu(chroot_dirname)
+    log.info(__salt__['file.remove'](chroot_dirname + "/etc/resolv.conf"))
+    ################ bug here ####################"
+    print '/etc/resolv.conf' +", "+ chroot_dirname + '/etc'
+    log.info(salt.utils.copyfile('/etc/resolv.conf', chroot_dirname + '/etc/resolv.conf'))
+    _print_if_error(_chroot_exec(chroot_dirname, 'apt-get --force-yes -y update && apt-get upgrade'))
+    _print_if_error(_chroot_exec(chroot_dirname, 'apt-get --force-yes -y install salt-minion'))
+    return files_to_delete
+
+def variate_image(chroot_dirname, variation_filename=DEFAULT_VARIATION_NAME):
+    '''
+    do all code relevant to personalizing the machine here
+    salt-minion should be available
+    '''
+    file_origin = 'salt://' + variation_filename
+
+    salt.utils.copyfile('/etc/salt/srv/' + variation_filename, chroot_dirname + '/etc/salt/srv')
+    __salt__['file.patch'](chroot_dirname + '/etc/salt/minion', '/etc/salt/srv/minion.patch')
+#    log.info(_chroot_exec(chroot_dirname, 'salt-call  --local state.highstate'))
+
+
+def get_initrd_kernel(path, image_dirname):
+    '''
+    copy the kernel and ramdisk from a mounted image to path
+
+    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
+            + '/boot/')
+                if filetype in myfile]
+        assert len(myfiles) == 1
+        myfile = myfiles[0]
+        log.info(__salt__['cmd.run']('cp ' + path + '/' + image_dirname + '/boot/'
+                + myfile + ' ' + path))
+        return_files.append(myfile)
+    return tuple(return_files)
+
+def create_variation(archive_name, variation_filename, path=DEFAULT_PATH,
+        upload_to_glance=None):
+    '''
+    complete function to create the new variation from a virtual machine
+
+    CLI Example::
+
+        salt '*' vm_create.create_variation raring_image.tar.gz vanilla
+    '''
+    image_dirname, image_name =  mount_image(path, archive_name)
+    update_image(path + '/' + image_dirname)
+    variate_image(path + '/' + image_dirname, variation_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 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'](
+                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:
+        log.info('Not uploading to glance')
+    log.info("kernel name " + kernel_name)
+    log.info("ramdisk name " + ramdisk_name)
+    log.info("Reverting to initial state")
+    revert_all(path, (kernel_name, ramdisk_name))
+    return True
+
 def _get_images(path):
     '''
     find .img files in path
     '''
-    images = [element for element in listdir(path) 
+    images = [element for element in listdir(path)
             if element.endswith('img')]
     return images
 
 def _create_apt_proxy(file_path=APT_PROXY_FILENAME,
         proxy=PROXY_ADDRESS, revert=False):
     if revert:
-        return __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")
@@ -48,191 +234,18 @@
 
 def _create_policy_ubuntu(image_path, revert=False):
     if revert:
-        print __salt__['file.remove'](image_path + '/' + "/sbin/initctl")
-        print _chroot_exec(image_path, "dpkg-divert --local --rename --remove /sbin/initctl" )
+        print __salt__['file.remove'](image_path + "/sbin/initctl")
+        log.info(_chroot_exec(image_path, "dpkg-divert --local --rename --remove /sbin/initctl" ))
     else:
-        print _chroot_exec(image_path, "dpkg-divert --local --rename --add /sbin/initctl" )
-        print _chroot_exec(image_path, "ln -s /bin/true /sbin/initctl")
+        log.info(_chroot_exec(image_path, "dpkg-divert --local --rename --add /sbin/initctl" ))
+        print __salt__['file.remove'](image_path + "/sbin/initctl")
+        log.info(_chroot_exec(image_path, "ln -s /bin/true /sbin/initctl"))
 
 
 def _chroot_exec(chroot_path, command):
-    return __salt__['cmd.run']('chroot ' + chroot_path + ' ' + command)
-
-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 path in mount], reverse = True)
-    for mount in mounts:
-        print 'umounting ' + mount
-        print __salt__['mount.umount'](mount)
-
-def clean_images(path, image_dirname):
-    _create_apt_proxy(path + '/' + image_dirname 
-            + '/etc/apt/apt.conf.d/01proxy', revert=True)
-    _create_policy_ubuntu(path + '/' + image_dirname, revert=True)
-
-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_all(path, files_to_delete=()):
-    '''
-    umounts and delete all images in path
-
-    CLI Example::
-      
-        salt '*' vm_create.revert /mnt
-    '''
-    umount_images(path)
-    remove_images(path)
-    for file_to_delete in files_to_delete:
-        remove(path + '/' + file_to_delete)
-       
-def resize_and_check(path, image_name, new_size=0):
-    '''
-    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:
-        print __salt__['cmd.run']('dd if=/dev/zero bs=1024k count=' 
-                + str(new_size - current_size) + ' >> ' + path + '/' 
-                + image_name)
-        minimize = ''
-    else:
-        minimize = '-M '
-    print __salt__['cmd.run']('e2fsck -n -f ' + path + '/' + image_name)
-    print __salt__['cmd.run']('resize2fs -f ' + minimize + path + '/' 
-            + image_name)
-
-def mount_image(path, archive_name, new_size = 4):
-    '''
-    unrar, resize, mount image in path, add /proc and console
+    return __salt__['cmd.run_all']('chroot ' + chroot_path + ' ' + command)
 
-    CLI Example::
-      
-        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
-    '''
-    log.info(__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 ' + path + '/' + archive_name 
-            + ' --wildcards -C ' + path + ' "*.img" ')
-    images = _get_images(path)
-    assert len(images) == 1
-    image_name = images[0]
-    resize_and_check(path, image_name, new_size * 1024)
-    image_dirname = image_name.replace('.img','')
-    print __salt__['file.makedirs'](path + '/' + image_dirname + '/') 
-    print __salt__['mount.mount'](path + '/' + image_dirname, path + '/'
-            + image_name, opts='loop')
-    print __salt__['mount.mount'](path + '/' + image_dirname + '/proc', 'none',
-            fstype='proc')
-    print __salt__['mount.mount'](path + '/' + image_dirname + '/dev/pts', 
-            'none', fstype='devpts')
-    return image_dirname, image_name
-
-def update_image(path, image_dirname, create_proxy=True, create_policy=True):
-    '''
-    update image using apt-get. Doing so, it creates a few files.
-    In order to be executed in chroot, you should use is with 
-    create_proxy=True unless you have /run mounted
-
-    CLI Example::
-      
-        salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
-    '''
-    files_to_delete = []
-    if bool(create_proxy):
-        _create_apt_proxy(path + '/' + image_dirname 
-            + '/etc/apt/apt.conf.d/01proxy')
-    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 update && apt-get upgrade')
-    print _chroot_exec(path + "/" + image_dirname, 'apt-get -f -y install salt-minion')
-    return files_to_delete
+def _print_if_error(ret):
+    if ret['retcode'] != 0:
+        log.error(ret['stderr'])
 
-def flavor_image(path, image_dirname, flavor_filename):
-    '''
-    do all code relevant to personalizing the machine here
-    salt-minion should be available
-    '''
-    __salt__['file.get_managed'](path + '/' +image_dirname + '/etc/salt/srv/init-client.sls', 'jinja', 'salt://init-client.sls',{}, 'root', 'root', '700', 'base', None, None)
-    #salt '*' file.get_managed /etc/httpd/conf.d/httpd.conf jinja salt://http/httpd.conf '{hash_type: 'md5', 'hsum': <md5sum>}' root root '755' base None None
-    pass
-
-def get_initrd_kernel(path, image_dirname):
-    '''
-    copy the kernel and ramdisk from a mounted image to path
-
-    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 
-            + '/boot/') 
-                if filetype in myfile]
-        assert len(myfiles) == 1
-        myfile = myfiles[0]
-        __salt__['cmd.run']('cp ' + path + '/' + image_dirname + '/boot/'
-                + myfile + ' ' + path)
-        return_files.append(myfile)
-    return tuple(return_files)
-
-def create_flavor(archive_name, flavor_filename, path=DEFAULT_PATH,
-        upload_to_glance=None):
-    '''
-    complete function to create the new flavor from a virtual machine
-
-    CLI Example::
-      
-        salt '*' vm_create.create_flavor raring_image.tar.gz vanilla
-    '''
-    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)
-    clean_images(path,image_dirname)
-    #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'](
-                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:
-        log.info('Not uploading to glance')
-    #clean
-    print "kernel name " + kernel_name
-    print "ramdisk name " + ramdisk_name
-    #revert_all(path, (kernel_name, ramdisk_name))
-    return True