Skip to content

Commit 9bd5e58

Browse files
author
prachi
committed
Bug 11481 - get manual live migration working with all VMs
Changes: - Added a new API 'migrateSystemVm' backed by MigrateSystemVMCmd.java to migrate system VMs (SSVM, consoleproxy, domain routers(router, LB, DHCP)) - This is Admin only action - The existing API 'migratevirtualmachine' is only for user VMs
1 parent fbc1904 commit 9bd5e58

11 files changed

Lines changed: 284 additions & 26 deletions

File tree

‎api/src/com/cloud/api/ResponseGenerator.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.cloud.api.response.SnapshotPolicyResponse;
5656
import com.cloud.api.response.SnapshotResponse;
5757
import com.cloud.api.response.StoragePoolResponse;
58+
import com.cloud.api.response.SystemVmInstanceResponse;
5859
import com.cloud.api.response.SystemVmResponse;
5960
import com.cloud.api.response.TemplatePermissionsResponse;
6061
import com.cloud.api.response.TemplateResponse;
@@ -230,4 +231,6 @@ public interface ResponseGenerator {
230231

231232
ProjectInvitationResponse createProjectInvitationResponse(ProjectInvitation invite);
232233

234+
SystemVmInstanceResponse createSystemVmInstanceResponse(VirtualMachine systemVM);
235+
233236
}

‎api/src/com/cloud/api/commands/ListHostsCmd.java‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import com.cloud.api.response.HostResponse;
3131
import com.cloud.api.response.ListResponse;
3232
import com.cloud.async.AsyncJob;
33-
import com.cloud.exception.InvalidParameterValueException;
3433
import com.cloud.host.Host;
35-
import com.cloud.uservm.UserVm;
3634
import com.cloud.utils.Pair;
3735

3836
@Implementation(description="Lists hosts.", responseObject=HostResponse.class)
@@ -130,11 +128,7 @@ public void execute(){
130128
List<? extends Host> hostsWithCapacity = new ArrayList<Host>();
131129

132130
if(getVirtualMachineId() != null){
133-
UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
134-
if (userVm == null) {
135-
throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
136-
}
137-
Pair<List<? extends Host>, List<? extends Host>> hostsForMigration = _mgr.listHostsForMigrationOfVM(userVm, this.getStartIndex(), this.getPageSizeVal());
131+
Pair<List<? extends Host>, List<? extends Host>> hostsForMigration = _mgr.listHostsForMigrationOfVM(getVirtualMachineId(), this.getStartIndex(), this.getPageSizeVal());
138132
result = hostsForMigration.first();
139133
hostsWithCapacity = hostsForMigration.second();
140134
}else{
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

‎api/src/com/cloud/api/commands/MigrateVMCmd.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
import com.cloud.user.Account;
3737
import com.cloud.user.UserContext;
3838
import com.cloud.uservm.UserVm;
39+
import com.cloud.vm.VirtualMachine;
3940

40-
@Implementation(description="Attempts Migration of a virtual machine to the host specified.", responseObject=UserVmResponse.class)
41+
@Implementation(description="Attempts Migration of a user virtual machine to the host specified.", responseObject=UserVmResponse.class)
4142
public class MigrateVMCmd extends BaseAsyncCmd {
4243
public static final Logger s_logger = Logger.getLogger(MigrateVMCmd.class.getName());
4344

@@ -109,11 +110,11 @@ public void execute(){
109110
}
110111
try{
111112
UserContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to host Id: "+ getHostId());
112-
UserVm migratedVm = _userVmService.migrateVirtualMachine(userVm, destinationHost);
113+
VirtualMachine migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost);
113114
if (migratedVm != null) {
114-
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", migratedVm).get(0);
115-
response.setResponseName(getCommandName());
116-
this.setResponseObject(response);
115+
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", (UserVm)migratedVm).get(0);
116+
response.setResponseName(getCommandName());
117+
this.setResponseObject(response);
117118
} else {
118119
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to migrate vm");
119120
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.response;
19+
20+
import com.cloud.serializer.Param;
21+
import com.google.gson.annotations.SerializedName;
22+
23+
/*
24+
* This is the generic response for all types of System VMs (SSVM, consoleproxy, domain routers(router, LB, DHCP))
25+
*/
26+
public class SystemVmInstanceResponse extends BaseResponse {
27+
@SerializedName("id") @Param(description="the ID of the system VM")
28+
private Long id;
29+
30+
@SerializedName("systemvmtype") @Param(description="the system VM type")
31+
private String systemVmType;
32+
33+
@SerializedName("name") @Param(description="the name of the system VM")
34+
private String name;
35+
36+
@SerializedName("hostid") @Param(description="the host ID for the system VM")
37+
private Long hostId;
38+
39+
@SerializedName("state") @Param(description="the state of the system VM")
40+
private String state;
41+
42+
@SerializedName("role") @Param(description="the role of the system VM")
43+
private String role;
44+
45+
46+
public Long getObjectId() {
47+
return getId();
48+
}
49+
50+
public Long getId() {
51+
return id;
52+
}
53+
54+
public void setId(Long id) {
55+
this.id = id;
56+
}
57+
58+
public String getSystemVmType() {
59+
return systemVmType;
60+
}
61+
62+
public void setSystemVmType(String systemVmType) {
63+
this.systemVmType = systemVmType;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public void setName(String name) {
71+
this.name = name;
72+
}
73+
74+
public Long getHostId() {
75+
return hostId;
76+
}
77+
78+
public void setHostId(Long hostId) {
79+
this.hostId = hostId;
80+
}
81+
82+
public String getState() {
83+
return state;
84+
}
85+
86+
public void setState(String state) {
87+
this.state = state;
88+
}
89+
90+
public String getRole() {
91+
return role;
92+
}
93+
94+
public void setRole(String role) {
95+
this.role = role;
96+
}
97+
98+
}

‎api/src/com/cloud/server/ManagementService.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import com.cloud.user.Account;
101101
import com.cloud.user.SSHKeyPair;
102102
import com.cloud.user.UserAccount;
103-
import com.cloud.uservm.UserVm;
104103
import com.cloud.utils.Pair;
105104
import com.cloud.vm.InstanceGroup;
106105
import com.cloud.vm.VirtualMachine;
@@ -479,11 +478,11 @@ public interface ManagementService {
479478
* List hosts for migrating the given VM. The API returns list of all hosts in the VM's cluster minus the current host and
480479
* also a list of hosts that seem to have enough CPU and RAM capacity to host this VM.
481480
*
482-
* @param UserVm
483-
* vm The VM to migrate
481+
* @param Long vmId
482+
* Id of The VM to migrate
484483
* @return Pair<List<? extends Host>, List<? extends Host>> List of all Hosts in VM's cluster and list of Hosts with enough capacity
485484
*/
486-
Pair<List<? extends Host>, List<? extends Host>> listHostsForMigrationOfVM(UserVm vm, Long startIndex, Long pageSize);
485+
Pair<List<? extends Host>, List<? extends Host>> listHostsForMigrationOfVM(Long vmId, Long startIndex, Long pageSize);
487486

488487
String[] listEventTypes();
489488

‎api/src/com/cloud/vm/UserVmService.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@ UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityExceptio
359359
* Migrate the given VM to the destination host provided. The API returns the migrated VM if migration succeeds. Only Root
360360
* Admin can migrate a VM.
361361
*
362-
* @param UserVm
363-
* vm The VM to migrate
362+
* @param Long vmId
363+
* vmId of The VM to migrate
364364
* @param Host
365365
* destinationHost to migrate the VM
366-
* @return UserVm migrated VM
366+
* @return VirtualMachine migrated VM
367367
* @throws ManagementServerException
368368
* in case we get error finding the VM or host or access errors or other internal errors.
369369
* @throws ConcurrentOperationException
@@ -373,7 +373,7 @@ UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityExceptio
373373
* @throws VirtualMachineMigrationException
374374
* if the VM to be migrated is not in Running state
375375
*/
376-
UserVm migrateVirtualMachine(UserVm vm, Host destinationHost) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;
376+
VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;
377377

378378
UserVm moveVMToUser(MoveUserVMCmd moveUserVMCmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException ;
379379
}

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ rebootSystemVm=com.cloud.api.commands.RebootSystemVmCmd;1
145145
stopSystemVm=com.cloud.api.commands.StopSystemVmCmd;1
146146
destroySystemVm=com.cloud.api.commands.DestroySystemVmCmd;1
147147
listSystemVms=com.cloud.api.commands.ListSystemVMsCmd;3
148+
migrateSystemVm=com.cloud.api.commands.MigrateSystemVMCmd;1
148149

149150
#### configuration commands
150151
updateConfiguration=com.cloud.api.commands.UpdateCfgCmd;1

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.cloud.api.response.SnapshotPolicyResponse;
7777
import com.cloud.api.response.SnapshotResponse;
7878
import com.cloud.api.response.StoragePoolResponse;
79+
import com.cloud.api.response.SystemVmInstanceResponse;
7980
import com.cloud.api.response.SystemVmResponse;
8081
import com.cloud.api.response.TemplatePermissionsResponse;
8182
import com.cloud.api.response.TemplateResponse;
@@ -2539,4 +2540,24 @@ public ProjectInvitationResponse createProjectInvitationResponse(ProjectInvitati
25392540
return response;
25402541
}
25412542

2543+
@Override
2544+
public SystemVmInstanceResponse createSystemVmInstanceResponse(VirtualMachine vm){
2545+
SystemVmInstanceResponse vmResponse = new SystemVmInstanceResponse();
2546+
vmResponse.setId(vm.getId());
2547+
vmResponse.setSystemVmType(vm.getType().toString().toLowerCase());
2548+
vmResponse.setName(vm.getHostName());
2549+
if (vm.getHostId() != null) {
2550+
vmResponse.setHostId(vm.getHostId());
2551+
}
2552+
if (vm.getState() != null) {
2553+
vmResponse.setState(vm.getState().toString());
2554+
}
2555+
if (vm.getType() == Type.DomainRouter) {
2556+
VirtualRouter router = (VirtualRouter)vm;
2557+
if(router.getRole() != null){
2558+
vmResponse.setRole(router.getRole().toString());
2559+
}
2560+
}
2561+
return vmResponse;
2562+
}
25422563
}

0 commit comments

Comments
 (0)