@@ -1233,62 +1233,65 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12331233 VirtualMachine vm = profile .getVirtualMachine ();
12341234 State state = vm .getState ();
12351235 s_logger .debug ("Cleaning up resources for the vm " + vm + " in " + state + " state" );
1236- if (state == State .Starting ) {
1237- Step step = work .getStep ();
1238- if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1239- s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1240- return false ;
1241- }
1236+ try {
1237+ if (state == State .Starting ) {
1238+ Step step = work .getStep ();
1239+ if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1240+ s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1241+ return false ;
1242+ }
12421243
1243- if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1244+ if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1245+ if (vm .getHostId () != null ) {
1246+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1247+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
1248+ return false ;
1249+ }
1250+ }
1251+ }
1252+
1253+ if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1254+ s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1255+ return true ;
1256+ }
1257+ } else if (state == State .Stopping ) {
12441258 if (vm .getHostId () != null ) {
12451259 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1246- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
1260+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Stopping + " state as a part of cleanup process" );
12471261 return false ;
12481262 }
12491263 }
1250- }
1251-
1252- if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1253- s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1254- return true ;
1255- }
1256- } else if (state == State .Stopping ) {
1257- if (vm .getHostId () != null ) {
1258- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1259- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Stopping + " state as a part of cleanup process" );
1260- return false ;
1264+ } else if (state == State .Migrating ) {
1265+ if (vm .getHostId () != null ) {
1266+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1267+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1268+ return false ;
1269+ }
12611270 }
1262- }
1263- } else if (state == State .Migrating ) {
1264- if (vm .getHostId () != null ) {
1265- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1266- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1267- return false ;
1271+ if (vm .getLastHostId () != null ) {
1272+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1273+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1274+ return false ;
1275+ }
12681276 }
1269- }
1270- if (vm .getLastHostId () != null ) {
1277+ } else if (state == State .Running ) {
12711278 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1272- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1279+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Running + " state as a part of cleanup process" );
12731280 return false ;
12741281 }
12751282 }
1276- } else if (state == State .Running ) {
1277- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop )) {
1278- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Running + " state as a part of cleanup process" );
1279- return false ;
1283+ } finally {
1284+ try {
1285+ _networkMgr .release (profile , cleanUpEvenIfUnableToStop );
1286+ s_logger .debug ("Successfully released network resources for the vm " + vm );
1287+ } catch (Exception e ) {
1288+ s_logger .warn ("Unable to release some network resources." , e );
12801289 }
1281- }
12821290
1283- try {
1284- _networkMgr .release (profile , cleanUpEvenIfUnableToStop );
1285- s_logger .debug ("Successfully released network resources for the vm " + vm );
1286- } catch (Exception e ) {
1287- s_logger .warn ("Unable to release some network resources." , e );
1291+ volumeMgr .release (profile );
1292+ s_logger .debug ("Successfully cleanued up resources for the vm " + vm + " in " + state + " state" );
12881293 }
12891294
1290- volumeMgr .release (profile );
1291- s_logger .debug ("Successfully cleanued up resources for the vm " + vm + " in " + state + " state" );
12921295 return true ;
12931296 }
12941297
0 commit comments