added missing module to connect to glance
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 12 Mar 2013 17:41:25 +0100
changeset 70 afe92adc13cc
parent 69 03a191bfee6f
child 71 d13233afb293
added missing module to connect to glance
_modules/openstack_client.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_modules/openstack_client.py	Tue Mar 12 17:41:25 2013 +0100
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+'''
+this module replaces temporarely the glance script from salt
+(unavailable due to glanceclient not being available in debian)
+
+it also relies on the nova script (which works)
+'''
+
+from os import environ
+from glance import client as glance_client
+
+import logging
+
+log = logging.getLogger(__name__)
+
+# Import third party libs
+HAS_NOVA = False
+try:
+    from novaclient.v1_1 import client
+    HAS_NOVA = True
+except ImportError:
+    pass
+
+def _auth_glance(id_elements=None, force_update=False):
+    '''
+    ugly, but works
+    to use glance with cmd.run
+    '''
+    if id_elements == None:
+        print __salt__['grains.item']('keystone.auth_url')
+        id_elements={
+                'OS_AUTH_URL': __salt__['grains.item']('keystone.auth_url'),
+                'OS_TENANT_ID': __salt__['grains.item']('keystone.tenant_id'),
+                'OS_TENANT_NAME': __salt__['grains.item']('keystone.tenant'),
+                'OS_USERNAME': __salt__['grains.item']('keystone.user'),
+                'OS_PASSWORD': __salt__['grains.item']('keystone.password')
+                }
+        for key in id_elements:
+            if force_update or key not in environ:
+                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):
+    '''
+    upload image to glance, used in vm_create
+    '''
+    g_client = _auth_glance()
+    if image_name == None:
+        image_name = filename.replace(".img","")
+        if '/' in image_name:
+            image_name = image_name.rsplit('/')[-1]
+    #glance upload initrd, kernel and get ids
+    print "sending "+ disk_format + " to glance"
+    log.info("glance add is_public=false disk_format="
+            + disk_format + " container_format=" + disk_format + " name=\""
+            + image_name + "\" " + other_params + " < " + filename)
+    output_add = __salt__['cmd.run']("glance add is_public=false disk_format="
+            + disk_format + " container_format=" + disk_format + " name=\""
+            + image_name + "\" " + other_params + " < " + filename)
+    log.info(output_add)
+    id_image = output_add.split("ID: ")[1].split('\n')[0]
+    print "Uploaded " + disk_format + " with id : " + id_image
+    return id_image
+
+def get_list():
+    '''
+    used to check auth is functionnal with glance
+    '''
+    _auth_glance()
+    output_add = __salt__['cmd.run']("glance index")
+    print output_add
+    return True
+