_modules/openstack_client.py
author Paul Tonelli <paul.tonelli@logilab.fr>
Wed, 13 Mar 2013 17:22:44 +0100
changeset 73 dae7b424b347
parent 70 afe92adc13cc
child 115 063b9cbe5aa1
permissions -rw-r--r--
code refactoring, begin to move parts of tmp_state resize state to tmp_extfs (module), should work *** corrections

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