@@ -113,69 +113,97 @@ struct PendingRollback {
113113}
114114
115115// ============================================================================
116- // Public Functions
116+ // Service
117117// ============================================================================
118118
119- pub fn rollback_network_config ( network : & NetworkConfig ) -> Result < ( ) > {
120- let config_file = network_config_file ! ( network. name) ;
121- let backup_file = network_backup_file ! ( network. name) ;
119+ /// Service for network configuration management operations
120+ pub struct NetworkConfigService ;
121+
122+ impl NetworkConfigService {
123+ /// Rollback network configuration to the previous backup
124+ ///
125+ /// # Arguments
126+ /// * `network` - Network configuration to rollback
127+ ///
128+ /// # Returns
129+ /// Result indicating success or failure
130+ pub fn rollback_network_config ( network : & NetworkConfig ) -> Result < ( ) > {
131+ let config_file = network_config_file ! ( network. name) ;
132+ let backup_file = network_backup_file ! ( network. name) ;
133+
134+ if !backup_file. exists ( ) {
135+ return Ok ( ( ) ) ;
136+ }
122137
123- if !backup_file. exists ( ) {
124- return Ok ( ( ) ) ;
125- }
138+ fs:: copy ( & backup_file, & config_file) . context ( format ! (
139+ "failed to restore {} from {}" ,
140+ config_file. display( ) ,
141+ backup_file. display( )
142+ ) ) ?;
126143
127- fs:: copy ( & backup_file, & config_file) . context ( format ! (
128- "failed to restore {} from {}" ,
129- config_file. display( ) ,
130- backup_file. display( )
131- ) ) ?;
144+ let _ = fs:: remove_file ( & backup_file) ;
132145
133- let _ = fs:: remove_file ( & backup_file) ;
146+ Ok ( ( ) )
147+ }
134148
135- Ok ( ( ) )
136- }
149+ /// Apply network configuration to systemd-networkd
150+ ///
151+ /// # Arguments
152+ /// * `service_client` - Device service client for network reload
153+ /// * `network` - Network configuration to apply
154+ ///
155+ /// # Returns
156+ /// Result indicating success or failure
157+ pub async fn apply_network_config < T > ( service_client : & T , network : & NetworkConfig ) -> Result < ( ) >
158+ where
159+ T : DeviceServiceClient ,
160+ {
161+ backup_current_network_config ( service_client, network) . await ?;
162+ write_network_config ( network) ?;
163+ service_client. reload_network ( ) . await ?;
137164
138- pub async fn apply_network_config < T > ( service_client : & T , network : & NetworkConfig ) -> Result < ( ) >
139- where
140- T : DeviceServiceClient ,
141- {
142- backup_current_network_config ( service_client, network) . await ?;
143- write_network_config ( network) ?;
144- service_client. reload_network ( ) . await ?;
165+ if network. is_server_addr && network. ip_changed {
166+ schedule_server_restart_with_certificate ( network) . await ?;
167+ }
145168
146- if network. is_server_addr && network. ip_changed {
147- schedule_server_restart_with_certificate ( network) . await ?;
169+ Ok ( ( ) )
148170 }
149171
150- Ok ( ( ) )
151- }
152-
153- pub async fn process_pending_rollback < T > ( service_client : & T ) -> Result < ( ) >
154- where
155- T : DeviceServiceClient ,
156- {
157- if let Some ( pending) = load_rollback ! ( ) {
158- let now = SystemTime :: now ( ) ;
159-
160- if now >= pending. rollback_time {
161- execute_rollback ( service_client, & pending. network_config , "pending" ) . await ;
162- clear_rollback ! ( ) ;
163- } else if let Ok ( remaining_time) = pending. rollback_time . duration_since ( now) {
164- sleep ( remaining_time) . await ;
172+ /// Process any pending network configuration rollback
173+ ///
174+ /// # Arguments
175+ /// * `service_client` - Device service client for rollback operations
176+ ///
177+ /// # Returns
178+ /// Result indicating success or failure
179+ pub async fn process_pending_rollback < T > ( service_client : & T ) -> Result < ( ) >
180+ where
181+ T : DeviceServiceClient ,
182+ {
183+ if let Some ( pending) = load_rollback ! ( ) {
184+ let now = SystemTime :: now ( ) ;
165185
166- if load_rollback ! ( ) . is_some ( ) {
167- execute_rollback ( service_client, & pending. network_config , "scheduled " ) . await ;
186+ if now >= pending . rollback_time {
187+ execute_rollback ( service_client, & pending. network_config , "pending " ) . await ;
168188 clear_rollback ! ( ) ;
189+ } else if let Ok ( remaining_time) = pending. rollback_time . duration_since ( now) {
190+ sleep ( remaining_time) . await ;
191+
192+ if load_rollback ! ( ) . is_some ( ) {
193+ execute_rollback ( service_client, & pending. network_config , "scheduled" ) . await ;
194+ clear_rollback ! ( ) ;
195+ }
169196 }
170197 }
198+ Ok ( ( ) )
171199 }
172- Ok ( ( ) )
173- }
174200
175- pub fn cancel_rollback ( ) {
176- if load_rollback ! ( ) . is_some ( ) {
177- clear_rollback ! ( ) ;
178- info ! ( "pending network rollback cancelled" ) ;
201+ /// Cancel any pending network configuration rollback
202+ pub fn cancel_rollback ( ) {
203+ if load_rollback ! ( ) . is_some ( ) {
204+ clear_rollback ! ( ) ;
205+ info ! ( "pending network rollback cancelled" ) ;
206+ }
179207 }
180208}
181209
@@ -230,7 +258,7 @@ async fn rollback_network_and_renew_certificate<T>(
230258where
231259 T : DeviceServiceClient ,
232260{
233- rollback_network_config ( network) ?;
261+ NetworkConfigService :: rollback_network_config ( network) ?;
234262 service_client. reload_network ( ) . await ?;
235263 trigger_server_restart ( ) ;
236264
0 commit comments