@@ -9,6 +9,7 @@ use clap::{Parser, Subcommand};
99use futures:: stream:: { self , StreamExt , TryStreamExt } ;
1010use indicatif:: { MultiProgress , ProgressBar , ProgressStyle } ;
1111use omicron_package:: { parse, BuildCommand , DeployCommand } ;
12+ use omicron_sled_agent:: cleanup_networking_resources;
1213use omicron_sled_agent:: zone;
1314use omicron_zone_package:: config:: Config ;
1415use omicron_zone_package:: config:: ExternalPackage ;
@@ -17,6 +18,10 @@ use omicron_zone_package::package::Package;
1718use omicron_zone_package:: package:: Progress ;
1819use rayon:: prelude:: * ;
1920use ring:: digest:: { Context as DigestContext , Digest , SHA256 } ;
21+ use slog:: info;
22+ use slog:: o;
23+ use slog:: Drain ;
24+ use slog:: Logger ;
2025use std:: env;
2126use std:: fs:: create_dir_all;
2227use std:: path:: { Path , PathBuf } ;
@@ -298,6 +303,7 @@ fn do_install(
298303 config : & Config ,
299304 artifact_dir : & Path ,
300305 install_dir : & Path ,
306+ log : & Logger ,
301307) -> Result < ( ) > {
302308 create_dir_all ( & install_dir) . map_err ( |err| {
303309 anyhow ! ( "Cannot create installation directory: {}" , err)
@@ -323,10 +329,11 @@ fn do_install(
323329
324330 let src = tarfile. as_path ( ) ;
325331 let dst = install_dir. join ( src. strip_prefix ( artifact_dir) ?) ;
326- println ! (
327- "Installing Service: {} -> {}" ,
328- src. to_string_lossy( ) ,
329- dst. to_string_lossy( )
332+ info ! (
333+ log,
334+ "Installing service" ;
335+ "src" => %src. to_string_lossy( ) ,
336+ "dst" => %dst. to_string_lossy( ) ,
330337 ) ;
331338 std:: fs:: copy ( & src, & dst) ?;
332339 Ok ( ( ) )
@@ -346,10 +353,11 @@ fn do_install(
346353 for service_name in global_zone_service_names {
347354 let tar_path = install_dir. join ( format ! ( "{}.tar" , service_name) ) ;
348355 let service_path = install_dir. join ( service_name) ;
349- println ! (
350- "Unpacking {} to {}" ,
351- tar_path. to_string_lossy( ) ,
352- service_path. to_string_lossy( )
356+ info ! (
357+ log,
358+ "Unpacking service tarball" ;
359+ "tar_path" => %tar_path. to_string_lossy( ) ,
360+ "service_path" => %service_path. to_string_lossy( ) ,
353361 ) ;
354362
355363 let tar_file = std:: fs:: File :: open ( & tar_path) ?;
@@ -366,8 +374,9 @@ fn do_install(
366374 . join ( & package. service_name )
367375 . join ( "pkg" )
368376 . join ( "manifest.xml" ) ;
369- println ! (
370- "Installing bootstrap service from {}" ,
377+ info ! (
378+ log,
379+ "Installing boostrap service from {}" ,
371380 manifest_path. to_string_lossy( )
372381 ) ;
373382 smf:: Config :: import ( ) . run ( & manifest_path) ?;
@@ -425,7 +434,11 @@ fn remove_all_unless_already_removed<P: AsRef<Path>>(path: P) -> Result<()> {
425434 Ok ( ( ) )
426435}
427436
428- fn remove_all_except < P : AsRef < Path > > ( path : P , to_keep : & [ & str ] ) -> Result < ( ) > {
437+ fn remove_all_except < P : AsRef < Path > > (
438+ path : P ,
439+ to_keep : & [ & str ] ,
440+ log : & Logger ,
441+ ) -> Result < ( ) > {
429442 let dir = match path. as_ref ( ) . read_dir ( ) {
430443 Ok ( dir) => dir,
431444 Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => return Ok ( ( ) ) ,
@@ -434,9 +447,9 @@ fn remove_all_except<P: AsRef<Path>>(path: P, to_keep: &[&str]) -> Result<()> {
434447 for entry in dir {
435448 let entry = entry?;
436449 if to_keep. contains ( & & * ( entry. file_name ( ) . to_string_lossy ( ) ) ) {
437- println ! ( " Keeping: '{}'", entry. path( ) . to_string_lossy( ) ) ;
450+ info ! ( log , " Keeping: '{}'", entry. path( ) . to_string_lossy( ) ) ;
438451 } else {
439- println ! ( " Removing: '{}'", entry. path( ) . to_string_lossy( ) ) ;
452+ info ! ( log , " Removing: '{}'", entry. path( ) . to_string_lossy( ) ) ;
440453 if entry. metadata ( ) ?. is_dir ( ) {
441454 remove_all_unless_already_removed ( entry. path ( ) ) ?;
442455 } else {
@@ -447,27 +460,32 @@ fn remove_all_except<P: AsRef<Path>>(path: P, to_keep: &[&str]) -> Result<()> {
447460 Ok ( ( ) )
448461}
449462
450- fn do_uninstall (
463+ async fn do_uninstall (
451464 config : & Config ,
452465 artifact_dir : & Path ,
453466 install_dir : & Path ,
467+ log : & Logger ,
454468) -> Result < ( ) > {
455- println ! ( "Removing all Omicron zones" ) ;
469+ info ! ( log , "Removing all Omicron zones" ) ;
456470 uninstall_all_omicron_zones ( ) ?;
457- println ! ( "Uninstalling all packages" ) ;
471+ info ! ( log , "Uninstalling all packages" ) ;
458472 uninstall_all_packages ( config) ;
459- println ! ( "Removing artifacts in: {}" , artifact_dir. to_string_lossy( ) ) ;
473+ info ! ( log , "Removing artifacts in: {}" , artifact_dir. to_string_lossy( ) ) ;
460474
461475 const ARTIFACTS_TO_KEEP : & [ & str ] =
462476 & [ "clickhouse" , "cockroachdb" , "xde" , "console-assets" , "downloads" ] ;
463- remove_all_except ( artifact_dir, ARTIFACTS_TO_KEEP ) ?;
477+ remove_all_except ( artifact_dir, ARTIFACTS_TO_KEEP , log ) ?;
464478
465- println ! (
479+ info ! (
480+ log,
466481 "Removing installed objects in: {}" ,
467482 install_dir. to_string_lossy( )
468483 ) ;
469484 const INSTALLED_OBJECTS_TO_KEEP : & [ & str ] = & [ "opte" ] ;
470- remove_all_except ( install_dir, INSTALLED_OBJECTS_TO_KEEP ) ?;
485+ remove_all_except ( install_dir, INSTALLED_OBJECTS_TO_KEEP , log) ?;
486+
487+ cleanup_networking_resources ( log) . await ?;
488+
471489 Ok ( ( ) )
472490}
473491
@@ -545,6 +563,11 @@ async fn main() -> Result<()> {
545563 let args = Args :: try_parse ( ) ?;
546564 let config = parse :: < _ , Config > ( & args. manifest ) ?;
547565
566+ let decorator = slog_term:: TermDecorator :: new ( ) . build ( ) ;
567+ let drain = slog_term:: CompactFormat :: new ( decorator) . build ( ) . fuse ( ) ;
568+ let drain = slog_async:: Async :: new ( drain) . build ( ) . fuse ( ) ;
569+ let log = slog:: Logger :: root ( drain, o ! ( ) ) ;
570+
548571 // Use a CWD that is the root of the Omicron repository.
549572 if let Ok ( manifest) = env:: var ( "CARGO_MANIFEST_DIR" ) {
550573 let manifest_dir = PathBuf :: from ( manifest) ;
@@ -561,13 +584,13 @@ async fn main() -> Result<()> {
561584 artifact_dir,
562585 install_dir,
563586 } ) => {
564- do_install ( & config, & artifact_dir, & install_dir) ?;
587+ do_install ( & config, & artifact_dir, & install_dir, & log ) ?;
565588 }
566589 SubCommand :: Deploy ( DeployCommand :: Uninstall {
567590 artifact_dir,
568591 install_dir,
569592 } ) => {
570- do_uninstall ( & config, & artifact_dir, & install_dir) ?;
593+ do_uninstall ( & config, & artifact_dir, & install_dir, & log ) . await ?;
571594 }
572595 }
573596
0 commit comments