multiple cleanup:
authorPaul Tonelli <paul.tonelli@logilab.fr>
Thu, 23 May 2013 19:05:40 +0200
changeset 140 539011bcde6c
parent 139 b7d52be2811a
child 141 638c1ec12cbf
multiple cleanup: - remove unused variables - rename upload_to_glance as upload_file in openstack_client - move upload_to_glance function from vm_create to openstack_client - remove defunct headers in vm_create
_modules/openstack_client.py
_modules/vm_create.py
ami_post/upload.sls
--- a/_modules/openstack_client.py	Wed May 22 17:01:27 2013 +0200
+++ b/_modules/openstack_client.py	Thu May 23 19:05:40 2013 +0200
@@ -6,7 +6,8 @@
 it also relies on the nova script (which works)
 '''
 
-from os import environ
+
+from os import listdir, environ
 from glance import client as glance_client
 
 import logging
@@ -39,7 +40,8 @@
                 environ[key] = id_elements[key]
     return glance_client.get_client(host='0.0.0.0')
 
-def upload_to_glance(filename, disk_format, other_params='', image_name=None):
+
+def upload_file(filename, disk_format, other_params='', image_name=None):
     '''
     upload image to glance, used in vm_create
     '''
@@ -73,3 +75,39 @@
     print output_add
     return True
 
+def upload_to_glance(path):
+    log.info('will now try to upload to glance server')
+    ami_name = __salt__['grains.get']('ami.name')
+    if not ami_name:
+        ami_name = "variation"
+    object_id = {}
+    object_types = {'kernel':'aki','ramdisk':'ari'}
+    ok_for_image = True
+    for o_type in object_types:
+        object_id[o_type] = __salt__['grains.get'](o_type + '_id')
+        if object_id[o_type]:
+            object_id[o_type] = object_id[o_type]
+        else:
+            o_path = _find_objects(path, o_type)
+            if len(o_path) == 1:
+                object_id[o_type] = __salt__['openstack_client.upload_file'](
+                        o_path[0], object_types[o_type], image_name= ami_name +
+                        '-' + o_type)
+            else:
+                log.error('no ' + o_type + ' found in ' + path)
+                ok_for_image = False
+    if ok_for_image:
+        o_path = _find_objects(path,'.img')
+        if len(o_path) == 1:
+            image_id = __salt__['openstack_client.upload_file'](
+                 o_path[0], 'ami', 'kernel_id=' + object_id['kernel'] +
+                 ' ramdisk_id=' + object_id['ramdisk'], image_name=ami_name)
+        return image_id
+    return False
+
+def _find_objects(path, substring):
+    object_names = [element for element in listdir(path)
+            if substring in element]
+    return [path + '/' + element for element in object_names]
+
+
--- a/_modules/vm_create.py	Wed May 22 17:01:27 2013 +0200
+++ b/_modules/vm_create.py	Thu May 23 19:05:40 2013 +0200
@@ -1,58 +1,6 @@
 #!/usr/bin/env python
-'''
-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'
-POLICY_FILENAME = '/usr/sbin/policy-rc.d'
-POLICY_CONTENT =  'exit 101'
-DEFAULT_MOUNT_DIR = 'variation'
-DEFAULT_VARIATION_NAME = DEFAULT_MOUNT_DIR + '.sls'
-
-from os import listdir, remove, stat, environ
-import shutil
-import salt.utils
-import logging
-
-log = logging.getLogger(__name__)
-
-def upload_to_glance(path):
-    log.info('will now try to upload to glance server')
-    ami_name = __salt__['grains.get']('ami.name')
-    if not ami_name:
-        ami_name = "variation"
-    object_id = {}
-    object_types = {'kernel':'aki','ramdisk':'ari'}
-    ok_for_image = True
-    for o_type in object_types:
-        object_id[o_type] = __salt__['grains.get'](o_type + '_id')
-        if object_id[o_type]:
-            object_id[o_type] = object_id[o_type]
-        else:
-            o_path = _find_objects(path, o_type)
-            if len(o_path) == 1:
-                object_id[o_type] = __salt__['openstack_client.upload_to_glance'](
-                        o_path[0], object_types[o_type], image_name= ami_name +
-                        '-' + o_type)
-            else:
-                log.error('no ' + o_type + ' found in ' + path)
-                ok_for_image = False
-    if ok_for_image:
-        o_path = _find_objects(path,'.img')
-        if len(o_path) == 1:
-            image_id = __salt__['openstack_client.upload_to_glance'](
-                 o_path[0], 'ami', 'kernel_id=' + object_id['kernel'] +
-                 ' ramdisk_id=' + object_id['ramdisk'], image_name=ami_name)
-        return image_id
-    return False
 
 def chroot_exec(path, command):
     return __salt__['cmd.run_all']('chroot ' + path + ' ' + command)
 
-def _find_objects(path, substring):
-    object_names = [element for element in listdir(path)
-            if substring in element]
-    return [path + '/' + element for element in object_names]
 
--- a/ami_post/upload.sls	Wed May 22 17:01:27 2013 +0200
+++ b/ami_post/upload.sls	Thu May 23 19:05:40 2013 +0200
@@ -1,7 +1,7 @@
 {% if 'keystone.user' in grains %}
 upload_variant:
     module.run:
-        - name: vm_create.upload_to_glance
+        - name: openstack_client.upload_to_glance
         - path: /mnt/output
         - require:
             - tmp_state.resized: resize_variation