Search in sources :

Example 6 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class UpdateResourceRef method execute.

/**
 * Execution method for updating the configuration of a ResourceRef. Will be
 * replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ResourceRefs that need to change
    List<ResourceRef> resourceRefsToChange = new ArrayList<>();
    // Add the ResourceRef from a named server if the target is a server
    Server server = domain.getServerNamed(target);
    // if the target is a server
    if (server != null) {
        ResourceRef serverResourceRef = server.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (serverResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(serverResourceRef);
    }
    // Add the ResourceRef from a named config if the target is a config
    Config config = domain.getConfigNamed(target);
    // if the target is a config
    if (config != null) {
        ResourceRef configResourceRef = config.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (configResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(configResourceRef);
    }
    // Add the ResourceRefs from a named cluster if the target is a cluster
    Cluster cluster = domain.getClusterNamed(target);
    // if the target is a cluster
    if (cluster != null) {
        ResourceRef clusterResourceRef = cluster.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (clusterResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(clusterResourceRef);
        for (Server instance : cluster.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the cluster contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
    DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
    if (dg != null) {
        ResourceRef ref = dg.getResourceRef(name);
        if (ref == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(ref);
        for (Server instance : dg.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the dg contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Apply the configuration to the listed ResourceRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ResourceRef) {
                        ResourceRef resourceRefProxy = (ResourceRef) proxy;
                        if (enabled != null) {
                            resourceRefProxy.setEnabled(enabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, resourceRefsToChange.toArray(new ResourceRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 7 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class ClusterCommandHelper method optimizeServerListOrder.

/**
 * Optimize the order of the list of servers. Basically we want the server list to be ordered such that we rotate over
 * the nodes. For example we want: n1, n2, n3, n1, n2, n3 not: n1, n1, n2, n2, n3, n3. This is to spread the load of
 * operations across nodes.
 *
 * @param original a list of servers
 * @return a list of servers with a more optimal order
 */
public List<Server> optimizeServerListOrder(List<Server> original) {
    // Don't bother with all this if it's just two instances
    if (original.size() < 3) {
        return original;
    }
    // There must be a more efficient way to do this, but this is what
    // we do. We first distribute all the server instances into a table
    // that is indexed by node name. Then we snake over that table to
    // create the final list.
    // Key is the node name, value is the list of servers on that node
    HashMap<String, List<Server>> serverTable = new HashMap<>();
    // Distribute servers into serverTable
    int count = 0;
    for (Server server : original) {
        String nodeName = server.getNodeRef();
        List<Server> serverList = serverTable.get(nodeName);
        if (serverList == null) {
            serverList = new ArrayList<>();
            serverTable.put(nodeName, serverList);
        }
        serverList.add(server);
        count++;
    }
    // Now snake through server table moving server entries from the
    // table to the final optimized list.
    List<Server> optimized = new ArrayList<>(count);
    Set<String> nodes = serverTable.keySet();
    while (count > 0) {
        for (String nodeName : nodes) {
            List<Server> serverList = serverTable.get(nodeName);
            if (!serverList.isEmpty()) {
                optimized.add(serverList.remove(0));
                count--;
            }
        }
    }
    return optimized;
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class CreateInstanceCommand method createInstanceFilesystem.

private void createInstanceFilesystem() {
    ActionReport report = ctx.getActionReport();
    report.setActionExitCode(SUCCESS);
    NodeUtils nodeUtils = new NodeUtils(serviceLocator, logger);
    Server dasServer = servers.getServer(SystemPropertyConstants.DAS_SERVER_NAME);
    String dasHost = dasServer.getAdminHost();
    String dasPort = Integer.toString(dasServer.getAdminPort());
    ArrayList<String> command = new ArrayList<String>();
    String humanCommand = null;
    if (!theNode.isLocal()) {
        // Only specify the DAS host if the node is remote. See issue 13993
        command.add("--host");
        command.add(dasHost);
    }
    command.add("--port");
    command.add(dasPort);
    command.add("_create-instance-filesystem");
    if (nodeDir != null) {
        command.add("--nodedir");
        command.add(StringUtils.quotePathIfNecessary(nodeDir));
    }
    command.add("--node");
    command.add(node);
    command.add(instance);
    humanCommand = makeCommandHuman(command);
    if (userManagedNodeType()) {
        report.setMessage(StringUtils.cat(NEWLINE, registerInstanceMessage, Strings.get("create.instance.config", instance, humanCommand)));
        return;
    }
    // First error message displayed if we fail
    StringBuilder output = new StringBuilder();
    // Run the command on the node and handle errors.
    nodeUtils.runAdminCommandOnNode(theNode, command, ctx, Strings.get("create.instance.filesystem.failed", instance, node, nodeHost), humanCommand, output);
    if (report.getActionExitCode() != SUCCESS) {
        // Something went wrong with the nonlocal command don't continue but set status to warning
        // because config was updated correctly or we would not be here.
        report.setActionExitCode(WARNING);
        return;
    }
    // If it was successful say so and display the command output
    String msg = Strings.get("create.instance.success", instance, nodeHost);
    if (!terse) {
        msg = StringUtils.cat(NEWLINE, output.toString().trim(), registerInstanceMessage, msg);
    }
    report.setMessage(msg);
    // Bootstrap secure admin files
    if (theNode.isLocal()) {
        bootstrapSecureAdminLocally();
    } else {
        bootstrapSecureAdminRemotely();
    }
    if (report.getActionExitCode() != SUCCESS) {
        // something went wrong with the nonlocal command don't continue but set status to warning
        // because config was updated correctly or we would not be here.
        report.setActionExitCode(WARNING);
    }
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport)

Example 9 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class PayaraNotificationFactory method newBuilder.

/**
 * @return a builder object used to configure notifiers
 */
public PayaraNotificationBuilder newBuilder() {
    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        LOGGER.log(Level.WARNING, "Unable to resolve local hostname", ex);
        hostName = "?.?.?.?";
    }
    final String domainName = environment.getDomainName();
    final String instanceName = environment.getInstanceName();
    final Server server = habitat.getService(Server.class, environment.getInstanceName());
    final String serverName = server.getName();
    return new PayaraNotificationBuilder(hostName, domainName, instanceName, serverName);
}
Also used : UnknownHostException(java.net.UnknownHostException) Server(com.sun.enterprise.config.serverbeans.Server)

Example 10 with Server

use of com.sun.enterprise.config.serverbeans.Server in project Payara by payara.

the class SetCommand method replicateSetCommand.

private boolean replicateSetCommand(AdminCommandContext context, String targetName, String value) {
    // "domain." on the front of the attribute name is optional.  So if it is
    // there, strip it off.
    List<Server> replicationInstances = null;
    String tName;
    if (targetName.startsWith("domain.")) {
        tName = targetName.substring("domain.".length());
        if (tName.indexOf('.') == -1) {
            // This is a domain-level attribute, replicate to all instances
            replicationInstances = targetService.getAllInstances();
        }
    } else {
        tName = targetName;
    }
    if (replicationInstances == null) {
        int dotIdx = tName.indexOf('.');
        String firstElementOfName = dotIdx != -1 ? tName.substring(0, dotIdx) : tName;
        Integer targetElementLocation = targetLevel.get(firstElementOfName);
        if (targetElementLocation == null) {
            targetElementLocation = 1;
        }
        if (targetElementLocation == 0) {
            if ("resources".equals(firstElementOfName)) {
                replicationInstances = targetService.getAllInstances();
            }
            if ("applications".equals(firstElementOfName)) {
                String appName = getElementFromString(tName, 3);
                if (appName == null) {
                    fail(context, localStrings.getLocalString("admin.set.invalid.appname", "Unable to extract application name from {0}", targetName));
                    return false;
                }
                replicationInstances = targetService.getInstances(domain.getAllReferencedTargetsForApplication(appName));
            }
        } else {
            String target = getElementFromString(tName, targetElementLocation);
            if (target == null) {
                fail(context, localStrings.getLocalString("admin.set.invalid.target", "Unable to extract replication target from {0}", targetName));
                return false;
            }
            replicationInstances = targetService.getInstances(target);
        }
    }
    if (replicationInstances != null && !replicationInstances.isEmpty()) {
        ParameterMap params = new ParameterMap();
        params.set("DEFAULT", targetName + "=" + value);
        ActionReport.ExitCode ret = ClusterOperationUtil.replicateCommand("set", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, replicationInstances, context, params, habitat);
        if (ret.equals(ActionReport.ExitCode.FAILURE)) {
            return false;
        }
    }
    return true;
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport)

Aggregations

Server (com.sun.enterprise.config.serverbeans.Server)101 ActionReport (org.glassfish.api.ActionReport)32 Cluster (com.sun.enterprise.config.serverbeans.Cluster)25 Domain (com.sun.enterprise.config.serverbeans.Domain)15 Node (com.sun.enterprise.config.serverbeans.Node)15 Config (com.sun.enterprise.config.serverbeans.Config)14 Properties (java.util.Properties)14 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)12 PropertyVetoException (java.beans.PropertyVetoException)11 DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)9 ParameterMap (org.glassfish.api.admin.ParameterMap)9 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)8 File (java.io.File)8 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)8 Logger (java.util.logging.Logger)7 Map (java.util.Map)6 ServerContext (org.glassfish.internal.api.ServerContext)6