_modules/openstack_client.py
author Paul Tonelli <paul.tonelli@logilab.fr>
Thu, 21 Mar 2013 14:35:02 +0100
changeset 4 9b1b64ddeebd
parent 2 23272de16625
child 5 430383a302c7
permissions -rw-r--r--
clean code

#!/usr/bin/env python
'''
translation of a bash script to connect to nova
'''

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