|
| 1 | +/** |
| 2 | + * Copyright (C) 2011 Citrix Systems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the GNU General Public License v3 or later. |
| 5 | + * |
| 6 | + * It is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or any later version. |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.cloud.api.commands; |
| 19 | + |
| 20 | +import org.apache.log4j.Logger; |
| 21 | + |
| 22 | +import com.cloud.api.ApiConstants; |
| 23 | +import com.cloud.api.BaseAsyncCmd; |
| 24 | +import com.cloud.api.BaseCmd; |
| 25 | +import com.cloud.api.Implementation; |
| 26 | +import com.cloud.api.Parameter; |
| 27 | +import com.cloud.api.ServerApiException; |
| 28 | +import com.cloud.api.response.SystemVmInstanceResponse; |
| 29 | +import com.cloud.event.EventTypes; |
| 30 | +import com.cloud.exception.ConcurrentOperationException; |
| 31 | +import com.cloud.exception.InvalidParameterValueException; |
| 32 | +import com.cloud.exception.ManagementServerException; |
| 33 | +import com.cloud.exception.ResourceUnavailableException; |
| 34 | +import com.cloud.exception.VirtualMachineMigrationException; |
| 35 | +import com.cloud.host.Host; |
| 36 | +import com.cloud.user.Account; |
| 37 | +import com.cloud.user.UserContext; |
| 38 | +import com.cloud.vm.VirtualMachine; |
| 39 | + |
| 40 | +@Implementation(description="Attempts Migration of a system virtual machine to the host specified.", responseObject=SystemVmInstanceResponse.class) |
| 41 | +public class MigrateSystemVMCmd extends BaseAsyncCmd { |
| 42 | + public static final Logger s_logger = Logger.getLogger(MigrateSystemVMCmd.class.getName()); |
| 43 | + |
| 44 | + private static final String s_name = "migratesystemvmresponse"; |
| 45 | + |
| 46 | + ///////////////////////////////////////////////////// |
| 47 | + //////////////// API parameters ///////////////////// |
| 48 | + ///////////////////////////////////////////////////// |
| 49 | + |
| 50 | + @Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, required=true, description="destination Host ID to migrate VM to") |
| 51 | + private Long hostId; |
| 52 | + |
| 53 | + @Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.LONG, required=true, description="the ID of the virtual machine") |
| 54 | + private Long virtualMachineId; |
| 55 | + |
| 56 | + |
| 57 | + ///////////////////////////////////////////////////// |
| 58 | + /////////////////// Accessors /////////////////////// |
| 59 | + ///////////////////////////////////////////////////// |
| 60 | + |
| 61 | + public Long getHostId() { |
| 62 | + return hostId; |
| 63 | + } |
| 64 | + |
| 65 | + public Long getVirtualMachineId() { |
| 66 | + return virtualMachineId; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + ///////////////////////////////////////////////////// |
| 71 | + /////////////// API Implementation/////////////////// |
| 72 | + ///////////////////////////////////////////////////// |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getCommandName() { |
| 76 | + return s_name; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public long getEntityOwnerId() { |
| 81 | + Account account = UserContext.current().getCaller(); |
| 82 | + if (account != null) { |
| 83 | + return account.getId(); |
| 84 | + } |
| 85 | + |
| 86 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getEventType() { |
| 91 | + return EventTypes.EVENT_VM_MIGRATE; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getEventDescription() { |
| 96 | + return "Attempting to migrate VM Id: " + getVirtualMachineId() + " to host Id: "+ getHostId(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void execute(){ |
| 101 | + |
| 102 | + Host destinationHost = _resourceService.getHost(getHostId()); |
| 103 | + if (destinationHost == null) { |
| 104 | + throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id=" + getHostId()); |
| 105 | + } |
| 106 | + try{ |
| 107 | + UserContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to host Id: "+ getHostId()); |
| 108 | + //FIXME : Should not be calling UserVmService to migrate all types of VMs - need a generic VM layer |
| 109 | + VirtualMachine migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost); |
| 110 | + if (migratedVm != null) { |
| 111 | + // return the generic system VM instance response |
| 112 | + SystemVmInstanceResponse response = _responseGenerator.createSystemVmInstanceResponse(migratedVm); |
| 113 | + response.setResponseName(getCommandName()); |
| 114 | + this.setResponseObject(response); |
| 115 | + } else { |
| 116 | + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to migrate the system vm"); |
| 117 | + } |
| 118 | + } catch (ResourceUnavailableException ex) { |
| 119 | + s_logger.warn("Exception: ", ex); |
| 120 | + throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); |
| 121 | + } catch (ConcurrentOperationException e) { |
| 122 | + s_logger.warn("Exception: ", e); |
| 123 | + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); |
| 124 | + } catch (ManagementServerException e) { |
| 125 | + s_logger.warn("Exception: ", e); |
| 126 | + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); |
| 127 | + } catch (VirtualMachineMigrationException e) { |
| 128 | + s_logger.warn("Exception: ", e); |
| 129 | + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments