use of com.cloud.exception.InternalErrorException in project cloudstack by apache.
the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.
@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
final String templateInstallFolder = "template/tmpl/" + templateFolder;
final String tmplName = libvirtUtilitiesHelper.generateUUIDName();
final String tmplFileName = tmplName + ".qcow2";
KVMStoragePool secondaryPool = null;
KVMStoragePool snapshotPool = null;
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
snapshotPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl());
final KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
final StorageLayer storage = libvirtComputingResource.getStorage();
storage.mkdirs(templatePath);
final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
final String createTmplPath = libvirtComputingResource.createTmplPath();
final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
scriptCommand.add("-t", templatePath);
scriptCommand.add("-n", tmplFileName);
scriptCommand.add("-f", snapshot.getPath());
scriptCommand.execute();
final Processor qcow2Processor = libvirtUtilitiesHelper.buildQCOW2Processor(storage);
final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
loc.create(1, true, tmplName);
loc.addFormat(info);
loc.save();
return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
} catch (final ConfigurationException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final InternalErrorException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final IOException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final CloudRuntimeException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} finally {
if (secondaryPool != null) {
storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
if (snapshotPool != null) {
storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
}
}
}
use of com.cloud.exception.InternalErrorException in project cloudstack by apache.
the class LibvirtComputingResource method prepareNetworkElementCommand.
public ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
Connect conn;
try {
conn = LibvirtConnection.getConnectionByVmName(routerName);
final List<InterfaceDef> nics = getInterfaces(conn, routerName);
final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
Integer nicPos = 0;
for (final InterfaceDef nic : nics) {
if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
} else {
if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
} else {
final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
}
}
nicPos++;
}
final IpAddressTO[] ips = cmd.getIpAddresses();
int nicNum = 0;
for (final IpAddressTO ip : ips) {
boolean newNic = false;
if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
/* plug a vif into router */
VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
newNic = true;
}
nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
networkUsage(routerIp, "addVif", "eth" + nicNum);
ip.setNicDevId(nicNum);
ip.setNewNic(newNic);
}
return new ExecutionResult(true, null);
} catch (final LibvirtException e) {
s_logger.error("ipassoccmd failed", e);
return new ExecutionResult(false, e.getMessage());
} catch (final InternalErrorException e) {
s_logger.error("ipassoccmd failed", e);
return new ExecutionResult(false, e.getMessage());
}
}
use of com.cloud.exception.InternalErrorException in project cloudstack by apache.
the class LibvirtAttachIsoCommandWrapper method execute.
@Override
public Answer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach());
} catch (final LibvirtException e) {
return new Answer(command, false, e.toString());
} catch (final URISyntaxException e) {
return new Answer(command, false, e.toString());
} catch (final InternalErrorException e) {
return new Answer(command, false, e.toString());
}
return new Answer(command);
}
use of com.cloud.exception.InternalErrorException in project cloudstack by apache.
the class LibvirtComputingResource method rebootVM.
public String rebootVM(final Connect conn, final String vmName) {
Domain dm = null;
String msg = null;
try {
dm = conn.domainLookupByName(vmName);
// Get XML Dump including the secure information such as VNC password
// By passing 1, or VIR_DOMAIN_XML_SECURE flag
// https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags
String vmDef = dm.getXMLDesc(1);
final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
parser.parseDomainXML(vmDef);
for (final InterfaceDef nic : parser.getInterfaces()) {
if (nic.getNetType() == GuestNetType.BRIDGE && nic.getBrName().startsWith("cloudVirBr")) {
try {
final int vnetId = Integer.parseInt(nic.getBrName().replaceFirst("cloudVirBr", ""));
final String pifName = getPif(_guestBridgeName);
final String newBrName = "br" + pifName + "-" + vnetId;
vmDef = vmDef.replaceAll("'" + nic.getBrName() + "'", "'" + newBrName + "'");
s_logger.debug("VM bridge name is changed from " + nic.getBrName() + " to " + newBrName);
} catch (final NumberFormatException e) {
continue;
}
}
}
s_logger.debug(vmDef);
msg = stopVM(conn, vmName);
msg = startVM(conn, vmName, vmDef);
return null;
} catch (final LibvirtException e) {
s_logger.warn("Failed to create vm", e);
msg = e.getMessage();
} catch (final InternalErrorException e) {
s_logger.warn("Failed to create vm", e);
msg = e.getMessage();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
return msg;
}
use of com.cloud.exception.InternalErrorException in project cloudstack by apache.
the class ServerDBSyncImpl method createVirtualMachine.
public void createVirtualMachine(VMInstanceVO dbVm, StringBuffer syncLogMesg) throws IOException {
syncLogMesg.append("VM# DB: " + dbVm.getInstanceName() + "/" + dbVm.getUuid() + "; VNC: none; action: create\n");
VirtualMachineModel vmModel = new VirtualMachineModel(dbVm, dbVm.getUuid());
vmModel.build(_manager.getModelController(), dbVm);
buildNicResources(vmModel, dbVm, syncLogMesg);
if (_rwMode) {
try {
vmModel.update(_manager.getModelController());
} catch (InternalErrorException ex) {
s_logger.warn("create virtual-machine", ex);
return;
}
_manager.getDatabase().getVirtualMachines().add(vmModel);
syncLogMesg.append("VM# VNC: " + dbVm.getUuid() + " created\n");
}
}
Aggregations