author Paul Tonelli <paul.tonelli@logilab.fr>
Tue, 12 Mar 2013 16:32:57 +0100
changeset 68 601c145e6326
parent 55 a8fc89e61f2b
child 69 03a191bfee6f
permissions -rw-r--r--
renamed user data parameters for clarity and added possibility to automatically upload to glance

#!/usr/bin/env python
translation of a bash script to create a flavored virtual machine

PROXY_ADDRESS = 'http://proxy.logilab.priv:3142/'
APT_PROXY_FILENAME = '/etc/apt/apt.conf.d/01proxy'
POLICY_FILENAME = '/usr/sbin/policy-rc.d'
POLICY_CONTENT =  'exit 101'
DEFAULT_MOUNT_DIR = 'variation'

from os import listdir, remove, stat, environ
import shutil
import salt.utils
import logging

log = logging.getLogger(__name__)

def update_image(chroot_dirname):
    update image using apt-get. Doing so, it creates a few files.
    In order to be executed in chroot, you should use is with
    create_proxy=True unless you have /run mounted

    CLI Example::

        salt '*' vm_create.update_image /mnt/variation
    _print_if_error(_chroot_exec(chroot_dirname, 'apt-get update'))
    _print_if_error(_chroot_exec(chroot_dirname, 'apt-get --force-yes -y install salt-minion'))

def variate_image(chroot_dirname, variation_filename=DEFAULT_VARIATION_NAME):
    do all code relevant to personalizing the machine here
    salt-minion should be available

    CLI Example::

        salt '*' vm_create.variate_image /mnt/variation

    log.info(_chroot_exec(chroot_dirname, 'salt-call  --local state.highstate'))

def create_variation(path=DEFAULT_PATH):
    complete function to create the new variation from a virtual machine

    CLI Example::

        salt '*' vm_create.create_variation
    image_dirname = DEFAULT_MOUNT_DIR
    ### Exec pre state here ###
    update_image(path + '/' + image_dirname)
    variate_image(path + '/' + image_dirname)

    if __salt__['grains.item']('keystone.user'): #true only if len>0
        log.info('Image Not uploaded to glance !')
    return True

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]
            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], '')
                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)

def _print_if_error(ret):
    if ret['retcode'] != 0:

def _find_objects(path, substring):
    object_names = [element for element in listdir(path)
            if substring in element]
    return object_names