Skip to content

Commit a3f2b16

Browse files
author
Anthony Xu
committed
XS VMmetadata platform contains virtual platform related info, CS should save platform info in DB to migrate the VM to other Cluster
1 parent f0df405 commit a3f2b16

7 files changed

Lines changed: 132 additions & 124 deletions

File tree

‎core/src/com/cloud/agent/api/StopAnswer.java‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,29 @@
1818

1919
public class StopAnswer extends RebootAnswer {
2020

21-
private String hypervisortoolsversion;
22-
Integer timeOffset;
21+
private String platform;
2322

2423
protected StopAnswer() {
2524
}
2625

27-
public StopAnswer(StopCommand cmd, String details, String hypervisortoolsversion, Integer timeOffset, boolean success) {
26+
public StopAnswer(StopCommand cmd, String details, String platform, boolean success) {
2827
super(cmd, details, success);
29-
this.hypervisortoolsversion = hypervisortoolsversion;
30-
this.timeOffset = timeOffset;
28+
this.platform = platform;
3129
}
3230

3331
public StopAnswer(StopCommand cmd, String details, boolean success) {
3432
super(cmd, details, success);
35-
this.hypervisortoolsversion = null;
36-
this.timeOffset = null;
33+
this.platform = null;
3734
}
3835

3936

4037
public StopAnswer(StopCommand cmd, Exception e) {
4138
super(cmd, e);
42-
this.hypervisortoolsversion = null;
43-
this.timeOffset = null;
39+
this.platform = null;
4440
}
4541

46-
public String getHypervisorToolsVersion() {
47-
return hypervisortoolsversion;
42+
public String getPlatform() {
43+
return platform;
4844
}
4945

50-
public Integer getTimeOffset() {
51-
return timeOffset;
52-
}
5346
}

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
import com.cloud.utils.StringUtils;
214214
import com.cloud.utils.exception.CloudRuntimeException;
215215
import com.cloud.utils.net.NetUtils;
216+
import com.cloud.utils.StringUtils;
216217
import com.cloud.vm.DiskProfile;
217218
import com.cloud.vm.VirtualMachine;
218219
import com.cloud.vm.VirtualMachine.State;
@@ -1298,23 +1299,6 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
12981299
vmr.VCPUsMax = 32L;
12991300
}
13001301

1301-
Map<String, String> details = vmSpec.getDetails();
1302-
if ( details != null ) {
1303-
String timeoffset = details.get("timeoffset");
1304-
if (timeoffset != null) {
1305-
Map<String, String> platform = vmr.platform;
1306-
platform.put("timeoffset", timeoffset);
1307-
vmr.platform = platform;
1308-
}
1309-
1310-
String coresPerSocket = details.get("cpu.corespersocket");
1311-
if (coresPerSocket != null) {
1312-
Map<String, String> platform = vmr.platform;
1313-
platform.put("cores-per-socket", coresPerSocket);
1314-
vmr.platform = platform;
1315-
}
1316-
}
1317-
13181302
vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
13191303
vmr.consoles.clear();
13201304

@@ -1399,9 +1383,40 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
13991383

14001384

14011385
protected void finalizeVmMetaData(VM vm, Connection conn, VirtualMachineTO vmSpec) throws Exception {
1386+
1387+
Map<String, String> details = vmSpec.getDetails();
1388+
if ( details != null ) {
1389+
String platformstring = details.get("platform");
1390+
if (platformstring != null && !platformstring.isEmpty() ) {
1391+
Map<String, String> platform = StringUtils.stringToMap(platformstring);
1392+
vm.setPlatform(conn, platform);
1393+
} else {
1394+
String timeoffset = details.get("timeoffset");
1395+
if (timeoffset != null) {
1396+
Map<String, String> platform = vm.getPlatform(conn);
1397+
platform.put("timeoffset", timeoffset);
1398+
vm.setPlatform(conn, platform);
1399+
}
1400+
String coresPerSocket = details.get("cpu.corespersocket");
1401+
if (coresPerSocket != null) {
1402+
Map<String, String> platform = vm.getPlatform(conn);
1403+
platform.put("cores-per-socket", coresPerSocket);
1404+
vm.setPlatform(conn, platform);
1405+
}
1406+
1407+
String xentoolsversion = details.get("hypervisortoolsversion");
1408+
if ( xentoolsversion == null || !xentoolsversion.equalsIgnoreCase("xenserver61") ) {
1409+
Map<String, String> platform = vm.getPlatform(conn);
1410+
platform.remove("device_id");
1411+
vm.setPlatform(conn, platform);
1412+
}
1413+
1414+
}
1415+
}
14021416
}
14031417

14041418

1419+
14051420
protected String handleVmStartFailure(Connection conn, String vmName, VM vm, String message, Throwable th) {
14061421
String msg = "Unable to start " + vmName + " due to " + message;
14071422
s_logger.warn(msg, th);
@@ -2957,7 +2972,7 @@ protected HashMap<String, Ternary<String, State, String>> getAllVms(Connection c
29572972
s_logger.trace("VM " + record.nameLabel + ": powerstate = " + ps + "; vm state=" + state.toString());
29582973
}
29592974
Host host = record.residentOn;
2960-
String xstoolsversion = getVMXenToolsVersion(record.platform);
2975+
String platformstring = StringUtils.mapToString(record.platform);
29612976
String host_uuid = null;
29622977
if( ! isRefNull(host) ) {
29632978
try {
@@ -2969,7 +2984,7 @@ protected HashMap<String, Ternary<String, State, String>> getAllVms(Connection c
29692984
} catch (XmlRpcException e) {
29702985
s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
29712986
}
2972-
vmStates.put(record.nameLabel, new Ternary<String, State, String>(host_uuid, state, xstoolsversion));
2987+
vmStates.put(record.nameLabel, new Ternary<String, State, String>(host_uuid, state, platformstring));
29732988
}
29742989
}
29752990

@@ -3974,6 +3989,7 @@ protected String callHostPluginAsync(Connection conn, String plugin, String cmd,
39743989
@Override
39753990
public StopAnswer execute(StopCommand cmd) {
39763991
String vmName = cmd.getVmName();
3992+
String platformstring = null;
39773993
try {
39783994
Connection conn = getConnection();
39793995
Set<VM> vms = VM.getByNameLabel(conn, vmName);
@@ -4003,7 +4019,7 @@ public StopAnswer execute(StopCommand cmd) {
40034019
}
40044020
for (VM vm : vms) {
40054021
VM.Record vmr = vm.getRecord(conn);
4006-
4022+
platformstring = StringUtils.mapToString(vmr.platform);
40074023
if (vmr.isControlDomain) {
40084024
String msg = "Tring to Shutdown control domain";
40094025
s_logger.warn(msg);
@@ -4013,7 +4029,7 @@ public StopAnswer execute(StopCommand cmd) {
40134029
if (vmr.powerState == VmPowerState.RUNNING && !isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(_host.uuid)) {
40144030
String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + _host.uuid + " but host:" + vmr.residentOn.getUuid(conn);
40154031
s_logger.warn(msg);
4016-
return new StopAnswer(cmd, msg, false);
4032+
return new StopAnswer(cmd, msg, platformstring, false);
40174033
}
40184034

40194035
State state = s_vms.getState(_cluster, vmName);
@@ -4042,21 +4058,11 @@ public StopAnswer execute(StopCommand cmd) {
40424058
} catch (Exception e) {
40434059
String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + cmd.getVmName() + " due to " + e.toString();
40444060
s_logger.debug(msg);
4045-
return new StopAnswer(cmd, msg, false);
4061+
return new StopAnswer(cmd, msg, platformstring, false);
40464062
} finally {
40474063

40484064
try {
40494065
if (vm.getPowerState(conn) == VmPowerState.HALTED) {
4050-
Map<String, String> platform = vm.getPlatform(conn);
4051-
Integer timeoffset = null;
4052-
try {
4053-
if (platform.containsKey("timeoffset")) {
4054-
timeoffset = Integer.valueOf(platform.get("timeoffset"));
4055-
}
4056-
} catch (NumberFormatException e) {
4057-
s_logger.error("Error while reading the platform:timeoffset field of the instance", e);
4058-
}
4059-
String xentoolsversion = getVMXenToolsVersion(platform);
40604066

40614067
Set<VIF> vifs = vm.getVIFs(conn);
40624068
List<Network> networks = new ArrayList<Network>();
@@ -4078,11 +4084,8 @@ public StopAnswer execute(StopCommand cmd) {
40784084
// network might be destroyed by other host
40794085
}
40804086
}
4081-
return new StopAnswer(cmd, "Stop VM " + vmName + " Succeed", xentoolsversion, timeoffset, true);
4087+
return new StopAnswer(cmd, "Stop VM " + vmName + " Succeed", platformstring ,true);
40824088
}
4083-
} catch (XenAPIException e) {
4084-
String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.toString();
4085-
s_logger.warn(msg, e);
40864089
} catch (Exception e) {
40874090
String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
40884091
s_logger.warn(msg, e);
@@ -4095,19 +4098,12 @@ public StopAnswer execute(StopCommand cmd) {
40954098
}
40964099
}
40974100

4098-
} catch (XenAPIException e) {
4101+
} catch (Exception e) {
40994102
String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
41004103
s_logger.warn(msg, e);
4101-
return new StopAnswer(cmd, msg, false);
4102-
} catch (XmlRpcException e) {
4103-
String msg = "Stop Vm " + vmName + " fail due to " + e.getMessage();
4104-
s_logger.warn(msg, e);
4105-
return new StopAnswer(cmd, msg, false);
4106-
} catch (Exception e) {
4107-
s_logger.warn("Unable to stop " + vmName + " due to ", e);
4108-
return new StopAnswer(cmd, e);
4104+
return new StopAnswer(cmd, msg, platformstring, false);
41094105
}
4110-
return new StopAnswer(cmd, "Stop VM failed", false);
4106+
return new StopAnswer(cmd, "Stop VM failed", platformstring, false);
41114107
}
41124108

41134109

@@ -8132,13 +8128,12 @@ protected HashMap<String, Ternary<String, State, String>> fullClusterSync(Connec
81328128
String vm_name = record.nameLabel;
81338129
VmPowerState ps = record.powerState;
81348130
final State state = convertToState(ps);
8135-
String xstoolsversion = getVMXenToolsVersion(record.platform);
81368131
Host host = record.residentOn;
81378132
String host_uuid = null;
81388133
if( ! isRefNull(host) ) {
81398134
host_uuid = host.getUuid(conn);
81408135
synchronized (_cluster.intern()) {
8141-
s_vms.put(_cluster, host_uuid, vm_name, state, xstoolsversion);
8136+
s_vms.put(_cluster, host_uuid, vm_name, state, null);
81428137
}
81438138
}
81448139
if (s_logger.isTraceEnabled()) {
@@ -8169,30 +8164,30 @@ protected HashMap<String, Ternary<String, State, String>> deltaClusterSync(Conne
81698164

81708165
for (final Map.Entry<String, Ternary<String, State, String>> entry : newStates.entrySet()) {
81718166
final String vm = entry.getKey();
8172-
String xstoolsversion = entry.getValue().third();
8167+
String platform = entry.getValue().third();
81738168
State newState = entry.getValue().second();
81748169
String host_uuid = entry.getValue().first();
81758170
final Ternary<String, State, String> oldState = oldStates.remove(vm);
81768171

8177-
// check if xstoolsversion changed
8178-
if (xstoolsversion != null && oldState != null){
8179-
if (xstoolsversion != oldState.third() && newState != State.Stopped && newState != State.Stopping){
8180-
s_logger.warn("Detecting a change in xstoolsversion for " + vm);
8181-
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, xstoolsversion));
8172+
// check if platform changed
8173+
if (platform != null && oldState != null){
8174+
if (!platform.equals(oldState.third()) && newState != State.Stopped && newState != State.Stopping){
8175+
s_logger.warn("Detecting a change in platform for " + vm);
8176+
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, platform));
81828177

81838178
s_logger.debug("11. The VM " + vm + " is in " + newState + " state");
8184-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8179+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
81858180
continue;
81868181
}
81878182
}
81888183
//check if host is changed
81898184
if (host_uuid != null && oldState != null){
81908185
if (!host_uuid.equals(oldState.first()) && newState != State.Stopped && newState != State.Stopping){
81918186
s_logger.warn("Detecting a change in host for " + vm);
8192-
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, null));
8187+
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, platform));
81938188

81948189
s_logger.debug("11. The VM " + vm + " is in " + newState + " state");
8195-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8190+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
81968191
continue;
81978192
}
81988193
}
@@ -8211,31 +8206,31 @@ protected HashMap<String, Ternary<String, State, String>> deltaClusterSync(Conne
82118206
continue;
82128207
}
82138208
if (oldState == null) {
8214-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8209+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
82158210
s_logger.warn("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm);
8216-
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, null));
8211+
changes.put(vm, new Ternary<String, State, String>(host_uuid, newState, platform));
82178212
} else if (oldState.second() == State.Starting) {
82188213
if (newState == State.Running) {
82198214
s_logger.debug("12. The VM " + vm + " is in " + State.Running + " state");
8220-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8215+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
82218216
} else if (newState == State.Stopped) {
82228217
s_logger.warn("Ignoring vm " + vm + " because of a lag in starting the vm.");
82238218
}
82248219
} else if (oldState.second() == State.Migrating) {
82258220
if (newState == State.Running) {
82268221
s_logger.debug("Detected that an migrating VM is now running: " + vm);
8227-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8222+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
82288223
}
82298224
} else if (oldState.second() == State.Stopping) {
82308225
if (newState == State.Stopped) {
82318226
s_logger.debug("13. The VM " + vm + " is in " + State.Stopped + " state");
8232-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8227+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
82338228
} else if (newState == State.Running) {
82348229
s_logger.warn("Ignoring vm " + vm + " because of a lag in stopping the vm. ");
82358230
}
82368231
} else if (oldState.second() != newState) {
82378232
s_logger.debug("14. The VM " + vm + " is in " + newState + " state was " + oldState.second());
8238-
s_vms.put(_cluster, host_uuid, vm, newState, xstoolsversion);
8233+
s_vms.put(_cluster, host_uuid, vm, newState, platform);
82398234
if (newState == State.Stopped) {
82408235
/*
82418236
* if (s_vmsKilled.remove(vm)) { s_logger.debug("VM " + vm + " has been killed for storage. ");

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer610Resource.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,4 @@ protected String getVMXenToolsVersion(Map<String, String> platform) {
462462
return "xenserver56";
463463
}
464464

465-
@Override
466-
protected void finalizeVmMetaData(VM vm, Connection conn, VirtualMachineTO vmSpec) throws Exception {
467-
Map<String, String> details = vmSpec.getDetails();
468-
if ( details!= null ) {
469-
String xentoolsversion = details.get("hypervisortoolsversion");
470-
if ( xentoolsversion == null || !xentoolsversion.equalsIgnoreCase("xenserver61") ) {
471-
Map<String, String> platform = vm.getPlatform(conn);
472-
platform.remove("device_id");
473-
vm.setPlatform(conn, platform);
474-
}
475-
}
476-
}
477-
478465
}

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerPoolVms.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public State getState(String clusterId, String name){
5252
}
5353

5454

55-
public void put(String clusterId, String hostUuid, String name, State state, String xstoolsversion){
55+
public void put(String clusterId, String hostUuid, String name, State state, String platform){
5656
HashMap<String, Ternary<String, State, String>> vms= getClusterVmState(clusterId);
57-
vms.put(name, new Ternary<String, State, String>(hostUuid, state, xstoolsversion));
57+
vms.put(name, new Ternary<String, State, String>(hostUuid, state, platform));
5858
}
5959

6060

‎server/src/com/cloud/template/TemplateManagerImpl.java‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Date;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.HashMap;
2425
import java.util.UUID;
2526
import java.util.concurrent.ExecutionException;
2627
import java.util.concurrent.ExecutorService;
@@ -181,6 +182,7 @@
181182
import com.cloud.vm.VMInstanceVO;
182183
import com.cloud.vm.VirtualMachine.State;
183184
import com.cloud.vm.dao.UserVmDao;
185+
import com.cloud.vm.dao.UserVmDetailsDao;
184186
import com.cloud.vm.dao.VMInstanceDao;
185187

186188
@Component
@@ -196,6 +198,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
196198
@Inject
197199
VMTemplateZoneDao _tmpltZoneDao;
198200
@Inject
201+
protected UserVmDetailsDao _vmDetailsDao;
202+
@Inject
199203
protected VMTemplateDetailsDao _templateDetailsDao;
200204
@Inject
201205
VMInstanceDao _vmInstanceDao;
@@ -1634,8 +1638,21 @@ public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd, Account t
16341638
VMTemplateVO template = this._tmpltDao.persist(privateTemplate);
16351639
// Increment the number of templates
16361640
if (template != null) {
1637-
if (cmd.getDetails() != null) {
1638-
this._templateDetailsDao.persist(template.getId(), cmd.getDetails());
1641+
Map<String, String> details = new HashMap<String, String>();
1642+
if ( volume != null ) {
1643+
Long vmId = volume.getInstanceId();
1644+
if ( vmId != null ) {
1645+
Map<String, String> vmdetails = _vmDetailsDao.findDetails(vmId);
1646+
if ( vmdetails != null ) {
1647+
details.putAll(vmdetails);
1648+
}
1649+
}
1650+
}
1651+
if(cmd.getDetails() != null) {
1652+
details.putAll(cmd.getDetails());
1653+
}
1654+
if( !details.isEmpty()) {
1655+
this._templateDetailsDao.persist(template.getId(), details);
16391656
}
16401657

16411658
_resourceLimitMgr.incrementResourceCount(templateOwner.getId(), ResourceType.template);

0 commit comments

Comments
 (0)