|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package com.cloud.hypervisor.xenserver.resource.wrapper.xenbase; |
| 19 | + |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.apache.log4j.Logger; |
| 24 | + |
| 25 | +import com.cloud.agent.api.Answer; |
| 26 | +import com.cloud.agent.api.CleanupVMCommand; |
| 27 | +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; |
| 28 | +import com.cloud.resource.CommandWrapper; |
| 29 | +import com.cloud.resource.ResourceWrapper; |
| 30 | +import com.xensource.xenapi.Connection; |
| 31 | +import com.xensource.xenapi.Types; |
| 32 | +import com.xensource.xenapi.VM; |
| 33 | + |
| 34 | +@ResourceWrapper(handles = CleanupVMCommand.class) |
| 35 | +public class CitrixCleanupVMCommandWrapper extends CommandWrapper<CleanupVMCommand, Answer, CitrixResourceBase> { |
| 36 | + |
| 37 | + private static final Logger s_logger = Logger.getLogger(CitrixCleanupVMCommandWrapper.class); |
| 38 | + |
| 39 | + @Override |
| 40 | + public Answer execute(final CleanupVMCommand command, final CitrixResourceBase citrixResourceBase) { |
| 41 | + if (citrixResourceBase.isDestroyHaltedVms()) { |
| 42 | + s_logger.debug(String.format("Cleanup VM is not needed for host with version %s", |
| 43 | + citrixResourceBase.getHost().getProductVersion())); |
| 44 | + return new Answer(command); |
| 45 | + } |
| 46 | + final String vmName = command.getVmName(); |
| 47 | + try { |
| 48 | + final Connection conn = citrixResourceBase.getConnection(); |
| 49 | + final Set<VM> vms = VM.getByNameLabel(conn, vmName); |
| 50 | + if (vms.isEmpty()) { |
| 51 | + return new Answer(command, true, "VM does not exist"); |
| 52 | + } |
| 53 | + // destroy vm which is in HALTED state on this host |
| 54 | + final Iterator<VM> iter = vms.iterator(); |
| 55 | + while (iter.hasNext()) { |
| 56 | + final VM vm = iter.next(); |
| 57 | + final VM.Record vmr = vm.getRecord(conn); |
| 58 | + if (!Types.VmPowerState.HALTED.equals(vmr.powerState)) { |
| 59 | + final String msg = String.format("VM %s is not in %s state", vmName, Types.VmPowerState.HALTED); |
| 60 | + s_logger.error(msg); |
| 61 | + return new Answer(command, false, msg); |
| 62 | + } |
| 63 | + if (citrixResourceBase.isRefNull(vmr.residentOn)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + iter.remove(); |
| 70 | + } |
| 71 | + for (final VM vm : vms) { |
| 72 | + citrixResourceBase.destroyVm(vm, conn, true); |
| 73 | + } |
| 74 | + |
| 75 | + } catch (final Exception e) { |
| 76 | + final String msg = String.format("Clean up VM %s fail due to %s", vmName, e); |
| 77 | + s_logger.error(msg, e); |
| 78 | + return new Answer(command, false, e.getMessage()); |
| 79 | + } |
| 80 | + return new Answer(command); |
| 81 | + } |
| 82 | +} |
0 commit comments