Search in sources :

Example 1 with LoadBalancer

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

the class CreateHTTPLBRefCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    report = context.getActionReport();
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    boolean isCluster = (target != null) ? tgt.isCluster(target) : false;
    if (config != null && lbname != null) {
        String msg = localStrings.getLocalString("EitherConfigOrLBName", "Either LB name or LB config name, not both");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (config == null && lbname == null) {
        String msg = localStrings.getLocalString("SpecifyConfigOrLBName", "Please specify either LB name or LB config name.");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    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) {
        if (lbconfigs.getLbConfig(config) == null) {
            String msg = localStrings.getLocalString("LbConfigDoesNotExist", "Specified LB config {0} does not exist", config);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    } else if (lbname != null) {
        LoadBalancers lbs = domain.getExtensionByType(LoadBalancers.class);
        if (lbs == null) {
            String msg = localStrings.getLocalString("NoLoadBalancersElement", "No Load balancers defined.");
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        LoadBalancer lb = lbs.getLoadBalancer(lbname);
        if (lb == null) {
            String msg = localStrings.getLocalString("LoadBalancerNotDefined", "Load balancer [{0}] not found.", lbname);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        config = lb.getLbConfigName();
    }
    if ((lbpolicy != null) || (lbpolicymodule != null)) {
        if (!isCluster) {
            String msg = localStrings.getLocalString("NotCluster", "{0} not a cluster", target);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    Cluster c = null;
    Server s = null;
    if (isCluster) {
        c = domain.getClusterNamed(target);
        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;
        }
    } else {
        s = domain.getServerNamed(target);
        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;
        }
    }
    // create lb ref
    createLBRef(lbconfigs, target, config);
    if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
        return;
    }
    if (healthcheckerurl != null) {
        try {
            final CreateHTTPHealthCheckerCommand command = (CreateHTTPHealthCheckerCommand) runner.getCommand("create-http-health-checker", report, context.getLogger());
            command.url = healthcheckerurl;
            command.interval = healthcheckerinterval;
            command.timeout = healthcheckertimeout;
            command.config = config;
            command.target = target;
            command.execute(context);
            checkCommandStatus(context);
        } catch (CommandException e) {
            String msg = e.getLocalizedMessage();
            logger.warning(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    }
    if (Boolean.parseBoolean(lbenableallinstances)) {
        try {
            final EnableHTTPLBServerCommand command = (EnableHTTPLBServerCommand) runner.getCommand("enable-http-lb-server", report, context.getLogger());
            command.target = target;
            command.execute(context);
            checkCommandStatus(context);
        } catch (CommandException e) {
            String msg = e.getLocalizedMessage();
            logger.warning(msg);
        // report.setActionExitCode(ExitCode.FAILURE);
        // report.setMessage(msg);
        // return;
        }
    }
    if (Boolean.parseBoolean(lbenableallapplications)) {
        List<ApplicationRef> appRefs = domain.getApplicationRefsInTarget(target);
        if ((appRefs.size() > 0) && Boolean.parseBoolean(lbenableallapplications)) {
            for (ApplicationRef ref : appRefs) {
                // enable only user applications
                if (isUserApp(ref.getRef())) {
                    enableApp(context, ref.getRef());
                }
            }
        }
    }
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LoadBalancers(org.glassfish.loadbalancer.config.LoadBalancers) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef)

Example 2 with LoadBalancer

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

the class DeleteHTTPLBRefCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    report = context.getActionReport();
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    boolean isCluster = tgt.isCluster(target);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("[LB-ADMIN] deleteLBRef called for target " + target);
    }
    if (config != null && lbname != null) {
        String msg = localStrings.getLocalString("EitherConfigOrLBName", "Either LB name or LB config name, not both");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    if (config == null && lbname == null) {
        String msg = localStrings.getLocalString("SpecifyConfigOrLBName", "Please specify either LB name or LB config name.");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
    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) {
        if (lbconfigs.getLbConfig(config) == null) {
            String msg = localStrings.getLocalString("LbConfigDoesNotExist", "Specified LB config {0} does not exist", config);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
    } else if (lbname != null) {
        LoadBalancers lbs = domain.getExtensionByType(LoadBalancers.class);
        if (lbs == null) {
            String msg = localStrings.getLocalString("NoLoadBalancersElement", "No Load balancers defined.");
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        LoadBalancer lb = lbs.getLoadBalancer(lbname);
        if (lb == null) {
            String msg = localStrings.getLocalString("LoadBalancerNotDefined", "Load balancer [{0}] not found.", lbname);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(msg);
            return;
        }
        config = lb.getLbConfigName();
    }
    // target is a cluster
    if (isCluster) {
        deleteClusterFromLBConfig(lbconfigs, config, target);
        String msg = localStrings.getLocalString("http_lb_admin.DeleteClusterFromConfig", "Deleted cluster {0} from Lb", target, config);
        logger.info(msg);
    // target is a server
    } else if (domain.isServer(target)) {
        deleteServerFromLBConfig(lbconfigs, config, target);
        String msg = localStrings.getLocalString("http_lb_admin.DeleteServerFromConfig", "Deleted server {0} from Lb", target, config);
        logger.info(msg);
    } else {
        String msg = localStrings.getLocalString("InvalidTarget", "Invalid target", target);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(msg);
        return;
    }
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LoadBalancers(org.glassfish.loadbalancer.config.LoadBalancers) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer)

Example 3 with LoadBalancer

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

the class LbConfigHelper method getLoadBalancer.

public static LoadBalancer getLoadBalancer(Domain domain, String lbName) throws Exception {
    LoadBalancers loadBalancers = domain.getExtensionByType(LoadBalancers.class);
    if (loadBalancers == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLoadbalancer", lbName));
    }
    LoadBalancer loadBalancer = loadBalancers.getLoadBalancer(lbName);
    if (loadBalancer == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLoadbalancer", lbName));
    }
    return loadBalancer;
}
Also used : LoadBalancers(org.glassfish.loadbalancer.config.LoadBalancers) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer)

Example 4 with LoadBalancer

use of org.glassfish.loadbalancer.config.LoadBalancer 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 5 with LoadBalancer

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

the class LoadBalancerConfigUpgrade method updateLoadBalancerElements.

private void updateLoadBalancerElements() {
    LoadBalancers loadBalancers = domain.getExtensionByType(LoadBalancers.class);
    if (loadBalancers == null) {
        return;
    }
    List<LoadBalancer> loadBalancerList = loadBalancers.getLoadBalancer();
    for (LoadBalancer loadBalancer : loadBalancerList) {
        try {
            ConfigSupport.apply(new LoadBalancerConfigCode(), loadBalancer);
        } catch (TransactionFailure ex) {
            String msg = LbLogUtil.getStringManager().getString("ErrorDuringUpgrade", loadBalancer.getName(), ex.getMessage());
            Logger.getAnonymousLogger().log(Level.SEVERE, msg);
            if (Logger.getAnonymousLogger().isLoggable(Level.FINE)) {
                Logger.getAnonymousLogger().log(Level.FINE, "Exception during upgrade operation", ex);
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) LoadBalancers(org.glassfish.loadbalancer.config.LoadBalancers) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer)

Aggregations

LoadBalancer (org.glassfish.loadbalancer.config.LoadBalancer)7 LoadBalancers (org.glassfish.loadbalancer.config.LoadBalancers)5 LbConfigs (org.glassfish.loadbalancer.config.LbConfigs)2 Property (org.jvnet.hk2.config.types.Property)2 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Cluster (com.sun.enterprise.config.serverbeans.Cluster)1 Server (com.sun.enterprise.config.serverbeans.Server)1 PropertyVetoException (java.beans.PropertyVetoException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ConnectionManager (org.glassfish.loadbalancer.admin.cli.connection.ConnectionManager)1 LoadbalancerReader (org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader)1 LoadbalancerReaderImpl (org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl)1 LbConfig (org.glassfish.loadbalancer.config.LbConfig)1 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)1