Skip to content

Commit b4f43de

Browse files
committed
Allow import from VMWare via another host in the cluster
1 parent 0602f46 commit b4f43de

File tree

7 files changed

+688
-90
lines changed

7 files changed

+688
-90
lines changed

core/src/main/java/com/cloud/agent/api/ConvertInstanceCommand.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class ConvertInstanceCommand extends Command {
2626

2727
private RemoteInstanceTO sourceInstance;
2828
private Hypervisor.HypervisorType destinationHypervisorType;
29-
private List<String> destinationStoragePools;
3029
private DataStoreTO conversionTemporaryLocation;
3130
private String templateDirOnConversionLocation;
3231
private boolean checkConversionSupport;
@@ -36,12 +35,10 @@ public class ConvertInstanceCommand extends Command {
3635
public ConvertInstanceCommand() {
3736
}
3837

39-
public ConvertInstanceCommand(RemoteInstanceTO sourceInstance, Hypervisor.HypervisorType destinationHypervisorType,
40-
List<String> destinationStoragePools, DataStoreTO conversionTemporaryLocation,
38+
public ConvertInstanceCommand(RemoteInstanceTO sourceInstance, Hypervisor.HypervisorType destinationHypervisorType, DataStoreTO conversionTemporaryLocation,
4139
String templateDirOnConversionLocation, boolean checkConversionSupport, boolean exportOvfToConversionLocation) {
4240
this.sourceInstance = sourceInstance;
4341
this.destinationHypervisorType = destinationHypervisorType;
44-
this.destinationStoragePools = destinationStoragePools;
4542
this.conversionTemporaryLocation = conversionTemporaryLocation;
4643
this.templateDirOnConversionLocation = templateDirOnConversionLocation;
4744
this.checkConversionSupport = checkConversionSupport;
@@ -56,10 +53,6 @@ public Hypervisor.HypervisorType getDestinationHypervisorType() {
5653
return destinationHypervisorType;
5754
}
5855

59-
public List<String> getDestinationStoragePools() {
60-
return destinationStoragePools;
61-
}
62-
6356
public DataStoreTO getConversionTemporaryLocation() {
6457
return conversionTemporaryLocation;
6558
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.agent.api;
18+
19+
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
20+
21+
public class ImportConvertedInstanceAnswer extends Answer {
22+
23+
public ImportConvertedInstanceAnswer() {
24+
super();
25+
}
26+
private UnmanagedInstanceTO convertedInstance;
27+
28+
public ImportConvertedInstanceAnswer(Command command, boolean success, String details) {
29+
super(command, success, details);
30+
}
31+
32+
public ImportConvertedInstanceAnswer(Command command, UnmanagedInstanceTO convertedInstance) {
33+
super(command, true, "");
34+
this.convertedInstance = convertedInstance;
35+
}
36+
37+
public UnmanagedInstanceTO getConvertedInstance() {
38+
return convertedInstance;
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.agent.api;
18+
19+
import com.cloud.agent.api.to.DataStoreTO;
20+
import com.cloud.agent.api.to.RemoteInstanceTO;
21+
import com.cloud.hypervisor.Hypervisor;
22+
23+
import java.util.List;
24+
25+
public class ImportConvertedInstanceCommand extends Command {
26+
27+
private RemoteInstanceTO sourceInstance;
28+
private List<String> destinationStoragePools;
29+
private DataStoreTO conversionTemporaryLocation;
30+
31+
public ImportConvertedInstanceCommand() {
32+
}
33+
34+
public ImportConvertedInstanceCommand(RemoteInstanceTO sourceInstance,
35+
List<String> destinationStoragePools, DataStoreTO conversionTemporaryLocation) {
36+
this.sourceInstance = sourceInstance;
37+
this.destinationStoragePools = destinationStoragePools;
38+
this.conversionTemporaryLocation = conversionTemporaryLocation;
39+
}
40+
41+
public RemoteInstanceTO getSourceInstance() {
42+
return sourceInstance;
43+
}
44+
45+
public List<String> getDestinationStoragePools() {
46+
return destinationStoragePools;
47+
}
48+
49+
public DataStoreTO getConversionTemporaryLocation() {
50+
return conversionTemporaryLocation;
51+
}
52+
53+
@Override
54+
public boolean executeInSequence() {
55+
return false;
56+
}
57+
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapper.java

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
7373
Hypervisor.HypervisorType sourceHypervisorType = sourceInstance.getHypervisorType();
7474
String sourceInstanceName = sourceInstance.getInstanceName();
7575
Hypervisor.HypervisorType destinationHypervisorType = cmd.getDestinationHypervisorType();
76-
List<String> destinationStoragePools = cmd.getDestinationStoragePools();
7776
DataStoreTO conversionTemporaryLocation = cmd.getConversionTemporaryLocation();
7877
long timeout = (long) cmd.getWait() * 1000;
7978

@@ -139,21 +138,7 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
139138
s_logger.error(err);
140139
return new ConvertInstanceAnswer(cmd, false, err);
141140
}
142-
String convertedBasePath = String.format("%s/%s", temporaryConvertPath, temporaryConvertUuid);
143-
LibvirtDomainXMLParser xmlParser = parseMigratedVMXmlDomain(convertedBasePath);
144-
145-
List<KVMPhysicalDisk> temporaryDisks = xmlParser == null ?
146-
getTemporaryDisksWithPrefixFromTemporaryPool(temporaryStoragePool, temporaryConvertPath, temporaryConvertUuid) :
147-
getTemporaryDisksFromParsedXml(temporaryStoragePool, xmlParser, convertedBasePath);
148-
149-
List<KVMPhysicalDisk> destinationDisks = moveTemporaryDisksToDestination(temporaryDisks,
150-
destinationStoragePools, storagePoolMgr);
151-
152-
cleanupDisksAndDomainFromTemporaryLocation(temporaryDisks, temporaryStoragePool, temporaryConvertUuid);
153-
154-
UnmanagedInstanceTO convertedInstanceTO = getConvertedUnmanagedInstance(temporaryConvertUuid,
155-
destinationDisks, xmlParser);
156-
return new ConvertInstanceAnswer(cmd, convertedInstanceTO);
141+
return new ConvertInstanceAnswer(cmd, false, null);
157142
} catch (Exception e) {
158143
String error = String.format("Error converting instance %s from %s, due to: %s",
159144
sourceInstanceName, sourceHypervisorType, e.getMessage());
@@ -165,10 +150,6 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
165150
s_logger.debug("Cleaning up exported OVA at dir " + sourceOVFDir);
166151
FileUtil.deletePath(sourceOVFDir);
167152
}
168-
if (conversionTemporaryLocation instanceof NfsTO) {
169-
s_logger.debug("Cleaning up secondary storage temporary location");
170-
storagePoolMgr.deleteStoragePool(temporaryStoragePool.getType(), temporaryStoragePool.getUuid());
171-
}
172153
}
173154
}
174155

@@ -247,17 +228,6 @@ protected List<KVMPhysicalDisk> getTemporaryDisksWithPrefixFromTemporaryPool(KVM
247228
return disksWithPrefix;
248229
}
249230

250-
private void cleanupDisksAndDomainFromTemporaryLocation(List<KVMPhysicalDisk> disks,
251-
KVMStoragePool temporaryStoragePool,
252-
String temporaryConvertUuid) {
253-
for (KVMPhysicalDisk disk : disks) {
254-
s_logger.info(String.format("Cleaning up temporary disk %s after conversion from temporary location", disk.getName()));
255-
temporaryStoragePool.deletePhysicalDisk(disk.getName(), Storage.ImageFormat.QCOW2);
256-
}
257-
s_logger.info(String.format("Cleaning up temporary domain %s after conversion from temporary location", temporaryConvertUuid));
258-
FileUtil.deleteFiles(temporaryStoragePool.getLocalPath(), temporaryConvertUuid, ".xml");
259-
}
260-
261231
protected void sanitizeDisksPath(List<LibvirtVMDef.DiskDef> disks) {
262232
for (LibvirtVMDef.DiskDef disk : disks) {
263233
String[] diskPathParts = disk.getDiskPath().split("/");
@@ -303,31 +273,6 @@ protected List<KVMPhysicalDisk> moveTemporaryDisksToDestination(List<KVMPhysical
303273
return targetDisks;
304274
}
305275

306-
private UnmanagedInstanceTO getConvertedUnmanagedInstance(String baseName,
307-
List<KVMPhysicalDisk> vmDisks,
308-
LibvirtDomainXMLParser xmlParser) {
309-
UnmanagedInstanceTO instanceTO = new UnmanagedInstanceTO();
310-
instanceTO.setName(baseName);
311-
instanceTO.setDisks(getUnmanagedInstanceDisks(vmDisks, xmlParser));
312-
instanceTO.setNics(getUnmanagedInstanceNics(xmlParser));
313-
return instanceTO;
314-
}
315-
316-
private List<UnmanagedInstanceTO.Nic> getUnmanagedInstanceNics(LibvirtDomainXMLParser xmlParser) {
317-
List<UnmanagedInstanceTO.Nic> nics = new ArrayList<>();
318-
if (xmlParser != null) {
319-
List<LibvirtVMDef.InterfaceDef> interfaces = xmlParser.getInterfaces();
320-
for (LibvirtVMDef.InterfaceDef interfaceDef : interfaces) {
321-
UnmanagedInstanceTO.Nic nic = new UnmanagedInstanceTO.Nic();
322-
nic.setMacAddress(interfaceDef.getMacAddress());
323-
nic.setNicId(interfaceDef.getBrName());
324-
nic.setAdapterType(interfaceDef.getModel().toString());
325-
nics.add(nic);
326-
}
327-
}
328-
return nics;
329-
}
330-
331276
protected List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<KVMPhysicalDisk> vmDisks, LibvirtDomainXMLParser xmlParser) {
332277
List<UnmanagedInstanceTO.Disk> instanceDisks = new ArrayList<>();
333278
List<LibvirtVMDef.DiskDef> diskDefs = xmlParser != null ? xmlParser.getDisks() : null;
@@ -416,27 +361,6 @@ protected boolean performInstanceConversion(String sourceOVFDirPath,
416361
return exitValue == 0;
417362
}
418363

419-
protected LibvirtDomainXMLParser parseMigratedVMXmlDomain(String installPath) throws IOException {
420-
String xmlPath = String.format("%s.xml", installPath);
421-
if (!new File(xmlPath).exists()) {
422-
String err = String.format("Conversion failed. Unable to find the converted XML domain, expected %s", xmlPath);
423-
s_logger.error(err);
424-
throw new CloudRuntimeException(err);
425-
}
426-
InputStream is = new BufferedInputStream(new FileInputStream(xmlPath));
427-
String xml = IOUtils.toString(is, Charset.defaultCharset());
428-
final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
429-
try {
430-
parser.parseDomainXML(xml);
431-
return parser;
432-
} catch (RuntimeException e) {
433-
String err = String.format("Error parsing the converted instance XML domain at %s: %s", xmlPath, e.getMessage());
434-
s_logger.error(err, e);
435-
s_logger.debug(xml);
436-
return null;
437-
}
438-
}
439-
440364
protected String encodeUsername(String username) {
441365
return URLEncoder.encode(username, Charset.defaultCharset());
442366
}

0 commit comments

Comments
 (0)