<s> """<STR_LIT>""" <EOL> import base64 <EOL> import json <EOL> import M2Crypto <EOL> import os <EOL> import pickle <EOL> import random <EOL> import subprocess <EOL> import sys <EOL> import time <EOL> import uuid <EOL> from nova import context as nova_context <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import ipv6 <EOL> from nova import log as logging <EOL> from nova import utils <EOL> from nova . compute import power_state <EOL> from nova . virt import driver <EOL> from nova . virt . xenapi . network_utils import NetworkHelper <EOL> from nova . virt . xenapi . vm_utils import VMHelper <EOL> from nova . virt . xenapi . vm_utils import ImageType <EOL> XenAPI = None <EOL> LOG = logging . getLogger ( "<STR_LIT>" ) <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def cmp_version ( a , b ) : <EOL> """<STR_LIT>""" <EOL> a = a . split ( '<STR_LIT:.>' ) <EOL> b = b . split ( '<STR_LIT:.>' ) <EOL> for va , vb in zip ( a , b ) : <EOL> ret = int ( va ) - int ( vb ) <EOL> if ret : <EOL> return ret <EOL> return len ( a ) - len ( b ) <EOL> class VMOps ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session ) : <EOL> self . XenAPI = session . get_imported_xenapi ( ) <EOL> self . _session = session <EOL> self . poll_rescue_last_ran = None <EOL> VMHelper . XenAPI = self . XenAPI <EOL> self . vif_driver = utils . import_object ( FLAGS . xenapi_vif_driver ) <EOL> def list_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> vm_refs = [ ] <EOL> for vm_ref in self . _session . get_xenapi ( ) . VM . get_all ( ) : <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> if not vm_rec [ "<STR_LIT>" ] and not vm_rec [ "<STR_LIT>" ] : <EOL> vm_refs . append ( vm_rec [ "<STR_LIT>" ] ) <EOL> return vm_refs <EOL> def list_instances_detail ( self ) : <EOL> """<STR_LIT>""" <EOL> instance_infos = [ ] <EOL> for vm_ref in self . _session . get_xenapi ( ) . VM . get_all ( ) : <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> if not vm_rec [ "<STR_LIT>" ] and not vm_rec [ "<STR_LIT>" ] : <EOL> name = vm_rec [ "<STR_LIT>" ] <EOL> openstack_format = VMHelper . compile_info ( vm_rec ) <EOL> state = openstack_format [ '<STR_LIT:state>' ] <EOL> instance_info = driver . InstanceInfo ( name , state ) <EOL> instance_infos . append ( instance_info ) <EOL> return instance_infos <EOL> def revert_migration ( self , instance ) : <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> self . _start ( instance , vm_ref ) <EOL> def finish_migration ( self , context , instance , disk_info , network_info , <EOL> resize_instance ) : <EOL> vdi_uuid = self . link_disks ( instance , disk_info [ '<STR_LIT>' ] , <EOL> disk_info [ '<STR_LIT>' ] ) <EOL> vm_ref = self . _create_vm ( context , instance , <EOL> [ dict ( vdi_type = '<STR_LIT>' , vdi_uuid = vdi_uuid ) ] , <EOL> network_info ) <EOL> if resize_instance : <EOL> self . resize_instance ( instance , vdi_uuid ) <EOL> self . _start ( instance , vm_ref = vm_ref ) <EOL> def _start ( self , instance , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if not vm_ref : <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> if vm_ref is None : <EOL> raise Exception ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % instance . id ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , instance . name ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , False , False ) <EOL> def _create_disks ( self , context , instance ) : <EOL> disk_image_type = VMHelper . determine_disk_image_type ( instance , context ) <EOL> vdis = VMHelper . fetch_image ( context , self . _session , <EOL> instance , instance . image_ref , <EOL> instance . user_id , instance . project_id , <EOL> disk_image_type ) <EOL> for vdi in vdis : <EOL> if vdi [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> self . resize_instance ( instance , vdi [ "<STR_LIT>" ] ) <EOL> return vdis <EOL> def spawn ( self , context , instance , network_info ) : <EOL> vdis = None <EOL> try : <EOL> vdis = self . _create_disks ( context , instance ) <EOL> vm_ref = self . _create_vm ( context , instance , vdis , network_info ) <EOL> self . _spawn ( instance , vm_ref ) <EOL> except ( self . XenAPI . Failure , OSError , IOError ) as spawn_error : <EOL> LOG . exception ( _ ( "<STR_LIT>" ) , <EOL> instance . id , exc_info = sys . exc_info ( ) ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , <EOL> instance . id ) <EOL> self . _handle_spawn_error ( vdis , spawn_error ) <EOL> raise spawn_error <EOL> def spawn_rescue ( self , context , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . spawn ( context , instance , network_info ) <EOL> def _create_vm ( self , context , instance , vdis , network_info ) : <EOL> """<STR_LIT>""" <EOL> instance_name = instance . name <EOL> vm_ref = VMHelper . lookup ( self . _session , instance_name ) <EOL> if vm_ref is not None : <EOL> raise exception . InstanceExists ( name = instance_name ) <EOL> if not VMHelper . ensure_free_mem ( self . _session , instance ) : <EOL> raise exception . InsufficientFreeMemory ( uuid = instance . uuid ) <EOL> disk_image_type = VMHelper . determine_disk_image_type ( instance , context ) <EOL> kernel = None <EOL> ramdisk = None <EOL> try : <EOL> if instance . kernel_id : <EOL> kernel = VMHelper . fetch_image ( context , self . _session , <EOL> instance , instance . kernel_id , instance . user_id , <EOL> instance . project_id , ImageType . KERNEL ) [ <NUM_LIT:0> ] <EOL> if instance . ramdisk_id : <EOL> ramdisk = VMHelper . fetch_image ( context , self . _session , <EOL> instance , instance . ramdisk_id , instance . user_id , <EOL> instance . project_id , ImageType . RAMDISK ) [ <NUM_LIT:0> ] <EOL> first_vdi_ref = None <EOL> for vdi in vdis : <EOL> if vdi . get ( '<STR_LIT>' ) != '<STR_LIT>' : <EOL> first_vdi_ref = self . _session . call_xenapi ( <EOL> '<STR_LIT>' , vdi [ '<STR_LIT>' ] ) <EOL> vm_mode = instance . vm_mode and instance . vm_mode . lower ( ) <EOL> if vm_mode == '<STR_LIT>' : <EOL> use_pv_kernel = True <EOL> elif vm_mode in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> use_pv_kernel = False <EOL> vm_mode = '<STR_LIT>' <EOL> else : <EOL> use_pv_kernel = VMHelper . determine_is_pv ( self . _session , <EOL> instance . id , first_vdi_ref , disk_image_type , <EOL> instance . os_type ) <EOL> vm_mode = use_pv_kernel and '<STR_LIT>' or '<STR_LIT>' <EOL> if instance . vm_mode != vm_mode : <EOL> db . instance_update ( nova_context . get_admin_context ( ) , <EOL> instance [ '<STR_LIT:id>' ] , { '<STR_LIT>' : vm_mode } ) <EOL> vm_ref = VMHelper . create_vm ( self . _session , instance , <EOL> kernel and kernel . get ( '<STR_LIT:file>' , None ) or None , <EOL> ramdisk and ramdisk . get ( '<STR_LIT:file>' , None ) or None , <EOL> use_pv_kernel ) <EOL> except ( self . XenAPI . Failure , OSError , IOError ) as vm_create_error : <EOL> LOG . exception ( _ ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) , <EOL> instance . id , exc_info = sys . exc_info ( ) ) <EOL> last_arg = None <EOL> resources = [ ] <EOL> if vm_create_error . args : <EOL> last_arg = vm_create_error . args [ - <NUM_LIT:1> ] <EOL> if isinstance ( last_arg , list ) : <EOL> resources = last_arg <EOL> else : <EOL> vm_create_error . args = vm_create_error . args + ( resources , ) <EOL> if kernel : <EOL> resources . append ( kernel ) <EOL> if ramdisk : <EOL> resources . append ( ramdisk ) <EOL> raise vm_create_error <EOL> self . _attach_disks ( instance , disk_image_type , vm_ref , first_vdi_ref , <EOL> vdis ) <EOL> if FLAGS . flat_injected : <EOL> VMHelper . preconfigure_instance ( self . _session , instance , <EOL> first_vdi_ref , network_info ) <EOL> self . create_vifs ( vm_ref , instance , network_info ) <EOL> self . inject_network_info ( instance , network_info , vm_ref ) <EOL> self . inject_hostname ( instance , vm_ref , instance [ '<STR_LIT>' ] ) <EOL> return vm_ref <EOL> def _attach_disks ( self , instance , disk_image_type , vm_ref , first_vdi_ref , <EOL> vdis ) : <EOL> userdevice = <NUM_LIT:0> <EOL> if disk_image_type == ImageType . DISK_ISO : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> cd_vdi_ref = first_vdi_ref <EOL> first_vdi_ref = VMHelper . fetch_blank_disk ( session = self . _session , <EOL> instance_type_id = instance . instance_type_id ) <EOL> VMHelper . create_vbd ( session = self . _session , vm_ref = vm_ref , <EOL> vdi_ref = first_vdi_ref , userdevice = userdevice , bootable = False ) <EOL> userdevice = <NUM_LIT:2> <EOL> VMHelper . create_cd_vbd ( session = self . _session , vm_ref = vm_ref , <EOL> vdi_ref = cd_vdi_ref , userdevice = userdevice , bootable = True ) <EOL> userdevice += <NUM_LIT:1> <EOL> else : <EOL> VMHelper . create_vbd ( session = self . _session , vm_ref = vm_ref , <EOL> vdi_ref = first_vdi_ref , userdevice = userdevice , bootable = True ) <EOL> userdevice = <NUM_LIT:2> <EOL> for vdi in vdis [ <NUM_LIT:1> : ] : <EOL> vdi_ref = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> vdi [ '<STR_LIT>' ] ) <EOL> VMHelper . create_vbd ( session = self . _session , vm_ref = vm_ref , <EOL> vdi_ref = vdi_ref , userdevice = userdevice , <EOL> bootable = False ) <EOL> userdevice += <NUM_LIT:1> <EOL> def _spawn ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vm_ref ) <EOL> self . _start ( instance , vm_ref ) <EOL> instance_name = instance . name <EOL> LOG . info ( _ ( '<STR_LIT>' ) <EOL> % locals ( ) ) <EOL> ctx = nova_context . get_admin_context ( ) <EOL> agent_build = db . agent_build_get_by_triple ( ctx , '<STR_LIT>' , <EOL> instance . os_type , instance . architecture ) <EOL> if agent_build : <EOL> LOG . info ( _ ( '<STR_LIT>' + '<STR_LIT>' ) % agent_build ) <EOL> else : <EOL> LOG . info ( _ ( '<STR_LIT>' + '<STR_LIT>' ) % { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : instance . os_type , <EOL> '<STR_LIT>' : instance . architecture } ) <EOL> def _check_agent_version ( ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> if instance . os_type == '<STR_LIT>' : <EOL> version = self . get_agent_version ( instance , <EOL> timeout = FLAGS . windows_version_timeout ) <EOL> else : <EOL> version = self . get_agent_version ( instance ) <EOL> if not version : <EOL> return <EOL> LOG . info ( _ ( '<STR_LIT>' ) % version ) <EOL> if not agent_build : <EOL> return <EOL> if cmp_version ( version , agent_build [ '<STR_LIT:version>' ] ) < <NUM_LIT:0> : <EOL> LOG . info ( _ ( '<STR_LIT>' ) % agent_build [ '<STR_LIT:version>' ] ) <EOL> self . agent_update ( instance , agent_build [ '<STR_LIT:url>' ] , <EOL> agent_build [ '<STR_LIT>' ] ) <EOL> def _inject_files ( ) : <EOL> injected_files = instance . injected_files <EOL> if injected_files : <EOL> if isinstance ( injected_files , basestring ) : <EOL> try : <EOL> injected_files = json . loads ( injected_files ) <EOL> except ValueError : <EOL> LOG . exception ( <EOL> _ ( "<STR_LIT>" ) <EOL> % injected_files ) <EOL> injected_files = [ ] <EOL> for path , contents in instance . injected_files : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % path ) <EOL> self . inject_file ( instance , path , contents ) <EOL> def _set_admin_password ( ) : <EOL> admin_password = instance . admin_pass <EOL> if admin_password : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> self . set_admin_password ( instance , admin_password ) <EOL> def _reset_network ( ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> self . reset_network ( instance , vm_ref ) <EOL> timer = utils . LoopingCall ( f = None ) <EOL> def _wait_for_boot ( ) : <EOL> try : <EOL> state = self . get_info ( instance_name ) [ '<STR_LIT:state>' ] <EOL> if state == power_state . RUNNING : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_name ) <EOL> timer . stop ( ) <EOL> _check_agent_version ( ) <EOL> _inject_files ( ) <EOL> _set_admin_password ( ) <EOL> _reset_network ( ) <EOL> return True <EOL> except Exception , exc : <EOL> LOG . warn ( exc ) <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , instance_name ) <EOL> timer . stop ( ) <EOL> return False <EOL> timer . f = _wait_for_boot <EOL> return timer . start ( interval = <NUM_LIT:0.5> , now = True ) <EOL> def _handle_spawn_error ( self , vdis , spawn_error ) : <EOL> resources = [ ] <EOL> if spawn_error . args : <EOL> last_arg = spawn_error . args [ - <NUM_LIT:1> ] <EOL> resources = last_arg <EOL> if vdis : <EOL> for vdi in vdis : <EOL> resources . append ( dict ( vdi_type = vdi [ '<STR_LIT>' ] , <EOL> vdi_uuid = vdi [ '<STR_LIT>' ] , <EOL> file = None ) ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , resources ) <EOL> kernel_file = None <EOL> ramdisk_file = None <EOL> for item in resources : <EOL> vdi_type = item [ '<STR_LIT>' ] <EOL> vdi_to_remove = item [ '<STR_LIT>' ] <EOL> if vdi_to_remove : <EOL> try : <EOL> vdi_ref = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> vdi_to_remove ) <EOL> LOG . debug ( _ ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) , locals ( ) ) <EOL> VMHelper . destroy_vdi ( self . _session , vdi_ref ) <EOL> except self . XenAPI . Failure : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , vdi_to_remove ) <EOL> if item [ '<STR_LIT:file>' ] : <EOL> if vdi_type == ImageType . KERNEL_STR : <EOL> kernel_file = item [ '<STR_LIT:file>' ] <EOL> elif vdi_type == ImageType . RAMDISK_STR : <EOL> ramdisk_file = item [ '<STR_LIT:file>' ] <EOL> if kernel_file or ramdisk_file : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> self . _destroy_kernel_ramdisk_plugin_call ( kernel_file , <EOL> ramdisk_file ) <EOL> def _get_vm_opaque_ref ( self , instance_or_vm ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( instance_or_vm , basestring ) : <EOL> obj = None <EOL> try : <EOL> obj = self . _session . get_xenapi ( ) . VM . get_uuid ( instance_or_vm ) <EOL> return instance_or_vm <EOL> except self . XenAPI . Failure : <EOL> instance_name = instance_or_vm <EOL> elif isinstance ( instance_or_vm , ( int , long ) ) : <EOL> ctx = nova_context . get_admin_context ( ) <EOL> instance_obj = db . instance_get ( ctx , instance_or_vm ) <EOL> instance_name = instance_obj . name <EOL> else : <EOL> instance_name = instance_or_vm . name <EOL> vm_ref = VMHelper . lookup ( self . _session , instance_name ) <EOL> if vm_ref is None : <EOL> raise exception . NotFound ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % instance_or_vm ) <EOL> return vm_ref <EOL> def _acquire_bootlock ( self , vm ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( <EOL> "<STR_LIT>" , <EOL> vm , <EOL> { "<STR_LIT:start>" : "<STR_LIT>" } ) <EOL> def _release_bootlock ( self , vm ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( <EOL> "<STR_LIT>" , <EOL> vm , <EOL> "<STR_LIT:start>" ) <EOL> def snapshot ( self , context , instance , image_id ) : <EOL> """<STR_LIT>""" <EOL> template_vm_ref = None <EOL> try : <EOL> template_vm_ref , template_vdi_uuids = self . _get_snapshot ( instance ) <EOL> VMHelper . upload_image ( context , <EOL> self . _session , instance , template_vdi_uuids , image_id ) <EOL> finally : <EOL> if template_vm_ref : <EOL> self . _destroy ( instance , template_vm_ref , <EOL> shutdown = False , destroy_kernel_ramdisk = False ) <EOL> logging . debug ( _ ( "<STR_LIT>" ) , instance ) <EOL> def _get_snapshot ( self , instance ) : <EOL> logging . debug ( _ ( "<STR_LIT>" ) , instance ) <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> label = "<STR_LIT>" % instance . name <EOL> try : <EOL> template_vm_ref , template_vdi_uuids = VMHelper . create_snapshot ( <EOL> self . _session , instance . id , vm_ref , label ) <EOL> return template_vm_ref , template_vdi_uuids <EOL> except self . XenAPI . Failure , exc : <EOL> logging . error ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> return <EOL> def migrate_disk_and_power_off ( self , instance , dest ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> base_copy_uuid = cow_uuid = None <EOL> template_vdi_uuids = template_vm_ref = None <EOL> try : <EOL> template_vm_ref , template_vdi_uuids = self . _get_snapshot ( instance ) <EOL> base_copy_uuid = template_vdi_uuids [ '<STR_LIT:image>' ] <EOL> vdi_ref , vm_vdi_rec = VMHelper . get_vdi_for_vm_safely ( self . _session , vm_ref ) <EOL> cow_uuid = vm_vdi_rec [ '<STR_LIT>' ] <EOL> params = { '<STR_LIT:host>' : dest , <EOL> '<STR_LIT>' : base_copy_uuid , <EOL> '<STR_LIT>' : instance . id , <EOL> '<STR_LIT>' : VMHelper . get_sr_path ( self . _session ) } <EOL> task = self . _session . async_call_plugin ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : pickle . dumps ( params ) } ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> self . _shutdown ( instance , vm_ref , hard = False ) <EOL> params = { '<STR_LIT:host>' : dest , <EOL> '<STR_LIT>' : cow_uuid , <EOL> '<STR_LIT>' : instance . id , <EOL> '<STR_LIT>' : VMHelper . get_sr_path ( self . _session ) , } <EOL> task = self . _session . async_call_plugin ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : pickle . dumps ( params ) } ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> finally : <EOL> if template_vm_ref : <EOL> self . _destroy ( instance , template_vm_ref , <EOL> shutdown = False , destroy_kernel_ramdisk = False ) <EOL> return { '<STR_LIT>' : base_copy_uuid , '<STR_LIT>' : cow_uuid } <EOL> def link_disks ( self , instance , base_copy_uuid , cow_uuid ) : <EOL> """<STR_LIT>""" <EOL> new_base_copy_uuid = str ( uuid . uuid4 ( ) ) <EOL> new_cow_uuid = str ( uuid . uuid4 ( ) ) <EOL> params = { '<STR_LIT>' : instance . id , <EOL> '<STR_LIT>' : base_copy_uuid , <EOL> '<STR_LIT>' : cow_uuid , <EOL> '<STR_LIT>' : new_base_copy_uuid , <EOL> '<STR_LIT>' : new_cow_uuid , <EOL> '<STR_LIT>' : VMHelper . get_sr_path ( self . _session ) , } <EOL> task = self . _session . async_call_plugin ( '<STR_LIT>' , <EOL> '<STR_LIT>' , { '<STR_LIT>' : pickle . dumps ( params ) } ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> VMHelper . scan_default_sr ( self . _session ) <EOL> return new_cow_uuid <EOL> def resize_instance ( self , instance , vdi_uuid ) : <EOL> """<STR_LIT>""" <EOL> new_disk_size = instance . local_gb * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> if new_disk_size > <NUM_LIT:0> : <EOL> instance_name = instance . name <EOL> instance_local_gb = instance . local_gb <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> vdi_ref = self . _session . call_xenapi ( '<STR_LIT>' , vdi_uuid ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vdi_ref , <EOL> str ( new_disk_size ) ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % ( instance . name ) ) <EOL> def reboot ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> def get_agent_version ( self , instance , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def _call ( ) : <EOL> transaction_id = str ( uuid . uuid4 ( ) ) <EOL> args = { '<STR_LIT:id>' : transaction_id } <EOL> resp = self . _make_agent_call ( '<STR_LIT:version>' , instance , '<STR_LIT>' , args ) <EOL> if resp [ '<STR_LIT>' ] != '<STR_LIT:0>' : <EOL> LOG . error ( _ ( '<STR_LIT>' ) % <EOL> locals ( ) ) <EOL> return None <EOL> return resp [ '<STR_LIT:message>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if timeout : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> domid = vm_rec [ '<STR_LIT>' ] <EOL> expiration = time . time ( ) + timeout <EOL> while time . time ( ) < expiration : <EOL> ret = _call ( ) <EOL> if ret : <EOL> return ret <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> if vm_rec [ '<STR_LIT>' ] != domid : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % { <EOL> '<STR_LIT>' : domid , <EOL> '<STR_LIT>' : vm_rec [ '<STR_LIT>' ] } ) <EOL> domid = vm_rec [ '<STR_LIT>' ] <EOL> else : <EOL> return _call ( ) <EOL> def agent_update ( self , instance , url , md5sum ) : <EOL> """<STR_LIT>""" <EOL> transaction_id = str ( uuid . uuid4 ( ) ) <EOL> args = { '<STR_LIT:id>' : transaction_id , '<STR_LIT:url>' : url , '<STR_LIT>' : md5sum } <EOL> resp = self . _make_agent_call ( '<STR_LIT>' , instance , '<STR_LIT>' , args ) <EOL> if resp [ '<STR_LIT>' ] != '<STR_LIT:0>' : <EOL> LOG . error ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> return None <EOL> return resp [ '<STR_LIT:message>' ] <EOL> def set_admin_password ( self , instance , new_pass ) : <EOL> """<STR_LIT>""" <EOL> key_init_transaction_id = str ( uuid . uuid4 ( ) ) <EOL> dh = SimpleDH ( ) <EOL> key_init_args = { '<STR_LIT:id>' : key_init_transaction_id , <EOL> '<STR_LIT>' : str ( dh . get_public ( ) ) } <EOL> resp = self . _make_agent_call ( '<STR_LIT>' , instance , '<STR_LIT>' , key_init_args ) <EOL> if resp [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> LOG . error ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> return None <EOL> agent_pub = int ( resp [ '<STR_LIT:message>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> dh . compute_shared ( agent_pub ) <EOL> enc_pass = dh . encrypt ( new_pass + '<STR_LIT:\n>' ) <EOL> password_transaction_id = str ( uuid . uuid4 ( ) ) <EOL> password_args = { '<STR_LIT:id>' : password_transaction_id , '<STR_LIT>' : enc_pass } <EOL> resp = self . _make_agent_call ( '<STR_LIT:password>' , instance , '<STR_LIT>' , password_args ) <EOL> if resp [ '<STR_LIT>' ] != '<STR_LIT:0>' : <EOL> LOG . error ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> return None <EOL> return resp [ '<STR_LIT:message>' ] <EOL> def inject_file ( self , instance , path , contents ) : <EOL> """<STR_LIT>""" <EOL> b64_path = base64 . b64encode ( path ) <EOL> b64_contents = base64 . b64encode ( contents ) <EOL> transaction_id = str ( uuid . uuid4 ( ) ) <EOL> args = { '<STR_LIT:id>' : transaction_id , '<STR_LIT>' : b64_path , <EOL> '<STR_LIT>' : b64_contents } <EOL> resp = self . _make_agent_call ( '<STR_LIT>' , instance , '<STR_LIT>' , args ) <EOL> if resp [ '<STR_LIT>' ] != '<STR_LIT:0>' : <EOL> LOG . error ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> return None <EOL> return resp [ '<STR_LIT:message>' ] <EOL> def _shutdown ( self , instance , vm_ref , hard = True ) : <EOL> """<STR_LIT>""" <EOL> state = self . get_info ( instance [ '<STR_LIT:name>' ] ) [ '<STR_LIT:state>' ] <EOL> if state == power_state . SHUTDOWN : <EOL> instance_name = instance . name <EOL> LOG . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> return <EOL> instance_id = instance . id <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> try : <EOL> task = None <EOL> if hard : <EOL> task = self . _session . call_xenapi ( "<STR_LIT>" , <EOL> vm_ref ) <EOL> else : <EOL> task = self . _session . call_xenapi ( "<STR_LIT>" , <EOL> vm_ref ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> def _find_rescue_vbd_ref ( self , vm_ref , rescue_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vbd_ref = self . _session . get_xenapi ( ) . VM . get_VBDs ( vm_ref ) [ <NUM_LIT:1> ] <EOL> vdi_ref = self . _session . get_xenapi ( ) . VBD . get_record ( vbd_ref ) [ "<STR_LIT>" ] <EOL> return VMHelper . create_vbd ( self . _session , rescue_vm_ref , vdi_ref , <NUM_LIT:1> , <EOL> False ) <EOL> def _shutdown_rescue ( self , rescue_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( "<STR_LIT>" , rescue_vm_ref ) <EOL> def _destroy_vdis ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> vdi_refs = VMHelper . lookup_vm_vdis ( self . _session , vm_ref ) <EOL> if not vdi_refs : <EOL> return <EOL> for vdi_ref in vdi_refs : <EOL> try : <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vdi_ref ) <EOL> self . _session . wait_for_task ( task , instance . id ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> def _destroy_rescue_vdis ( self , rescue_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vdi_refs = VMHelper . lookup_vm_vdis ( self . _session , rescue_vm_ref ) <EOL> for vdi_ref in vdi_refs : <EOL> try : <EOL> self . _session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> except self . XenAPI . Failure : <EOL> continue <EOL> def _destroy_rescue_vbds ( self , rescue_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vbd_refs = self . _session . get_xenapi ( ) . VM . get_VBDs ( rescue_vm_ref ) <EOL> for vbd_ref in vbd_refs : <EOL> vbd_rec = self . _session . get_xenapi ( ) . VBD . get_record ( vbd_ref ) <EOL> if vbd_rec . get ( "<STR_LIT>" , None ) == "<STR_LIT:1>" : <EOL> VMHelper . unplug_vbd ( self . _session , vbd_ref ) <EOL> VMHelper . destroy_vbd ( self . _session , vbd_ref ) <EOL> def _destroy_kernel_ramdisk_plugin_call ( self , kernel , ramdisk ) : <EOL> args = { } <EOL> if kernel : <EOL> args [ '<STR_LIT>' ] = kernel <EOL> if ramdisk : <EOL> args [ '<STR_LIT>' ] = ramdisk <EOL> task = self . _session . async_call_plugin ( <EOL> '<STR_LIT>' , '<STR_LIT>' , args ) <EOL> self . _session . wait_for_task ( task ) <EOL> def _destroy_kernel_ramdisk ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> if not instance . kernel_id and not instance . ramdisk_id : <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> return <EOL> if not ( instance . kernel_id and instance . ramdisk_id ) : <EOL> raise exception . InstanceUnacceptable ( instance_id = instance_id , <EOL> reason = _ ( "<STR_LIT>" ) ) <EOL> ( kernel , ramdisk ) = VMHelper . lookup_kernel_ramdisk ( self . _session , <EOL> vm_ref ) <EOL> self . _destroy_kernel_ramdisk_plugin_call ( kernel , ramdisk ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> def _destroy_vm ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> try : <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> self . _session . wait_for_task ( task , instance_id ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % locals ( ) ) <EOL> def _destroy_rescue_instance ( self , rescue_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> self . _destroy_rescue_vbds ( rescue_vm_ref ) <EOL> self . _shutdown_rescue ( rescue_vm_ref ) <EOL> self . _destroy_rescue_vdis ( rescue_vm_ref ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , rescue_vm_ref ) <EOL> def destroy ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> instance_id = instance . id <EOL> LOG . info ( _ ( "<STR_LIT>" ) % locals ( ) ) <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> return self . _destroy ( instance , vm_ref , network_info , shutdown = True ) <EOL> def _destroy ( self , instance , vm_ref , network_info = None , shutdown = True , <EOL> destroy_kernel_ramdisk = True ) : <EOL> """<STR_LIT>""" <EOL> if vm_ref is None : <EOL> LOG . warning ( _ ( "<STR_LIT>" ) ) <EOL> return <EOL> if shutdown : <EOL> self . _shutdown ( instance , vm_ref ) <EOL> self . _destroy_vdis ( instance , vm_ref ) <EOL> if destroy_kernel_ramdisk : <EOL> self . _destroy_kernel_ramdisk ( instance , vm_ref ) <EOL> self . _destroy_vm ( instance , vm_ref ) <EOL> if network_info : <EOL> for ( network , mapping ) in network_info : <EOL> self . vif_driver . unplug ( instance , network , mapping ) <EOL> def _wait_with_callback ( self , instance_id , task , callback ) : <EOL> ret = None <EOL> try : <EOL> ret = self . _session . wait_for_task ( task , instance_id ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . exception ( exc ) <EOL> callback ( ret ) <EOL> def pause ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> self . _wait_with_callback ( instance . id , task , callback ) <EOL> def unpause ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> self . _wait_with_callback ( instance . id , task , callback ) <EOL> def suspend ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> self . _wait_with_callback ( instance . id , task , callback ) <EOL> def resume ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> task = self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , False , <EOL> True ) <EOL> self . _wait_with_callback ( instance . id , task , callback ) <EOL> def rescue ( self , context , instance , _callback , network_info ) : <EOL> """<STR_LIT>""" <EOL> rescue_vm_ref = VMHelper . lookup ( self . _session , <EOL> "<STR_LIT>" % instance . name ) <EOL> if rescue_vm_ref : <EOL> raise RuntimeError ( _ ( <EOL> "<STR_LIT>" % instance . name ) ) <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> self . _shutdown ( instance , vm_ref ) <EOL> self . _acquire_bootlock ( vm_ref ) <EOL> instance . _rescue = True <EOL> self . spawn_rescue ( context , instance , network_info ) <EOL> rescue_vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> rescue_vbd_ref = self . _find_rescue_vbd_ref ( vm_ref , rescue_vm_ref ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , rescue_vbd_ref ) <EOL> def unrescue ( self , instance , _callback ) : <EOL> """<STR_LIT>""" <EOL> rescue_vm_ref = VMHelper . lookup ( self . _session , <EOL> "<STR_LIT>" % instance . name ) <EOL> if not rescue_vm_ref : <EOL> raise exception . InstanceNotInRescueMode ( instance_id = instance . id ) <EOL> original_vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> instance . _rescue = False <EOL> self . _destroy_rescue_instance ( rescue_vm_ref ) <EOL> self . _release_bootlock ( original_vm_ref ) <EOL> self . _start ( instance , original_vm_ref ) <EOL> def poll_rescued_instances ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> last_ran = self . poll_rescue_last_ran <EOL> if not last_ran : <EOL> self . poll_rescue_last_ran = utils . utcnow ( ) <EOL> return <EOL> if not utils . is_older_than ( last_ran , timeout ) : <EOL> return <EOL> self . poll_rescue_last_ran = utils . utcnow ( ) <EOL> rescue_vms = [ ] <EOL> for instance in self . list_instances ( ) : <EOL> if instance . endswith ( "<STR_LIT>" ) : <EOL> rescue_vms . append ( dict ( name = instance , <EOL> vm_ref = VMHelper . lookup ( self . _session , <EOL> instance ) ) ) <EOL> for vm in rescue_vms : <EOL> rescue_vm_ref = vm [ "<STR_LIT>" ] <EOL> self . _destroy_rescue_instance ( rescue_vm_ref ) <EOL> original_name = vm [ "<STR_LIT:name>" ] . split ( "<STR_LIT>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> original_vm_ref = VMHelper . lookup ( self . _session , original_name ) <EOL> self . _release_bootlock ( original_vm_ref ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , original_vm_ref , False , <EOL> False ) <EOL> def get_info ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> return VMHelper . compile_info ( vm_rec ) <EOL> def get_diagnostics ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> return VMHelper . compile_diagnostics ( self . _session , vm_rec ) <EOL> def get_console_output ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def get_ajax_console ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def host_power_action ( self , host , action ) : <EOL> """<STR_LIT>""" <EOL> args = { "<STR_LIT:action>" : json . dumps ( action ) } <EOL> methods = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> json_resp = self . _call_xenhost ( methods [ action ] , args ) <EOL> resp = json . loads ( json_resp ) <EOL> return resp [ "<STR_LIT>" ] <EOL> def set_host_enabled ( self , host , enabled ) : <EOL> """<STR_LIT>""" <EOL> args = { "<STR_LIT>" : json . dumps ( enabled ) } <EOL> xenapi_resp = self . _call_xenhost ( "<STR_LIT>" , args ) <EOL> try : <EOL> resp = json . loads ( xenapi_resp ) <EOL> except TypeError as e : <EOL> return xenapi_resp . details [ - <NUM_LIT:1> ] <EOL> return resp [ "<STR_LIT:status>" ] <EOL> def _call_xenhost ( self , method , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> task_id = random . randint ( - <NUM_LIT> , - <NUM_LIT> ) <EOL> try : <EOL> task = self . _session . async_call_plugin ( "<STR_LIT>" , method , <EOL> args = arg_dict ) <EOL> ret = self . _session . wait_for_task ( task , task_id ) <EOL> except self . XenAPI . Failure as e : <EOL> ret = e <EOL> LOG . error ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> return ret <EOL> def inject_network_info ( self , instance , network_info , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if vm_ref : <EOL> self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> else : <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> logging . debug ( _ ( "<STR_LIT>" ) , vm_ref ) <EOL> for ( network , info ) in network_info : <EOL> location = '<STR_LIT>' % info [ '<STR_LIT>' ] . replace ( '<STR_LIT::>' , '<STR_LIT>' ) <EOL> self . write_to_param_xenstore ( vm_ref , { location : info } ) <EOL> try : <EOL> self . _make_plugin_call ( '<STR_LIT>' , '<STR_LIT>' , instance , <EOL> location , { '<STR_LIT:value>' : json . dumps ( info ) } , <EOL> vm_ref ) <EOL> except KeyError : <EOL> pass <EOL> def create_vifs ( self , vm_ref , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( _ ( "<STR_LIT>" ) , vm_ref ) <EOL> self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> for device , ( network , info ) in enumerate ( network_info ) : <EOL> vif_rec = self . vif_driver . plug ( self . _session , <EOL> vm_ref , instance , device , network , info ) <EOL> network_ref = vif_rec [ '<STR_LIT>' ] <EOL> LOG . debug ( _ ( '<STR_LIT>' '<STR_LIT>' ) % locals ( ) ) <EOL> vif_ref = self . _session . call_xenapi ( '<STR_LIT>' , vif_rec ) <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> def plug_vifs ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> for ( network , mapping ) in network_info : <EOL> self . vif_driver . plug ( self . _session , instance , network , mapping ) <EOL> def reset_network ( self , instance , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if not vm_ref : <EOL> vm_ref = VMHelper . lookup ( self . _session , instance . name ) <EOL> args = { '<STR_LIT:id>' : str ( uuid . uuid4 ( ) ) } <EOL> resp = self . _make_plugin_call ( '<STR_LIT>' , '<STR_LIT>' , instance , '<STR_LIT>' , <EOL> args , vm_ref ) <EOL> def inject_hostname ( self , instance , vm_ref , hostname ) : <EOL> """<STR_LIT>""" <EOL> if instance . os_type == "<STR_LIT>" : <EOL> hostname = hostname [ : <NUM_LIT:15> ] <EOL> logging . debug ( _ ( "<STR_LIT>" ) , vm_ref ) <EOL> self . _session . call_xenapi_request ( "<STR_LIT>" , <EOL> ( vm_ref , "<STR_LIT>" , hostname ) ) <EOL> def list_from_xenstore ( self , vm , path ) : <EOL> """<STR_LIT>""" <EOL> ret = self . _make_xenstore_call ( '<STR_LIT>' , vm , path ) <EOL> return json . loads ( ret ) <EOL> def read_from_xenstore ( self , vm , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ret = self . _make_xenstore_call ( '<STR_LIT>' , vm , path , <EOL> { '<STR_LIT>' : '<STR_LIT:True>' } ) <EOL> except self . XenAPI . Failure : <EOL> return None <EOL> ret = json . loads ( ret ) <EOL> if ret == "<STR_LIT:None>" : <EOL> return None <EOL> return ret <EOL> def write_to_xenstore ( self , vm , path , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _make_xenstore_call ( '<STR_LIT>' , vm , path , <EOL> { '<STR_LIT:value>' : json . dumps ( value ) } ) <EOL> def clear_xenstore ( self , vm , path ) : <EOL> """<STR_LIT>""" <EOL> self . _make_xenstore_call ( '<STR_LIT>' , vm , path ) <EOL> def _make_xenstore_call ( self , method , vm , path , addl_args = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _make_plugin_call ( '<STR_LIT>' , method = method , vm = vm , <EOL> path = path , addl_args = addl_args ) <EOL> def _make_agent_call ( self , method , vm , path , addl_args = None ) : <EOL> """<STR_LIT>""" <EOL> ret = self . _make_plugin_call ( '<STR_LIT>' , method = method , vm = vm , <EOL> path = path , addl_args = addl_args ) <EOL> if isinstance ( ret , dict ) : <EOL> return ret <EOL> try : <EOL> return json . loads ( ret ) <EOL> except TypeError : <EOL> instance_id = vm . id <EOL> LOG . error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return { '<STR_LIT>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' } <EOL> def _make_plugin_call ( self , plugin , method , vm , path , addl_args = None , <EOL> vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> instance_id = vm . id <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( vm ) <EOL> vm_rec = self . _session . get_xenapi ( ) . VM . get_record ( vm_ref ) <EOL> args = { '<STR_LIT>' : vm_rec [ '<STR_LIT>' ] , '<STR_LIT:path>' : path } <EOL> args . update ( addl_args or { } ) <EOL> try : <EOL> task = self . _session . async_call_plugin ( plugin , method , args ) <EOL> ret = self . _session . wait_for_task ( task , instance_id ) <EOL> except self . XenAPI . Failure , e : <EOL> ret = None <EOL> err_msg = e . details [ - <NUM_LIT:1> ] . splitlines ( ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in err_msg : <EOL> LOG . error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:message>' : err_msg } <EOL> elif '<STR_LIT>' in err_msg : <EOL> LOG . error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:message>' : err_msg } <EOL> else : <EOL> LOG . error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> return { '<STR_LIT>' : '<STR_LIT:error>' , '<STR_LIT:message>' : err_msg } <EOL> return ret <EOL> def add_to_xenstore ( self , vm , path , key , value ) : <EOL> """<STR_LIT>""" <EOL> current = self . read_from_xenstore ( vm , path ) <EOL> if not current : <EOL> current = { key : value } <EOL> else : <EOL> current [ key ] = value <EOL> self . write_to_xenstore ( vm , path , current ) <EOL> def remove_from_xenstore ( self , vm , path , key_or_keys ) : <EOL> """<STR_LIT>""" <EOL> current = self . list_from_xenstore ( vm , path ) <EOL> if not current : <EOL> return <EOL> if isinstance ( key_or_keys , basestring ) : <EOL> keys = [ key_or_keys ] <EOL> else : <EOL> keys = key_or_keys <EOL> keys . sort ( lambda x , y : cmp ( y . count ( '<STR_LIT:/>' ) , x . count ( '<STR_LIT:/>' ) ) ) <EOL> for key in keys : <EOL> if path : <EOL> keypath = "<STR_LIT>" % ( path , key ) <EOL> else : <EOL> keypath = key <EOL> self . _make_xenstore_call ( '<STR_LIT>' , vm , keypath ) <EOL> def read_partial_from_param_xenstore ( self , instance_or_vm , key_prefix ) : <EOL> """<STR_LIT>""" <EOL> data = self . read_from_param_xenstore ( instance_or_vm ) <EOL> badkeys = [ k for k in data . keys ( ) <EOL> if not k . startswith ( key_prefix ) ] <EOL> for badkey in badkeys : <EOL> del data [ badkey ] <EOL> return data <EOL> def read_from_param_xenstore ( self , instance_or_vm , keys = None ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance_or_vm ) <EOL> data = self . _session . call_xenapi_request ( '<STR_LIT>' , <EOL> ( vm_ref , ) ) <EOL> ret = { } <EOL> if keys is None : <EOL> keys = data . keys ( ) <EOL> elif isinstance ( keys , basestring ) : <EOL> keys = [ keys ] <EOL> for key in keys : <EOL> raw = data . get ( key ) <EOL> if raw : <EOL> ret [ key ] = json . loads ( raw ) <EOL> else : <EOL> ret [ key ] = raw <EOL> return ret <EOL> def add_to_param_xenstore ( self , instance_or_vm , key , val ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance_or_vm ) <EOL> self . remove_from_param_xenstore ( instance_or_vm , key ) <EOL> jsonval = json . dumps ( val ) <EOL> self . _session . call_xenapi_request ( '<STR_LIT>' , <EOL> ( vm_ref , key , jsonval ) ) <EOL> def write_to_param_xenstore ( self , instance_or_vm , mapping ) : <EOL> """<STR_LIT>""" <EOL> for k , v in mapping . iteritems ( ) : <EOL> self . add_to_param_xenstore ( instance_or_vm , k , v ) <EOL> def remove_from_param_xenstore ( self , instance_or_vm , key_or_keys ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance_or_vm ) <EOL> if isinstance ( key_or_keys , basestring ) : <EOL> keys = [ key_or_keys ] <EOL> else : <EOL> keys = key_or_keys <EOL> for key in keys : <EOL> self . _session . call_xenapi_request ( '<STR_LIT>' , <EOL> ( vm_ref , key ) ) <EOL> def clear_param_xenstore ( self , instance_or_vm ) : <EOL> """<STR_LIT>""" <EOL> self . write_to_param_xenstore ( instance_or_vm , { } ) <EOL> class SimpleDH ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prime = None , base = None , secret = None ) : <EOL> """<STR_LIT>""" <EOL> if prime is None : <EOL> self . _prime = <NUM_LIT> <EOL> else : <EOL> self . _prime = prime <EOL> if base is None : <EOL> self . _base = <NUM_LIT:5> <EOL> else : <EOL> self . _base = base <EOL> self . _shared = self . _public = None <EOL> self . _dh = M2Crypto . DH . set_params ( <EOL> self . dec_to_mpi ( self . _prime ) , <EOL> self . dec_to_mpi ( self . _base ) ) <EOL> self . _dh . gen_key ( ) <EOL> self . _public = self . mpi_to_dec ( self . _dh . pub ) <EOL> def get_public ( self ) : <EOL> return self . _public <EOL> def compute_shared ( self , other ) : <EOL> self . _shared = self . bin_to_dec ( <EOL> self . _dh . compute_key ( self . dec_to_mpi ( other ) ) ) <EOL> return self . _shared <EOL> def mpi_to_dec ( self , mpi ) : <EOL> bn = M2Crypto . m2 . mpi_to_bn ( mpi ) <EOL> hexval = M2Crypto . m2 . bn_to_hex ( bn ) <EOL> dec = int ( hexval , <NUM_LIT:16> ) <EOL> return dec <EOL> def bin_to_dec ( self , binval ) : <EOL> bn = M2Crypto . m2 . bin_to_bn ( binval ) <EOL> hexval = M2Crypto . m2 . bn_to_hex ( bn ) <EOL> dec = int ( hexval , <NUM_LIT:16> ) <EOL> return dec <EOL> def dec_to_mpi ( self , dec ) : <EOL> bn = M2Crypto . m2 . dec_to_bn ( '<STR_LIT:%s>' % dec ) <EOL> mpi = M2Crypto . m2 . bn_to_mpi ( bn ) <EOL> return mpi <EOL> def _run_ssl ( self , text , decrypt = False ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % self . _shared , '<STR_LIT>' ] <EOL> if decrypt : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> out , err = utils . execute ( * cmd , process_input = text ) <EOL> if err : <EOL> raise RuntimeError ( _ ( '<STR_LIT>' ) % err ) <EOL> return out <EOL> def encrypt ( self , text ) : <EOL> return self . _run_ssl ( text ) . strip ( '<STR_LIT:\n>' ) <EOL> def decrypt ( self , text ) : <EOL> return self . _run_ssl ( text , decrypt = True ) </s>
