Search in sources :

Example 26 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class Xenserver625StorageProcessor method createFileSR.

protected SR createFileSR(final Connection conn, final String path) {
    SR sr = null;
    PBD pbd = null;
    try {
        final String srname = path.trim();
        synchronized (srname.intern()) {
            final Set<SR> srs = SR.getByNameLabel(conn, srname);
            if (srs != null && !srs.isEmpty()) {
                return srs.iterator().next();
            }
            final Map<String, String> smConfig = new HashMap<String, String>();
            final Host host = Host.getByUuid(conn, hypervisorResource.getHost().getUuid());
            final String uuid = UUID.randomUUID().toString();
            sr = SR.introduce(conn, uuid, srname, srname, "file", "file", false, smConfig);
            final PBD.Record record = new PBD.Record();
            record.host = host;
            record.SR = sr;
            smConfig.put("location", path);
            record.deviceConfig = smConfig;
            pbd = PBD.create(conn, record);
            pbd.plug(conn);
            sr.scan(conn);
        }
        return sr;
    } catch (final Exception ex) {
        try {
            if (pbd != null) {
                pbd.destroy(conn);
            }
        } catch (final Exception e1) {
            s_logger.debug("Failed to destroy PBD", ex);
        }
        try {
            if (sr != null) {
                sr.forget(conn);
            }
        } catch (final Exception e2) {
            s_logger.error("Failed to forget SR", ex);
        }
        final String msg = "createFileSR failed! due to the following: " + ex.toString();
        s_logger.warn(msg, ex);
        throw new CloudRuntimeException(msg, ex);
    }
}
Also used : PBD(com.xensource.xenapi.PBD) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.xensource.xenapi.Host) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) SR(com.xensource.xenapi.SR)

Example 27 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixScaleVmCommandWrapper method execute.

@Override
public Answer execute(final ScaleVmCommand command, final CitrixResourceBase citrixResourceBase) {
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    try {
        final Connection conn = citrixResourceBase.getConnection();
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        // If DMC is not enable then don't execute this command.
        if (!citrixResourceBase.isDmcEnabled(conn, host)) {
            throw new CloudRuntimeException("Unable to scale the vm: " + vmName + " as DMC - Dynamic memory control is not enabled for the XenServer:" + citrixResourceBase.getHost().getUuid() + " ,check your license and hypervisor version.");
        }
        if (vms == null || vms.size() == 0) {
            s_logger.info("No running VM " + vmName + " exists on XenServer" + citrixResourceBase.getHost().getUuid());
            return new ScaleVmAnswer(command, false, "VM does not exist");
        }
        // stop vm which is running on this host or is in halted state
        final Iterator<VM> iter = vms.iterator();
        while (iter.hasNext()) {
            final VM vm = iter.next();
            final VM.Record vmr = vm.getRecord(conn);
            if (vmr.powerState == VmPowerState.HALTED || vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                iter.remove();
            }
        }
        for (final VM vm : vms) {
            vm.getRecord(conn);
            try {
                citrixResourceBase.scaleVM(conn, vm, vmSpec, host);
            } catch (final Exception e) {
                final String msg = "Catch exception " + e.getClass().getName() + " when scaling VM:" + vmName + " due to " + e.toString();
                s_logger.debug(msg);
                return new ScaleVmAnswer(command, false, msg);
            }
        }
        final String msg = "scaling VM " + vmName + " is successful on host " + host;
        s_logger.debug(msg);
        return new ScaleVmAnswer(command, true, msg);
    } catch (final XenAPIException e) {
        final String msg = "Upgrade Vm " + vmName + " fail due to " + e.toString();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    } catch (final XmlRpcException e) {
        final String msg = "Upgrade Vm " + vmName + " fail due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    } catch (final Exception e) {
        final String msg = "Unable to upgrade " + vmName + " due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 28 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixStartCommandWrapper method execute.

@Override
public Answer execute(final StartCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    VmPowerState state = VmPowerState.HALTED;
    VM vm = null;
    // if a VDI is created, record its UUID to send back to the CS MS
    final Map<String, String> iqnToPath = new HashMap<String, String>();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        if (vms != null) {
            for (final VM v : vms) {
                final VM.Record vRec = v.getRecord(conn);
                if (vRec.powerState == VmPowerState.HALTED) {
                    v.destroy(conn);
                } else if (vRec.powerState == VmPowerState.RUNNING) {
                    final String host = vRec.residentOn.getUuid(conn);
                    final String msg = "VM " + vmName + " is runing on host " + host;
                    s_logger.debug(msg);
                    return new StartAnswer(command, msg, host);
                } else {
                    final String msg = "There is already a VM having the same name " + vmName + " vm record " + vRec.toString();
                    s_logger.warn(msg);
                    return new StartAnswer(command, msg);
                }
            }
        }
        s_logger.debug("1. The VM " + vmName + " is in Starting state.");
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.createVmFromTemplate(conn, vmSpec, host);
        final GPUDeviceTO gpuDevice = vmSpec.getGpuDevice();
        if (gpuDevice != null) {
            s_logger.debug("Creating VGPU for of VGPU type: " + gpuDevice.getVgpuType() + " in GPU group " + gpuDevice.getGpuGroup() + " for VM " + vmName);
            citrixResourceBase.createVGPU(conn, command, vm, gpuDevice);
        }
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            citrixResourceBase.createPatchVbd(conn, vmName, vm);
        }
        // put cdrom at the first place in the list
        List<DiskTO> disks = new ArrayList<DiskTO>(vmSpec.getDisks().length);
        int index = 0;
        for (final DiskTO disk : vmSpec.getDisks()) {
            if (Volume.Type.ISO.equals(disk.getType())) {
                disks.add(0, disk);
            } else {
                disks.add(index, disk);
            }
            index++;
        }
        for (DiskTO disk : disks) {
            final VDI newVdi = citrixResourceBase.prepareManagedDisk(conn, disk, vmSpec.getId(), vmSpec.getName());
            if (newVdi != null) {
                final String path = newVdi.getUuid(conn);
                iqnToPath.put(disk.getDetails().get(DiskTO.IQN), path);
            }
            citrixResourceBase.createVbd(conn, disk, vmName, vm, vmSpec.getBootloader(), newVdi);
        }
        for (final NicTO nic : vmSpec.getNics()) {
            citrixResourceBase.createVif(conn, vmName, vm, vmSpec, nic);
        }
        citrixResourceBase.startVM(conn, host, vm, vmName);
        if (citrixResourceBase.isOvs()) {
            // TODO(Salvatore-orlando): This code should go
            for (final NicTO nic : vmSpec.getNics()) {
                if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vswitch) {
                    final HashMap<String, String> args = citrixResourceBase.parseDefaultOvsRuleComamnd(BroadcastDomainType.getValue(nic.getBroadcastUri()));
                    final OvsSetTagAndFlowCommand flowCmd = new OvsSetTagAndFlowCommand(args.get("vmName"), args.get("tag"), args.get("vlans"), args.get("seqno"), Long.parseLong(args.get("vmId")));
                    final CitrixRequestWrapper citrixRequestWrapper = CitrixRequestWrapper.getInstance();
                    final OvsSetTagAndFlowAnswer r = (OvsSetTagAndFlowAnswer) citrixRequestWrapper.execute(flowCmd, citrixResourceBase);
                    if (!r.getResult()) {
                        s_logger.warn("Failed to set flow for VM " + r.getVmId());
                    } else {
                        s_logger.info("Success to set flow for VM " + r.getVmId());
                    }
                }
            }
        }
        if (citrixResourceBase.canBridgeFirewall()) {
            String result = null;
            if (vmSpec.getType() != VirtualMachine.Type.User) {
                final NicTO[] nics = vmSpec.getNics();
                boolean secGrpEnabled = false;
                for (final NicTO nic : nics) {
                    if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                        secGrpEnabled = true;
                        break;
                    }
                }
                if (secGrpEnabled) {
                    result = citrixResourceBase.callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", vmName);
                    if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                        s_logger.warn("Failed to program default network rules for " + vmName);
                    } else {
                        s_logger.info("Programmed default network rules for " + vmName);
                    }
                }
            } else {
                // For user vm, program the rules for each nic if the
                // isolation uri scheme is ec2
                final NicTO[] nics = vmSpec.getNics();
                for (final NicTO nic : nics) {
                    if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                        final List<String> nicSecIps = nic.getNicSecIps();
                        String secIpsStr;
                        final StringBuilder sb = new StringBuilder();
                        if (nicSecIps != null) {
                            for (final String ip : nicSecIps) {
                                sb.append(ip).append(":");
                            }
                            secIpsStr = sb.toString();
                        } else {
                            secIpsStr = "0:";
                        }
                        result = citrixResourceBase.callHostPlugin(conn, "vmops", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID", Long.toString(vmSpec.getId()), "secIps", secIpsStr);
                        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                            s_logger.warn("Failed to program default network rules for " + vmName + " on nic with ip:" + nic.getIp() + " mac:" + nic.getMac());
                        } else {
                            s_logger.info("Programmed default network rules for " + vmName + " on nic with ip:" + nic.getIp() + " mac:" + nic.getMac());
                        }
                    }
                }
            }
        }
        state = VmPowerState.RUNNING;
        final StartAnswer startAnswer = new StartAnswer(command);
        startAnswer.setIqnToPath(iqnToPath);
        return startAnswer;
    } catch (final Exception e) {
        s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
        final String msg = citrixResourceBase.handleVmStartFailure(conn, vmName, vm, "", e);
        final StartAnswer startAnswer = new StartAnswer(command, msg);
        startAnswer.setIqnToPath(iqnToPath);
        return startAnswer;
    } finally {
        if (state != VmPowerState.HALTED) {
            s_logger.debug("2. The VM " + vmName + " is in " + state + " state.");
        } else {
            s_logger.debug("The VM is in stopped state, detected problem during startup : " + vmName);
        }
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VDI(com.xensource.xenapi.VDI) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) OvsSetTagAndFlowAnswer(com.cloud.agent.api.OvsSetTagAndFlowAnswer) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) OvsSetTagAndFlowCommand(com.cloud.agent.api.OvsSetTagAndFlowCommand) VM(com.xensource.xenapi.VM) VmPowerState(com.xensource.xenapi.Types.VmPowerState) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 29 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixMaintainCommandWrapper method execute.

@Override
public Answer execute(final MaintainCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final XsHost xsHost = citrixResourceBase.getHost();
        final String uuid = xsHost.getUuid();
        final Host host = Host.getByUuid(conn, uuid);
        // remove all tags cloud stack
        final Host.Record hr = host.getRecord(conn);
        // Adding this check because could not get the mock to work. Will push the code and fix it afterwards.
        if (hr == null) {
            s_logger.warn("Host.Record is null.");
            return new MaintainAnswer(command, false, "Host.Record is null");
        }
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.contains("cloud")) {
                it.remove();
            }
        }
        host.setTags(conn, hr.tags);
        return new MaintainAnswer(command);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 30 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method CheckXenHostInfo.

private void CheckXenHostInfo() throws ConfigurationException {
    final Connection conn = ConnPool.getConnect(_host.getIp(), _username, _password);
    if (conn == null) {
        throw new ConfigurationException("Can not create connection to " + _host.getIp());
    }
    try {
        Host.Record hostRec = null;
        try {
            final Host host = Host.getByUuid(conn, _host.getUuid());
            hostRec = host.getRecord(conn);
            final Pool.Record poolRec = Pool.getAllRecords(conn).values().iterator().next();
            _host.setPool(poolRec.uuid);
        } catch (final Exception e) {
            throw new ConfigurationException("Can not get host information from " + _host.getIp());
        }
        if (!hostRec.address.equals(_host.getIp())) {
            final String msg = "Host " + _host.getIp() + " seems be reinstalled, please remove this host and readd";
            s_logger.error(msg);
            throw new ConfigurationException(msg);
        }
    } finally {
        try {
            Session.logout(conn);
        } catch (final Exception e) {
        }
    }
}
Also used : ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) Host(com.xensource.xenapi.Host) Pool(com.xensource.xenapi.Pool) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

Host (com.xensource.xenapi.Host)47 XenAPIException (com.xensource.xenapi.Types.XenAPIException)35 XmlRpcException (org.apache.xmlrpc.XmlRpcException)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)32 HashMap (java.util.HashMap)24 Connection (com.xensource.xenapi.Connection)17 SR (com.xensource.xenapi.SR)17 IOException (java.io.IOException)15 ConfigurationException (javax.naming.ConfigurationException)14 InternalErrorException (com.cloud.exception.InternalErrorException)13 MalformedURLException (java.net.MalformedURLException)13 URISyntaxException (java.net.URISyntaxException)12 TimeoutException (java.util.concurrent.TimeoutException)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 SAXException (org.xml.sax.SAXException)12 VM (com.xensource.xenapi.VM)11 PBD (com.xensource.xenapi.PBD)10 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)7 Map (java.util.Map)7 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)6