code cleanup and improvements in vm_create.py:
authorPaul Tonelli <paul.tonelli@logilab.fr>
Fri, 01 Mar 2013 09:27:29 +0100
changeset 11 729b32f1b0f0
parent 10 623c964b59f5
child 12 8f6330ef64ec
code cleanup and improvements in vm_create.py: - put revert option inside all _create_* - add create_policy for ubuntu to avoid accessing /run (debian already functionnal) - improvements in ``flavor_image`` function
_modules/vm_create.py
--- a/_modules/vm_create.py	Thu Feb 28 16:50:42 2013 +0100
+++ b/_modules/vm_create.py	Fri Mar 01 09:27:29 2013 +0100
@@ -3,8 +3,11 @@
 translation of a bash script to create a flavored virtual machine
 '''
 
-PROXY_ADDRESS = 'http://proxy.logilab.priv:3142'
+PROXY_ADDRESS = 'http://proxy.logilab.priv:3142/'
 DEFAULT_PATH = '/mnt'
+APT_PROXY_FILENAME = '/etc/apt/apt.conf.d/01proxy'
+POLICY_FILENAME = '/usr/sbin/policy-rc.d'
+POLICY_CONTENT =  'exit 101'
 
 from os import listdir, remove, stat, environ
 def _get_images(path):
@@ -15,21 +18,34 @@
             if element.split('.')[-1] == 'img']
     return images
 
-def _create_apt_proxy(path='/etc/apt/apt.conf.d/01proxy',
-        proxy=PROXY_ADDRESS):
-    proxy_file = open(path,'w')
-    proxy_file.write(proxy)
-    proxy_file.close()
-    return path
+def _create_apt_proxy(file_path=APT_PROXY_FILENAME,
+        proxy=PROXY_ADDRESS, revert=False):
+    if revert:
+        print __salt__['file.remove'](file_path)
+    else:
+        proxy_file = open(file_path,'w')
+        proxy_file.write("Acquire::http::Proxy \"" + proxy + "\";\n")
+        proxy_file.close()
 
-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 _create_policy_debian(file_path=POLICY_FILENAME,
+        content=POLICY_CONTENT, revert=False):
+    if revert:
+        print __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')
+
+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" )
+    else:
+        print _chroot_exec(image_path, "dpkg-divert --local --rename --add /sbin/initctl" )
+        print _chroot_exec(image_path, "ln -s /bin/true /sbin/initctl")
+
 
 def _chroot_exec(chroot_path, command):
     return __salt__['cmd.run']('chroot ' + chroot_path + ' ' + command)
@@ -48,6 +64,11 @@
         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
@@ -60,7 +81,7 @@
         print 'removing ' + path + '/' + image
         remove(path + '/'+ image)
 
-def revert(path=DEFAULT_PATH, files_to_delete=()):
+def revert_all(path, files_to_delete=()):
     '''
     umounts and delete all images in path
 
@@ -71,7 +92,7 @@
     umount_images(path)
     remove_images(path)
     for file_to_delete in files_to_delete:
-        remove(path + '/' +file_to_delete)
+        remove(path + '/' + file_to_delete)
        
 def resize_and_check(path, image_name, new_size=0):
     '''
@@ -139,14 +160,16 @@
     '''
     files_to_delete = []
     if create_proxy:
-        files_to_delete.append(_create_apt_proxy(path + '/' + image_dirname 
-            + '/etc/apt/apt.conf.d/01proxy'))
+        _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')
+        _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
 
 def flavor_image(path, image_dirname, flavor_filename):
@@ -154,6 +177,8 @@
     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):
@@ -189,7 +214,8 @@
     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)
+    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'
@@ -203,5 +229,7 @@
     else:
         print 'Done, no upload to do'
     #clean
-    revert(path, (kernel_name, ramdisk_name))
+    print "kernel name " + kernel_name
+    print "ramdisk name " + ramdisk_name
+    revert_all(path, (kernel_name, ramdisk_name))
     return True