Пример #1
0
  private String getInstanceLogFileDirectory(Server targetServer) throws IOException {

    String logFileDetailsForServer = "";
    String targetConfigName = "";

    Cluster clusterForInstance = targetServer.getCluster();
    if (clusterForInstance != null) {
      targetConfigName = clusterForInstance.getConfigRef();
    } else {
      targetConfigName = targetServer.getConfigRef();
    }

    logFileDetailsForServer = loggingConfig.getLoggingFileDetails(targetConfigName);

    return logFileDetailsForServer;
  }
Пример #2
0
  public void execute(AdminCommandContext context) {

    final ActionReport report = context.getActionReport();

    Properties props = initFileXferProps();

    Server targetServer = domain.getServerNamed(target);

    if (targetServer != null && targetServer.isDas()) {

      // This loop if target instance is DAS
      String logFileDetails = "";
      String zipFile = "";

      try {
        // getting log file values from logging.propertie file.
        logFileDetails = loggingConfig.getLoggingFileDetails();
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errGettingLogFiles",
                "Error while getting log file attribute for {0}.",
                target);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      File targetDir = makingDirectoryOnDas(targetServer.getName(), report);

      try {

        String sourceDir = "";
        if (logFileDetails.contains("${com.sun.aas.instanceRoot}/logs")) {
          sourceDir = env.getDomainRoot() + File.separator + "logs";
        } else {
          sourceDir = logFileDetails.substring(0, logFileDetails.lastIndexOf(File.separator));
        }

        copyLogFilesForLocalhost(
            sourceDir, targetDir.getAbsolutePath(), report, targetServer.getName());
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errInstanceDownloading",
                "Error while downloading log files from {0}.",
                target);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      try {
        String zipFilePath = getZipFilePath().getAbsolutePath();
        zipFile = loggingConfig.createZipFile(zipFilePath);
        if (zipFile == null || new File(zipFile) == null) {
          // Failure during zip
          final String errorMsg =
              localStrings.getLocalString(
                  "collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFilePath);
          report.setMessage(errorMsg);
          report.setActionExitCode(ActionReport.ExitCode.FAILURE);
          return;
        }

      } catch (Exception e) {
        // Catching Exception if any
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
        report.setMessage(errorMsg);
        report.setFailureCause(e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      if (this.retrieve) {
        retrieveFile(zipFile, context, getZipFilePath(), props, report);
        report.setMessage(
            localStrings.getLocalString(
                "collectlogfiles.instance.success",
                "Created Zip file under {0}.",
                retrieveFilePath + File.separator + new File(zipFile).getName()));
      } else {
        report.setMessage(
            localStrings.getLocalString(
                "collectlogfiles.instance.success", "Created Zip file under {0}.", zipFile));
      }

    } else if (targetServer != null && targetServer.isInstance()) {

      // This loop if target standalone instance
      String instanceName = targetServer.getName();
      String serverNode = targetServer.getNodeRef();
      Node node = domain.getNodes().getNode(serverNode);
      String zipFile = "";
      File targetDir = null;

      String logFileDetails = "";
      try {
        // getting log file values from logging.propertie file.
        logFileDetails = getInstanceLogFileDirectory(targetServer);
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errGettingLogFiles",
                "Error while getting log file attribute for {0}.",
                target);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      targetDir = makingDirectoryOnDas(targetServer.getName(), report);

      try {
        if (node.isLocal()) {
          String sourceDir = getLogDirForLocalNode(logFileDetails, node, serverNode, instanceName);
          copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, instanceName);
        } else {
          new LogFilterForInstance()
              .downloadAllInstanceLogFiles(
                  habitat,
                  targetServer,
                  domain,
                  LOGGER,
                  instanceName,
                  targetDir.getAbsolutePath(),
                  logFileDetails);
        }
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errInstanceDownloading",
                "Error while downloading log files from {0}.",
                instanceName);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      try {
        // Creating zip file and returning zip file absolute path.
        String zipFilePath = getZipFilePath().getAbsolutePath();
        zipFile = loggingConfig.createZipFile(zipFilePath);
        if (zipFile == null || new File(zipFile) == null) {
          // Failure during zip
          final String errorMsg =
              localStrings.getLocalString(
                  "collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFilePath);
          report.setMessage(errorMsg);
          report.setActionExitCode(ActionReport.ExitCode.FAILURE);
          return;
        }
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
        report.setMessage(errorMsg);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      if (this.retrieve) {
        retrieveFile(zipFile, context, getZipFilePath(), props, report);
        report.setMessage(
            localStrings.getLocalString(
                "collectlogfiles.instance.success",
                "Created Zip file under {0}.",
                retrieveFilePath + File.separator + new File(zipFile).getName()));
      } else {
        report.setMessage(
            localStrings.getLocalString(
                "collectlogfiles.instance.success", "Created Zip file under {0}.", zipFile));
      }

    } else {
      // This loop if target is cluster

      String finalMessage = "";
      String zipFile = "";
      File targetDir = null;

      // code to download server.log file for DAS. Bug fix 16088
      String logFileDetails = "";
      try {
        // getting log file values from logging.propertie file.
        logFileDetails = loggingConfig.getLoggingFileDetails();
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errGettingLogFiles",
                "Error while getting log file attribute for {0}.",
                target);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }

      targetDir =
          makingDirectoryOnDas(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME, report);

      try {
        String sourceDir = "";
        if (logFileDetails.contains("${com.sun.aas.instanceRoot}/logs")) {
          sourceDir = env.getDomainRoot() + File.separator + "logs";
        } else {
          sourceDir = logFileDetails.substring(0, logFileDetails.lastIndexOf(File.separator));
        }

        copyLogFilesForLocalhost(
            sourceDir,
            targetDir.getAbsolutePath(),
            report,
            SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME);
      } catch (Exception ex) {
        final String errorMsg =
            localStrings.getLocalString(
                "collectlogfiles.errInstanceDownloading",
                "Error while downloading log files from {0}.",
                target);
        report.setMessage(errorMsg);
        report.setFailureCause(ex);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
      }
      /** *************************************************** */
      com.sun.enterprise.config.serverbeans.Cluster cluster = domain.getClusterNamed(target);

      List<Server> instances = cluster.getInstances();

      int instanceCount = 0;
      int errorCount = 0;
      for (Server instance : instances) {
        // downloading log files for all instances which is part of cluster under temp directory.
        String instanceName = instance.getName();
        String serverNode = instance.getNodeRef();
        Node node = domain.getNodes().getNode(serverNode);
        boolean errorOccur = false;
        instanceCount++;

        logFileDetails = "";
        try {
          // getting log file values from logging.propertie file.
          logFileDetails = getInstanceLogFileDirectory(domain.getServerNamed(instanceName));
        } catch (Exception ex) {
          final String errorMsg =
              localStrings.getLocalString(
                  "collectlogfiles.errGettingLogFiles",
                  "Error while getting log file attribute for {0}.",
                  target);
          report.setMessage(errorMsg);
          report.setFailureCause(ex);
          report.setActionExitCode(ActionReport.ExitCode.FAILURE);
          return;
        }

        try {
          targetDir = makingDirectoryOnDas(instanceName, report);

          if (node.isLocal()) {
            String sourceDir =
                getLogDirForLocalNode(logFileDetails, node, serverNode, instanceName);
            copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, instanceName);
          } else {
            new LogFilterForInstance()
                .downloadAllInstanceLogFiles(
                    habitat,
                    instance,
                    domain,
                    LOGGER,
                    instanceName,
                    targetDir.getAbsolutePath(),
                    logFileDetails);
          }
        } catch (Exception ex) {
          errorCount++;
          final String errorMsg =
              localStrings.getLocalString(
                  "collectlogfiles.errInstanceDownloading",
                  "Error while downloading log files from {0}.",
                  instanceName);
          errorOccur = true;
          finalMessage += errorMsg + "\n";
        }
        if (!errorOccur) {
          final String successMsg =
              localStrings.getLocalString(
                  "collectlogfiles.successInstanceDownloading",
                  "Log files are downloaded for {0}.",
                  instanceName);
          finalMessage += successMsg + "\n";
        }
      }
      report.setMessage(finalMessage);

      if (instanceCount != errorCount) {
        try {
          String zipFilePath = getZipFilePath().getAbsolutePath();
          // Creating zip file and returning zip file absolute path.
          zipFile = loggingConfig.createZipFile(zipFilePath);
          if (zipFile == null || new File(zipFile) == null) {
            // Failure during zip
            final String errorMsg =
                localStrings.getLocalString(
                    "collectlogfiles.creatingZip",
                    "Error while creating zip file {0}.",
                    zipFilePath);
            report.setMessage(errorMsg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
          }
        } catch (Exception ex) {
          final String errorMsg =
              localStrings.getLocalString(
                  "collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
          report.setMessage(errorMsg);
          report.setFailureCause(ex);
          report.setActionExitCode(ActionReport.ExitCode.FAILURE);
          return;
        }

        if (this.retrieve) {
          retrieveFile(zipFile, context, getZipFilePath(), props, report);
          report.setMessage(
              localStrings.getLocalString(
                  "collectlogfiles.cluster.success",
                  "{0} Created Zip file under {1}.",
                  finalMessage,
                  retrieveFilePath + File.separator + new File(zipFile).getName()));
        } else {
          report.setMessage(
              localStrings.getLocalString(
                  "collectlogfiles.cluster.success",
                  "{0} Created Zip file under {1}.",
                  finalMessage,
                  zipFile));
        }

        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
      } else if (instanceCount == 0) {
        report.setMessage(
            localStrings.getLocalString(
                "collectlogfiles.noinstance",
                "No instances are defined as part of {0}. So there are no files to zip.",
                target));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      } else {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      }
    }

    deleteDir(
        new File(
            env.getInstanceRoot() + File.separator + "collected-logs" + File.separator + "logs"));
  }