Skip to content

Commit 94e8090

Browse files
author
Prachi Damle
committed
Deploy, Start, Stop, Destroy VM orchestration service changes
1 parent ee39ec8 commit 94e8090

42 files changed

Lines changed: 1462 additions & 229 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/src/com/cloud/vm/VirtualMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static boolean isSystemVM(VirtualMachine.Type vmtype) {
284284

285285
public long getServiceOfferingId();
286286

287-
public long getDiskOfferingId();
287+
public Long getDiskOfferingId();
288288

289289
Type getType();
290290

core/src/com/cloud/vm/UserVmVO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public UserVmVO(long id,
7878
long accountId,
7979
long serviceOfferingId,
8080
String userData,
81-
String name) {
82-
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled, limitCpuUse);
81+
String name, Long diskOfferingId) {
82+
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled, limitCpuUse, diskOfferingId);
8383
this.userData = userData;
8484
this.displayName = displayName;
8585
this.details = new HashMap<String, String>();

core/src/com/cloud/vm/VMInstanceVO.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
155155
protected String uuid = UUID.randomUUID().toString();
156156
;
157157

158+
@Column(name="disk_offering_id")
159+
protected Long diskOfferingId;
160+
158161
public VMInstanceVO(long id,
159162
long serviceOfferingId,
160163
String name,
@@ -195,9 +198,10 @@ public VMInstanceVO(long id,
195198
long domainId,
196199
long accountId,
197200
boolean haEnabled,
198-
boolean limitResourceUse) {
201+
boolean limitResourceUse, Long diskOfferingId) {
199202
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
200203
this.limitCpuUse = limitResourceUse;
204+
this.diskOfferingId = diskOfferingId;
201205
}
202206

203207
protected VMInstanceVO() {
@@ -472,9 +476,8 @@ public void setServiceOfferingId(long serviceOfferingId) {
472476
}
473477

474478
@Override
475-
public long getDiskOfferingId() {
476-
// TODO Auto-generated method stub
477-
return 0;
479+
public Long getDiskOfferingId() {
480+
return diskOfferingId;
478481
}
479482

480483
}

engine/api/src/org/apache/cloudstack/engine/cloud/entity/api/VirtualMachineEntity.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@
2828

2929
import org.apache.cloudstack.engine.entity.api.CloudStackEntity;
3030

31-
import com.cloud.deploy.DeployDestination;
31+
32+
import com.cloud.deploy.DeploymentPlan;
3233
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
33-
import com.cloud.vm.VirtualMachine;
34+
import com.cloud.exception.AgentUnavailableException;
35+
import com.cloud.exception.CloudException;
36+
import com.cloud.exception.ConcurrentOperationException;
37+
import com.cloud.exception.InsufficientCapacityException;
38+
import com.cloud.exception.ResourceUnavailableException;
39+
3440

3541
/**
3642
* VirtualMachineEntity represents a Virtual Machine in Cloud Orchestration
@@ -78,33 +84,33 @@ public interface VirtualMachineEntity extends CloudStackEntity {
7884
void delTag();
7985

8086
/**
81-
* Start the virtual machine with a given deploy destination
87+
* Start the virtual machine with a given deployment plan
8288
* @param plannerToUse the Deployment Planner that should be used
83-
* @param dest destination to which to deploy the machine
89+
* @param plan plan to which to deploy the machine
8490
* @param exclude list of areas to exclude
8591
* @return a reservation id
8692
*/
87-
String reserve(String plannerToUse, @BeanParam DeployDestination dest, ExcludeList exclude);
93+
String reserve(String plannerToUse, @BeanParam DeploymentPlan plan, ExcludeList exclude, String caller) throws InsufficientCapacityException, ResourceUnavailableException;
8894

8995
/**
9096
* Migrate this VM to a certain destination.
9197
*
9298
* @param reservationId reservation id from reserve call.
9399
*/
94-
void migrateTo(String reservationId);
100+
void migrateTo(String reservationId, String caller);
95101

96102
/**
97103
* Deploy this virtual machine according to the reservation from before.
98104
* @param reservationId reservation id from reserve call.
99105
*
100106
*/
101-
void deploy(String reservationId);
107+
void deploy(String reservationId, String caller) throws InsufficientCapacityException, ResourceUnavailableException;
102108

103109
/**
104110
* Stop the virtual machine
105111
*
106112
*/
107-
void stop();
113+
boolean stop(String caller) throws ResourceUnavailableException, CloudException;
108114

109115
/**
110116
* Cleans up after any botched starts. CloudStack Orchestration Platform
@@ -116,7 +122,7 @@ public interface VirtualMachineEntity extends CloudStackEntity {
116122
/**
117123
* Destroys the VM.
118124
*/
119-
void destroy();
125+
boolean destroy(String caller) throws AgentUnavailableException, CloudException, ConcurrentOperationException;
120126

121127
/**
122128
* Duplicate this VM in the database so that it will start new

engine/api/src/org/apache/cloudstack/engine/service/api/OrchestrationService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.net.URL;
2222
import java.util.List;
23+
import java.util.Map;
2324

2425
import javax.ws.rs.DELETE;
2526
import javax.ws.rs.POST;
@@ -35,6 +36,7 @@
3536
import com.cloud.deploy.DeploymentPlan;
3637
import com.cloud.exception.InsufficientCapacityException;
3738
import com.cloud.hypervisor.Hypervisor;
39+
import com.cloud.vm.NicProfile;
3840

3941
@Path("orchestration")
4042
@Produces({"application/json", "application/xml"})
@@ -70,7 +72,7 @@ VirtualMachineEntity createVirtualMachine(
7072
@QueryParam("disk-size") Long diskSize,
7173
@QueryParam("compute-tags") List<String> computeTags,
7274
@QueryParam("root-disk-tags") List<String> rootDiskTags,
73-
@QueryParam("networks") List<String> networks,
75+
@QueryParam("network-nic-map") Map<String, NicProfile> networkNicMap,
7476
@QueryParam("deploymentplan") DeploymentPlan plan
7577
) throws InsufficientCapacityException;
7678

@@ -89,7 +91,7 @@ VirtualMachineEntity createVirtualMachineFromScratch(
8991
@QueryParam("disk-size") Long diskSize,
9092
@QueryParam("compute-tags") List<String> computeTags,
9193
@QueryParam("root-disk-tags") List<String> rootDiskTags,
92-
@QueryParam("networks") List<String> networks,
94+
@QueryParam("network-nic-map") Map<String, NicProfile> networkNicMap,
9395
@QueryParam("deploymentplan") DeploymentPlan plan
9496
) throws InsufficientCapacityException;
9597

@@ -107,4 +109,6 @@ VirtualMachineEntity createVirtualMachineFromScratch(
107109

108110
@POST
109111
TemplateEntity registerTemplate(String name, URL path, String os, Hypervisor hypervisor);
112+
113+
VirtualMachineEntity getVirtualMachine(@QueryParam("id") String id);
110114
}

engine/orchestration/src/org/apache/cloudstack/engine/cloud/entity/api/VMEntityManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,25 @@
1818

1919
import org.apache.cloudstack.engine.cloud.entity.api.db.VMEntityVO;
2020

21+
import com.cloud.deploy.DeploymentPlan;
22+
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
23+
import com.cloud.exception.AgentUnavailableException;
24+
import com.cloud.exception.ConcurrentOperationException;
25+
import com.cloud.exception.InsufficientCapacityException;
26+
import com.cloud.exception.OperationTimedoutException;
27+
import com.cloud.exception.ResourceUnavailableException;
28+
2129
public interface VMEntityManager {
2230

2331
VMEntityVO loadVirtualMachine(String vmId);
2432

2533
void saveVirtualMachine(VMEntityVO vmInstanceVO);
34+
35+
String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse, DeploymentPlan plan, ExcludeList exclude) throws InsufficientCapacityException, ResourceUnavailableException;
36+
37+
void deployVirtualMachine(String reservationId, String caller) throws InsufficientCapacityException, ResourceUnavailableException;
38+
39+
boolean stopvirtualmachine(VMEntityVO vmEntityVO, String caller) throws ResourceUnavailableException;
40+
41+
boolean destroyVirtualMachine(VMEntityVO vmEntityVO, String caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException;
2642
}

engine/orchestration/src/org/apache/cloudstack/engine/cloud/entity/api/VMEntityManagerImpl.java

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,209 @@
1616
// under the License.
1717
package org.apache.cloudstack.engine.cloud.entity.api;
1818

19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.UUID;
23+
24+
import javax.inject.Inject;
25+
1926
import org.apache.cloudstack.engine.cloud.entity.api.db.VMEntityVO;
27+
import org.apache.cloudstack.engine.cloud.entity.api.db.VMReservationVO;
28+
import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMEntityDao;
29+
import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMReservationDao;
2030
import org.springframework.stereotype.Component;
2131

32+
import com.cloud.dc.DataCenter;
33+
import com.cloud.deploy.DataCenterDeployment;
34+
import com.cloud.deploy.DeployDestination;
35+
import com.cloud.deploy.DeploymentPlan;
36+
import com.cloud.deploy.DeploymentPlanner;
37+
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
38+
import com.cloud.exception.AgentUnavailableException;
39+
import com.cloud.exception.ConcurrentOperationException;
40+
import com.cloud.exception.InsufficientCapacityException;
41+
import com.cloud.exception.InsufficientServerCapacityException;
42+
import com.cloud.exception.OperationTimedoutException;
43+
import com.cloud.exception.ResourceUnavailableException;
44+
import com.cloud.network.dao.NetworkDao;
45+
import com.cloud.org.Cluster;
46+
import com.cloud.service.dao.ServiceOfferingDao;
47+
import com.cloud.storage.StoragePoolVO;
48+
import com.cloud.storage.Volume;
49+
import com.cloud.storage.VolumeVO;
50+
import com.cloud.storage.dao.DiskOfferingDao;
51+
import com.cloud.storage.dao.StoragePoolDao;
52+
import com.cloud.storage.dao.VMTemplateDao;
53+
import com.cloud.storage.dao.VolumeDao;
54+
import com.cloud.user.Account;
55+
import com.cloud.user.User;
56+
import com.cloud.user.dao.AccountDao;
57+
import com.cloud.user.dao.UserDao;
58+
import com.cloud.utils.component.ComponentContext;
59+
import com.cloud.utils.exception.CloudRuntimeException;
60+
import com.cloud.vm.VMInstanceVO;
61+
import com.cloud.vm.VirtualMachineManager;
62+
import com.cloud.vm.VirtualMachineProfile;
63+
import com.cloud.vm.VirtualMachineProfileImpl;
64+
import com.cloud.vm.dao.VMInstanceDao;
65+
2266
@Component
2367
public class VMEntityManagerImpl implements VMEntityManager {
2468

69+
@Inject
70+
protected VMInstanceDao _vmDao;
71+
@Inject
72+
protected VMTemplateDao _templateDao = null;
73+
74+
@Inject
75+
protected ServiceOfferingDao _serviceOfferingDao;
76+
77+
@Inject
78+
protected DiskOfferingDao _diskOfferingDao = null;
79+
80+
@Inject
81+
protected NetworkDao _networkDao;
82+
83+
@Inject
84+
protected AccountDao _accountDao = null;
85+
86+
@Inject
87+
protected UserDao _userDao = null;
88+
89+
@Inject
90+
protected VMEntityDao _vmEntityDao;
91+
92+
@Inject
93+
protected VMReservationDao _reservationDao;
94+
95+
@Inject
96+
protected VirtualMachineManager _itMgr;
97+
98+
@Inject
99+
protected List<DeploymentPlanner> _planners;
100+
101+
@Inject
102+
protected VolumeDao _volsDao;
103+
104+
@Inject
105+
protected StoragePoolDao _storagePoolDao;
106+
25107
@Override
26108
public VMEntityVO loadVirtualMachine(String vmId) {
27109
// TODO Auto-generated method stub
28110
return null;
29111
}
30112

31113
@Override
32-
public void saveVirtualMachine(VMEntityVO vmInstanceVO) {
33-
// TODO Auto-generated method stub
114+
public void saveVirtualMachine(VMEntityVO entity) {
115+
_vmEntityDao.persist(entity);
34116

35117
}
36118

119+
@Override
120+
public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse, DeploymentPlan planToDeploy, ExcludeList exclude)
121+
throws InsufficientCapacityException, ResourceUnavailableException {
122+
123+
//call planner and get the deployDestination.
124+
//load vm instance and offerings and call virtualMachineManagerImpl
125+
//FIXME: profile should work on VirtualMachineEntity
126+
VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid());
127+
VirtualMachineProfileImpl<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
128+
DeploymentPlan plan = planToDeploy;
129+
130+
List<VolumeVO> vols = _volsDao.findReadyRootVolumesByInstance(vm.getId());
131+
if(!vols.isEmpty()){
132+
VolumeVO vol = vols.get(0);
133+
StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId());
134+
if (!pool.isInMaintenance()) {
135+
long rootVolDcId = pool.getDataCenterId();
136+
Long rootVolPodId = pool.getPodId();
137+
Long rootVolClusterId = pool.getClusterId();
138+
if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) {
139+
Long clusterIdSpecified = planToDeploy.getClusterId();
140+
if (clusterIdSpecified != null && rootVolClusterId != null) {
141+
if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) {
142+
// cannot satisfy the plan passed in to the
143+
// planner
144+
throw new ResourceUnavailableException("Root volume is ready in different cluster, Deployment plan provided cannot be satisfied, unable to create a deployment for "
145+
+ vm, Cluster.class, clusterIdSpecified);
146+
}
147+
}
148+
plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), vol.getPoolId(), null, null);
149+
}else{
150+
plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId(), null, null);
151+
152+
}
153+
}
154+
155+
}
156+
157+
DeploymentPlanner planner = ComponentContext.getCompanent(plannerToUse);
158+
DeployDestination dest = null;
159+
160+
if (planner.canHandle(vmProfile, plan, exclude)) {
161+
dest = planner.plan(vmProfile, plan, exclude);
162+
}
163+
164+
if (dest != null) {
165+
//save destination with VMEntityVO
166+
VMReservationVO vmReservation = new VMReservationVO(vm.getId(), dest.getDataCenter().getId(), dest.getPod().getId(), dest.getCluster().getId(), dest.getHost().getId());
167+
Map<Long,Long> volumeReservationMap = new HashMap<Long,Long>();
168+
for(Volume vo : dest.getStorageForDisks().keySet()){
169+
volumeReservationMap.put(vo.getId(), dest.getStorageForDisks().get(vo).getId());
170+
}
171+
vmReservation.setVolumeReservation(volumeReservationMap);
172+
173+
vmEntityVO.setVmReservation(vmReservation);
174+
_vmEntityDao.persist(vmEntityVO);
175+
176+
return vmReservation.getUuid();
177+
}else{
178+
throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId());
179+
}
180+
181+
}
182+
183+
@Override
184+
public void deployVirtualMachine(String reservationId, String caller) throws InsufficientCapacityException, ResourceUnavailableException{
185+
//grab the VM Id and destination using the reservationId.
186+
187+
VMReservationVO vmReservation = _reservationDao.findByReservationId(reservationId);
188+
long vmId = vmReservation.getVmId();
189+
190+
VMInstanceVO vm = _vmDao.findById(vmId);
191+
//Pass it down
192+
Long poolId = null;
193+
Map<Long,Long> storage = vmReservation.getVolumeReservation();
194+
if(storage != null){
195+
Long[] array = new Long[storage.keySet().size()];
196+
storage.keySet().toArray(array);
197+
poolId = array[0];
198+
}
199+
200+
DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), vmReservation.getPodId(), vmReservation.getClusterId(),
201+
vmReservation.getHostId(), poolId , null);
202+
203+
VMInstanceVO vmDeployed = _itMgr.start(vm, null, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId()), plan);
204+
205+
}
206+
207+
@Override
208+
public boolean stopvirtualmachine(VMEntityVO vmEntityVO, String caller) throws ResourceUnavailableException {
209+
210+
VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid());
211+
return _itMgr.stop(vm, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId()));
212+
213+
}
214+
215+
@Override
216+
public boolean destroyVirtualMachine(VMEntityVO vmEntityVO, String caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException{
217+
218+
VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid());
219+
return _itMgr.destroy(vm, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId()));
220+
221+
222+
}
223+
37224
}

0 commit comments

Comments
 (0)