<s> """<STR_LIT>""" <EOL> import json <EOL> import os <EOL> import pickle <EOL> import re <EOL> import sys <EOL> import tempfile <EOL> import time <EOL> import urllib <EOL> import uuid <EOL> from xml . dom import minidom <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova . image import glance <EOL> from nova import log as logging <EOL> from nova import utils <EOL> from nova . compute import instance_types <EOL> from nova . compute import power_state <EOL> from nova . virt import disk <EOL> from nova . virt import images <EOL> from nova . virt . xenapi import HelperBase <EOL> from nova . virt . xenapi . volume_utils import StorageError <EOL> LOG = logging . getLogger ( "<STR_LIT>" ) <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:10> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:16> * <NUM_LIT> * <NUM_LIT> , <EOL> '<STR_LIT>' ) <EOL> XENAPI_POWER_STATE = { <EOL> '<STR_LIT>' : power_state . SHUTDOWN , <EOL> '<STR_LIT>' : power_state . RUNNING , <EOL> '<STR_LIT>' : power_state . PAUSED , <EOL> '<STR_LIT>' : power_state . SUSPENDED , <EOL> '<STR_LIT>' : power_state . CRASHED } <EOL> SECTOR_SIZE = <NUM_LIT> <EOL> MBR_SIZE_SECTORS = <NUM_LIT> <EOL> MBR_SIZE_BYTES = MBR_SIZE_SECTORS * SECTOR_SIZE <EOL> KERNEL_DIR = '<STR_LIT>' <EOL> class ImageType : <EOL> """<STR_LIT>""" <EOL> KERNEL = <NUM_LIT:0> <EOL> RAMDISK = <NUM_LIT:1> <EOL> DISK = <NUM_LIT:2> <EOL> DISK_RAW = <NUM_LIT:3> <EOL> DISK_VHD = <NUM_LIT:4> <EOL> DISK_ISO = <NUM_LIT:5> <EOL> _ids = ( KERNEL , RAMDISK , DISK , DISK_RAW , DISK_VHD , DISK_ISO ) <EOL> KERNEL_STR = "<STR_LIT>" <EOL> RAMDISK_STR = "<STR_LIT>" <EOL> DISK_STR = "<STR_LIT>" <EOL> DISK_RAW_STR = "<STR_LIT>" <EOL> DISK_VHD_STR = "<STR_LIT>" <EOL> DISK_ISO_STR = "<STR_LIT>" <EOL> _strs = ( KERNEL_STR , RAMDISK_STR , DISK_STR , DISK_RAW_STR , DISK_VHD_STR , <EOL> DISK_ISO_STR ) <EOL> @ classmethod <EOL> def to_string ( cls , image_type ) : <EOL> return dict ( zip ( ImageType . _ids , ImageType . _strs ) ) . get ( image_type ) <EOL> @ classmethod <EOL> def from_string ( cls , image_type_str ) : <EOL> return dict ( zip ( ImageType . _strs , ImageType . _ids ) ) . get ( image_type_str ) <EOL> class VMHelper ( HelperBase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def create_vm ( cls , session , instance , kernel , ramdisk , <EOL> use_pv_kernel = False ) : <EOL> """<STR_LIT>""" <EOL> inst_type_id = instance . instance_type_id <EOL> instance_type = instance_types . get_instance_type ( inst_type_id ) <EOL> mem = str ( long ( instance_type [ '<STR_LIT>' ] ) * <NUM_LIT> * <NUM_LIT> ) <EOL> vcpus = str ( instance_type [ '<STR_LIT>' ] ) <EOL> rec = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : mem , <EOL> '<STR_LIT>' : mem , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : mem , <EOL> '<STR_LIT>' : mem , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : instance . name , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:true>' , '<STR_LIT>' : '<STR_LIT:true>' , '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , '<STR_LIT>' : '<STR_LIT:0>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : vcpus , <EOL> '<STR_LIT>' : vcpus , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } } <EOL> if use_pv_kernel : <EOL> rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:false>' <EOL> if instance . kernel_id : <EOL> rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rec [ '<STR_LIT>' ] = kernel <EOL> rec [ '<STR_LIT>' ] = ramdisk <EOL> else : <EOL> rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> rec [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT>' } <EOL> rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> vm_ref = session . call_xenapi ( '<STR_LIT>' , rec ) <EOL> instance_name = instance . name <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> return vm_ref <EOL> @ classmethod <EOL> def ensure_free_mem ( cls , session , instance ) : <EOL> inst_type_id = instance . instance_type_id <EOL> instance_type = instance_types . get_instance_type ( inst_type_id ) <EOL> mem = long ( instance_type [ '<STR_LIT>' ] ) * <NUM_LIT> * <NUM_LIT> <EOL> host = session . get_xenapi_host ( ) <EOL> host_free_mem = long ( session . get_xenapi ( ) . host . <EOL> compute_free_memory ( host ) ) <EOL> return host_free_mem >= mem <EOL> @ classmethod <EOL> def create_vbd ( cls , session , vm_ref , vdi_ref , userdevice , bootable ) : <EOL> """<STR_LIT>""" <EOL> vbd_rec = { } <EOL> vbd_rec [ '<STR_LIT>' ] = vm_ref <EOL> vbd_rec [ '<STR_LIT>' ] = vdi_ref <EOL> vbd_rec [ '<STR_LIT>' ] = str ( userdevice ) <EOL> vbd_rec [ '<STR_LIT>' ] = bootable <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = True <EOL> vbd_rec [ '<STR_LIT>' ] = False <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = [ ] <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> vbd_ref = session . call_xenapi ( '<STR_LIT>' , vbd_rec ) <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return vbd_ref <EOL> @ classmethod <EOL> def create_cd_vbd ( cls , session , vm_ref , vdi_ref , userdevice , bootable ) : <EOL> """<STR_LIT>""" <EOL> vbd_rec = { } <EOL> vbd_rec [ '<STR_LIT>' ] = vm_ref <EOL> vbd_rec [ '<STR_LIT>' ] = vdi_ref <EOL> vbd_rec [ '<STR_LIT>' ] = str ( userdevice ) <EOL> vbd_rec [ '<STR_LIT>' ] = bootable <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = True <EOL> vbd_rec [ '<STR_LIT>' ] = False <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = [ ] <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> vbd_ref = session . call_xenapi ( '<STR_LIT>' , vbd_rec ) <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return vbd_ref <EOL> @ classmethod <EOL> def find_vbd_by_number ( cls , session , vm_ref , number ) : <EOL> """<STR_LIT>""" <EOL> vbd_refs = session . get_xenapi ( ) . VM . get_VBDs ( vm_ref ) <EOL> if vbd_refs : <EOL> for vbd_ref in vbd_refs : <EOL> try : <EOL> vbd_rec = session . get_xenapi ( ) . VBD . get_record ( vbd_ref ) <EOL> if vbd_rec [ '<STR_LIT>' ] == str ( number ) : <EOL> return vbd_ref <EOL> except cls . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> raise StorageError ( _ ( '<STR_LIT>' ) % vm_ref ) <EOL> @ classmethod <EOL> def unplug_vbd ( cls , session , vbd_ref ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vbd_ref = session . call_xenapi ( '<STR_LIT>' , vbd_ref ) <EOL> except cls . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> if exc . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise StorageError ( _ ( '<STR_LIT>' ) % vbd_ref ) <EOL> @ classmethod <EOL> def destroy_vbd ( cls , session , vbd_ref ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> task = session . call_xenapi ( '<STR_LIT>' , vbd_ref ) <EOL> session . wait_for_task ( task ) <EOL> except cls . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> raise StorageError ( _ ( '<STR_LIT>' ) % vbd_ref ) <EOL> @ classmethod <EOL> def destroy_vdi ( cls , session , vdi_ref ) : <EOL> try : <EOL> task = session . call_xenapi ( '<STR_LIT>' , vdi_ref ) <EOL> session . wait_for_task ( task ) <EOL> except cls . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> raise StorageError ( _ ( '<STR_LIT>' ) % vdi_ref ) <EOL> @ classmethod <EOL> def create_vdi ( cls , session , sr_ref , name_label , virtual_size , read_only ) : <EOL> """<STR_LIT>""" <EOL> vdi_ref = session . get_xenapi ( ) . VDI . create ( <EOL> { '<STR_LIT>' : name_label , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : sr_ref , <EOL> '<STR_LIT>' : str ( virtual_size ) , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : read_only , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ ] } ) <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % locals ( ) ) <EOL> return vdi_ref <EOL> @ classmethod <EOL> def get_vdi_for_vm_safely ( cls , session , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vbd_refs = session . get_xenapi ( ) . VM . get_VBDs ( vm_ref ) <EOL> for vbd in vbd_refs : <EOL> vbd_rec = session . get_xenapi ( ) . VBD . get_record ( vbd ) <EOL> if vbd_rec [ '<STR_LIT>' ] == '<STR_LIT:0>' : <EOL> vdi_rec = session . get_xenapi ( ) . VDI . get_record ( vbd_rec [ '<STR_LIT>' ] ) <EOL> return vbd_rec [ '<STR_LIT>' ] , vdi_rec <EOL> raise exception . Error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> @ classmethod <EOL> def create_snapshot ( cls , session , instance_id , vm_ref , label ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> vm_vdi_ref , vm_vdi_rec = cls . get_vdi_for_vm_safely ( session , vm_ref ) <EOL> sr_ref = vm_vdi_rec [ "<STR_LIT>" ] <EOL> original_parent_uuid = get_vhd_parent_uuid ( session , vm_vdi_ref ) <EOL> task = session . call_xenapi ( '<STR_LIT>' , vm_ref , label ) <EOL> template_vm_ref = session . wait_for_task ( task , instance_id ) <EOL> template_vdi_rec = cls . get_vdi_for_vm_safely ( session , <EOL> template_vm_ref ) [ <NUM_LIT:1> ] <EOL> template_vdi_uuid = template_vdi_rec [ "<STR_LIT>" ] <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> parent_uuid = wait_for_vhd_coalesce ( <EOL> session , instance_id , sr_ref , vm_vdi_ref , original_parent_uuid ) <EOL> template_vdi_uuids = { '<STR_LIT:image>' : parent_uuid , <EOL> '<STR_LIT>' : template_vdi_uuid } <EOL> return template_vm_ref , template_vdi_uuids <EOL> @ classmethod <EOL> def get_sr_path ( cls , session ) : <EOL> """<STR_LIT>""" <EOL> sr_ref = safe_find_sr ( session ) <EOL> sr_rec = session . get_xenapi ( ) . SR . get_record ( sr_ref ) <EOL> sr_uuid = sr_rec [ "<STR_LIT>" ] <EOL> return os . path . join ( FLAGS . xenapi_sr_base_path , sr_uuid ) <EOL> @ classmethod <EOL> def upload_image ( cls , context , session , instance , vdi_uuids , image_id ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> os_type = instance . os_type or FLAGS . default_os_type <EOL> glance_host , glance_port = glance . pick_glance_api_server ( ) <EOL> params = { '<STR_LIT>' : vdi_uuids , <EOL> '<STR_LIT>' : image_id , <EOL> '<STR_LIT>' : glance_host , <EOL> '<STR_LIT>' : glance_port , <EOL> '<STR_LIT>' : cls . get_sr_path ( session ) , <EOL> '<STR_LIT>' : os_type , <EOL> '<STR_LIT>' : getattr ( context , '<STR_LIT>' , None ) } <EOL> kwargs = { '<STR_LIT>' : pickle . dumps ( params ) } <EOL> task = session . async_call_plugin ( '<STR_LIT>' , '<STR_LIT>' , kwargs ) <EOL> session . wait_for_task ( task , instance . id ) <EOL> @ classmethod <EOL> def fetch_blank_disk ( cls , session , instance_type_id ) : <EOL> one_gig = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> req_type = instance_types . get_instance_type ( instance_type_id ) <EOL> req_size = req_type [ '<STR_LIT>' ] <EOL> LOG . debug ( "<STR_LIT>" <EOL> % locals ( ) ) <EOL> vdi_size = one_gig * req_size <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> sr_ref = safe_find_sr ( session ) <EOL> vdi_ref = cls . create_vdi ( session , sr_ref , '<STR_LIT>' , vdi_size , False ) <EOL> return vdi_ref <EOL> @ classmethod <EOL> def fetch_image ( cls , context , session , instance , image , user_id , <EOL> project_id , image_type ) : <EOL> """<STR_LIT>""" <EOL> if image_type == ImageType . DISK_VHD : <EOL> return cls . _fetch_image_glance_vhd ( context , <EOL> session , instance , image , image_type ) <EOL> else : <EOL> return cls . _fetch_image_glance_disk ( context , <EOL> session , instance , image , image_type ) <EOL> @ classmethod <EOL> def _fetch_image_glance_vhd ( cls , context , session , instance , image , <EOL> image_type ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> sr_ref = safe_find_sr ( session ) <EOL> uuid_stack = [ str ( uuid . uuid4 ( ) ) for i in xrange ( <NUM_LIT:2> ) ] <EOL> glance_host , glance_port = glance . pick_glance_api_server ( ) <EOL> params = { '<STR_LIT>' : image , <EOL> '<STR_LIT>' : glance_host , <EOL> '<STR_LIT>' : glance_port , <EOL> '<STR_LIT>' : uuid_stack , <EOL> '<STR_LIT>' : cls . get_sr_path ( session ) , <EOL> '<STR_LIT>' : getattr ( context , '<STR_LIT>' , None ) } <EOL> kwargs = { '<STR_LIT>' : pickle . dumps ( params ) } <EOL> task = session . async_call_plugin ( '<STR_LIT>' , '<STR_LIT>' , kwargs ) <EOL> result = session . wait_for_task ( task , instance_id ) <EOL> vdis = json . loads ( result ) <EOL> for vdi in vdis : <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" % vdi ) ) <EOL> cls . scan_sr ( session , instance_id , sr_ref ) <EOL> os_vdi_uuid = vdis [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> vdi_ref = session . get_xenapi ( ) . VDI . get_by_uuid ( os_vdi_uuid ) <EOL> primary_name_label = get_name_label_for_image ( image ) <EOL> session . get_xenapi ( ) . VDI . set_name_label ( vdi_ref , primary_name_label ) <EOL> cls . _check_vdi_size ( context , session , instance , os_vdi_uuid ) <EOL> return vdis <EOL> @ classmethod <EOL> def _get_vdi_chain_size ( cls , context , session , vdi_uuid ) : <EOL> """<STR_LIT>""" <EOL> size_bytes = <NUM_LIT:0> <EOL> for vdi_rec in walk_vdi_chain ( session , vdi_uuid ) : <EOL> cur_vdi_uuid = vdi_rec [ '<STR_LIT>' ] <EOL> vdi_size_bytes = int ( vdi_rec [ '<STR_LIT>' ] ) <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) ) <EOL> size_bytes += vdi_size_bytes <EOL> return size_bytes <EOL> @ classmethod <EOL> def _check_vdi_size ( cls , context , session , instance , vdi_uuid ) : <EOL> size_bytes = cls . _get_vdi_chain_size ( context , session , vdi_uuid ) <EOL> instance_type_id = instance [ '<STR_LIT>' ] <EOL> instance_type = db . instance_type_get ( context , <EOL> instance_type_id ) <EOL> allowed_size_gb = instance_type [ '<STR_LIT>' ] <EOL> allowed_size_bytes = allowed_size_gb * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> if size_bytes > allowed_size_bytes : <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> raise exception . ImageTooLarge ( ) <EOL> @ classmethod <EOL> def _fetch_image_glance_disk ( cls , context , session , instance , image , <EOL> image_type ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % locals ( ) ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , ImageType . to_string ( image_type ) ) <EOL> if image_type == ImageType . DISK_ISO : <EOL> sr_ref = safe_find_iso_sr ( session ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> sr_ref = safe_find_sr ( session ) <EOL> glance_client , image_id = glance . get_glance_client ( context , image ) <EOL> glance_client . set_auth_token ( getattr ( context , '<STR_LIT>' , None ) ) <EOL> meta , image_file = glance_client . get_image ( image_id ) <EOL> virtual_size = int ( meta [ '<STR_LIT:size>' ] ) <EOL> vdi_size = virtual_size <EOL> LOG . debug ( _ ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> if image_type == ImageType . DISK : <EOL> vdi_size += MBR_SIZE_BYTES <EOL> elif image_type in ( ImageType . KERNEL , ImageType . RAMDISK ) and vdi_size > FLAGS . max_kernel_ramdisk_size : <EOL> max_size = FLAGS . max_kernel_ramdisk_size <EOL> raise exception . Error ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> name_label = get_name_label_for_image ( image ) <EOL> vdi_ref = cls . create_vdi ( session , sr_ref , name_label , vdi_size , False ) <EOL> try : <EOL> filename = None <EOL> vdi_uuid = session . get_xenapi ( ) . VDI . get_uuid ( vdi_ref ) <EOL> with_vdi_attached_here ( session , vdi_ref , False , <EOL> lambda dev : <EOL> _stream_disk ( dev , image_type , <EOL> virtual_size , image_file ) ) <EOL> if image_type in ( ImageType . KERNEL , ImageType . RAMDISK ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , vdi_ref ) <EOL> fn = "<STR_LIT>" <EOL> args = { } <EOL> args [ '<STR_LIT>' ] = vdi_ref <EOL> args [ '<STR_LIT>' ] = str ( vdi_size ) <EOL> task = session . async_call_plugin ( '<STR_LIT>' , fn , args ) <EOL> filename = session . wait_for_task ( task , instance_id ) <EOL> session . get_xenapi ( ) . VDI . destroy ( vdi_ref ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , vdi_ref ) <EOL> return [ dict ( vdi_type = ImageType . to_string ( image_type ) , <EOL> vdi_uuid = None , <EOL> file = filename ) ] <EOL> else : <EOL> return [ dict ( vdi_type = ImageType . to_string ( image_type ) , <EOL> vdi_uuid = vdi_uuid , <EOL> file = None ) ] <EOL> except ( cls . XenAPI . Failure , IOError , OSError ) as e : <EOL> LOG . exception ( _ ( "<STR_LIT>" ) , <EOL> instance_id , exc_info = sys . exc_info ( ) ) <EOL> e . args = e . args + ( [ dict ( vdi_type = ImageType . <EOL> to_string ( image_type ) , <EOL> vdi_uuid = vdi_uuid , <EOL> file = filename ) ] , ) <EOL> raise e <EOL> @ classmethod <EOL> def determine_disk_image_type ( cls , instance , context ) : <EOL> """<STR_LIT>""" <EOL> def log_disk_format ( image_type ) : <EOL> pretty_format = { ImageType . KERNEL : '<STR_LIT>' , <EOL> ImageType . RAMDISK : '<STR_LIT>' , <EOL> ImageType . DISK : '<STR_LIT>' , <EOL> ImageType . DISK_RAW : '<STR_LIT>' , <EOL> ImageType . DISK_VHD : '<STR_LIT>' , <EOL> ImageType . DISK_ISO : '<STR_LIT>' } <EOL> disk_format = pretty_format [ image_type ] <EOL> image_ref = instance . image_ref <EOL> instance_id = instance . id <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> def determine_from_glance ( ) : <EOL> glance_disk_format2nova_type = { <EOL> '<STR_LIT>' : ImageType . DISK , <EOL> '<STR_LIT>' : ImageType . KERNEL , <EOL> '<STR_LIT>' : ImageType . RAMDISK , <EOL> '<STR_LIT>' : ImageType . DISK_RAW , <EOL> '<STR_LIT>' : ImageType . DISK_VHD , <EOL> '<STR_LIT>' : ImageType . DISK_ISO } <EOL> image_ref = instance . image_ref <EOL> glance_client , image_id = glance . get_glance_client ( context , <EOL> image_ref ) <EOL> meta = glance_client . get_image_meta ( image_id ) <EOL> disk_format = meta [ '<STR_LIT>' ] <EOL> try : <EOL> return glance_disk_format2nova_type [ disk_format ] <EOL> except KeyError : <EOL> raise exception . InvalidDiskFormat ( disk_format = disk_format ) <EOL> def determine_from_instance ( ) : <EOL> if instance . kernel_id : <EOL> return ImageType . DISK <EOL> else : <EOL> return ImageType . DISK_RAW <EOL> image_type = determine_from_glance ( ) <EOL> log_disk_format ( image_type ) <EOL> return image_type <EOL> @ classmethod <EOL> def determine_is_pv ( cls , session , instance_id , vdi_ref , disk_image_type , <EOL> os_type ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , vdi_ref ) <EOL> if disk_image_type == ImageType . DISK_VHD : <EOL> if os_type == '<STR_LIT>' : <EOL> is_pv = False <EOL> else : <EOL> is_pv = True <EOL> elif disk_image_type == ImageType . DISK_RAW : <EOL> is_pv = with_vdi_attached_here ( session , vdi_ref , True , _is_vdi_pv ) <EOL> elif disk_image_type == ImageType . DISK : <EOL> is_pv = True <EOL> elif disk_image_type == ImageType . DISK_ISO : <EOL> is_pv = False <EOL> else : <EOL> raise exception . Error ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> return is_pv <EOL> @ classmethod <EOL> def lookup ( cls , session , name_label ) : <EOL> """<STR_LIT>""" <EOL> vm_refs = session . get_xenapi ( ) . VM . get_by_name_label ( name_label ) <EOL> n = len ( vm_refs ) <EOL> if n == <NUM_LIT:0> : <EOL> return None <EOL> elif n > <NUM_LIT:1> : <EOL> raise exception . InstanceExists ( name = name_label ) <EOL> else : <EOL> return vm_refs [ <NUM_LIT:0> ] <EOL> @ classmethod <EOL> def lookup_vm_vdis ( cls , session , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vbd_refs = session . get_xenapi ( ) . VM . get_VBDs ( vm_ref ) <EOL> vdi_refs = [ ] <EOL> if vbd_refs : <EOL> for vbd_ref in vbd_refs : <EOL> try : <EOL> vdi_ref = session . get_xenapi ( ) . VBD . get_VDI ( vbd_ref ) <EOL> record = session . get_xenapi ( ) . VDI . get_record ( vdi_ref ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , record [ '<STR_LIT>' ] ) <EOL> except cls . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> else : <EOL> vdi_refs . append ( vdi_ref ) <EOL> if len ( vdi_refs ) > <NUM_LIT:0> : <EOL> return vdi_refs <EOL> else : <EOL> return None <EOL> @ classmethod <EOL> def preconfigure_instance ( cls , session , instance , vdi_ref , network_info ) : <EOL> """<STR_LIT>""" <EOL> mount_required = False <EOL> key , net , metadata = _prepare_injectables ( instance , network_info ) <EOL> mount_required = key or net or metadata <EOL> if not mount_required : <EOL> return <EOL> with_vdi_attached_here ( session , vdi_ref , False , <EOL> lambda dev : _mounted_processing ( dev , key , net , <EOL> metadata ) ) <EOL> @ classmethod <EOL> def lookup_kernel_ramdisk ( cls , session , vm ) : <EOL> vm_rec = session . get_xenapi ( ) . VM . get_record ( vm ) <EOL> if '<STR_LIT>' in vm_rec and '<STR_LIT>' in vm_rec : <EOL> return ( vm_rec [ '<STR_LIT>' ] , vm_rec [ '<STR_LIT>' ] ) <EOL> else : <EOL> return ( None , None ) <EOL> @ classmethod <EOL> def compile_info ( cls , record ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _ ( "<STR_LIT>" ) , <EOL> record [ '<STR_LIT>' ] ) <EOL> LOG . info ( _ ( "<STR_LIT>" ) , <EOL> XENAPI_POWER_STATE [ record [ '<STR_LIT>' ] ] ) <EOL> return { '<STR_LIT:state>' : XENAPI_POWER_STATE [ record [ '<STR_LIT>' ] ] , <EOL> '<STR_LIT>' : long ( record [ '<STR_LIT>' ] ) >> <NUM_LIT:10> , <EOL> '<STR_LIT>' : long ( record [ '<STR_LIT>' ] ) >> <NUM_LIT:10> , <EOL> '<STR_LIT>' : record [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT:0> } <EOL> @ classmethod <EOL> def compile_diagnostics ( cls , session , record ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> host = session . get_xenapi_host ( ) <EOL> host_ip = session . get_xenapi ( ) . host . get_record ( host ) [ "<STR_LIT:address>" ] <EOL> except ( cls . XenAPI . Failure , KeyError ) as e : <EOL> return { "<STR_LIT>" : e } <EOL> try : <EOL> diags = { } <EOL> xml = get_rrd ( host_ip , record [ "<STR_LIT>" ] ) <EOL> if xml : <EOL> rrd = minidom . parseString ( xml ) <EOL> for i , node in enumerate ( rrd . firstChild . childNodes ) : <EOL> if i >= <NUM_LIT:3> and i <= <NUM_LIT:11> : <EOL> ref = node . childNodes <EOL> if len ( ref ) > <NUM_LIT:6> : <EOL> diags [ ref [ <NUM_LIT:0> ] . firstChild . data ] = ref [ <NUM_LIT:6> ] . firstChild . data <EOL> return diags <EOL> except cls . XenAPI . Failure as e : <EOL> return { "<STR_LIT>" : e } <EOL> @ classmethod <EOL> def scan_sr ( cls , session , instance_id = None , sr_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if sr_ref : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , sr_ref ) <EOL> task = session . call_xenapi ( '<STR_LIT>' , sr_ref ) <EOL> session . wait_for_task ( task , instance_id ) <EOL> @ classmethod <EOL> def scan_default_sr ( cls , session ) : <EOL> """<STR_LIT>""" <EOL> sr_ref = find_sr ( session ) <EOL> session . call_xenapi ( '<STR_LIT>' , sr_ref ) <EOL> def get_rrd ( host , vm_uuid ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> xml = urllib . urlopen ( "<STR_LIT>" % ( <EOL> FLAGS . xenapi_connection_username , <EOL> FLAGS . xenapi_connection_password , <EOL> host , <EOL> vm_uuid ) ) <EOL> return xml . read ( ) <EOL> except IOError : <EOL> return None <EOL> def get_vhd_parent ( session , vdi_rec ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in vdi_rec [ '<STR_LIT>' ] : <EOL> parent_uuid = vdi_rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> parent_ref = session . get_xenapi ( ) . VDI . get_by_uuid ( parent_uuid ) <EOL> parent_rec = session . get_xenapi ( ) . VDI . get_record ( parent_ref ) <EOL> vdi_uuid = vdi_rec [ '<STR_LIT>' ] <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % locals ( ) ) <EOL> return parent_ref , parent_rec <EOL> else : <EOL> return None <EOL> def get_vhd_parent_uuid ( session , vdi_ref ) : <EOL> vdi_rec = session . get_xenapi ( ) . VDI . get_record ( vdi_ref ) <EOL> ret = get_vhd_parent ( session , vdi_rec ) <EOL> if ret : <EOL> parent_ref , parent_rec = ret <EOL> return parent_rec [ "<STR_LIT>" ] <EOL> else : <EOL> return None <EOL> def walk_vdi_chain ( session , vdi_uuid ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> vdi_ref = session . get_xenapi ( ) . VDI . get_by_uuid ( vdi_uuid ) <EOL> vdi_rec = session . get_xenapi ( ) . VDI . get_record ( vdi_ref ) <EOL> yield vdi_rec <EOL> parent_uuid = vdi_rec [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> if parent_uuid : <EOL> vdi_uuid = parent_uuid <EOL> else : <EOL> break <EOL> def wait_for_vhd_coalesce ( session , instance_id , sr_ref , vdi_ref , <EOL> original_parent_uuid ) : <EOL> """<STR_LIT>""" <EOL> max_attempts = FLAGS . xenapi_vhd_coalesce_max_attempts <EOL> attempts = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> def _poll_vhds ( ) : <EOL> attempts [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if attempts [ '<STR_LIT>' ] > max_attempts : <EOL> counter = attempts [ '<STR_LIT>' ] <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> raise exception . Error ( msg ) <EOL> VMHelper . scan_sr ( session , instance_id , sr_ref ) <EOL> parent_uuid = get_vhd_parent_uuid ( session , vdi_ref ) <EOL> if original_parent_uuid and ( parent_uuid != original_parent_uuid ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> else : <EOL> raise utils . LoopingCallDone ( parent_uuid ) <EOL> loop = utils . LoopingCall ( _poll_vhds ) <EOL> loop . start ( FLAGS . xenapi_vhd_coalesce_poll_interval , now = True ) <EOL> parent_uuid = loop . wait ( ) <EOL> return parent_uuid <EOL> def get_vdi_for_vm_safely ( session , vm_ref ) : <EOL> vdi_refs = VMHelper . lookup_vm_vdis ( session , vm_ref ) <EOL> if vdi_refs is None : <EOL> raise Exception ( _ ( "<STR_LIT>" ) % vm_ref ) <EOL> else : <EOL> num_vdis = len ( vdi_refs ) <EOL> if num_vdis != <NUM_LIT:1> : <EOL> raise exception . Error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> vdi_ref = vdi_refs [ <NUM_LIT:0> ] <EOL> vdi_rec = session . get_xenapi ( ) . VDI . get_record ( vdi_ref ) <EOL> return vdi_ref , vdi_rec <EOL> def safe_find_sr ( session ) : <EOL> """<STR_LIT>""" <EOL> sr_ref = find_sr ( session ) <EOL> if sr_ref is None : <EOL> raise exception . StorageRepositoryNotFound ( ) <EOL> return sr_ref <EOL> def find_sr ( session ) : <EOL> """<STR_LIT>""" <EOL> host = session . get_xenapi_host ( ) <EOL> sr_refs = session . get_xenapi ( ) . SR . get_all ( ) <EOL> for sr_ref in sr_refs : <EOL> sr_rec = session . get_xenapi ( ) . SR . get_record ( sr_ref ) <EOL> if not ( '<STR_LIT>' in sr_rec [ '<STR_LIT>' ] and <EOL> sr_rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> continue <EOL> for pbd_ref in sr_rec [ '<STR_LIT>' ] : <EOL> pbd_rec = session . get_xenapi ( ) . PBD . get_record ( pbd_ref ) <EOL> if pbd_rec [ '<STR_LIT:host>' ] == host : <EOL> return sr_ref <EOL> return None <EOL> def safe_find_iso_sr ( session ) : <EOL> """<STR_LIT>""" <EOL> sr_ref = find_iso_sr ( session ) <EOL> if sr_ref is None : <EOL> raise exception . NotFound ( _ ( '<STR_LIT>' ) ) <EOL> return sr_ref <EOL> def find_iso_sr ( session ) : <EOL> """<STR_LIT>""" <EOL> host = session . get_xenapi_host ( ) <EOL> sr_refs = session . get_xenapi ( ) . SR . get_all ( ) <EOL> for sr_ref in sr_refs : <EOL> sr_rec = session . get_xenapi ( ) . SR . get_record ( sr_ref ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % locals ( ) ) <EOL> if not sr_rec [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> continue <EOL> if not '<STR_LIT>' in sr_rec [ '<STR_LIT>' ] : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> continue <EOL> if not sr_rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> continue <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> for pbd_ref in sr_rec [ '<STR_LIT>' ] : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> pbd_rec = session . get_xenapi ( ) . PBD . get_record ( pbd_ref ) <EOL> pbd_rec_host = pbd_rec [ '<STR_LIT:host>' ] <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % <EOL> locals ( ) ) <EOL> if pbd_rec_host == host : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> return sr_ref <EOL> return None <EOL> def remap_vbd_dev ( dev ) : <EOL> """<STR_LIT>""" <EOL> should_remap = FLAGS . xenapi_remap_vbd_dev <EOL> if not should_remap : <EOL> return dev <EOL> old_prefix = '<STR_LIT>' <EOL> new_prefix = FLAGS . xenapi_remap_vbd_dev_prefix <EOL> remapped_dev = dev . replace ( old_prefix , new_prefix ) <EOL> return remapped_dev <EOL> def _wait_for_device ( dev ) : <EOL> """<STR_LIT>""" <EOL> for i in xrange ( <NUM_LIT:0> , FLAGS . block_device_creation_timeout ) : <EOL> if os . path . exists ( '<STR_LIT>' % dev ) : <EOL> return <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> raise StorageError ( _ ( '<STR_LIT>' ) % dev ) <EOL> def with_vdi_attached_here ( session , vdi_ref , read_only , f ) : <EOL> this_vm_ref = get_this_vm_ref ( session ) <EOL> vbd_rec = { } <EOL> vbd_rec [ '<STR_LIT>' ] = this_vm_ref <EOL> vbd_rec [ '<STR_LIT>' ] = vdi_ref <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = False <EOL> vbd_rec [ '<STR_LIT>' ] = read_only and '<STR_LIT>' or '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = True <EOL> vbd_rec [ '<STR_LIT>' ] = False <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> vbd_rec [ '<STR_LIT>' ] = { } <EOL> vbd_rec [ '<STR_LIT>' ] = [ ] <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vdi_ref ) <EOL> vbd_ref = session . get_xenapi ( ) . VBD . create ( vbd_rec ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vdi_ref ) <EOL> try : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vbd_ref ) <EOL> session . get_xenapi ( ) . VBD . plug ( vbd_ref ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vbd_ref ) <EOL> orig_dev = session . get_xenapi ( ) . VBD . get_device ( vbd_ref ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> dev = remap_vbd_dev ( orig_dev ) <EOL> if dev != orig_dev : <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> if dev != '<STR_LIT>' : <EOL> _wait_for_device ( dev ) <EOL> return f ( dev ) <EOL> finally : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vdi_ref ) <EOL> vbd_unplug_with_retry ( session , vbd_ref ) <EOL> ignore_failure ( session . get_xenapi ( ) . VBD . destroy , vbd_ref ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vdi_ref ) <EOL> def vbd_unplug_with_retry ( session , vbd_ref ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> session . get_xenapi ( ) . VBD . unplug ( vbd_ref ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) ) <EOL> return <EOL> except VMHelper . XenAPI . Failure , e : <EOL> if ( len ( e . details ) > <NUM_LIT:0> and <EOL> e . details [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) ) <EOL> elif ( len ( e . details ) > <NUM_LIT:0> and <EOL> e . details [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) ) <EOL> return <EOL> else : <EOL> LOG . error ( _ ( '<STR_LIT>' ) , <EOL> e ) <EOL> return <EOL> def ignore_failure ( func , * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except VMHelper . XenAPI . Failure , e : <EOL> LOG . error ( _ ( '<STR_LIT>' ) , e ) <EOL> return None <EOL> def get_this_vm_uuid ( ) : <EOL> with file ( '<STR_LIT>' ) as f : <EOL> return f . readline ( ) . strip ( ) <EOL> def get_this_vm_ref ( session ) : <EOL> return session . get_xenapi ( ) . VM . get_by_uuid ( get_this_vm_uuid ( ) ) <EOL> def _is_vdi_pv ( dev ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , dev ) <EOL> output = os . popen ( '<STR_LIT>' % dev ) <EOL> for line in output . readlines ( ) : <EOL> m = re . search ( '<STR_LIT>' , line ) <EOL> if m and m . group ( <NUM_LIT:0> ) . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % m . group ( <NUM_LIT:0> ) ) <EOL> return True <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> return False <EOL> def _stream_disk ( dev , image_type , virtual_size , image_file ) : <EOL> offset = <NUM_LIT:0> <EOL> if image_type == ImageType . DISK : <EOL> offset = MBR_SIZE_BYTES <EOL> _write_partition ( virtual_size , dev ) <EOL> utils . execute ( '<STR_LIT>' , os . getuid ( ) , '<STR_LIT>' % dev , run_as_root = True ) <EOL> with open ( '<STR_LIT>' % dev , '<STR_LIT:wb>' ) as f : <EOL> f . seek ( offset ) <EOL> for chunk in image_file : <EOL> f . write ( chunk ) <EOL> def _write_partition ( virtual_size , dev ) : <EOL> dest = '<STR_LIT>' % dev <EOL> primary_first = MBR_SIZE_SECTORS <EOL> primary_last = MBR_SIZE_SECTORS + ( virtual_size / SECTOR_SIZE ) - <NUM_LIT:1> <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> def execute ( * cmd , ** kwargs ) : <EOL> return utils . execute ( * cmd , ** kwargs ) <EOL> execute ( '<STR_LIT>' , '<STR_LIT>' , dest , '<STR_LIT>' , '<STR_LIT>' , run_as_root = True ) <EOL> execute ( '<STR_LIT>' , '<STR_LIT>' , dest , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % primary_first , <EOL> '<STR_LIT>' % primary_last , <EOL> run_as_root = True ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , dest ) <EOL> def get_name_label_for_image ( image ) : <EOL> return _ ( '<STR_LIT>' ) % image <EOL> def _mount_filesystem ( dev_path , dir ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> out , err = utils . execute ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> dev_path , dir , run_as_root = True ) <EOL> except exception . ProcessExecutionError as e : <EOL> err = str ( e ) <EOL> return err <EOL> def _find_guest_agent ( base_dir , agent_rel_path ) : <EOL> """<STR_LIT>""" <EOL> agent_path = os . path . join ( base_dir , agent_rel_path ) <EOL> if os . path . isfile ( agent_path ) : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return True <EOL> xe_daemon_filename = os . path . join ( base_dir , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . isfile ( xe_daemon_filename ) : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return False <EOL> def _mounted_processing ( device , key , net , metadata ) : <EOL> """<STR_LIT>""" <EOL> dev_path = '<STR_LIT>' + device + '<STR_LIT:1>' <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> try : <EOL> err = _mount_filesystem ( dev_path , tmpdir ) <EOL> if not err : <EOL> try : <EOL> if not _find_guest_agent ( tmpdir , FLAGS . xenapi_agent_path ) : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> disk . inject_data_into_fs ( tmpdir , key , net , metadata , <EOL> utils . execute ) <EOL> finally : <EOL> utils . execute ( '<STR_LIT>' , dev_path , run_as_root = True ) <EOL> else : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % err ) <EOL> finally : <EOL> os . rmdir ( tmpdir ) <EOL> def _prepare_injectables ( inst , networks_info ) : <EOL> """<STR_LIT>""" <EOL> from Cheetah import Template as t <EOL> template = t . Template <EOL> template_data = open ( FLAGS . injected_network_template ) . read ( ) <EOL> metadata = inst [ '<STR_LIT>' ] <EOL> key = str ( inst [ '<STR_LIT>' ] ) <EOL> net = None <EOL> if networks_info : <EOL> ifc_num = - <NUM_LIT:1> <EOL> interfaces_info = [ ] <EOL> have_injected_networks = False <EOL> for ( network_ref , info ) in networks_info : <EOL> ifc_num += <NUM_LIT:1> <EOL> if not network_ref [ '<STR_LIT>' ] : <EOL> continue <EOL> have_injected_networks = True <EOL> ip_v4 = ip_v6 = None <EOL> if '<STR_LIT>' in info and len ( info [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> ip_v4 = info [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in info and len ( info [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> ip_v6 = info [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if len ( info [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> dns = info [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> else : <EOL> dns = '<STR_LIT>' <EOL> interface_info = { '<STR_LIT:name>' : '<STR_LIT>' % ifc_num , <EOL> '<STR_LIT:address>' : ip_v4 and ip_v4 [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> '<STR_LIT>' : ip_v4 and ip_v4 [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> '<STR_LIT>' : info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : dns , <EOL> '<STR_LIT>' : ip_v6 and ip_v6 [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> '<STR_LIT>' : ip_v6 and ip_v6 [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> '<STR_LIT>' : ip_v6 and info [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> '<STR_LIT>' : FLAGS . use_ipv6 } <EOL> interfaces_info . append ( interface_info ) <EOL> if have_injected_networks : <EOL> net = str ( template ( template_data , <EOL> searchList = [ { '<STR_LIT>' : interfaces_info , <EOL> '<STR_LIT>' : FLAGS . use_ipv6 } ] ) ) <EOL> return key , net , metadata </s>
