ALPHA: wire is a tool to deploy nixos systems wire.althaea.zone/

refactor activate step a bit

+83 -81
+83 -81
wire/lib/src/hive/steps/activate.rs
··· 8 use crate::{ 9 HiveLibError, 10 commands::{CommandArguments, WireCommandChip, run_command}, 11 - errors::{ActivationError, NetworkError}, 12 hive::node::{Context, ExecuteStep, Goal, SwitchToConfigurationGoal}, 13 }; 14 ··· 69 info!("Set system profile"); 70 71 Ok(()) 72 } 73 74 impl ExecuteStep for SwitchToConfiguration { ··· 120 let result = child.wait_till_success().await; 121 122 match result { 123 - Ok(_) => { 124 - if !ctx.reboot { 125 - return Ok(()); 126 - } 127 - 128 - if ctx.should_apply_locally { 129 - error!("Refusing to reboot local machine!"); 130 - 131 - return Ok(()); 132 - } 133 - 134 - warn!("Rebooting {name}!", name = ctx.name); 135 - 136 - let reboot = run_command( 137 - &CommandArguments::new("reboot now", ctx.modifiers) 138 - .log_stdout() 139 - .on_target(Some(&ctx.node.target)) 140 - .elevated(ctx.node), 141 - ) 142 - .await?; 143 - 144 - // consume result, impossible to know if the machine failed to reboot or we 145 - // simply disconnected 146 - let _ = reboot 147 - .wait_till_success() 148 - .await 149 - .map_err(HiveLibError::CommandError)?; 150 - 151 - info!("Rebooted {name}, waiting to reconnect...", name = ctx.name); 152 - 153 - if wait_for_ping(ctx).await.is_ok() { 154 - return Ok(()); 155 - } 156 - 157 - error!( 158 - "Failed to get regain connection to {name} via {host} after reboot.", 159 - name = ctx.name, 160 - host = ctx.node.target.get_preferred_host()? 161 - ); 162 - 163 - return Err(HiveLibError::NetworkError( 164 - NetworkError::HostUnreachableAfterReboot( 165 - ctx.node.target.get_preferred_host()?.to_string(), 166 - ), 167 - )); 168 - } 169 - Err(error) => { 170 - warn!( 171 - "Activation command for {name} exited unsuccessfully.", 172 - name = ctx.name 173 - ); 174 - 175 - // Bail if the command couldn't of broken the system 176 - // and don't try to regain connection to localhost 177 - if matches!(goal, SwitchToConfigurationGoal::DryActivate) 178 - || ctx.should_apply_locally 179 - { 180 - return Err(HiveLibError::ActivationError( 181 - ActivationError::SwitchToConfigurationError(*goal, ctx.name.clone(), error), 182 - )); 183 - } 184 - 185 - if wait_for_ping(ctx).await.is_ok() { 186 - return Err(HiveLibError::ActivationError( 187 - ActivationError::SwitchToConfigurationError(*goal, ctx.name.clone(), error), 188 - )); 189 - } 190 - 191 - error!( 192 - "Failed to get regain connection to {name} via {host} after {goal} activation.", 193 - name = ctx.name, 194 - host = ctx.node.target.get_preferred_host()? 195 - ); 196 - 197 - return Err(HiveLibError::NetworkError( 198 - NetworkError::HostUnreachableAfterReboot( 199 - ctx.node.target.get_preferred_host()?.to_string(), 200 - ), 201 - )); 202 - } 203 } 204 } 205 }
··· 8 use crate::{ 9 HiveLibError, 10 commands::{CommandArguments, WireCommandChip, run_command}, 11 + errors::{ActivationError, CommandError, NetworkError}, 12 hive::node::{Context, ExecuteStep, Goal, SwitchToConfigurationGoal}, 13 }; 14 ··· 69 info!("Set system profile"); 70 71 Ok(()) 72 + } 73 + 74 + async fn reboot(ctx: &Context<'_>) -> Result<(), HiveLibError> { 75 + if !ctx.reboot { 76 + return Ok(()); 77 + } 78 + 79 + if ctx.should_apply_locally { 80 + error!("Refusing to reboot local machine!"); 81 + 82 + return Ok(()); 83 + } 84 + 85 + warn!("Rebooting {name}!", name = ctx.name); 86 + 87 + let reboot = run_command( 88 + &CommandArguments::new("reboot now", ctx.modifiers) 89 + .log_stdout() 90 + .on_target(Some(&ctx.node.target)) 91 + .elevated(ctx.node), 92 + ) 93 + .await?; 94 + 95 + // consume result, impossible to know if the machine failed to reboot or we 96 + // simply disconnected 97 + let _ = reboot 98 + .wait_till_success() 99 + .await 100 + .map_err(HiveLibError::CommandError)?; 101 + 102 + info!("Rebooted {name}, waiting to reconnect...", name = ctx.name); 103 + 104 + if wait_for_ping(ctx).await.is_ok() { 105 + return Ok(()); 106 + } 107 + 108 + error!( 109 + "Failed to get regain connection to {name} via {host} after reboot.", 110 + name = ctx.name, 111 + host = ctx.node.target.get_preferred_host()? 112 + ); 113 + 114 + return Err(HiveLibError::NetworkError( 115 + NetworkError::HostUnreachableAfterReboot(ctx.node.target.get_preferred_host()?.to_string()), 116 + )); 117 + } 118 + 119 + async fn reconnect( 120 + ctx: &Context<'_>, 121 + goal: &SwitchToConfigurationGoal, 122 + error: CommandError, 123 + ) -> Result<(), HiveLibError> { 124 + warn!( 125 + "Activation command for {name} exited unsuccessfully.", 126 + name = ctx.name 127 + ); 128 + 129 + // Bail if the command couldn't of broken the system 130 + // and don't try to regain connection to localhost 131 + if matches!(goal, SwitchToConfigurationGoal::DryActivate) || ctx.should_apply_locally { 132 + return Err(HiveLibError::ActivationError( 133 + ActivationError::SwitchToConfigurationError(*goal, ctx.name.clone(), error), 134 + )); 135 + } 136 + 137 + if wait_for_ping(ctx).await.is_ok() { 138 + return Err(HiveLibError::ActivationError( 139 + ActivationError::SwitchToConfigurationError(*goal, ctx.name.clone(), error), 140 + )); 141 + } 142 + 143 + error!( 144 + "Failed to get regain connection to {name} via {host} after {goal} activation.", 145 + name = ctx.name, 146 + host = ctx.node.target.get_preferred_host()? 147 + ); 148 + 149 + return Err(HiveLibError::NetworkError( 150 + NetworkError::HostUnreachableAfterReboot(ctx.node.target.get_preferred_host()?.to_string()), 151 + )); 152 } 153 154 impl ExecuteStep for SwitchToConfiguration { ··· 200 let result = child.wait_till_success().await; 201 202 match result { 203 + Ok(_) => reboot(ctx).await, 204 + Err(error) => reconnect(ctx, goal, error).await, 205 } 206 } 207 }