Search in sources :

Example 41 with Connection

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

the class XcpServerNetworkUsageCommandWrapper method execute.

@Override
public Answer execute(final NetworkUsageCommand command, final XcpServerResource xcpServerResource) {
    try {
        final Connection conn = xcpServerResource.getConnection();
        if (command.getOption() != null && command.getOption().equals("create")) {
            final String result = xcpServerResource.networkUsage(conn, command.getPrivateIP(), "create", null);
            final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
            return answer;
        }
        final long[] stats = xcpServerResource.getNetworkStats(conn, command.getPrivateIP());
        final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
        return answer;
    } catch (final Exception ex) {
        s_logger.warn("Failed to get network usage stats due to ", ex);
        return new NetworkUsageAnswer(command, ex);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer)

Example 42 with Connection

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

the class XenServer56FenceCommandWrapper method execute.

@Override
public Answer execute(final FenceCommand command, final XenServer56Resource xenServer56) {
    final Connection conn = xenServer56.getConnection();
    try {
        final Boolean alive = xenServer56.checkHeartbeat(command.getHostGuid());
        if (alive == null) {
            s_logger.debug("Failed to check heartbeat,  so unable to fence");
            return new FenceAnswer(command, false, "Failed to check heartbeat, so unable to fence");
        }
        if (alive) {
            s_logger.debug("Heart beat is still going so unable to fence");
            return new FenceAnswer(command, false, "Heartbeat is still going on unable to fence");
        }
        final Set<VM> vms = VM.getByNameLabel(conn, command.getVmName());
        for (final VM vm : vms) {
            s_logger.info("Fence command for VM " + command.getVmName());
            vm.powerStateReset(conn);
            vm.destroy(conn);
        }
        return new FenceAnswer(command);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    }
}
Also used : VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) FenceAnswer(com.cloud.agent.api.FenceAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException)

Example 43 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 44 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 45 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)

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