Search in sources :

Example 1 with Connection

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

the class NotAValidCommand method testCleanupNetworkRulesCmd.

@Test
public void testCleanupNetworkRulesCmd() {
    final Connection conn = Mockito.mock(Connection.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final CleanupNetworkRulesCmd cleanupNets = new CleanupNetworkRulesCmd(20);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.canBridgeFirewall()).thenReturn(true);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    when(citrixResourceBase.getVMInstanceName()).thenReturn("VM");
    when(citrixResourceBase.callHostPlugin(conn, "vmops", "cleanup_rules", "instance", citrixResourceBase.getVMInstanceName())).thenReturn("1");
    final Answer answer = wrapper.execute(cleanupNets, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) Connection(com.xensource.xenapi.Connection) CleanupNetworkRulesCmd(com.cloud.agent.api.CleanupNetworkRulesCmd) Test(org.junit.Test)

Example 2 with Connection

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

the class CitrixPrepareForMigrationCommandWrapper method execute.

@Override
public Answer execute(final PrepareForMigrationCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VirtualMachineTO vm = command.getVirtualMachine();
    List<String[]> vmDataList = vm.getVmData();
    String configDriveLabel = vm.getConfigDriveLabel();
    if (configDriveLabel == null) {
        configDriveLabel = "config";
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    final NicTO[] nics = vm.getNics();
    try {
        citrixResourceBase.prepareISO(conn, vm.getName(), vmDataList, configDriveLabel);
        for (final NicTO nic : nics) {
            citrixResourceBase.getNetwork(conn, nic);
        }
        s_logger.debug("4. The VM " + vm.getName() + " is in Migrating state");
        return new PrepareForMigrationAnswer(command);
    } catch (final Exception e) {
        s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e);
        return new PrepareForMigrationAnswer(command, e);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) NicTO(com.cloud.agent.api.to.NicTO) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer)

Example 3 with Connection

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

the class CitrixPrimaryStorageDownloadCommandWrapper method execute.

@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final CitrixResourceBase citrixResourceBase) {
    final String tmplturl = command.getUrl();
    final String poolName = command.getPoolUuid();
    final int wait = command.getWait();
    try {
        final URI uri = new URI(tmplturl);
        final String tmplpath = uri.getHost() + ":" + uri.getPath();
        final Connection conn = citrixResourceBase.getConnection();
        SR poolsr = null;
        final Set<SR> srs = SR.getByNameLabel(conn, poolName);
        if (srs.size() != 1) {
            final String msg = "There are " + srs.size() + " SRs with same name: " + poolName;
            s_logger.warn(msg);
            return new PrimaryStorageDownloadAnswer(msg);
        } else {
            poolsr = srs.iterator().next();
        }
        final String pUuid = poolsr.getUuid(conn);
        final boolean isISCSI = citrixResourceBase.IsISCSI(poolsr.getType(conn));
        final String uuid = citrixResourceBase.copyVhdFromSecondaryStorage(conn, tmplpath, pUuid, wait);
        final VDI tmpl = citrixResourceBase.getVDIbyUuid(conn, uuid);
        final VDI snapshotvdi = tmpl.snapshot(conn, new HashMap<String, String>());
        final String snapshotUuid = snapshotvdi.getUuid(conn);
        snapshotvdi.setNameLabel(conn, "Template " + command.getName());
        final String parentuuid = citrixResourceBase.getVhdParent(conn, pUuid, snapshotUuid, isISCSI);
        final VDI parent = citrixResourceBase.getVDIbyUuid(conn, parentuuid);
        final Long phySize = parent.getPhysicalUtilisation(conn);
        tmpl.destroy(conn);
        poolsr.scan(conn);
        try {
            Thread.sleep(5000);
        } catch (final Exception e) {
        }
        return new PrimaryStorageDownloadAnswer(snapshotvdi.getUuid(conn), phySize);
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " on host:" + citrixResourceBase.getHost().getUuid() + " for template: " + tmplturl + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new PrimaryStorageDownloadAnswer(msg);
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) URI(java.net.URI) SR(com.xensource.xenapi.SR)

Example 4 with Connection

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

the class CitrixPvlanSetupCommandWrapper method execute.

@Override
public Answer execute(final PvlanSetupCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String primaryPvlan = command.getPrimary();
    final String isolatedPvlan = command.getIsolated();
    final String op = command.getOp();
    final String dhcpName = command.getDhcpName();
    final String dhcpMac = command.getDhcpMac();
    final String dhcpIp = command.getDhcpIp();
    final String vmMac = command.getVmMac();
    final String networkTag = command.getNetworkTag();
    String nwNameLabel = null;
    try {
        final XsLocalNetwork nw = citrixResourceBase.getNativeNetworkForTraffic(conn, TrafficType.Guest, networkTag);
        if (nw == null) {
            s_logger.error("Network is not configured on the backend for pvlan " + primaryPvlan);
            throw new CloudRuntimeException("Network for the backend is not configured correctly for pvlan primary: " + primaryPvlan);
        }
        nwNameLabel = nw.getNetwork().getNameLabel(conn);
    } catch (final XenAPIException e) {
        s_logger.warn("Fail to get network", e);
        return new Answer(command, false, e.toString());
    } catch (final XmlRpcException e) {
        s_logger.warn("Fail to get network", e);
        return new Answer(command, false, e.toString());
    }
    String result = null;
    if (command.getType() == PvlanSetupCommand.Type.DHCP) {
        result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac);
        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
            s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
            return new Answer(command, false, result);
        } else {
            s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
        }
    } else if (command.getType() == PvlanSetupCommand.Type.VM) {
        result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac);
        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
            s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
            return new Answer(command, false, result);
        } else {
            s_logger.info("Programmed pvlan for vm with mac " + vmMac);
        }
    }
    return new Answer(command, true, result);
}
Also used : Answer(com.cloud.agent.api.Answer) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 5 with Connection

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

the class CitrixReadyCommandWrapper method execute.

@Override
public Answer execute(final ReadyCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final Long dcId = command.getDataCenterId();
    // Ignore the result of the callHostPlugin. Even if unmounting the
    // snapshots dir fails, let Ready command
    // succeed.
    citrixResourceBase.umountSnapshotDir(conn, dcId);
    citrixResourceBase.setupLinkLocalNetwork(conn);
    // try to destroy CD-ROM device for all system VMs on this host
    try {
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        final Set<VM> vms = host.getResidentVMs(conn);
        for (final VM vm : vms) {
            citrixResourceBase.destroyPatchVbd(conn, vm.getNameLabel(conn));
        }
    } catch (final Exception e) {
    }
    try {
        final boolean result = citrixResourceBase.cleanupHaltedVms(conn);
        if (!result) {
            return new ReadyAnswer(command, "Unable to cleanup halted vms");
        }
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to cleanup halted vms", e);
        return new ReadyAnswer(command, "Unable to cleanup halted vms");
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to cleanup halted vms", e);
        return new ReadyAnswer(command, "Unable to cleanup halted vms");
    }
    return new ReadyAnswer(command);
}
Also used : VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

Connection (com.xensource.xenapi.Connection)165 XenAPIException (com.xensource.xenapi.Types.XenAPIException)88 XmlRpcException (org.apache.xmlrpc.XmlRpcException)86 Answer (com.cloud.agent.api.Answer)79 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)58 Test (org.junit.Test)53 VDI (com.xensource.xenapi.VDI)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)46 SR (com.xensource.xenapi.SR)42 InternalErrorException (com.cloud.exception.InternalErrorException)39 RebootAnswer (com.cloud.agent.api.RebootAnswer)38 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)38 Network (com.xensource.xenapi.Network)35 VM (com.xensource.xenapi.VM)32 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)23 HashMap (java.util.HashMap)23 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)23 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)20 Host (com.xensource.xenapi.Host)19 URI (java.net.URI)19