@@ -5,7 +5,6 @@ pub mod windows10;
55use core:: str;
66use std:: {
77 collections:: { BTreeMap , BTreeSet } ,
8- ffi:: OsStr ,
98 fs:: { create_dir_all, read_dir, remove_file, set_permissions, File } ,
109 io:: { Seek , Write } ,
1110 mem:: take,
@@ -18,9 +17,9 @@ use std::{
1817
1918use bytesize:: ByteSize ;
2019use chrono:: { SecondsFormat , Utc } ;
21- use cmd_lib:: { run_cmd , run_fun , spawn_with_output} ;
22- use hypervisor:: { list_rebuild_guests, list_template_guests} ;
23- use jane_eyre:: eyre:: { self , bail , OptionExt } ;
20+ use cmd_lib:: spawn_with_output;
21+ use hypervisor:: { delete_guest , list_rebuild_guests, list_template_guests} ;
22+ use jane_eyre:: eyre:: { self , OptionExt } ;
2423use settings:: {
2524 profile:: { parse_rebuild_guest_name, parse_template_guest_name, Profile } ,
2625 TOML ,
@@ -119,7 +118,7 @@ impl Rebuilds {
119118 . collect :: < BTreeSet < _ > > ( ) ;
120119 for rebuild_guest_name in list_rebuild_guests ( ) ? {
121120 if !current_known_rebuild_guest_names. contains ( & rebuild_guest_name) {
122- undefine_libvirt_guest ( & rebuild_guest_name) ?;
121+ delete_guest ( & rebuild_guest_name) ?;
123122 let ( profile_key, snapshot_name) =
124123 match parse_rebuild_guest_name ( & rebuild_guest_name) {
125124 Ok ( result) => result,
@@ -344,7 +343,7 @@ pub(self) fn prune_templates(profile: &Profile) -> eyre::Result<()> {
344343 snapshot_names. push ( snapshot_name. to_owned ( ) ) ;
345344 }
346345 } else {
347- undefine_libvirt_guest ( & template_guest_name) ?;
346+ delete_guest ( & template_guest_name) ?;
348347 }
349348 }
350349 snapshot_names. sort ( ) ;
@@ -423,78 +422,3 @@ pub(self) fn create_disk_image<'icp>(
423422
424423 Ok ( base_image_path)
425424}
426-
427- pub ( self ) fn define_libvirt_guest (
428- profile_name : & str ,
429- guest_name : & str ,
430- guest_xml_path : impl AsRef < Path > ,
431- args : & [ & dyn AsRef < OsStr > ] ,
432- cdrom_images : & [ CdromImage ] ,
433- ) -> eyre:: Result < ( ) > {
434- // This dance is needed to randomise the MAC address of the guest.
435- let guest_xml_path = guest_xml_path. as_ref ( ) ;
436- let args = args. iter ( ) . map ( |x| x. as_ref ( ) ) . collect :: < Vec < _ > > ( ) ;
437- run_cmd ! ( virsh define -- $guest_xml_path) ?;
438- run_cmd ! ( virt-clone --preserve-data --check path_in_use=off -o $profile_name. init -n $guest_name $[ args] ) ?;
439- libvirt_change_media ( guest_name, cdrom_images) ?;
440- run_cmd ! ( virsh undefine -- $profile_name. init) ?;
441-
442- Ok ( ( ) )
443- }
444-
445- pub ( self ) fn libvirt_change_media (
446- guest_name : & str ,
447- cdrom_images : & [ CdromImage ] ,
448- ) -> eyre:: Result < ( ) > {
449- for CdromImage { target_dev, path } in cdrom_images {
450- run_cmd ! ( virsh change-media -- $guest_name $target_dev $path) ?;
451- }
452-
453- Ok ( ( ) )
454- }
455-
456- pub ( self ) fn undefine_libvirt_guest ( guest_name : & str ) -> eyre:: Result < ( ) > {
457- if run_cmd ! ( virsh domstate -- $guest_name) . is_ok ( ) {
458- // FIXME make this idempotent in a less noisy way?
459- let _ = run_cmd ! ( virsh destroy -- $guest_name) ;
460- run_cmd ! ( virsh undefine --nvram -- $guest_name) ?;
461- }
462-
463- Ok ( ( ) )
464- }
465-
466- pub struct CdromImage < ' path > {
467- pub target_dev : & ' static str ,
468- pub path : & ' path str ,
469- }
470- impl < ' path > CdromImage < ' path > {
471- pub fn new ( target_dev : & ' static str , path : & ' path str ) -> Self {
472- Self { target_dev, path }
473- }
474- }
475- pub fn start_libvirt_guest ( guest_name : & str ) -> eyre:: Result < ( ) > {
476- info ! ( ?guest_name, "Starting guest" ) ;
477- run_cmd ! ( virsh start -- $guest_name) ?;
478-
479- Ok ( ( ) )
480- }
481-
482- pub ( self ) fn wait_for_guest ( guest_name : & str , timeout : Duration ) -> eyre:: Result < ( ) > {
483- let timeout = timeout. as_secs ( ) ;
484- info ! ( "Waiting for guest to shut down (max {timeout} seconds)" ) ;
485- if !run_cmd ! ( time virsh event --timeout $timeout -- $guest_name lifecycle) . is_ok ( ) {
486- bail ! ( "`virsh event` failed or timed out!" ) ;
487- }
488- for _ in 0 ..100 {
489- if run_fun ! ( virsh domstate -- $guest_name) ?. trim_ascii ( ) == "shut off" {
490- return Ok ( ( ) ) ;
491- }
492- }
493-
494- bail ! ( "Guest did not shut down as expected" )
495- }
496-
497- pub ( self ) fn rename_guest ( old_guest_name : & str , new_guest_name : & str ) -> eyre:: Result < ( ) > {
498- run_cmd ! ( virsh domrename -- $old_guest_name $new_guest_name) ?;
499- Ok ( ( ) )
500- }
0 commit comments