_modules/openstack_client.py
author Paul Tonelli <paul.tonelli@logilab.fr>
Thu, 28 Feb 2013 16:47:40 +0100
changeset 5 430383a302c7
parent 4 9b1b64ddeebd
child 6 7fc32398ed73
permissions -rw-r--r--
improve comments im vm_create and openstack_client

#!/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

def _set_credentials(id_elements=None, force_update=False):
    '''
    really ugly, but works
    until credentials expire
    '''
    if id_elements == None:
        id_elements={
                'OS_AUTH_URL': 'http://control.example.com:5000/v2.0',
                'OS_TENANT_ID': '00000000000000000000000000000000',
                'OS_TENANT_NAME':'tenant',
                'OS_USERNAME': 'username',
                'OS_PASSWORD': 'password'
                }
        for key in id_elements:
            if force_update or key not in environ:
                environ[key] = id_elements[key]
    return True

def delete_image(id_image):
     '''
    delete image from glance 

    CLI Example::
      
        salt '*'  openstack_client.delete_image 00000000-0000-0000-0000-000000000000
    '''
    print __salt__['cmd.run']("glance delete " + id_image)


def get_running_instances():
    _set_credentials()
    data = __salt__['nova.list']()
    for i in data:
        print str(i) + " len : " + str(len(i))
    gc = glance_client.get_client(host='0.0.0.0')
    print gc.get_images()
    return True

def test_pillars():
    pillars_val = __salt__['pillar.raw']()['master']
    print pillars_val
    for i in pillars_val:
        if i.find("keystone") != -1:
            print i + " : " + pillars_val[i]
    return True

def upload_to_glance(path, filename, disk_format, other_params, 
        image_name=None):
    _set_credentials()
    if image_name == None:
        image_name = filename.replace(".img","")
    #glance upload initrd, kernel and get ids
    print "sending "+ disk_format + " to glance"
    output_add = __salt__['cmd.run']("glance add is_public=false disk_format=" 
            + disk_format + " container_format=" + disk_format + " name=\""
            + image_name + "\" " + other_params + " < " + path + "/" + filename)
    id_image = output_add.split("ID: ")[1].split('\n')[0]
    print "Uploaded " + disk_format + " with id : " + id_image
    return id_image