Search in sources :

Example 1 with SecurityGroupRuleAnswer

use of com.cloud.agent.api.SecurityGroupRuleAnswer in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

private Answer execute(SecurityGroupRulesCmd cmd) {
    String vif = null;
    String brname = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        List<InterfaceDef> nics = getInterfaces(conn, cmd.getVmName());
        vif = nics.get(0).getDevName();
        brname = nics.get(0).getBrName();
    } catch (LibvirtException e) {
        return new SecurityGroupRuleAnswer(cmd, false, e.toString());
    }
    boolean result = add_network_rules(cmd.getVmName(), Long.toString(cmd.getVmId()), cmd.getGuestIp(), cmd.getSignature(), Long.toString(cmd.getSeqNum()), cmd.getGuestMac(), cmd.stringifyRules(), vif, brname);
    if (!result) {
        s_logger.warn("Failed to program network rules for vm " + cmd.getVmName());
        return new SecurityGroupRuleAnswer(cmd, false, "programming network rules failed");
    } else {
        s_logger.debug("Programmed network rules for vm " + cmd.getVmName() + " guestIp=" + cmd.getGuestIp() + ",ingress numrules=" + cmd.getIngressRuleSet().length + ",egress numrules=" + cmd.getEgressRuleSet().length);
        return new SecurityGroupRuleAnswer(cmd);
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Example 2 with SecurityGroupRuleAnswer

use of com.cloud.agent.api.SecurityGroupRuleAnswer in project cloudstack by apache.

the class SecurityGroupHttpClient method call.

public SecurityGroupRuleAnswer call(String agentIp, SecurityGroupRulesCmd cmd) {
    PostMethod post = new PostMethod(String.format("http://%s:%s", agentIp, getPort()));
    try {
        SecurityGroupVmRuleSet rset = new SecurityGroupVmRuleSet();
        rset.getEgressRules().addAll(generateRules(cmd.getEgressRuleSet()));
        rset.getIngressRules().addAll(generateRules(cmd.getIngressRuleSet()));
        rset.setVmName(cmd.getVmName());
        rset.setVmIp(cmd.getGuestIp());
        rset.setVmMac(cmd.getGuestMac());
        rset.setVmId(cmd.getVmId());
        rset.setSignature(cmd.getSignature());
        rset.setSequenceNumber(cmd.getSeqNum());
        Marshaller marshaller = context.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(rset, writer);
        String xmlContents = writer.toString();
        logger.debug(xmlContents);
        post.addRequestHeader("command", "set_rules");
        StringRequestEntity entity = new StringRequestEntity(xmlContents);
        post.setRequestEntity(entity);
        if (httpClient.executeMethod(post) != 200) {
            return new SecurityGroupRuleAnswer(cmd, false, post.getResponseBodyAsString());
        } else {
            return new SecurityGroupRuleAnswer(cmd);
        }
    } catch (Exception e) {
        return new SecurityGroupRuleAnswer(cmd, false, e.getMessage());
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) StringWriter(java.io.StringWriter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) SecurityGroupVmRuleSet(com.cloud.baremetal.networkservice.schema.SecurityGroupVmRuleSet) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 3 with SecurityGroupRuleAnswer

use of com.cloud.agent.api.SecurityGroupRuleAnswer in project cloudstack by apache.

the class SecurityGroupListener method processAnswers.

@Override
public boolean processAnswers(long agentId, long seq, Answer[] answers) {
    List<Long> affectedVms = new ArrayList<Long>();
    for (Answer ans : answers) {
        if (ans instanceof SecurityGroupRuleAnswer) {
            SecurityGroupRuleAnswer ruleAnswer = (SecurityGroupRuleAnswer) ans;
            if (ans.getResult()) {
                s_logger.debug("Successfully programmed rule " + ruleAnswer.toString() + " into host " + agentId);
                _workDao.updateStep(ruleAnswer.getVmId(), ruleAnswer.getLogSequenceNumber(), Step.Done);
                recordSuccess(ruleAnswer.getVmId());
            } else {
                _workDao.updateStep(ruleAnswer.getVmId(), ruleAnswer.getLogSequenceNumber(), Step.Error);
                ;
                s_logger.debug("Failed to program rule " + ruleAnswer.toString() + " into host " + agentId + " due to " + ruleAnswer.getDetails() + " and updated  jobs");
                if (ruleAnswer.getReason() == FailureReason.CANNOT_BRIDGE_FIREWALL) {
                    s_logger.debug("Not retrying security group rules for vm " + ruleAnswer.getVmId() + " on failure since host " + agentId + " cannot do bridge firewalling");
                } else if (ruleAnswer.getReason() == FailureReason.PROGRAMMING_FAILED) {
                    if (checkShouldRetryOnFailure(ruleAnswer.getVmId())) {
                        s_logger.debug("Retrying security group rules on failure for vm " + ruleAnswer.getVmId());
                        affectedVms.add(ruleAnswer.getVmId());
                    } else {
                        s_logger.debug("Not retrying security group rules for vm " + ruleAnswer.getVmId() + " on failure: too many retries");
                    }
                }
            }
            if (_workTracker != null)
                _workTracker.processAnswers(agentId, seq, answers);
        }
    }
    if (affectedVms.size() > 0) {
        _securityGroupManager.scheduleRulesetUpdateToHosts(affectedVms, false, new Long(10 * 1000l));
    }
    return true;
}
Also used : SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) ArrayList(java.util.ArrayList) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Example 4 with SecurityGroupRuleAnswer

use of com.cloud.agent.api.SecurityGroupRuleAnswer in project cloudstack by apache.

the class CitrixSecurityGroupRulesCommandWrapper method execute.

@Override
public Answer execute(final SecurityGroupRulesCmd command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Sending network rules command to " + citrixResourceBase.getHost().getIp());
    }
    if (!citrixResourceBase.canBridgeFirewall()) {
        s_logger.warn("Host " + citrixResourceBase.getHost().getIp() + " cannot do bridge firewalling");
        return new SecurityGroupRuleAnswer(command, false, "Host " + citrixResourceBase.getHost().getIp() + " cannot do bridge firewalling", SecurityGroupRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL);
    }
    final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "network_rules", "vmName", command.getVmName(), "vmIP", command.getGuestIp(), "vmMAC", command.getGuestMac(), "vmID", Long.toString(command.getVmId()), "signature", command.getSignature(), "seqno", Long.toString(command.getSeqNum()), "deflated", "true", "rules", command.compressStringifiedRules(), "secIps", command.getSecIpsString());
    if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
        s_logger.warn("Failed to program network rules for vm " + command.getVmName());
        return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
    } else {
        s_logger.info("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ", ingress numrules=" + command.getIngressRuleSet().size() + ", egress numrules=" + command.getEgressRuleSet().size());
        return new SecurityGroupRuleAnswer(command);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Example 5 with SecurityGroupRuleAnswer

use of com.cloud.agent.api.SecurityGroupRuleAnswer in project cloudstack by apache.

the class LibvirtSecurityGroupRulesCommandWrapper method execute.

@Override
public Answer execute(final SecurityGroupRulesCmd command, final LibvirtComputingResource libvirtComputingResource) {
    String vif = null;
    String brname = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName());
        vif = nics.get(0).getDevName();
        brname = nics.get(0).getBrName();
    } catch (final LibvirtException e) {
        return new SecurityGroupRuleAnswer(command, false, e.toString());
    }
    final boolean result = libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getGuestIp6(), command.getSignature(), Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString());
    if (!result) {
        s_logger.warn("Failed to program network rules for vm " + command.getVmName());
        return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
    } else {
        s_logger.debug("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ",ingress numrules=" + command.getIngressRuleSet().size() + ",egress numrules=" + command.getEgressRuleSet().size());
        return new SecurityGroupRuleAnswer(command);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Aggregations

SecurityGroupRuleAnswer (com.cloud.agent.api.SecurityGroupRuleAnswer)5 Connect (org.libvirt.Connect)2 LibvirtException (org.libvirt.LibvirtException)2 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 Answer (com.cloud.agent.api.Answer)1 InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)1 SecurityGroupVmRuleSet (com.cloud.baremetal.networkservice.schema.SecurityGroupVmRuleSet)1 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Connection (com.xensource.xenapi.Connection)1 StringWriter (java.io.StringWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 Marshaller (javax.xml.bind.Marshaller)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)1