renamed user data parameters for clarity and added possibility to automatically upload to glance
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 12 Mar 2013 16:32:57 +0100
changeset 68 601c145e6326
parent 67 3ad62a5b9c17
child 69 03a191bfee6f
renamed user data parameters for clarity and added possibility to automatically upload to glance
_modules/vm_create.py
ami_creator.sls
ami_creator_pre.sls
others/ec2_ami_parser.py
readme.txt
--- a/_modules/vm_create.py	Tue Mar 12 11:52:31 2013 +0100
+++ b/_modules/vm_create.py	Tue Mar 12 16:32:57 2013 +0100
@@ -44,7 +44,7 @@
     log.info(_chroot_exec(chroot_dirname, 'salt-call  --local state.highstate'))
 
 
-def create_variation(path=DEFAULT_PATH, upload_to_glance=None):
+def create_variation(path=DEFAULT_PATH):
     '''
     complete function to create the new variation from a virtual machine
 
@@ -58,20 +58,37 @@
     update_image(path + '/' + image_dirname)
     variate_image(path + '/' + image_dirname)
     log.info(__salt__['state.sls']('ami_creator_post'))
-    log.info('Image Not uploaded to glance !')
+
+    if __salt__['grains.item']('keystone.user'): #true only if len>0
+        upload_to_glance(path)
+    else:
+        log.info('Image Not uploaded to glance !')
     return True
 
-def upload_to_glance(path=DEFAULT_PATH, kernel_id=None, ramdisk_id=None):
-    if not kernel_id:
-        kernel_id = __salt__['openstack_client.upload_to_glance'](
-                path, kernel_name, 'aki', '', image_dirname)
-    if not ramdisk_id:
-        ramdisk_id = __salt__['openstack_client.upload_to_glance'](
-                path, ramdisk_name, 'ari', '', image_dirname)
-    image_id = __salt__['openstack_client.upload_to_glance'](
-            path, image_name, 'ami', 'kernel_id='
-            + kernel_id + ' ramdisk_id=' + ramdisk_id)
-    return image_id
+def upload_to_glance(path):
+    log.info('will now try to upload to glance server')
+    object_id = {}
+    object_types = {'kernel':'aki','initrd':'ari'}
+    ok_for_image = True
+    for o_type in object_types:
+        object_id[o_type] = __salt__['grains.item'](+'_id')
+        if object_id[o_type]:
+            object_id[o_type] = object_id[o_type].values()[0]
+        else:
+            o_path = _find_objects(path, object_types[o_type])
+            if len(o_path) == 1:
+                object_id[o_type] = __salt__['openstack_client.upload_to_glance'](
+                        o_path[0], object_types[o_type], '')
+            else:
+                ok_for_image = False
+    if ok_for_image:
+        o_path = _find_objects(path, object_types['.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['initrd'])
+        return image_id
+    return False
 
 def _chroot_exec(chroot_path, command):
     return __salt__['cmd.run_all']('chroot ' + chroot_path + ' ' + command)
@@ -80,3 +97,8 @@
     if ret['retcode'] != 0:
         log.error(ret['stderr'])
 
+def _find_objects(path, substring):
+    object_names = [element for element in listdir(path)
+            if substring in element]
+    return object_names
+
--- a/ami_creator.sls	Tue Mar 12 11:52:31 2013 +0100
+++ b/ami_creator.sls	Tue Mar 12 16:32:57 2013 +0100
@@ -11,15 +11,15 @@
 
 
 {% if  salt['grains.item']('ami_hg_address') %}
-{% set ami_hg_address = salt['grains.item']('ami_hg_address') %}
+{% set ami_hg_address = salt['grains.item']('ami.hg_address') %}
 {% else %}
 {% set ami_hg_address = 'http://hg.logilab.fr/users/ptonelli/srv_salt' %}
 {% endif %}
 hg_ami_creation:
     hg.latest:
         - name: {{ ami_hg_address }}
-{% if  salt['grains.item']('ami_hg_revision') %}
-{% set ami_hg_revision = salt['grains.item']('ami_hg_revision') %}
+{% if  salt['grains.item']('ami.hg_rev') %}
+{% set ami_hg_revision = salt['grains.item']('ami.hg_rev') %}
         - rev: {{ ami_hg_revision }}
 {% endif %}
         - target: /etc/salt/srv
--- a/ami_creator_pre.sls	Tue Mar 12 11:52:31 2013 +0100
+++ b/ami_creator_pre.sls	Tue Mar 12 16:32:57 2013 +0100
@@ -1,7 +1,7 @@
 ### unarchived files ###
 
-{% if  salt['grains.item']('ami_origin') %}
-{% set ami_origin = salt['grains.item']('ami_origin') %}
+{% if  salt['grains.item']('ami.type') %}
+{% set ami_origin = salt['grains.item']('ami.type') %}
 {% else %}
 {% set ami_origin = 'debian' %}
 {% endif %}
--- a/others/ec2_ami_parser.py	Tue Mar 12 11:52:31 2013 +0100
+++ b/others/ec2_ami_parser.py	Tue Mar 12 16:32:57 2013 +0100
@@ -18,17 +18,20 @@
     state.highstate() #does not work and requires __salt__ dict
     '''
     line = line.rsplit('#')[0].lstrip().rstrip()
-    if line.startswith('address:'):
+    if line.startswith('ami.hg_address:'):
         #grains.present(name='custom_hg_address', value=line)
         Popen(['salt-call', 'state.single', 'grains.present',
-            'name=ami_hg_address', 'value=' +
-            line.split(':',1)[-1].lstrip().rstrip()])
-    elif line.startswith('rev:') or line.startswith('revision:') :
+            'name=ami.hg_address', 'value=' +
+            line.split(':',1)[-1].lstrip()])
+    elif line.startswith('ami.hg_rev:'):
         Popen(['salt-call', 'state.single', 'grains.present',
-            'name=ami_hg_revision', 'value=' + line.split(':',1)[-1].lstrip().rstrip()])
-    elif line.startswith('origin:'):
+            'name=ami.hg_rev', 'value=' + line.split(':',1)[-1].lstrip()])
+    elif line.startswith('ami.type:'):
         Popen(['salt-call', 'state.single', 'grains.present',
-            'name=ami_origin', 'value=' + line.split(':',1)[-1].lstrip().rstrip()])
+            'name=ami.type', 'value=' + line.split(':',1)[-1].lstrip()])
+    elif ':' in line:
+        Popen(['salt-call', 'state.single', 'grains.present',
+            'name=' + line.split(':',1)[0].rstrip(), 'value=' + line.split(':',1)[-1].lstrip()])
     sleep(1)
 
 if __name__ == "__main__":
--- a/readme.txt	Tue Mar 12 11:52:31 2013 +0100
+++ b/readme.txt	Tue Mar 12 16:32:57 2013 +0100
@@ -18,9 +18,21 @@
 
 ====== START =======
 
-rev: c3984088f6b5
-address: http://hg.logilab.fr/users/ptonelli/srv_salt
-origin: debian #or ubuntu
+ami.hg_rev: c3984088f6b5
+ami.hg_address: http://hg.logilab.fr/users/ptonelli/srv_salt
+ami.type: debian #or ubuntu
+
+### optionnal ###
+keystone.user: ptonelli
+keystone.password: ptonelli
+keystone.tenant: ptonelli
+keystone.tenant_id: 7050e8e884414ef985826434c52d93f3
+keystone.auth_url: 'http://127.0.0.1:5000/v2.0/'
+keystone.insecure: False   #(optional)
+
+### only if you know what you are doing ! ###
+kernel_id:a5b900bd-c009-40b2-a763-32b32996b1ee
+ramdisk_id:b953e40a-2605-402b-8663-c8556a5899f2
 
 ====== END ========