Skip to content

Commit 378d64e

Browse files
committed
changes for orchestrator prepare using virtualmachineto
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 2c59171 commit 378d64e

File tree

15 files changed

+490
-153
lines changed

15 files changed

+490
-153
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public class ApiConstants {
158158
public static final String DISK = "disk";
159159
public static final String DISK_OFFERING_ID = "diskofferingid";
160160
public static final String NEW_DISK_OFFERING_ID = "newdiskofferingid";
161+
public static final String ORCHESTRATOR_REQUIRES_PREPARE_VM = "orchestratorrequirespreparevm";
161162
public static final String OVERRIDE_DISK_OFFERING_ID = "overridediskofferingid";
162163
public static final String DISK_KBS_READ = "diskkbsread";
163164
public static final String DISK_KBS_WRITE = "diskkbswrite";

api/src/main/java/org/apache/cloudstack/api/response/ExtensionResponse.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717

1818
package org.apache.cloudstack.api.response;
1919

20-
import org.apache.cloudstack.extension.Extension;
21-
import com.cloud.serializer.Param;
22-
import com.google.gson.annotations.SerializedName;
20+
import java.util.Date;
21+
import java.util.List;
22+
import java.util.Map;
23+
2324
import org.apache.cloudstack.api.ApiConstants;
2425
import org.apache.cloudstack.api.BaseResponse;
2526
import org.apache.cloudstack.api.EntityReference;
27+
import org.apache.cloudstack.api.Parameter;
28+
import org.apache.cloudstack.extension.Extension;
2629

27-
import java.util.Date;
28-
import java.util.List;
29-
import java.util.Map;
30+
import com.cloud.serializer.Param;
31+
import com.google.gson.annotations.SerializedName;
3032

3133
@EntityReference(value = Extension.class)
3234
public class ExtensionResponse extends BaseResponse {
@@ -59,6 +61,10 @@ public class ExtensionResponse extends BaseResponse {
5961
@Param(description = "True if the extension is added by admin")
6062
private Boolean userDefined;
6163

64+
@SerializedName(ApiConstants.ORCHESTRATOR_REQUIRES_PREPARE_VM)
65+
@Parameter(description = "Only honored when type is Orchestrator. Whether prepare VM is needed or not")
66+
private Boolean orchestratorRequiresPrepareVm;
67+
6268
@SerializedName(ApiConstants.STATE)
6369
@Param(description = "The state of the extension")
6470
private String state;
@@ -98,6 +104,10 @@ public void setUserDefined(Boolean userDefined) {
98104
this.userDefined = userDefined;
99105
}
100106

107+
public void setOrchestratorRequiresPrepareVm(Boolean orchestratorRequiresPrepareVm) {
108+
this.orchestratorRequiresPrepareVm = orchestratorRequiresPrepareVm;
109+
}
110+
101111
public void setState(String state) {
102112
this.state = state;
103113
}

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
import org.apache.cloudstack.framework.ca.Certificate;
7272
import org.apache.cloudstack.framework.config.ConfigKey;
7373
import org.apache.cloudstack.framework.config.Configurable;
74+
import org.apache.cloudstack.framework.extensions.dao.ExtensionDetailsDao;
7475
import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager;
76+
import org.apache.cloudstack.framework.extensions.vo.ExtensionDetailsVO;
7577
import org.apache.cloudstack.framework.jobs.AsyncJob;
7678
import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext;
7779
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
@@ -442,6 +444,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
442444
private VolumeDataFactory volumeDataFactory;
443445
@Inject
444446
ExtensionsManager extensionsManager;
447+
@Inject
448+
ExtensionDetailsDao extensionDetailsDao;
445449

446450

447451
VmWorkJobHandlerProxy _jobHandlerProxy = new VmWorkJobHandlerProxy(this);
@@ -1163,68 +1167,125 @@ protected void updateVmMetadataManufacturerAndProduct(VirtualMachineTO vmTO, VMI
11631167
vmTO.setMetadataProductName(metadataProduct);
11641168
}
11651169

1166-
protected void updateExternalVmPrepareAnswer(VirtualMachineTO vmTO, VirtualMachineTO updatedTO) {
1167-
if (updatedTO == null) {
1170+
protected void updateExternalVmDetailsFromPrepareAnswer(VirtualMachineTO vmTO, UserVmVO userVmVO,
1171+
Map<String, String> newDetails) {
1172+
if (newDetails != null || newDetails.equals(vmTO.getDetails())) {
1173+
return;
1174+
}
1175+
vmTO.setDetails(newDetails);
1176+
userVmVO.setDetails(newDetails);
1177+
_userVmDao.saveDetails(userVmVO);
1178+
}
1179+
1180+
protected void updateExternalVmDataFromPrepareAnswer(VirtualMachineTO vmTO, VirtualMachineTO updatedTO) {
1181+
final String vncPassword = updatedTO.getVncPassword();
1182+
final Map<String, String> details = updatedTO.getDetails();
1183+
if ((vncPassword == null || vncPassword.equals(vmTO.getVncPassword())) &&
1184+
(details == null || details.equals(vmTO.getDetails()))) {
1185+
return;
1186+
}
1187+
UserVmVO userVmVO = _userVmDao.findById(vmTO.getId());
1188+
if (userVmVO == null) {
1189+
return;
1190+
}
1191+
if (vncPassword != null && !vncPassword.equals(userVmVO.getPassword())) {
1192+
userVmVO.setVncPassword(vncPassword);
1193+
vmTO.setVncPassword(vncPassword);
1194+
}
1195+
updateExternalVmDetailsFromPrepareAnswer(vmTO, userVmVO, updatedTO.getDetails());
1196+
}
1197+
1198+
protected void updateExternalVmNicsFromPrepareAnswer(VirtualMachineTO vmTO, VirtualMachineTO updatedTO) {
1199+
if (ObjectUtils.anyNull(vmTO.getNics(), updatedTO.getNics())) {
11681200
return;
11691201
}
11701202
Map<String, NicTO> originalNicsByUuid = new HashMap<>();
11711203
for (NicTO nic : vmTO.getNics()) {
1172-
originalNicsByUuid.put(nic.getUuid(), nic);
1204+
originalNicsByUuid.put(nic.getNicUuid(), nic);
11731205
}
11741206
for (NicTO updatedNicTO : updatedTO.getNics()) {
1175-
if (StringUtils.isNotBlank(updatedNicTO.getMac())) {
1176-
NicVO nicVO = _nicsDao.findByUuid(updatedNicTO.getUuid());
1177-
if (nicVO == null || Objects.equals(nicVO.getMacAddress(), updatedNicTO.getMac())) {
1178-
continue;
1179-
}
1180-
nicVO.setMacAddress(updatedNicTO.getMac());
1181-
_nicsDao.update(nicVO.getId(), nicVO);
1182-
NicTO originalNicTO = originalNicsByUuid.get(updatedNicTO.getUuid());
1183-
if (originalNicTO != null) {
1184-
originalNicTO.setMac(updatedNicTO.getMac());
1185-
}
1207+
final String nicUuid = updatedNicTO.getNicUuid();
1208+
NicTO originalNicTO = originalNicsByUuid.get(nicUuid);
1209+
if (originalNicTO == null) {
1210+
continue;
11861211
}
1212+
final String mac = updatedNicTO.getMac();
1213+
final String ip4 = updatedNicTO.getIp();
1214+
final String ip6 = updatedNicTO.getIp6Address();
1215+
if (Objects.equals(mac, originalNicTO.getMac()) &&
1216+
Objects.equals(ip4, originalNicTO.getIp()) &&
1217+
Objects.equals(ip6, originalNicTO.getIp6Address())) {
1218+
continue;
1219+
}
1220+
NicVO nicVO = _nicsDao.findByUuid(nicUuid);
1221+
if (nicVO == null) {
1222+
continue;
1223+
}
1224+
logger.debug("Updating {} during External VM preparation", nicVO);
1225+
if (ip4 != null && !ip4.equals(nicVO.getIPv4Address())) {
1226+
nicVO.setIPv4Address(ip4);
1227+
originalNicTO.setIp(ip4);
1228+
}
1229+
if (ip6 != null && !ip6.equals(nicVO.getIPv6Address())) {
1230+
nicVO.setIPv6Address(ip6);
1231+
originalNicTO.setIp6Address(ip6);
1232+
}
1233+
if (mac != null && !mac.equals(nicVO.getMacAddress())) {
1234+
nicVO.setMacAddress(mac);
1235+
originalNicTO.setMac(mac);
1236+
}
1237+
_nicsDao.update(nicVO.getId(), nicVO);
1238+
}
1239+
}
1240+
1241+
protected void updateExternalVmFromPrepareAnswer(VirtualMachineTO vmTO, VirtualMachineTO updatedTO) {
1242+
if (updatedTO == null) {
1243+
return;
11871244
}
1245+
updateExternalVmDataFromPrepareAnswer(vmTO, updatedTO);
1246+
updateExternalVmNicsFromPrepareAnswer(vmTO, updatedTO);
11881247
}
11891248

1190-
@SuppressWarnings("unchecked")
1191-
protected void processPrepareExternalProvisioning(Host host, VirtualMachineTO virtualMachineTO) {
1192-
if (host == null || !HypervisorType.External.equals(host.getHypervisorType()) || host.getName() != null) {
1249+
protected void processPrepareExternalProvisioning(Host host, VirtualMachineTO virtualMachineTO,
1250+
VirtualMachineTemplate template) {
1251+
if (host == null || !HypervisorType.External.equals(host.getHypervisorType()) ||
1252+
template.getExtensionId() == null) {
1253+
return;
1254+
}
1255+
ExtensionDetailsVO detailsVO = extensionDetailsDao.findDetail(template.getExtensionId(),
1256+
ApiConstants.ORCHESTRATOR_REQUIRES_PREPARE_VM);
1257+
if (detailsVO == null || !Boolean.parseBoolean(detailsVO.getValue())) {
11931258
return;
11941259
}
11951260
Map<String, String> vmDetails = virtualMachineTO.getExternalDetails();
11961261
Map<String, Object> externalDetails = extensionsManager.getExternalAccessDetails(host,
11971262
vmDetails);
1198-
Map<String, String> extensionDetails = (Map<String, String>)externalDetails.get(ApiConstants.EXTENSION);
1199-
Map<String, String> resourceMapDetails = (Map<String, String>)externalDetails.get(ApiConstants.RESOURCE_MAP);
1200-
Map<String, String> hostDetails = (Map<String, String>)externalDetails.get(ApiConstants.EXTENSION);
1201-
boolean shouldPrepareVm =
1202-
Boolean.parseBoolean(extensionDetails.get(ApiConstants.PREPARE_VM)) ||
1203-
Boolean.parseBoolean(resourceMapDetails.get(ApiConstants.PREPARE_VM)) ||
1204-
Boolean.parseBoolean(hostDetails.get(ApiConstants.PREPARE_VM));
1205-
if (!shouldPrepareVm) {
1206-
return;
1207-
}
12081263
PrepareExternalProvisioningCommand cmd = new PrepareExternalProvisioningCommand(virtualMachineTO);
12091264
cmd.setExternalDetails(externalDetails);
12101265
Answer answer = null;
1266+
CloudRuntimeException cre = new CloudRuntimeException("Failed to prepare VM");
12111267
try {
12121268
answer = _agentMgr.send(host.getId(), cmd);
12131269
} catch (AgentUnavailableException | OperationTimedoutException e) {
12141270
logger.error("Failed PrepareExternalProvisioningCommand due to : {}", e.getMessage(), e);
1215-
return;
1271+
throw cre;
12161272
}
12171273
if (answer == null) {
12181274
logger.error("Invalid answer received for PrepareExternalProvisioningCommand");
1219-
return;
1275+
throw cre;
12201276
}
12211277
if (!(answer instanceof PrepareExternalProvisioningAnswer)) {
12221278
logger.error("Unexpected answer received for PrepareExternalProvisioningCommand: [result: {}, details: {}]",
12231279
answer.getResult(), answer.getDetails());
1224-
return;
1280+
throw cre;
12251281
}
12261282
PrepareExternalProvisioningAnswer prepareAnswer = (PrepareExternalProvisioningAnswer)answer;
1227-
updateExternalVmPrepareAnswer(virtualMachineTO, prepareAnswer.getVirtualMachineTO());
1283+
if (!prepareAnswer.getResult()) {
1284+
logger.error("Unexpected answer received for PrepareExternalProvisioningCommand: [result: {}, details: {}]",
1285+
answer.getResult(), answer.getDetails());
1286+
throw cre;
1287+
}
1288+
updateExternalVmFromPrepareAnswer(virtualMachineTO, prepareAnswer.getVirtualMachineTO());
12281289
}
12291290

12301291
@Override
@@ -1400,7 +1461,7 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14001461
handlePath(vmTO.getDisks(), vm.getHypervisorType());
14011462
setVmNetworkDetails(vm, vmTO);
14021463

1403-
processPrepareExternalProvisioning(dest.getHost(), vmTO);
1464+
processPrepareExternalProvisioning(dest.getHost(), vmTO, template);
14041465

14051466
Commands cmds = new Commands(Command.OnError.Stop);
14061467
final Map<String, String> sshAccessDetails = _networkMgr.getSystemVMAccessDetails(vm);

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.apache.cloudstack.framework.config.ConfigKey.Scope;
5353
import org.apache.cloudstack.framework.config.Configurable;
5454
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
55-
import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager;
5655
import org.apache.cloudstack.framework.messagebus.MessageBus;
5756
import org.apache.cloudstack.framework.messagebus.PublishScope;
5857
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
@@ -126,9 +125,7 @@
126125
import com.cloud.host.HostVO;
127126
import com.cloud.host.Status;
128127
import com.cloud.host.dao.HostDao;
129-
import com.cloud.host.dao.HostDetailsDao;
130128
import com.cloud.hypervisor.Hypervisor.HypervisorType;
131-
import com.cloud.hypervisor.HypervisorGuruManager;
132129
import com.cloud.network.IpAddress;
133130
import com.cloud.network.IpAddressManager;
134131
import com.cloud.network.Ipv6Service;
@@ -265,7 +262,6 @@
265262
import com.cloud.vm.dao.NicSecondaryIpDao;
266263
import com.cloud.vm.dao.NicSecondaryIpVO;
267264
import com.cloud.vm.dao.UserVmDao;
268-
import com.cloud.vm.dao.UserVmDetailsDao;
269265
import com.cloud.vm.dao.VMInstanceDao;
270266
import com.googlecode.ipv6.IPv6Address;
271267

@@ -289,8 +285,6 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra
289285
@Inject
290286
UserVmDao _userVmDao;
291287
@Inject
292-
UserVmDetailsDao userVmDetailsDao;
293-
@Inject
294288
AlertManager _alertMgr;
295289
@Inject
296290
ConfigurationManager _configMgr;
@@ -364,10 +358,6 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra
364358
private ASNumberDao asNumberDao;
365359
@Inject
366360
private BGPService bgpService;
367-
@Inject
368-
private HypervisorGuruManager hvGuruMgr;
369-
@Inject
370-
ExtensionsManager extensionsManager;
371361

372362
@Override
373363
public List<NetworkGuru> getNetworkGurus() {
@@ -435,8 +425,6 @@ public void setDhcpProviders(final List<DhcpServiceProvider> dhcpProviders) {
435425
@Inject
436426
HostDao _hostDao;
437427
@Inject
438-
HostDetailsDao hostDetailsDao;
439-
@Inject
440428
NetworkServiceMapDao _ntwkSrvcDao;
441429
@Inject
442430
VpcManager _vpcMgr;
@@ -2160,8 +2148,6 @@ public NicProfile prepareNic(final VirtualMachineProfile vmProfile, final Deploy
21602148

21612149
final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vmProfile.getId());
21622150
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
2163-
2164-
prepareNicIfExternalProvisionerInvolved(vmProfile, dest, nicId);
21652151
final NicVO nic = _nicDao.findById(nicId);
21662152

21672153
NicProfile profile = null;
@@ -2230,68 +2216,6 @@ public NicProfile prepareNic(final VirtualMachineProfile vmProfile, final Deploy
22302216
return profile;
22312217
}
22322218

2233-
private void prepareNicIfExternalProvisionerInvolved(VirtualMachineProfile vmProfile, DeployDestination dest, long nicId) {
2234-
logger.debug("SimpleEx {}, {}, {}", vmProfile.getId(), dest, nicId);
2235-
// if (!Hypervisor.HypervisorType.External.equals(vmProfile.getHypervisorType())) {
2236-
// return;
2237-
// }
2238-
// if (userVmDetailsDao.findDetail(vmProfile.getId(), VmDetailConstants.DEPLOY_VM) == null) {
2239-
// return;
2240-
// }
2241-
// HypervisorGuru hvGuru = hvGuruMgr.getGuru(vmProfile.getHypervisorType());
2242-
// VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
2243-
//
2244-
// HostVO host = _hostDao.findById(dest.getHost().getId());
2245-
// PrepareExternalProvisioningCommand command = new PrepareExternalProvisioningCommand(vmTO, host.getClusterId());
2246-
// Map<String, Object> externalDetails = extensionsManager.getExternalAccessDetails(host, vmTO.getExternalDetails());
2247-
// command.setExternalDetails(externalDetails);
2248-
// final PrepareExternalProvisioningAnswer prepareExternalProvisioningAnswer;
2249-
// try {
2250-
// Long hostID = dest.getHost().getId();
2251-
// final Answer answer = _agentMgr.send(hostID, command);
2252-
//
2253-
// if (!(answer instanceof PrepareExternalProvisioningAnswer)) {
2254-
// String errorMsg = String.format("Trying to prepare the instance on external hypervisor for the CloudStack instance %s failed: %s", vmProfile.getUuid(), answer.getDetails());
2255-
// logger.debug(errorMsg);
2256-
// throw new CloudRuntimeException(errorMsg);
2257-
// }
2258-
//
2259-
// prepareExternalProvisioningAnswer = (PrepareExternalProvisioningAnswer) answer;
2260-
// } catch (AgentUnavailableException | OperationTimedoutException e) {
2261-
// String errorMsg = String.format("Trying to prepare the instance on external hypervisor for the CloudStack instance %s failed: %s", vmProfile.getUuid(), e);
2262-
// logger.debug(errorMsg);
2263-
// throw new CloudRuntimeException(errorMsg);
2264-
// }
2265-
//
2266-
// if (prepareExternalProvisioningAnswer == null || !prepareExternalProvisioningAnswer.getResult()) {
2267-
// if (prepareExternalProvisioningAnswer != null && StringUtils.isNotBlank(prepareExternalProvisioningAnswer.getDetails())) {
2268-
// throw new CloudRuntimeException(String.format("Unable to prepare the instance on external system due to %s", prepareExternalProvisioningAnswer.getDetails()));
2269-
// } else {
2270-
// throw new CloudRuntimeException("Unable to prepare the instance on external system, please check the access details");
2271-
// }
2272-
// }
2273-
//
2274-
// Map<String, String> serverDetails = prepareExternalProvisioningAnswer.getServerDetails();
2275-
// if (ExternalAgentManagerImpl.expectMacAddressFromExternalProvisioner.valueIn(host.getClusterId())) {
2276-
// String macAddress = serverDetails.get(VmDetailConstants.MAC_ADDRESS);
2277-
// if (StringUtils.isEmpty(macAddress)) {
2278-
// throw new CloudRuntimeException("Unable to fetch macaddress from the external provisioner while preparing the instance");
2279-
// }
2280-
// final NicVO nic = _nicDao.findById(nicId);
2281-
// nic.setMacAddress(macAddress);
2282-
// _nicDao.update(nicId, nic);
2283-
// }
2284-
//
2285-
// if (MapUtils.isNotEmpty(serverDetails)) {
2286-
// UserVmVO userVm = _userVmDao.findById(vmProfile.getId());
2287-
// _userVmDao.loadDetails(userVm);
2288-
// Map<String, String> details = userVm.getDetails();
2289-
// details.putAll(serverDetails);
2290-
// userVm.setDetails(details);
2291-
// _userVmDao.saveDetails(userVm);
2292-
// }
2293-
}
2294-
22952219
@Override
22962220
public Map<Integer, String> getExtraDhcpOptions(long nicId) {
22972221
List<NicExtraDhcpOptionVO> nicExtraDhcpOptionVOList = _nicExtraDhcpOptionDao.listByNicId(nicId);

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/api/CreateExtensionCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public class CreateExtensionCmd extends BaseCmd {
7070
description = "Relative path for entry point for extension")
7171
private String entryPoint;
7272

73+
@Parameter(name = ApiConstants.ORCHESTRATOR_REQUIRES_PREPARE_VM,
74+
type = CommandType.BOOLEAN,
75+
description = "Only honored when type is Orchestrator. Whether prepare VM is needed or not")
76+
private Boolean orchestratorRequiresPrepareVm;
77+
7378
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING,
7479
description = "State of the extension")
7580
private String state;
@@ -98,6 +103,10 @@ public String getEntryPoint() {
98103
return entryPoint;
99104
}
100105

106+
public Boolean isOrchestratorRequiresPrepareVm() {
107+
return orchestratorRequiresPrepareVm;
108+
}
109+
101110
public String getState() {
102111
return state;
103112
}

0 commit comments

Comments
 (0)