Search in sources :

Example 1 with Node

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

the class GMSAdapterImpl method generateDiscoveryUriList.

/*
     * Get existing nodes based on cluster element in domain.
     * Then check for DAS address in das.properties. When the
     * list is set to 'generate' then the gms listener port
     * must also be specified. So the same port is used for
     * each cluster member.
     */
private String generateDiscoveryUriList() {
    String clusterPort = null;
    Property gmsPortProp = cluster.getProperty("GMS_LISTENER_PORT");
    if (gmsPortProp == null || gmsPortProp.getValue() == null || gmsPortProp.getValue().trim().charAt(0) == '$') {
        clusterPort = "9090";
        GMS_LOGGER.log(LogLevel.WARNING, GMS_LISTENER_PORT_REQUIRED, new Object[] { cluster.getName(), clusterPort });
    } else {
        clusterPort = gmsPortProp.getValue();
        if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
            GMS_LOGGER.log(LogLevel.FINE, "will use gms listener port: " + clusterPort);
        }
    }
    // get cluster member server refs
    Set<String> instanceNames = new HashSet<String>();
    if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
        GMS_LOGGER.log(LogLevel.FINE, String.format("checking cluster.getServerRef() for '%s'", cluster.getName()));
    }
    for (ServerRef sRef : cluster.getServerRef()) {
        /*
             * When an instance (not DAS) starts up, it will add
             * its own address to the discovery list. This is ok
             * now. If we want to skip it, here's the place to
             * check.
             */
        if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
            GMS_LOGGER.log(LogLevel.FINE, String.format("adding server ref %s to set of instance names", sRef.getRef()));
        }
        instanceNames.add(sRef.getRef());
    }
    StringBuilder sb = new StringBuilder();
    final String SEP = ",";
    final String scheme = "tcp://";
    // use server refs to find matching nodes
    for (String name : instanceNames) {
        Server server = servers.getServer(name);
        if (server != null) {
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("found server for name %s", name));
            }
            Node node = nodes.getNode(server.getNodeRef());
            if (node != null) {
                String host = scheme + node.getNodeHost() + ":" + clusterPort;
                if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                    GMS_LOGGER.log(LogLevel.FINE, String.format("Adding host '%s' to discovery list", host));
                }
                sb.append(host).append(SEP);
            }
        }
    }
    // add das location from das.properties if needed
    if (server.isInstance()) {
        try {
            ServerDirs sDirs = new ServerDirs(env.getInstanceRoot());
            File dasPropsFile = sDirs.getDasPropertiesFile();
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("found das.props file at %s", dasPropsFile.getAbsolutePath()));
            }
            Properties dasProps = getProperties(dasPropsFile);
            String host = scheme + dasProps.getProperty("agent.das.host") + ":" + clusterPort;
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("adding '%s' from das.props file", host));
            }
            sb.append(host).append(SEP);
        } catch (IOException ioe) {
            GMS_LOGGER.log(LogLevel.WARNING, ioe.toString());
        }
    }
    // trim list if needed and return
    int lastCommaIndex = sb.lastIndexOf(SEP);
    if (lastCommaIndex != -1) {
        sb.deleteCharAt(lastCommaIndex);
    }
    if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
        GMS_LOGGER.log(LogLevel.FINE, String.format("returning discovery list '%s'", sb.toString()));
    }
    return sb.toString();
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) Node(com.sun.enterprise.config.serverbeans.Node) IOException(java.io.IOException) Properties(java.util.Properties) ServerDirs(com.sun.enterprise.util.io.ServerDirs) Property(org.jvnet.hk2.config.types.Property) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with Node

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

the class LogFilterForInstance method downloadGivenInstanceLogFile.

public File downloadGivenInstanceLogFile(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String domainRoot, String logFileName, String instanceLogFileName) throws IOException {
    File instanceLogFile = null;
    // method is used from logviewer back end code logfilter.
    // for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
    // store in domains/domain1/logs/<instance name> which is used to get LogFile object.
    // Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
    SSHLauncher sshL = getSSHL(habitat);
    String sNode = targetServer.getNodeRef();
    Nodes nodes = domain.getNodes();
    Node node = nodes.getNode(sNode);
    if (node.getType().equals("SSH")) {
        sshL.init(node, logger);
        SFTPClient sftpClient = sshL.getSFTPClient();
        File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs" + File.separator + instanceName);
        boolean noFileFound = true;
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            List instanceLogFileNames = sftpClient.ls(loggingDir);
            for (int i = 0; i < instanceLogFileNames.size(); i++) {
                SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                String fileName = file.filename;
                // code to remove . and .. file which is return from sftpclient ls method
                if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..") && fileName.contains(".log") && !fileName.contains(".log.")) {
                    noFileFound = false;
                    break;
                }
            }
        } catch (Exception e) {
            // if directory doesn't present or missing on remote machine. It happens due to bug 16451
            noFileFound = true;
        }
        if (noFileFound) {
            // this loop is used when user has changed value for server.log but not restarted the server.
            loggingDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileName, node, sNode, instanceName);
        }
        String loggingFile = loggingDir + File.separator + logFileName;
        if (!sftpClient.exists(loggingFile)) {
            loggingFile = loggingDir + File.separator + "server.log";
        } else if (!sftpClient.exists(loggingFile)) {
            loggingFile = instanceLogFileName;
        }
        // creating local file name on DAS
        long instanceLogFileSize = 0;
        instanceLogFile = new File(logFileDirectoryOnServer.getAbsolutePath() + File.separator + loggingFile.substring(loggingFile.lastIndexOf(File.separator), loggingFile.length()));
        // getting size of the file on DAS
        if (instanceLogFile.exists()) {
            instanceLogFileSize = instanceLogFile.length();
        }
        SFTPv3FileAttributes sftPv3FileAttributes = sftpClient._stat(loggingFile);
        // getting size of the file on instance machine
        long fileSizeOnNode = sftPv3FileAttributes.size;
        // if differ both size then downloading
        if (instanceLogFileSize != fileSizeOnNode) {
            try (BufferedInputStream in = new BufferedInputStream(sftpClient.read(loggingFile));
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(instanceLogFile))) {
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                out.flush();
            }
        }
        sftpClient.close();
    } else if (node.getType().equals("DCOM")) {
        File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs" + File.separator + instanceName);
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            DcomInfo info = new DcomInfo(node);
            WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
            if (logFileName == null || logFileName.equals("")) {
                logFileName = "server.log";
            }
            WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, loggingDir + File.separator + logFileName);
            instanceLogFile = new File(logFileDirectoryOnServer + File.separator + logFileName);
            wrf.copyTo(instanceLogFile);
        } catch (WindowsException ex) {
            throw new IOException("Unable to download instance log file from DCOM Instance Node");
        }
    }
    return instanceLogFile;
}
Also used : SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) Node(com.sun.enterprise.config.serverbeans.Node) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) Nodes(com.sun.enterprise.config.serverbeans.Nodes) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with Node

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

the class LogFilter method getLogFileForGivenTarget.

/*
    * This method is used by LogViewerResource which is used to display raw log data like 'tail -f server.log'.
    */
public String getLogFileForGivenTarget(String targetServerName) throws IOException {
    Server targetServer = domain.getServerNamed(targetServerName);
    String serverNode = targetServer.getNodeRef();
    if (targetServer.isDas()) {
        // getting log file for DAS from logging.properties and returning the same
        String logFileDetailsForServer = loggingConfigFactory.provide().getLoggingFileDetails();
        logFileDetailsForServer = TranslatedConfigView.expandConfigValue(logFileDetailsForServer);
        logFileDetailsForServer = new File(logFileDetailsForServer).getAbsolutePath();
        return logFileDetailsForServer;
    } else {
        // getting log file for instance from logging.properties
        String logFileDetailsForInstance = getInstanceLogFileDetails(targetServer);
        Node node = domain.getNodes().getNode(serverNode);
        String loggingDir = "";
        String loggingFile = "";
        // replacing instanceRoot value if it's there
        if (logFileDetailsForInstance.contains("${com.sun.aas.instanceRoot}/logs") && node.getNodeDir() != null) {
            // this code is used if no changes made in logging.properties file
            loggingDir = node.getNodeDir() + File.separator + serverNode + File.separator + targetServerName;
            loggingFile = logFileDetailsForInstance.replace("${com.sun.aas.instanceRoot}", loggingDir);
        } else if (logFileDetailsForInstance.contains("${com.sun.aas.instanceRoot}/logs") && node.getInstallDir() != null) {
            loggingDir = node.getInstallDir() + File.separator + "glassfish" + File.separator + "nodes" + File.separator + serverNode + File.separator + targetServerName;
            loggingFile = logFileDetailsForInstance.replace("${com.sun.aas.instanceRoot}", loggingDir);
        } else {
            loggingFile = logFileDetailsForInstance;
        }
        if (node.isLocal()) {
            // if local just returning log file to view
            return loggingFile;
        } else {
            // if remote then need to download log file on DAS and returning that log file for view
            String logFileName = logFileDetailsForInstance.substring(logFileDetailsForInstance.lastIndexOf(File.separator) + 1, logFileDetailsForInstance.length());
            File instanceFile = null;
            instanceFile = new LogFilterForInstance().downloadGivenInstanceLogFile(habitat, targetServer, domain, LOGGER, targetServerName, env.getInstanceRoot().getAbsolutePath(), logFileName, logFileDetailsForInstance);
            return instanceFile.getAbsolutePath();
        }
    }
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) Node(com.sun.enterprise.config.serverbeans.Node) File(java.io.File)

Example 4 with Node

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

the class AddTruststoreEntryCommand method addToTrustStore.

/**
 * Runs the 'add-to-truststore' command on the local instance.
 * @param context The admin command context.
 * @throws CommandException If there's an issue running the command.
 */
private void addToTrustStore(AdminCommandContext context) throws CommandException {
    NodeRunner nodeRunner = new NodeRunner(serviceLocator, context.getLogger());
    Node node = nodes.getNode(servers.getServer(serverEnvironment.getInstanceName()).getNodeRef());
    // The DAS doesn't have a node-ref
    if (node == null && serverEnvironment.isDas()) {
        node = nodes.getDefaultLocalNode();
    }
    try {
        StringBuilder stringBuilder = new StringBuilder();
        nodeRunner.runAdminCommandOnNode(node, stringBuilder, createAddToStoreCommand("add-to-truststore", node, new File(filePath), alias), context);
        if (stringBuilder.toString().contains("Command add-to-truststore failed")) {
            throw new CommandException();
        }
    } catch (SSHCommandExecutionException | ProcessManagerException e) {
        throw new CommandException(e);
    }
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) Node(com.sun.enterprise.config.serverbeans.Node) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) CommandException(org.glassfish.api.admin.CommandException) File(java.io.File) SSHCommandExecutionException(org.glassfish.api.admin.SSHCommandExecutionException)

Example 5 with Node

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

the class ServerHelper method getAdminHost.

public final String getAdminHost() {
    if (server == null || config == null) {
        return null;
    }
    // Look at the address for the admin-listener first
    String addr = translateAddressAndPort(getAdminListener(config), server, config)[0];
    if (addr != null && !addr.equals("0.0.0.0")) {
        return addr;
    }
    Dom serverDom = Dom.unwrap(server);
    Domain domain = serverDom.getHabitat().getService(Domain.class);
    Nodes nodes = serverDom.getHabitat().getService(Nodes.class);
    ServerEnvironment env = serverDom.getHabitat().getService(ServerEnvironment.class);
    if (server.isDas()) {
        if (env.isDas()) {
            // We are the DAS. Return our hostname
            return System.getProperty(SystemPropertyConstants.HOST_NAME_PROPERTY);
        } else {
            // IT 12778 -- it is impossible to know
            return null;
        }
    }
    String hostName = null;
    // Get it from the node associated with the server
    String nodeName = server.getNodeRef();
    if (StringUtils.ok(nodeName)) {
        Node node = nodes.getNode(nodeName);
        if (node != null) {
            hostName = node.getNodeHost();
        }
        // node entry is malformed
        if (hostName == null && nodeName.equals("localhost-" + domain.getName())) {
            hostName = "localhost";
        }
    }
    if (StringUtils.ok(hostName)) {
        return hostName;
    }
    return null;
}
Also used : Dom(org.jvnet.hk2.config.Dom) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) Node(com.sun.enterprise.config.serverbeans.Node) Domain(com.sun.enterprise.config.serverbeans.Domain) Nodes(com.sun.enterprise.config.serverbeans.Nodes)

Aggregations

Node (com.sun.enterprise.config.serverbeans.Node)38 Server (com.sun.enterprise.config.serverbeans.Server)15 ActionReport (org.glassfish.api.ActionReport)13 File (java.io.File)9 ArrayList (java.util.ArrayList)7 Properties (java.util.Properties)7 ParameterMap (org.glassfish.api.admin.ParameterMap)7 Nodes (com.sun.enterprise.config.serverbeans.Nodes)6 IOException (java.io.IOException)6 Logger (java.util.logging.Logger)5 Domain (com.sun.enterprise.config.serverbeans.Domain)4 JsonObject (javax.json.JsonObject)4 CommandInvocation (org.glassfish.api.admin.CommandRunner.CommandInvocation)4 Test (org.junit.Test)4 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)4 WindowsRemoteFile (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile)3 WindowsRemoteFileSystem (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem)3 WindowsException (com.sun.enterprise.util.cluster.windows.process.WindowsException)3 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)3 PropertyVetoException (java.beans.PropertyVetoException)3