Search in sources :

Example 31 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException 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)

Example 32 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testPvlanSetupCommandDhcpFailure.

@Test
public void testPvlanSetupCommandDhcpFailure() {
    final String label = "net";
    final Connection conn = Mockito.mock(Connection.class);
    final XsLocalNetwork network = Mockito.mock(XsLocalNetwork.class);
    final Network network2 = Mockito.mock(Network.class);
    final PvlanSetupCommand lanSetup = PvlanSetupCommand.createDhcpSetup("add", URI.create("http://127.0.0.1"), "tag", "dhcp", "0:0:0:0:0:0", "127.0.0.1");
    final String primaryPvlan = lanSetup.getPrimary();
    final String isolatedPvlan = lanSetup.getIsolated();
    final String op = lanSetup.getOp();
    final String dhcpName = lanSetup.getDhcpName();
    final String dhcpMac = lanSetup.getDhcpMac();
    final String dhcpIp = lanSetup.getDhcpIp();
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    try {
        when(citrixResourceBase.getNativeNetworkForTraffic(conn, TrafficType.Guest, "tag")).thenReturn(network);
        when(network.getNetwork()).thenReturn(network2);
        when(network2.getNameLabel(conn)).thenReturn(label);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    when(citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", label, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac)).thenReturn("false");
    final Answer answer = wrapper.execute(lanSetup, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(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) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 33 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testMaintainCommand.

@Test
public void testMaintainCommand() {
    // This test needs further work.
    final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
    final Connection conn = Mockito.mock(Connection.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final XmlRpcClient client = Mockito.mock(XmlRpcClient.class);
    // final Host.Record hr = PowerMockito.mock(Host.Record.class);
    // final Host host = PowerMockito.mock(Host.class);
    final MaintainCommand maintainCommand = new MaintainCommand();
    final Map<String, Object> map = new Hashtable<String, Object>();
    map.put("Value", "Xen");
    final Map<String, Object> spiedMap = spy(map);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    when(xsHost.getUuid()).thenReturn(uuid);
    when(conn.getSessionReference()).thenReturn("befc4dcd");
    try {
        final Object[] params = { Marshalling.toXMLRPC("befc4dcd"), Marshalling.toXMLRPC(uuid) };
        when(client.execute("host.get_by_uuid", new Object[] { "befc4dcd", uuid })).thenReturn(spiedMap);
        PowerMockito.when(conn, "dispatch", "host.get_by_uuid", params).thenReturn(spiedMap);
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    // try {
    // PowerMockito.mockStatic(Host.class);
    // //BDDMockito.given(Host.getByUuid(conn,
    // xsHost.getUuid())).willReturn(host);
    // PowerMockito.when(Host.getByUuid(conn,
    // xsHost.getUuid())).thenReturn(host);
    // PowerMockito.verifyStatic(times(1));
    // } catch (final BadServerResponse e) {
    // fail(e.getMessage());
    // } catch (final XenAPIException e) {
    // fail(e.getMessage());
    // } catch (final XmlRpcException e) {
    // fail(e.getMessage());
    // }
    //
    // PowerMockito.mockStatic(Types.class);
    // PowerMockito.when(Types.toHostRecord(spiedMap)).thenReturn(hr);
    // PowerMockito.verifyStatic(times(1));
    //
    // try {
    // PowerMockito.mockStatic(Host.Record.class);
    // when(host.getRecord(conn)).thenReturn(hr);
    // verify(host, times(1)).getRecord(conn);
    // } catch (final BadServerResponse e) {
    // fail(e.getMessage());
    // } catch (final XenAPIException e) {
    // fail(e.getMessage());
    // } catch (final XmlRpcException e) {
    // fail(e.getMessage());
    // }
    final Answer answer = wrapper.execute(maintainCommand, citrixResourceBase);
    assertFalse(answer.getResult());
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) 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) XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) Hashtable(java.util.Hashtable) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 34 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testOvsFetchInterfaceCommand.

@Test
public void testOvsFetchInterfaceCommand() {
    final String label = "[abc]";
    final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
    final Connection conn = Mockito.mock(Connection.class);
    final XsLocalNetwork network = Mockito.mock(XsLocalNetwork.class);
    final Network network2 = Mockito.mock(Network.class);
    final PIF pif = Mockito.mock(PIF.class);
    final PIF.Record pifRec = Mockito.mock(PIF.Record.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final OvsFetchInterfaceCommand fetchInterCommand = new OvsFetchInterfaceCommand(label);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.isXcp()).thenReturn(true);
    when(citrixResourceBase.getLabel()).thenReturn("[abc]");
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    try {
        when(network.getNetwork()).thenReturn(network2);
        when(network.getPif(conn)).thenReturn(pif);
        when(network.getPif(conn)).thenReturn(pif);
        when(pif.getRecord(conn)).thenReturn(pifRec);
        when(network.getNetwork().getUuid(conn)).thenReturn(uuid);
        when(citrixResourceBase.getNetworkByName(conn, label)).thenReturn(network);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(fetchInterCommand, 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) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) OvsFetchInterfaceCommand(com.cloud.agent.api.OvsFetchInterfaceCommand) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PIF(com.xensource.xenapi.PIF) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 35 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class CitrixSetupCommandWrapper method execute.

@Override
public Answer execute(final SetupCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final Map<Pool, Pool.Record> poolRecs = Pool.getAllRecords(conn);
        if (poolRecs.size() != 1) {
            throw new CloudRuntimeException("There are " + poolRecs.size() + " pool for host :" + citrixResourceBase.getHost().getUuid());
        }
        final Host master = poolRecs.values().iterator().next().master;
        citrixResourceBase.setupServer(conn, master);
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        citrixResourceBase.setupServer(conn, host);
        if (!citrixResourceBase.setIptables(conn)) {
            s_logger.warn("set xenserver Iptable failed");
            return null;
        }
        if (citrixResourceBase.isSecurityGroupEnabled()) {
            final boolean canBridgeFirewall = citrixResourceBase.canBridgeFirewall(conn);
            citrixResourceBase.setCanBridgeFirewall(canBridgeFirewall);
            if (!canBridgeFirewall) {
                final String msg = "Failed to configure brige firewall";
                s_logger.warn(msg);
                s_logger.warn("Check host " + citrixResourceBase.getHost().getIp() + " for CSP is installed or not and check network mode for bridge");
                return new SetupAnswer(command, msg);
            }
        }
        final boolean r = citrixResourceBase.launchHeartBeat(conn);
        if (!r) {
            return null;
        }
        citrixResourceBase.cleanupTemplateSR(conn);
        try {
            if (command.useMultipath()) {
                // the config value is set to true
                host.addToOtherConfig(conn, "multipathing", "true");
                host.addToOtherConfig(conn, "multipathhandle", "dmp");
            }
        } catch (final Types.MapDuplicateKey e) {
            s_logger.debug("multipath is already set");
        }
        if (command.needSetup()) {
            final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "setup_iscsi", "uuid", citrixResourceBase.getHost().getUuid());
            if (!result.contains("> DONE <")) {
                s_logger.warn("Unable to setup iscsi: " + result);
                return new SetupAnswer(command, result);
            }
            Pair<PIF, PIF.Record> mgmtPif = null;
            final Set<PIF> hostPifs = host.getPIFs(conn);
            for (final PIF pif : hostPifs) {
                final PIF.Record rec = pif.getRecord(conn);
                if (rec.management) {
                    if (rec.VLAN != null && rec.VLAN != -1) {
                        final String msg = new StringBuilder("Unsupported configuration.  Management network is on a VLAN.  host=").append(citrixResourceBase.getHost().getUuid()).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN).toString();
                        s_logger.warn(msg);
                        return new SetupAnswer(command, msg);
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Management network is on pif=" + rec.uuid);
                    }
                    mgmtPif = new Pair<PIF, PIF.Record>(pif, rec);
                    break;
                }
            }
            if (mgmtPif == null) {
                final String msg = "Unable to find management network for " + citrixResourceBase.getHost().getUuid();
                s_logger.warn(msg);
                return new SetupAnswer(command, msg);
            }
            final Map<Network, Network.Record> networks = Network.getAllRecords(conn);
            if (networks == null) {
                final String msg = "Unable to setup as there are no networks in the host: " + citrixResourceBase.getHost().getUuid();
                s_logger.warn(msg);
                return new SetupAnswer(command, msg);
            }
            for (final Network.Record network : networks.values()) {
                if (network.nameLabel.equals("cloud-private")) {
                    for (final PIF pif : network.PIFs) {
                        final PIF.Record pr = pif.getRecord(conn);
                        if (citrixResourceBase.getHost().getUuid().equals(pr.host.getUuid(conn))) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Found a network called cloud-private. host=" + citrixResourceBase.getHost().getUuid() + ";  Network=" + network.uuid + "; pif=" + pr.uuid);
                            }
                            if (pr.VLAN != null && pr.VLAN != -1) {
                                final String msg = new StringBuilder("Unsupported configuration.  Network cloud-private is on a VLAN.  Network=").append(network.uuid).append(" ; pif=").append(pr.uuid).toString();
                                s_logger.warn(msg);
                                return new SetupAnswer(command, msg);
                            }
                            if (!pr.management && pr.bondMasterOf != null && pr.bondMasterOf.size() > 0) {
                                if (pr.bondMasterOf.size() > 1) {
                                    final String msg = new StringBuilder("Unsupported configuration.  Network cloud-private has more than one bond.  Network=").append(network.uuid).append("; pif=").append(pr.uuid).toString();
                                    s_logger.warn(msg);
                                    return new SetupAnswer(command, msg);
                                }
                                final Bond bond = pr.bondMasterOf.iterator().next();
                                final Set<PIF> slaves = bond.getSlaves(conn);
                                for (final PIF slave : slaves) {
                                    final PIF.Record spr = slave.getRecord(conn);
                                    if (spr.management) {
                                        if (!citrixResourceBase.transferManagementNetwork(conn, host, slave, spr, pif)) {
                                            final String msg = new StringBuilder("Unable to transfer management network.  slave=" + spr.uuid + "; master=" + pr.uuid + "; host=" + citrixResourceBase.getHost().getUuid()).toString();
                                            s_logger.warn(msg);
                                            return new SetupAnswer(command, msg);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return new SetupAnswer(command, false);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    }
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) PIF(com.xensource.xenapi.PIF) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) SetupAnswer(com.cloud.agent.api.SetupAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) Pool(com.xensource.xenapi.Pool) Bond(com.xensource.xenapi.Bond) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

XenAPIException (com.xensource.xenapi.Types.XenAPIException)101 XmlRpcException (org.apache.xmlrpc.XmlRpcException)93 Connection (com.xensource.xenapi.Connection)56 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)53 Answer (com.cloud.agent.api.Answer)33 InternalErrorException (com.cloud.exception.InternalErrorException)31 SR (com.xensource.xenapi.SR)30 Host (com.xensource.xenapi.Host)29 Network (com.xensource.xenapi.Network)27 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)26 VDI (com.xensource.xenapi.VDI)25 IOException (java.io.IOException)25 HashMap (java.util.HashMap)25 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)25 Test (org.junit.Test)25 ConfigurationException (javax.naming.ConfigurationException)22 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)21 MalformedURLException (java.net.MalformedURLException)21 URISyntaxException (java.net.URISyntaxException)21 TimeoutException (java.util.concurrent.TimeoutException)21