Search in sources :

Example 1 with LbConfig

use of org.glassfish.loadbalancer.config.LbConfig in project Payara by payara.

the class DeleteHTTPLBRefCommand method deleteServerFromLBConfig.

private void deleteServerFromLBConfig(LbConfigs lbconfigs, String configName, String serverName) {
    LbConfig lbConfig = lbconfigs.getLbConfig(configName);
    ServerRef sRef = lbConfig.getRefByRef(ServerRef.class, serverName);
    if (sRef == null) {
        // does not exist, just return from here
        String msg = localStrings.getLocalString("ServerNotDefined", "Server {0} cannot be used as target", target);
        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("Server " + serverName + " does not exist in any cluster in the domain");
        }
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (!Boolean.parseBoolean(force)) {
        if (Boolean.parseBoolean(sRef.getLbEnabled())) {
            String msg = localStrings.getLocalString("ServerNeedsToBeDisabled", "Server [{0}] needs to be disabled before it can be removed from the load balancer.", serverName);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        // check if its applications are LB disabled.
        Server s = domain.getServerNamed(serverName);
        if (s == null) {
            String msg = localStrings.getLocalString("ServerNotDefined", "Server {0} cannot be used as target", target);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        List<ApplicationRef> appRefs = domain.getApplicationRefsInTarget(target);
        if (appRefs == null) {
            String msg = localStrings.getLocalString("AppRefsNotDefined", "Application refs does not exist in server {0}", target);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        boolean appLbEnabled = false;
        for (ApplicationRef aRef : appRefs) {
            if (Boolean.parseBoolean(aRef.getLbEnabled())) {
                appLbEnabled = true;
                break;
            }
        }
        if (appLbEnabled) {
            String msg = localStrings.getLocalString("AppsNotDisabled", "All referenced applications must be disabled in LB");
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    removeServerRef(lbConfig, sRef);
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) LbConfig(org.glassfish.loadbalancer.config.LbConfig) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef)

Example 2 with LbConfig

use of org.glassfish.loadbalancer.config.LbConfig in project Payara by payara.

the class DisableHTTPLBServerCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    Logger logger = context.getLogger();
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    LbConfigs lbconfigs = domain.getExtensionByType(LbConfigs.class);
    if (lbconfigs == null) {
        String msg = localStrings.getLocalString("NoLbConfigsElement", "Empty lb-configs");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    int t = Integer.parseInt(timeout);
    if (t < 0) {
        String msg = localStrings.getLocalString("InvalidTimeout", "Invalid timeout {0}", timeout);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (tgt.isCluster(target)) {
        // disable all servers in cluster
        updateLBForCluster(report, target, "false", timeout);
    } else {
        boolean foundTarget = false;
        List<LbConfig> lbConfigs = lbconfigs.getLbConfig();
        for (LbConfig lc : lbConfigs) {
            ServerRef sRef = lc.getRefByRef(ServerRef.class, target);
            if (sRef == null) {
                // log a warning and continue search
                logger.warning(localStrings.getLocalString("InvalidInstance", "Server {0} does not exist in {1}", target, lc.getName()));
            } else {
                int curTout = Integer.parseInt(sRef.getDisableTimeoutInMinutes());
                boolean enabled = sRef.getLbEnabled().equals("true");
                if ((enabled == false) && (curTout == t)) {
                    String msg = localStrings.getLocalString("ServerDisabled", "Server [{0}] is already disabled.", sRef.getRef());
                    report.setMessage(msg);
                    return;
                }
                try {
                    updateLbEnabled(sRef, "false", timeout);
                } catch (TransactionFailure ex) {
                    String msg = localStrings.getLocalString("FailedToUpdateAttr", "Failed to update lb-enabled attribute for {0}", target);
                    report.setMessage(msg);
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(ex);
                    return;
                }
                foundTarget = true;
            }
        }
        // did not find server target
        if (!foundTarget) {
            ServerRef sRef = getServerRefFromCluster(report, target);
            if (sRef == null) {
                String msg = localStrings.getLocalString("InvalidServer", "Server {0} does not exist", target);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                report.setMessage(msg);
                return;
            } else {
                int curTout = Integer.parseInt(sRef.getDisableTimeoutInMinutes());
                boolean enabled = sRef.getLbEnabled().equals("true");
                if ((enabled == false) && (curTout == t)) {
                    String msg = localStrings.getLocalString("ServerDisabled", "Server [{0}] is already disabled.", sRef.getRef());
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setMessage(msg);
                    return;
                }
                try {
                    updateLbEnabled(sRef, "false", timeout);
                } catch (TransactionFailure ex) {
                    String msg = localStrings.getLocalString("FailedToUpdateAttr", "Failed to update lb-enabled attribute for {0}", target);
                    report.setMessage(msg);
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(ex);
                    return;
                }
            }
        }
    }
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LbConfig(org.glassfish.loadbalancer.config.LbConfig) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef)

Example 3 with LbConfig

use of org.glassfish.loadbalancer.config.LbConfig in project Payara by payara.

the class CreateHTTPLBRefCommand method addServerToLBConfig.

private void addServerToLBConfig(final LbConfigs lbconfigs, final String configName, final String serverName) {
    LbConfig lbConfig = lbconfigs.getLbConfig(configName);
    ServerRef sRef = lbConfig.getRefByRef(ServerRef.class, serverName);
    if (sRef != null) {
        String msg = localStrings.getLocalString("LBServerRefExists", "LB config already contains a server-ref for target {0}", target);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    Server server = domain.getServerNamed(serverName);
    boolean isStandAlone = server.getCluster() == null && server.isInstance();
    if (!isStandAlone) {
        String msg = localStrings.getLocalString("NotStandAloneInstance", "[{0}] is not a stand alone instance. Only stand alone instance can be added to a load balancer.", serverName);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<LbConfig>() {

            @Override
            public Object run(LbConfig param) throws PropertyVetoException, TransactionFailure {
                ServerRef ref = param.createChild(ServerRef.class);
                ref.setRef(serverName);
                param.getClusterRefOrServerRef().add(ref);
                return Boolean.TRUE;
            }
        }, lbConfig);
    } catch (TransactionFailure ex) {
        String msg = localStrings.getLocalString("FailedToAddServerRef", "Failed to add server-ref");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        report.setFailureCause(ex);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) Server(com.sun.enterprise.config.serverbeans.Server) LbConfig(org.glassfish.loadbalancer.config.LbConfig) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef)

Example 4 with LbConfig

use of org.glassfish.loadbalancer.config.LbConfig in project Payara by payara.

the class LbConfigHelper method getLbReader.

/**
 * exports the loadbalancer.xml from the config to the outputstream provided
 * @param ctx ConfigContext
 * @param lbConfigName name of lb-config
 * @param out OutputStream into which the loadbalancer.xml is written
 */
public static LoadbalancerReader getLbReader(Domain domain, ApplicationRegistry appRegistry, String lbConfigName) throws Exception {
    // reads the load balancer related data
    LbConfigs lbConfigs = domain.getExtensionByType(LbConfigs.class);
    if (lbConfigs == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLbConfig", lbConfigName));
    }
    LbConfig lbConfig = lbConfigs.getLbConfig(lbConfigName);
    if (lbConfig == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLbConfig", lbConfigName));
    }
    return new LoadbalancerReaderImpl(domain, appRegistry, lbConfig);
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LoadbalancerReaderImpl(org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl) LbConfig(org.glassfish.loadbalancer.config.LbConfig)

Example 5 with LbConfig

use of org.glassfish.loadbalancer.config.LbConfig in project Payara by payara.

the class CreateHTTPHealthCheckerCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    report = context.getActionReport();
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    LbConfigs lbconfigs = domain.getExtensionByType(LbConfigs.class);
    if (lbconfigs == null) {
        String msg = localStrings.getLocalString("NoLbConfigsElement", "Empty lb-configs");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (config != null) {
        LbConfig lbConfig = lbconfigs.getLbConfig(config);
        createHealthCheckerInternal(url, interval, timeout, lbConfig, config, target);
    } else {
        List<LbConfig> lbConfigs = lbconfigs.getLbConfig();
        if (lbConfigs.size() == 0) {
            String msg = localStrings.getLocalString("NoLbConfigsElement", "No LB configs defined");
            logger.warning(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        List<LbConfig> match = null;
        match = matchLbConfigToTarget(lbConfigs, target);
        if ((match == null) || (match.size() == 0)) {
            String msg = localStrings.getLocalString("UnassociatedTarget", "No LB config references target {0}", target);
            logger.warning(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        for (LbConfig lc : match) {
            createHealthCheckerInternal(url, interval, timeout, lc, lc.getName(), target);
        }
    }
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LbConfig(org.glassfish.loadbalancer.config.LbConfig)

Aggregations

LbConfig (org.glassfish.loadbalancer.config.LbConfig)12 ServerRef (com.sun.enterprise.config.serverbeans.ServerRef)7 LbConfigs (org.glassfish.loadbalancer.config.LbConfigs)6 ClusterRef (com.sun.enterprise.config.serverbeans.ClusterRef)4 ActionReport (org.glassfish.api.ActionReport)3 Server (com.sun.enterprise.config.serverbeans.Server)2 PropertyVetoException (java.beans.PropertyVetoException)2 Logger (java.util.logging.Logger)2 LoadbalancerReaderImpl (org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl)2 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Cluster (com.sun.enterprise.config.serverbeans.Cluster)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 LoadbalancerReader (org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader)1 LoadBalancer (org.glassfish.loadbalancer.config.LoadBalancer)1