<s> """<STR_LIT>""" <EOL> import doctest <EOL> import os <EOL> import sys <EOL> from oslo_config import cfg <EOL> import XenAPI <EOL> possible_topdir = os . getcwd ( ) <EOL> if os . path . exists ( os . path . join ( possible_topdir , "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> sys . path . insert ( <NUM_LIT:0> , possible_topdir ) <EOL> from nova import config <EOL> from nova import context <EOL> import nova . conf <EOL> from nova import db <EOL> from nova import exception <EOL> from oslo_utils import timeutils <EOL> from nova . virt import virtapi <EOL> from nova . virt . xenapi import driver as xenapi_driver <EOL> cleaner_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> cli_opt = cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> CONF = nova . conf . CONF <EOL> CONF . register_opts ( cleaner_opts ) <EOL> CONF . register_cli_opt ( cli_opt ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ALLOWED_COMMANDS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:test>" ] <EOL> def call_xenapi ( xenapi , method , * args ) : <EOL> """<STR_LIT>""" <EOL> return xenapi . _session . call_xenapi ( method , * args ) <EOL> def find_orphaned_instances ( xenapi ) : <EOL> """<STR_LIT>""" <EOL> ctxt = context . get_admin_context ( read_deleted = "<STR_LIT>" ) <EOL> orphaned_instances = [ ] <EOL> for vm_ref , vm_rec in _get_applicable_vm_recs ( xenapi ) : <EOL> try : <EOL> uuid = vm_rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> instance = db . instance_get_by_uuid ( ctxt , uuid ) <EOL> except ( KeyError , exception . InstanceNotFound ) : <EOL> print_xen_object ( "<STR_LIT>" , vm_rec , indent_level = <NUM_LIT:0> ) <EOL> continue <EOL> is_active_and_deleting = ( instance . vm_state == "<STR_LIT>" and <EOL> instance . task_state == "<STR_LIT>" ) <EOL> is_zombie_vm = ( instance . vm_state != "<STR_LIT>" <EOL> and timeutils . is_older_than ( instance . updated_at , <EOL> CONF . zombie_instance_updated_at_window ) ) <EOL> if is_active_and_deleting or is_zombie_vm : <EOL> orphaned_instances . append ( ( vm_ref , vm_rec , instance ) ) <EOL> return orphaned_instances <EOL> def cleanup_instance ( xenapi , instance , vm_ref , vm_rec ) : <EOL> """<STR_LIT>""" <EOL> xenapi . _vmops . _destroy ( instance , vm_ref ) <EOL> def _get_applicable_vm_recs ( xenapi ) : <EOL> """<STR_LIT>""" <EOL> for vm_ref in call_xenapi ( xenapi , '<STR_LIT>' ) : <EOL> try : <EOL> vm_rec = call_xenapi ( xenapi , '<STR_LIT>' , vm_ref ) <EOL> except XenAPI . Failure , e : <EOL> if e . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> continue <EOL> if vm_rec [ "<STR_LIT>" ] or vm_rec [ "<STR_LIT>" ] : <EOL> continue <EOL> yield vm_ref , vm_rec <EOL> def print_xen_object ( obj_type , obj , indent_level = <NUM_LIT:0> , spaces_per_indent = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> if not CONF . verbose : <EOL> return <EOL> uuid = obj [ "<STR_LIT>" ] <EOL> try : <EOL> name_label = obj [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> name_label = "<STR_LIT>" <EOL> msg = "<STR_LIT>" % locals ( ) <EOL> indent = "<STR_LIT:U+0020>" * spaces_per_indent * indent_level <EOL> print "<STR_LIT>" . join ( [ indent , msg ] ) <EOL> def _find_vdis_connected_to_vm ( xenapi , connected_vdi_uuids ) : <EOL> """<STR_LIT>""" <EOL> def _is_null_ref ( ref ) : <EOL> return ref == "<STR_LIT>" <EOL> def _add_vdi_and_parents_to_connected ( vdi_rec , indent_level ) : <EOL> indent_level += <NUM_LIT:1> <EOL> vdi_and_parent_uuids = [ ] <EOL> cur_vdi_rec = vdi_rec <EOL> while True : <EOL> cur_vdi_uuid = cur_vdi_rec [ "<STR_LIT>" ] <EOL> print_xen_object ( "<STR_LIT>" , vdi_rec , indent_level = indent_level ) <EOL> connected_vdi_uuids . add ( cur_vdi_uuid ) <EOL> vdi_and_parent_uuids . append ( cur_vdi_uuid ) <EOL> try : <EOL> parent_vdi_uuid = vdi_rec [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> parent_vdi_uuid = None <EOL> if parent_vdi_uuid and parent_vdi_uuid != cur_vdi_uuid : <EOL> indent_level += <NUM_LIT:1> <EOL> cur_vdi_ref = call_xenapi ( xenapi , '<STR_LIT>' , <EOL> parent_vdi_uuid ) <EOL> try : <EOL> cur_vdi_rec = call_xenapi ( xenapi , '<STR_LIT>' , <EOL> cur_vdi_ref ) <EOL> except XenAPI . Failure , e : <EOL> if e . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> break <EOL> else : <EOL> break <EOL> for vm_ref , vm_rec in _get_applicable_vm_recs ( xenapi ) : <EOL> indent_level = <NUM_LIT:0> <EOL> print_xen_object ( "<STR_LIT>" , vm_rec , indent_level = indent_level ) <EOL> vbd_refs = vm_rec [ "<STR_LIT>" ] <EOL> for vbd_ref in vbd_refs : <EOL> try : <EOL> vbd_rec = call_xenapi ( xenapi , '<STR_LIT>' , vbd_ref ) <EOL> except XenAPI . Failure , e : <EOL> if e . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> continue <EOL> indent_level = <NUM_LIT:1> <EOL> print_xen_object ( "<STR_LIT>" , vbd_rec , indent_level = indent_level ) <EOL> vbd_vdi_ref = vbd_rec [ "<STR_LIT>" ] <EOL> if _is_null_ref ( vbd_vdi_ref ) : <EOL> continue <EOL> try : <EOL> vdi_rec = call_xenapi ( xenapi , '<STR_LIT>' , vbd_vdi_ref ) <EOL> except XenAPI . Failure , e : <EOL> if e . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> continue <EOL> _add_vdi_and_parents_to_connected ( vdi_rec , indent_level ) <EOL> def _find_all_vdis_and_system_vdis ( xenapi , all_vdi_uuids , connected_vdi_uuids ) : <EOL> """<STR_LIT>""" <EOL> def _system_owned ( vdi_rec ) : <EOL> vdi_name = vdi_rec [ "<STR_LIT>" ] <EOL> return ( vdi_name . startswith ( "<STR_LIT>" ) or <EOL> vdi_name . endswith ( "<STR_LIT>" ) or <EOL> vdi_rec [ "<STR_LIT:type>" ] == "<STR_LIT>" ) <EOL> for vdi_ref in call_xenapi ( xenapi , '<STR_LIT>' ) : <EOL> try : <EOL> vdi_rec = call_xenapi ( xenapi , '<STR_LIT>' , vdi_ref ) <EOL> except XenAPI . Failure , e : <EOL> if e . details [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> continue <EOL> vdi_uuid = vdi_rec [ "<STR_LIT>" ] <EOL> all_vdi_uuids . add ( vdi_uuid ) <EOL> if _system_owned ( vdi_rec ) : <EOL> print_xen_object ( "<STR_LIT>" , vdi_rec , indent_level = <NUM_LIT:0> ) <EOL> connected_vdi_uuids . add ( vdi_uuid ) <EOL> elif not vdi_rec [ "<STR_LIT>" ] : <EOL> print_xen_object ( "<STR_LIT>" , vdi_rec , indent_level = <NUM_LIT:0> ) <EOL> connected_vdi_uuids . add ( vdi_uuid ) <EOL> def find_orphaned_vdi_uuids ( xenapi ) : <EOL> """<STR_LIT>""" <EOL> connected_vdi_uuids = set ( ) <EOL> _find_vdis_connected_to_vm ( xenapi , connected_vdi_uuids ) <EOL> all_vdi_uuids = set ( ) <EOL> _find_all_vdis_and_system_vdis ( xenapi , all_vdi_uuids , connected_vdi_uuids ) <EOL> orphaned_vdi_uuids = all_vdi_uuids - connected_vdi_uuids <EOL> return orphaned_vdi_uuids <EOL> def list_orphaned_vdis ( vdi_uuids ) : <EOL> """<STR_LIT>""" <EOL> for vdi_uuid in vdi_uuids : <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" % vdi_uuid <EOL> else : <EOL> print vdi_uuid <EOL> def clean_orphaned_vdis ( xenapi , vdi_uuids ) : <EOL> """<STR_LIT>""" <EOL> for vdi_uuid in vdi_uuids : <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" % vdi_uuid <EOL> vdi_ref = call_xenapi ( xenapi , '<STR_LIT>' , vdi_uuid ) <EOL> try : <EOL> call_xenapi ( xenapi , '<STR_LIT>' , vdi_ref ) <EOL> except XenAPI . Failure , exc : <EOL> print >> sys . stderr , "<STR_LIT>" % ( vdi_uuid , exc ) <EOL> def list_orphaned_instances ( orphaned_instances ) : <EOL> """<STR_LIT>""" <EOL> for vm_ref , vm_rec , orphaned_instance in orphaned_instances : <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" % orphaned_instance . name <EOL> else : <EOL> print orphaned_instance . name <EOL> def clean_orphaned_instances ( xenapi , orphaned_instances ) : <EOL> """<STR_LIT>""" <EOL> for vm_ref , vm_rec , instance in orphaned_instances : <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" % instance . name <EOL> cleanup_instance ( xenapi , instance , vm_ref , vm_rec ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> config . parse_args ( sys . argv ) <EOL> args = CONF ( args = sys . argv [ <NUM_LIT:1> : ] , usage = '<STR_LIT>' + <EOL> '<STR_LIT:|>' . join ( ALLOWED_COMMANDS ) + '<STR_LIT:}>' ) <EOL> command = CONF . command <EOL> if not command or command not in ALLOWED_COMMANDS : <EOL> CONF . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if CONF . zombie_instance_updated_at_window < CONF . resize_confirm_window : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> xenapi = xenapi_driver . XenAPIDriver ( virtapi . VirtAPI ( ) ) <EOL> if command == "<STR_LIT>" : <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" <EOL> orphaned_vdi_uuids = find_orphaned_vdi_uuids ( xenapi ) <EOL> if CONF . verbose : <EOL> print "<STR_LIT>" <EOL> list_orphaned_vdis ( orphaned_vdi_uuids ) <EOL> elif command == "<STR_LIT>" : <EOL> orphaned_vdi_uuids = find_orphaned_vdi_uuids ( xenapi ) <EOL> clean_orphaned_vdis ( xenapi , orphaned_vdi_uuids ) <EOL> elif command == "<STR_LIT>" : <EOL> orphaned_instances = find_orphaned_instances ( xenapi ) <EOL> list_orphaned_instances ( orphaned_instances ) <EOL> elif command == "<STR_LIT>" : <EOL> orphaned_instances = find_orphaned_instances ( xenapi ) <EOL> clean_orphaned_instances ( xenapi , orphaned_instances ) <EOL> elif command == "<STR_LIT:test>" : <EOL> doctest . testmod ( ) <EOL> else : <EOL> print "<STR_LIT>" % command <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
