Skip to content

Commit 6c01b62

Browse files
Harikrishna PatnalaAbhinandan Prateek
authored andcommitted
CLOUDSTACK-667: VM's base image update facility
1 parent 59db01c commit 6c01b62

3 files changed

Lines changed: 126 additions & 66 deletions

File tree

‎api/src/org/apache/cloudstack/api/command/user/vm/RestoreVMCmd.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.cloudstack.api.BaseAsyncCmd;
2323
import org.apache.cloudstack.api.Parameter;
2424
import org.apache.cloudstack.api.ServerApiException;
25+
import org.apache.cloudstack.api.response.TemplateResponse;
2526
import org.apache.cloudstack.api.response.UserVmResponse;
2627
import org.apache.log4j.Logger;
2728

@@ -34,7 +35,7 @@
3435
import com.cloud.user.UserContext;
3536
import com.cloud.uservm.UserVm;
3637

37-
@APICommand(name = "restoreVirtualMachine", description="Restore a VM to original template or specific snapshot", responseObject=UserVmResponse.class, since="3.0.0")
38+
@APICommand(name = "restoreVirtualMachine", description="Restore a VM to original template or new template", responseObject=UserVmResponse.class, since="3.0.0")
3839
public class RestoreVMCmd extends BaseAsyncCmd {
3940
public static final Logger s_logger = Logger.getLogger(RestoreVMCmd.class);
4041
private static final String s_name = "restorevmresponse";
@@ -43,6 +44,9 @@ public class RestoreVMCmd extends BaseAsyncCmd {
4344
required=true, description="Virtual Machine ID")
4445
private Long vmId;
4546

47+
@Parameter(name=ApiConstants.TEMPLATE_ID, type=CommandType.UUID, entityType = TemplateResponse.class, description="an optional template Id to restore vm from the new template")
48+
private Long templateId;
49+
4650
@Override
4751
public String getEventType() {
4852
return EventTypes.EVENT_VM_RESTORE;
@@ -85,4 +89,8 @@ public long getEntityOwnerId() {
8589
public long getVmId() {
8690
return vmId;
8791
}
92+
93+
public Long getTemplateId() {
94+
return templateId;
95+
}
8896
}

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,8 +2675,12 @@ public UserVm rebootVirtualMachine(RebootVMCmd cmd)
26752675
// If the VM is Volatile in nature, on reboot discard the VM's root disk and create a new root disk for it: by calling restoreVM
26762676
long serviceOfferingId = vmInstance.getServiceOfferingId();
26772677
ServiceOfferingVO offering = _serviceOfferingDao.findById(serviceOfferingId);
2678-
if(offering.getVolatileVm()){
2679-
return restoreVMInternal(caller, vmInstance);
2678+
if(offering != null && offering.getRemoved() == null) {
2679+
if(offering.getVolatileVm()){
2680+
return restoreVMInternal(caller, vmInstance, null);
2681+
}
2682+
} else {
2683+
throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId + " corresponding to the vm");
26802684
}
26812685

26822686
return rebootVirtualMachine(UserContext.current().getCallerUserId(),
@@ -4795,20 +4799,22 @@ public UserVm moveVMToUser(AssignVMCmd cmd)
47954799
public UserVm restoreVM(RestoreVMCmd cmd) {
47964800
// Input validation
47974801
Account caller = UserContext.current().getCaller();
4798-
Long userId = UserContext.current().getCallerUserId();
47994802

48004803
long vmId = cmd.getVmId();
4804+
Long newTemplateId = cmd.getTemplateId();
48014805
UserVmVO vm = _vmDao.findById(vmId);
48024806
if (vm == null) {
48034807
InvalidParameterValueException ex = new InvalidParameterValueException("Cannot find VM with ID " + vmId);
48044808
ex.addProxyObject(vm, vmId, "vmId");
48054809
throw ex;
48064810
}
48074811

4808-
return restoreVMInternal(caller, vm);
4812+
_accountMgr.checkAccess(caller, null, true, vm);
4813+
4814+
return restoreVMInternal(caller, vm, newTemplateId);
48094815
}
48104816

4811-
public UserVm restoreVMInternal(Account caller, UserVmVO vm){
4817+
public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId){
48124818

48134819
Long userId = caller.getId();
48144820
Account owner = _accountDao.findById(vm.getAccountId());
@@ -4857,13 +4863,19 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm){
48574863
throw ex;
48584864
}
48594865

4860-
VMTemplateVO template = _templateDao.findById(templateId);
4861-
if (template == null) {
4862-
InvalidParameterValueException ex = new InvalidParameterValueException(
4863-
"Cannot find template for specified volumeid and vmId");
4864-
ex.addProxyObject(vm, vmId, "vmId");
4865-
ex.addProxyObject(root, root.getId(), "volumeId");
4866-
throw ex;
4866+
VMTemplateVO template = null;
4867+
if(newTemplateId != null) {
4868+
template = _templateDao.findById(newTemplateId);
4869+
_accountMgr.checkAccess(caller, null, true, template);
4870+
} else {
4871+
template = _templateDao.findById(templateId);
4872+
if (template == null) {
4873+
InvalidParameterValueException ex = new InvalidParameterValueException(
4874+
"Cannot find template for specified volumeid and vmId");
4875+
ex.addProxyObject(vm, vmId, "vmId");
4876+
ex.addProxyObject(root, root.getId(), "volumeId");
4877+
throw ex;
4878+
}
48674879
}
48684880

48694881
if (needRestart) {
@@ -4878,8 +4890,15 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm){
48784890
}
48794891
}
48804892

4881-
/* allocate a new volume from original template */
4882-
VolumeVO newVol = _storageMgr.allocateDuplicateVolume(root, null);
4893+
/* If new template is provided allocate a new volume from new template otherwise allocate new volume from original template */
4894+
VolumeVO newVol = null;
4895+
if (newTemplateId != null){
4896+
newVol = _storageMgr.allocateDuplicateVolume(root, newTemplateId);
4897+
vm.setGuestOSId(template.getGuestOSId());
4898+
vm.setTemplateId(newTemplateId);
4899+
_vmDao.update(vmId, vm);
4900+
} else newVol = _storageMgr.allocateDuplicateVolume(root, null);
4901+
48834902
_volsDao.attachVolume(newVol.getId(), vmId, newVol.getDeviceId());
48844903

48854904
/* Detach and destory the old root volume */
@@ -4903,8 +4922,8 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm){
49034922
}
49044923
}
49054924

4906-
s_logger.debug("Restore VM " + vm.getUuid() + " with template "
4907-
+ root.getTemplateId() + " successfully");
4925+
s_logger.debug("Restore VM " + vmId + " with template "
4926+
+ template.getUuid() + " done successfully");
49084927
return vm;
49094928

49104929
}

‎server/test/com/cloud/vm/UserVmManagerTest.java‎

Lines changed: 82 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ public class UserVmManagerTest {
5454
@Spy UserVmManagerImpl _userVmMgr = new UserVmManagerImpl();
5555
@Mock VirtualMachineManager _itMgr;
5656
@Mock StorageManager _storageMgr;
57-
@Mock Account account;
57+
@Mock Account _account;
5858
@Mock AccountManager _accountMgr;
5959
@Mock AccountDao _accountDao;
6060
@Mock UserDao _userDao;
6161
@Mock UserVmDao _vmDao;
6262
@Mock VMTemplateDao _templateDao;
6363
@Mock VolumeDao _volsDao;
64-
@Mock RestoreVMCmd restoreVMCmd;
65-
@Mock AccountVO accountMock;
66-
@Mock UserVO userMock;
67-
@Mock UserVmVO vmMock;
68-
@Mock VMTemplateVO templateMock;
69-
@Mock VolumeVO volumeMock;
70-
@Mock List<VolumeVO> rootVols;
64+
@Mock RestoreVMCmd _restoreVMCmd;
65+
@Mock AccountVO _accountMock;
66+
@Mock UserVO _userMock;
67+
@Mock UserVmVO _vmMock;
68+
@Mock VMTemplateVO _templateMock;
69+
@Mock VolumeVO _volumeMock;
70+
@Mock List<VolumeVO> _rootVols;
7171
@Before
7272
public void setup(){
7373
MockitoAnnotations.initMocks(this);
@@ -79,69 +79,102 @@ public void setup(){
7979
_userVmMgr._storageMgr = _storageMgr;
8080
_userVmMgr._accountDao = _accountDao;
8181
_userVmMgr._userDao = _userDao;
82+
_userVmMgr._accountMgr = _accountMgr;
8283

83-
doReturn(3L).when(account).getId();
84-
doReturn(8L).when(vmMock).getAccountId();
85-
when(_accountDao.findById(anyLong())).thenReturn(accountMock);
86-
when(_userDao.findById(anyLong())).thenReturn(userMock);
87-
doReturn(Account.State.enabled).when(account).getState();
88-
when(vmMock.getId()).thenReturn(314L);
84+
doReturn(3L).when(_account).getId();
85+
doReturn(8L).when(_vmMock).getAccountId();
86+
when(_accountDao.findById(anyLong())).thenReturn(_accountMock);
87+
when(_userDao.findById(anyLong())).thenReturn(_userMock);
88+
doReturn(Account.State.enabled).when(_account).getState();
89+
when(_vmMock.getId()).thenReturn(314L);
8990

9091
}
9192

92-
// VM state not in running/stopped case
93+
// Test restoreVm when VM state not in running/stopped case
9394
@Test(expected=CloudRuntimeException.class)
9495
public void testRestoreVMF1() throws ResourceAllocationException {
9596

96-
when(_vmDao.findById(anyLong())).thenReturn(vmMock);
97-
when(_templateDao.findById(anyLong())).thenReturn(templateMock);
98-
doReturn(VirtualMachine.State.Error).when(vmMock).getState();
99-
_userVmMgr.restoreVMInternal(account, vmMock);
97+
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
98+
when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
99+
doReturn(VirtualMachine.State.Error).when(_vmMock).getState();
100+
_userVmMgr.restoreVMInternal(_account, _vmMock, null);
100101
}
101102

102-
// when VM is in stopped state
103+
// Test restoreVm when VM is in stopped state
103104
@Test
104105
public void testRestoreVMF2() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
105-
ConcurrentOperationException, ResourceAllocationException {
106-
107-
doReturn(VirtualMachine.State.Stopped).when(vmMock).getState();
108-
when(_vmDao.findById(anyLong())).thenReturn(vmMock);
109-
when(_volsDao.findByInstance(anyLong())).thenReturn(rootVols);
110-
doReturn(false).when(rootVols).isEmpty();
111-
when(rootVols.get(eq(0))).thenReturn(volumeMock);
112-
doReturn(3L).when(volumeMock).getTemplateId();
113-
when(_templateDao.findById(anyLong())).thenReturn(templateMock);
114-
when(_storageMgr.allocateDuplicateVolume(volumeMock, null)).thenReturn(volumeMock);
106+
ConcurrentOperationException, ResourceAllocationException {
107+
108+
doReturn(VirtualMachine.State.Stopped).when(_vmMock).getState();
109+
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
110+
when(_volsDao.findByInstance(anyLong())).thenReturn(_rootVols);
111+
doReturn(false).when(_rootVols).isEmpty();
112+
when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
113+
doReturn(3L).when(_volumeMock).getTemplateId();
114+
when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
115+
when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
115116
doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
116-
when(volumeMock.getId()).thenReturn(3L);
117+
when(_volumeMock.getId()).thenReturn(3L);
117118
doNothing().when(_volsDao).detachVolume(anyLong());
118-
when(_storageMgr.destroyVolume(volumeMock)).thenReturn(true);
119+
when(_storageMgr.destroyVolume(_volumeMock)).thenReturn(true);
120+
when(_templateMock.getUuid()).thenReturn("e0552266-7060-11e2-bbaa-d55f5db67735");
119121

120-
_userVmMgr.restoreVMInternal(account, vmMock);
122+
_userVmMgr.restoreVMInternal(_account, _vmMock, null);
121123

122124
}
123125

124-
// when VM is in running state
126+
// Test restoreVM when VM is in running state
125127
@Test
126128
public void testRestoreVMF3() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
127-
ConcurrentOperationException, ResourceAllocationException {
128-
129-
doReturn(VirtualMachine.State.Running).when(vmMock).getState();
130-
when(_vmDao.findById(anyLong())).thenReturn(vmMock);
131-
when(_volsDao.findByInstance(anyLong())).thenReturn(rootVols);
132-
doReturn(false).when(rootVols).isEmpty();
133-
when(rootVols.get(eq(0))).thenReturn(volumeMock);
134-
doReturn(3L).when(volumeMock).getTemplateId();
135-
when(_templateDao.findById(anyLong())).thenReturn(templateMock);
136-
when(_itMgr.stop(vmMock, userMock, account)).thenReturn(true);
137-
when(_itMgr.start(vmMock, null, userMock, account)).thenReturn(vmMock);
138-
when(_storageMgr.allocateDuplicateVolume(volumeMock, null)).thenReturn(volumeMock);
129+
ConcurrentOperationException, ResourceAllocationException {
130+
131+
doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
132+
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
133+
when(_volsDao.findByInstance(anyLong())).thenReturn(_rootVols);
134+
doReturn(false).when(_rootVols).isEmpty();
135+
when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
136+
doReturn(3L).when(_volumeMock).getTemplateId();
137+
when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
138+
when(_itMgr.stop(_vmMock, _userMock, _account)).thenReturn(true);
139+
when(_itMgr.start(_vmMock, null, _userMock, _account)).thenReturn(_vmMock);
140+
when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
139141
doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
140-
when(volumeMock.getId()).thenReturn(3L);
142+
when(_volumeMock.getId()).thenReturn(3L);
141143
doNothing().when(_volsDao).detachVolume(anyLong());
142-
when(_storageMgr.destroyVolume(volumeMock)).thenReturn(true);
144+
when(_storageMgr.destroyVolume(_volumeMock)).thenReturn(true);
145+
when(_templateMock.getUuid()).thenReturn("e0552266-7060-11e2-bbaa-d55f5db67735");
143146

144-
_userVmMgr.restoreVMInternal(account, vmMock);
147+
_userVmMgr.restoreVMInternal(_account, _vmMock, null);
148+
149+
}
150+
151+
// Test restoreVM on providing new template Id, when VM is in running state
152+
@Test
153+
public void testRestoreVMF4() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
154+
ConcurrentOperationException, ResourceAllocationException {
155+
doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
156+
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
157+
when(_volsDao.findByInstance(anyLong())).thenReturn(_rootVols);
158+
doReturn(false).when(_rootVols).isEmpty();
159+
when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
160+
doReturn(3L).when(_volumeMock).getTemplateId();
161+
when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
162+
doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
163+
when(_itMgr.stop(_vmMock, _userMock, _account)).thenReturn(true);
164+
when(_storageMgr.allocateDuplicateVolume(_volumeMock, 14L)).thenReturn(_volumeMock);
165+
when(_templateMock.getGuestOSId()).thenReturn(5L);
166+
doNothing().when(_vmMock).setGuestOSId(anyLong());
167+
doNothing().when(_vmMock).setTemplateId(3L);
168+
when(_vmDao.update(314L, _vmMock)).thenReturn(true);
169+
when(_itMgr.start(_vmMock, null, _userMock, _account)).thenReturn(_vmMock);
170+
when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
171+
doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
172+
when(_volumeMock.getId()).thenReturn(3L);
173+
doNothing().when(_volsDao).detachVolume(anyLong());
174+
when(_storageMgr.destroyVolume(_volumeMock)).thenReturn(true);
175+
when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
176+
177+
_userVmMgr.restoreVMInternal(_account, _vmMock, 14L);
145178

146179
}
147180

0 commit comments

Comments
 (0)