1010from packaging import version
1111
1212import lib .commands as commands
13- import lib .pif as pif
1413
1514from typing import TYPE_CHECKING , Dict , List , Literal , Mapping , Optional , TypedDict , Union , overload
1615
3332 wait_for_not ,
3433)
3534from lib .netutil import wrap_ip
35+ from lib .pif import PIF
3636from lib .sr import SR
3737from lib .vdi import VDI
3838from lib .vm import VM
@@ -313,7 +313,7 @@ def xo_server_reconnect(self):
313313 def vm_cache_key (uri ):
314314 return f"[Cache for { strip_suffix (uri , '.xva' )} ]"
315315
316- def cached_vm (self , uri , sr_uuid ):
316+ def cached_vm (self , uri , sr_uuid ) -> Optional [ VM ] :
317317 assert sr_uuid , "A SR UUID is necessary to use import cache"
318318 cache_key = self .vm_cache_key (uri )
319319 # Look for an existing cache VM
@@ -328,8 +328,9 @@ def cached_vm(self, uri, sr_uuid):
328328 logging .info (f"Reusing cached VM { vm .uuid } for { uri } " )
329329 return vm
330330 logging .info ("Could not find a VM in cache for %r" , uri )
331+ return None
331332
332- def import_vm (self , uri , sr_uuid = None , use_cache = False ):
333+ def import_vm (self , uri , sr_uuid = None , use_cache = False ) -> VM :
333334 vm = None
334335 if use_cache :
335336 if '://' in uri and uri .startswith ("clone" ):
@@ -373,7 +374,7 @@ def import_vm(self, uri, sr_uuid=None, use_cache=False):
373374 vm .param_set ('name-description' , cache_key )
374375 return vm
375376
376- def import_iso (self , uri , sr : SR ):
377+ def import_iso (self , uri , sr : SR ) -> VDI :
377378 random_name = str (uuid .uuid4 ())
378379
379380 vdi_uuid = self .xe (
@@ -404,7 +405,7 @@ def import_iso(self, uri, sr: SR):
404405
405406 return VDI (vdi_uuid , sr = sr )
406407
407- def vm_from_template (self , name , template ):
408+ def vm_from_template (self , name , template ) -> VM :
408409 params = {
409410 "new-name-label" : prefix_object_name (name ),
410411 "template" : template ,
@@ -413,7 +414,7 @@ def vm_from_template(self, name, template):
413414 vm_uuid = self .xe ('vm-install' , params )
414415 return VM (vm_uuid , self )
415416
416- def pool_has_vm (self , vm_uuid , vm_type = 'vm' ):
417+ def pool_has_vm (self , vm_uuid , vm_type = 'vm' ) -> bool :
417418 if vm_type == 'snapshot' :
418419 return self .xe ('snapshot-list' , {'uuid' : vm_uuid }, minimal = True ) == vm_uuid
419420 else :
@@ -436,7 +437,7 @@ def is_enabled(self) -> bool:
436437 # If XAPI is not ready yet, or the host is down, this will throw. We return False in that case.
437438 return False
438439
439- def has_updates (self ):
440+ def has_updates (self ) -> bool :
440441 try :
441442 # yum check-update returns 100 if there are updates, 1 if there's an error, 0 if no updates
442443 self .ssh (['yum' , 'check-update' ])
@@ -504,14 +505,14 @@ def packages(self):
504505 self .ssh (['rpm' , '-qa' , '--qf' , '%{NAME}-%{VERSION}-%{RELEASE}-%{ARCH}-%{EPOCH}\\ \\ n' ]).splitlines ()
505506 )
506507
507- def check_packages_available (self , packages ):
508+ def check_packages_available (self , packages ) -> bool :
508509 """ Check if a given package list is available in the YUM repositories. """
509510 return len (self .ssh (['repoquery' ] + packages ).splitlines ()) == len (packages )
510511
511512 def get_available_package_versions (self , package ):
512513 return self .ssh (['repoquery' , '--show-duplicates' , package ]).splitlines ()
513514
514- def is_package_installed (self , package ):
515+ def is_package_installed (self , package ) -> bool :
515516 return self .ssh_with_result (['rpm' , '-q' , package ]).returncode == 0
516517
517518 def yum_save_state (self ):
@@ -563,12 +564,12 @@ def reboot(self, verify=False):
563564 "Wait for ssh up on host" , timeout_secs = 10 * 60 , retry_delay_secs = 5 )
564565 wait_for (self .is_enabled , "Wait for XAPI to be ready" , timeout_secs = 30 * 60 )
565566
566- def management_network (self ):
567+ def management_network (self ) -> str :
567568 return self .xe ('network-list' , {'bridge' : self .inventory ['MANAGEMENT_INTERFACE' ]}, minimal = True )
568569
569- def management_pif (self ):
570+ def management_pif (self ) -> PIF :
570571 uuid = self .xe ('pif-list' , {'management' : True , 'host-uuid' : self .uuid }, minimal = True )
571- return pif . PIF (uuid , self )
572+ return PIF (uuid , self )
572573
573574 def rescan_block_devices_info (self ) -> None :
574575 """
@@ -607,17 +608,17 @@ def disk_is_available(self, disk: DiskDevName) -> bool:
607608 """
608609 return len (self .ssh (['lsblk' , '--noheadings' , '-o' , 'MOUNTPOINT' , '/dev/' + disk ]).strip ()) == 0
609610
610- def file_exists (self , filepath , regular_file = True ):
611+ def file_exists (self , filepath , regular_file = True ) -> bool :
611612 option = '-f' if regular_file else '-e'
612613 return self .ssh_with_result (['test' , option , filepath ]).returncode == 0
613614
614- def binary_exists (self , binary ):
615+ def binary_exists (self , binary ) -> bool :
615616 return self .ssh_with_result (['which' , binary ]).returncode == 0
616617
617- def is_symlink (self , filepath ):
618+ def is_symlink (self , filepath ) -> bool :
618619 return self .ssh_with_result (['test' , '-L' , filepath ]).returncode == 0
619620
620- def sr_create (self , sr_type , label , device_config , shared = False , verify = False ):
621+ def sr_create (self , sr_type , label , device_config , shared = False , verify = False ) -> SR :
621622 params = {
622623 'host-uuid' : self .uuid ,
623624 'type' : sr_type ,
@@ -637,10 +638,10 @@ def sr_create(self, sr_type, label, device_config, shared=False, verify=False):
637638 wait_for (sr .exists , "Wait for SR to exist" )
638639 return sr
639640
640- def is_master (self ):
641+ def is_master (self ) -> bool :
641642 return self .ssh (['cat' , '/etc/xensource/pool.conf' ]) == 'master'
642643
643- def local_vm_srs (self ):
644+ def local_vm_srs (self ) -> list [ SR ] :
644645 srs = []
645646 sr_uuids = safe_split (self .xe ('pbd-list' , {'host-uuid' : self .uuid , 'params' : 'sr-uuid' }, minimal = True ))
646647 for sr_uuid in sr_uuids :
@@ -649,7 +650,7 @@ def local_vm_srs(self):
649650 srs .append (sr )
650651 return srs
651652
652- def main_sr_uuid (self ):
653+ def main_sr_uuid (self ) -> str :
653654 """ Main SR is the default SR, the first local SR, or a specific SR depending on data.py's DEFAULT_SR. """
654655 try :
655656 from data import DEFAULT_SR
@@ -695,7 +696,7 @@ def call_plugin(self, plugin_name: str, function: str,
695696 params ['args:%s' % k ] = v
696697 return self .xe ('host-call-plugin' , params )
697698
698- def join_pool (self , pool ):
699+ def join_pool (self , pool : Pool ):
699700 master = pool .master
700701 self .xe ('pool-join' , {
701702 'master-address' : master .hostname_or_ip ,
0 commit comments