@@ -181,7 +181,11 @@ def deploy_environment(self, environment: Environment) -> None:
181
181
log .info (f"deploying environment: { environment .name } " )
182
182
timer = create_timer ()
183
183
environment .platform = self
184
- self ._deploy_environment (environment , log )
184
+ try :
185
+ self ._deploy_environment (environment , log )
186
+ except Exception as identifier :
187
+ environment .status = EnvironmentStatus .Bad
188
+ raise identifier
185
189
environment .status = EnvironmentStatus .Deployed
186
190
187
191
# initialize features
@@ -210,29 +214,38 @@ def deploy_environment(self, environment: Environment) -> None:
210
214
def delete_environment (self , environment : Environment ) -> None :
211
215
log = get_logger (f"del[{ environment .name } ]" , parent = self ._log )
212
216
213
- # mark environment is deleted firstly, if there is any error on
214
- # deleting, it should be ignored.
215
- environment .status = EnvironmentStatus .Deleted
216
- environment .cleanup ()
217
- if self .runbook .keep_environment == constants .ENVIRONMENT_KEEP_ALWAYS :
218
- log .info (
219
- f"skipped to delete environment { environment .name } , "
220
- f"as runbook set to keep environment."
221
- )
217
+ try :
218
+ environment .cleanup ()
219
+ if (self .runbook .keep_environment == constants .ENVIRONMENT_KEEP_ALWAYS ) or (
220
+ self .runbook .keep_environment == constants .ENVIRONMENT_KEEP_FAILED
221
+ and environment .status == EnvironmentStatus .Bad
222
+ ):
223
+ log .info (
224
+ f"skipped to delete environment { environment .name } , "
225
+ "as on runbook, keep_environment value "
226
+ f"is set to { self .runbook .keep_environment } "
227
+ f"and env status is { environment .status } "
228
+ )
222
229
223
- # output addresses for troubleshooting easier.
224
- remote_addresses = [
225
- x .connection_info [constants .ENVIRONMENTS_NODES_REMOTE_ADDRESS ]
226
- for x in environment .nodes .list ()
227
- if isinstance (x , RemoteNode ) and hasattr (x , "_connection_info" )
228
- ]
229
- # if the connection info is not found, there is no ip address to
230
- # output.
231
- if remote_addresses :
232
- log .info (f"node ip addresses: { remote_addresses } " )
233
- else :
234
- log .debug ("deleting" )
235
- self ._delete_environment (environment , log )
230
+ # output addresses for troubleshooting easier.
231
+ remote_addresses = [
232
+ x .connection_info [constants .ENVIRONMENTS_NODES_REMOTE_ADDRESS ]
233
+ for x in environment .nodes .list ()
234
+ if isinstance (x , RemoteNode ) and hasattr (x , "_connection_info" )
235
+ ]
236
+ # if the connection info is not found, there is no ip address to
237
+ # output.
238
+ if remote_addresses :
239
+ log .info (f"node ip addresses: { remote_addresses } " )
240
+ else :
241
+ log .debug ("deleting" )
242
+ self ._delete_environment (environment , log )
243
+ log .info ("deleted" )
244
+
245
+ finally :
246
+ # mark environment is deleted.
247
+ # if there is any error on deleting, it should be ignored.
248
+ environment .status = EnvironmentStatus .Deleted
236
249
237
250
def cleanup (self ) -> None :
238
251
self ._cleanup ()
0 commit comments