Search in sources :

Example 6 with LbConfig

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

the class DeleteHTTPHealthCheckerCommand 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);
        deleteHealthCheckerInternal(lbConfig, target, false);
    } else {
        List<LbConfig> lbConfigs = lbconfigs.getLbConfig();
        if (lbConfigs.isEmpty()) {
            String msg = localStrings.getLocalString("NoLbConfigsElement", "Empty lb-configs");
            logger.warning(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        for (LbConfig lc : lbConfigs) {
            deleteHealthCheckerInternal(lc, target, true);
        }
    }
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LbConfig(org.glassfish.loadbalancer.config.LbConfig)

Example 7 with LbConfig

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

the class DeleteHTTPLBRefCommand method deleteClusterFromLBConfig.

private void deleteClusterFromLBConfig(LbConfigs lbconfigs, String configName, String clusterName) {
    LbConfig lbConfig = lbconfigs.getLbConfig(configName);
    ClusterRef cRef = lbConfig.getRefByRef(ClusterRef.class, clusterName);
    if (cRef == null) {
        // does not exist, just return from here
        String msg = localStrings.getLocalString("ClusterNotDefined", "Cluster {0} cannot be used as target", target);
        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("Cluster " + clusterName + " does not exist.");
        }
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (!Boolean.parseBoolean(force)) {
        Cluster c = domain.getClusterNamed(clusterName);
        if (c == null) {
            String msg = localStrings.getLocalString("ClusterNotDefined", "Cluster {0} cannot be used as target", target);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        List<ServerRef> sRefs = c.getServerRef();
        boolean refLbEnabled = false;
        for (ServerRef ref : sRefs) {
            if (Boolean.parseBoolean(ref.getLbEnabled())) {
                refLbEnabled = true;
            }
        }
        if (refLbEnabled) {
            String msg = localStrings.getLocalString("ServerNeedsToBeDisabled", "Server [{0}] needs to be disabled before it can be removed from the load balancer.", target);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    removeClusterRef(lbConfig, cRef);
}
Also used : LbConfig(org.glassfish.loadbalancer.config.LbConfig) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ClusterRef(com.sun.enterprise.config.serverbeans.ClusterRef) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef)

Example 8 with LbConfig

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

the class EnableHTTPLBServerCommand 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;
    }
    if (tgt.isCluster(target)) {
        // enable all servers in cluster
        updateLBForCluster(report, target, "true", null);
    } else {
        boolean foundTarget = false;
        List<LbConfig> lbConfigs = lbconfigs.getLbConfig();
        for (LbConfig lc : lbConfigs) {
            // ServerRef  sRef = lc.getServerRefByRef(target);
            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 {
                boolean enabled = sRef.getLbEnabled().equals("true");
                if (enabled == true) {
                    String msg = localStrings.getLocalString("ServerEnabled", "Server [{0}] is already enabled.", sRef.getRef());
                    report.setMessage(msg);
                    return;
                }
                try {
                    updateLbEnabled(sRef, "true", null);
                } 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 {
                boolean enabled = sRef.getLbEnabled().equals("true");
                if (enabled == true) {
                    String msg = localStrings.getLocalString("ServerEnabled", "Server [{0}] is already enabled.", sRef.getRef());
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setMessage(msg);
                    return;
                }
                try {
                    updateLbEnabled(sRef, "true", null);
                } 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 9 with LbConfig

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

the class ExportHttpLbConfig method process.

public String process(AdminCommandContext context) throws Exception {
    LoadbalancerReader lbr = null;
    if (lbName != null && lbConfigName == null && target == null) {
        LoadBalancer lb = LbConfigHelper.getLoadBalancer(domain, lbName);
        lbr = LbConfigHelper.getLbReader(domain, appRegistry, lb.getLbConfigName());
    } else if (lbConfigName != null && lbName == null && target == null) {
        lbr = LbConfigHelper.getLbReader(domain, appRegistry, lbConfigName);
    } else if (target != null && lbName == null && lbConfigName == null) {
        Set<String> clusters = new HashSet<String>();
        clusters.addAll(target);
        lbr = new LoadbalancerReaderImpl(domain, appRegistry, clusters, properties);
    } else {
        String msg = LbLogUtil.getStringManager().getString("ExportHttpLbConfigInvalidArgs");
        throw new Exception(msg);
    }
    if (fileName == null) {
        String configName = lbr.getName();
        if (configName != null) {
            fileName = DEFAULT_LB_XML_FILE_NAME + "." + configName;
        } else {
            fileName = DEFAULT_LB_XML_FILE_NAME;
        }
    }
    File lbConfigFile = new File(fileName);
    if (!lbConfigFile.isAbsolute() && !retrieveFile) {
        File loadbalancerDir = new File(env.getInstanceRoot(), "load-balancer");
        if (!loadbalancerDir.exists()) {
            boolean isMkdirSuccess = loadbalancerDir.mkdir();
            if (!isMkdirSuccess) {
                String msg = LbLogUtil.getStringManager().getString("directoryCreationFailed");
                throw new Exception(msg);
            }
        }
        lbConfigFile = new File(loadbalancerDir, fileName);
    }
    File tmpLbXmlFile = null;
    if (retrieveFile) {
        tmpLbXmlFile = File.createTempFile("load-balancer", ".xml");
        tmpLbXmlFile.deleteOnExit();
    } else {
        if (lbConfigFile.exists()) {
            String msg = LbLogUtil.getStringManager().getString("FileExists", lbConfigFile.getPath());
            throw new Exception(msg);
        }
        if (!(lbConfigFile.getParentFile().exists())) {
            String msg = LbLogUtil.getStringManager().getString("ParentFileMissing", lbConfigFile.getParent());
            throw new Exception(msg);
        }
        tmpLbXmlFile = lbConfigFile;
    }
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(tmpLbXmlFile);
        LbConfigHelper.exportXml(lbr, fo);
        if (retrieveFile) {
            retrieveLbConfig(context, lbConfigFile, tmpLbXmlFile);
        }
        LbConfig lbConfig = lbr.getLbConfig();
        // In such a case, lbconfig will be null
        if (lbConfig != null) {
            lbConfig.setLastExported();
        }
        String msg = LbLogUtil.getStringManager().getString("GeneratedFileLocation", lbConfigFile.toString());
        return msg;
    } finally {
        if (fo != null) {
            fo.close();
            fo = null;
        }
    }
}
Also used : LoadbalancerReaderImpl(org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl) FileOutputStream(java.io.FileOutputStream) LbConfig(org.glassfish.loadbalancer.config.LbConfig) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer) LoadbalancerReader(org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader) File(java.io.File) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 10 with LbConfig

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

the class ListLBConfigsCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    report = context.getActionReport();
    ActionReport.MessagePart part = report.getTopMessagePart();
    boolean isCluster = tgt.isCluster(list_target);
    LbConfigs lbconfigs = domain.getExtensionByType(LbConfigs.class);
    if (lbconfigs == null) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(localStrings.getLocalString("http_lb_admin.NoLbConfigs", "No lb configs"));
        }
        return;
    }
    List<LbConfig> lbconfigsList = lbconfigs.getLbConfig();
    if (lbconfigsList.size() == 0) {
        logger.fine(localStrings.getLocalString("http_lb_admin.NoLbConfigs", "No lb configs"));
        return;
    }
    if (list_target == null) {
        for (LbConfig lbc : lbconfigsList) {
            ActionReport.MessagePart childPart = part.addChild();
            childPart.setMessage(lbc.getName());
        }
    } else {
        // target is a cluster
        if (isCluster) {
            for (LbConfig lbc : lbconfigsList) {
                List<ClusterRef> refs = lbc.getRefs(ClusterRef.class);
                for (ClusterRef cRef : refs) {
                    if (cRef.getRef().equals(list_target)) {
                        ActionReport.MessagePart childPart = part.addChild();
                        childPart.setMessage(lbc.getName());
                    }
                }
            }
        // target is a server
        } else if (domain.isServer(list_target)) {
            for (LbConfig lbc : lbconfigsList) {
                List<ServerRef> refs = lbc.getRefs(ServerRef.class);
                for (ServerRef sRef : refs) {
                    if (sRef.getRef().equals(list_target)) {
                        ActionReport.MessagePart childPart = part.addChild();
                        childPart.setMessage(lbc.getName());
                    }
                }
            }
        } else {
            // target is a lb config
            LbConfig lbConfig = lbconfigs.getLbConfig(list_target);
            if (lbConfig != null) {
                List<ClusterRef> cRefs = lbConfig.getRefs(ClusterRef.class);
                for (ClusterRef ref : cRefs) {
                    String s = localStrings.getLocalString("ClusterPrefix", "Cluster:");
                    ActionReport.MessagePart childPart = part.addChild();
                    childPart.setMessage(s + ref.getRef());
                }
                List<ServerRef> sRefs = lbConfig.getRefs(ServerRef.class);
                for (ServerRef ref : sRefs) {
                    String s = localStrings.getLocalString("ServerPrefix", "Server:");
                    ActionReport.MessagePart childPart = part.addChild();
                    childPart.setMessage(s + ref.getRef());
                }
            }
        }
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LbConfig(org.glassfish.loadbalancer.config.LbConfig) List(java.util.List) ActionReport(org.glassfish.api.ActionReport) ClusterRef(com.sun.enterprise.config.serverbeans.ClusterRef) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef)

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