@@ -1273,62 +1273,65 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12731273 VirtualMachine vm = profile .getVirtualMachine ();
12741274 State state = vm .getState ();
12751275 s_logger .debug ("Cleaning up resources for the vm " + vm + " in " + state + " state" );
1276- if (state == State .Starting ) {
1277- Step step = work .getStep ();
1278- if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1279- s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1280- return false ;
1281- }
1276+ try {
1277+ if (state == State .Starting ) {
1278+ Step step = work .getStep ();
1279+ if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1280+ s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1281+ return false ;
1282+ }
12821283
1283- if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1284+ if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1285+ if (vm .getHostId () != null ) {
1286+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1287+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
1288+ return false ;
1289+ }
1290+ }
1291+ }
1292+
1293+ if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1294+ s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1295+ return true ;
1296+ }
1297+ } else if (state == State .Stopping ) {
12841298 if (vm .getHostId () != null ) {
12851299 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1286- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
1300+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Stopping + " state as a part of cleanup process" );
12871301 return false ;
12881302 }
12891303 }
1290- }
1291-
1292- if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1293- s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1294- return true ;
1295- }
1296- } else if (state == State .Stopping ) {
1297- if (vm .getHostId () != null ) {
1298- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1299- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Stopping + " state as a part of cleanup process" );
1300- return false ;
1304+ } else if (state == State .Migrating ) {
1305+ if (vm .getHostId () != null ) {
1306+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1307+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1308+ return false ;
1309+ }
13011310 }
1302- }
1303- } else if (state == State .Migrating ) {
1304- if (vm .getHostId () != null ) {
1305- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1306- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1307- return false ;
1311+ if (vm .getLastHostId () != null ) {
1312+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1313+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1314+ return false ;
1315+ }
13081316 }
1309- }
1310- if (vm .getLastHostId () != null ) {
1317+ } else if (state == State .Running ) {
13111318 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1312- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Migrating + " state as a part of cleanup process" );
1319+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Running + " state as a part of cleanup process" );
13131320 return false ;
13141321 }
13151322 }
1316- } else if (state == State .Running ) {
1317- if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1318- s_logger .warn ("Failed to stop vm " + vm + " in " + State .Running + " state as a part of cleanup process" );
1319- return false ;
1323+ } finally {
1324+ try {
1325+ _networkMgr .release (profile , cleanUpEvenIfUnableToStop );
1326+ s_logger .debug ("Successfully released network resources for the vm " + vm );
1327+ } catch (Exception e ) {
1328+ s_logger .warn ("Unable to release some network resources." , e );
13201329 }
1321- }
13221330
1323- try {
1324- _networkMgr .release (profile , cleanUpEvenIfUnableToStop );
1325- s_logger .debug ("Successfully released network resources for the vm " + vm );
1326- } catch (Exception e ) {
1327- s_logger .warn ("Unable to release some network resources." , e );
1331+ volumeMgr .release (profile );
1332+ s_logger .debug ("Successfully cleanued up resources for the vm " + vm + " in " + state + " state" );
13281333 }
13291334
1330- volumeMgr .release (profile );
1331- s_logger .debug ("Successfully cleanued up resources for the vm " + vm + " in " + state + " state" );
13321335 return true ;
13331336 }
13341337
0 commit comments