All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.management.internal.cli.commands.ConfigCommands Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.management.internal.cli.commands;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;

import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.cache.CacheClosedException;
import com.gemstone.gemfire.cache.execute.FunctionInvocationTargetException;
import com.gemstone.gemfire.cache.execute.ResultCollector;
import com.gemstone.gemfire.distributed.DistributedMember;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.management.cli.CliMetaData;
import com.gemstone.gemfire.management.cli.ConverterHint;
import com.gemstone.gemfire.management.cli.Result;
import com.gemstone.gemfire.management.internal.cli.AbstractCliAroundInterceptor;
import com.gemstone.gemfire.management.internal.cli.CliUtil;
import com.gemstone.gemfire.management.internal.cli.GfshParseResult;
import com.gemstone.gemfire.management.internal.cli.domain.MemberConfigurationInfo;
import com.gemstone.gemfire.management.internal.cli.functions.AlterRuntimeConfigFunction;
import com.gemstone.gemfire.management.internal.cli.functions.CliFunctionResult;
import com.gemstone.gemfire.management.internal.cli.functions.ExportConfigFunction;
import com.gemstone.gemfire.management.internal.cli.functions.GetMemberConfigInformationFunction;
import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings;
import com.gemstone.gemfire.management.internal.cli.result.CommandResultException;
import com.gemstone.gemfire.management.internal.cli.result.CompositeResultData;
import com.gemstone.gemfire.management.internal.cli.result.CompositeResultData.SectionResultData;
import com.gemstone.gemfire.management.internal.cli.result.ErrorResultData;
import com.gemstone.gemfire.management.internal.cli.result.InfoResultData;
import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder;
import com.gemstone.gemfire.management.internal.cli.result.TabularResultData;
import com.gemstone.gemfire.management.internal.cli.shell.Gfsh;
/****
 *
 * @author David Hoots
 *         Sourabh Bansod
 * @since 7.0
 *
 */
public class ConfigCommands implements CommandMarker {
  private final ExportConfigFunction exportConfigFunction = new ExportConfigFunction();
  private final GetMemberConfigInformationFunction getMemberConfigFunction = new GetMemberConfigInformationFunction();
  private final AlterRuntimeConfigFunction alterRunTimeConfigFunction = new AlterRuntimeConfigFunction();
  private static Gfsh getGfsh() {
    return Gfsh.getCurrentInstance();
  }

  @CliCommand(value = { CliStrings.DESCRIBE_CONFIG }, help = CliStrings.DESCRIBE_CONFIG__HELP)
  @CliMetaData(shellOnly = false, relatedTopic = {CliStrings.TOPIC_GEMFIRE_CONFIG})
  public Result describeConfig(
      @CliOption (key = CliStrings.DESCRIBE_CONFIG__MEMBER,
      optionContext = ConverterHint.ALL_MEMBER_IDNAME,
      help = CliStrings.DESCRIBE_CONFIG__MEMBER__HELP,
      mandatory = true)

      String memberNameOrId,
      @CliOption (key = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS,
      help = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS__HELP,
      unspecifiedDefaultValue="true",
      specifiedDefaultValue="true")
      boolean hideDefaults) {

    Result result = null;
    try {
      DistributedMember targetMember = null;

      if (memberNameOrId != null && !memberNameOrId.isEmpty()) {
        targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
      }
      if (targetMember != null) {
        ResultCollector rc = CliUtil.executeFunction(getMemberConfigFunction, new Boolean(hideDefaults), targetMember);
        ArrayList output = (ArrayList) rc.getResult();
        Object obj = output.get(0);

        if (obj != null && obj instanceof MemberConfigurationInfo) {
          MemberConfigurationInfo memberConfigInfo = (MemberConfigurationInfo) obj;

          CompositeResultData crd = ResultBuilder.createCompositeResultData();
          crd.setHeader(CliStrings.format(CliStrings.DESCRIBE_CONFIG__HEADER__TEXT, memberNameOrId));

          List jvmArgsList = memberConfigInfo.getJvmInputArguments();
          TabularResultData jvmInputArgs = crd.addSection().addSection().addTable();

          for (String jvmArg : jvmArgsList) {
            jvmInputArgs.accumulate("JVM command line arguments", jvmArg);
          }

          addSection(crd, memberConfigInfo.getGfePropsSetUsingApi(), "GemFire properties defined using the API");
          addSection(crd, memberConfigInfo.getGfePropsRuntime(), "GemFire properties defined at the runtime");
          addSection(crd, memberConfigInfo.getGfePropsSetFromFile(), "GemFire properties defined with the property file");
          addSection(crd, memberConfigInfo.getGfePropsSetWithDefaults(), "GemFire properties using default values");
          addSection(crd, memberConfigInfo.getCacheAttributes(), "Cache attributes");

          List> cacheServerAttributesList = memberConfigInfo.getCacheServerAttributes();

          if (cacheServerAttributesList != null && !cacheServerAttributesList.isEmpty()) {
            SectionResultData cacheServerSection = crd.addSection();
            cacheServerSection.setHeader("Cache-server attributes");

            Iterator> iters = cacheServerAttributesList.iterator();

            while (iters.hasNext()) {
              Map cacheServerAttributes = iters.next();
              addSubSection(cacheServerSection, cacheServerAttributes, "");
            }
          }
          result = ResultBuilder.buildResult(crd);
        }

      } else {
        ErrorResultData erd = ResultBuilder.createErrorResultData();
        erd.addLine(CliStrings.format(CliStrings.DESCRIBE_CONFIG__MEMBER__NOT__FOUND, new Object[] {memberNameOrId}));
        result = ResultBuilder.buildResult(erd);
      }
    } catch (FunctionInvocationTargetException e) {
      result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, CliStrings.DESCRIBE_CONFIG));
    } catch (Exception e) {
      ErrorResultData erd = ResultBuilder.createErrorResultData();
      erd.addLine(e.getMessage());
      result = ResultBuilder.buildResult(erd);
    }
    return result;
  }


  private void addSection(CompositeResultData crd, Map attrMap, String headerText) {
    if (attrMap != null && !attrMap.isEmpty()) {
      SectionResultData section = crd.addSection();
      section.setHeader(headerText);
      section.addSeparator('.');
      Set attributes = new TreeSet(attrMap.keySet());

      for (String attribute : attributes) {
        String attributeValue = attrMap.get(attribute);
        section.addData(attribute, attributeValue);
      }
    }
  }

  private void addSubSection (SectionResultData section, Map attrMap, String headerText) {
    if (!attrMap.isEmpty()) {
      SectionResultData subSection = section.addSection();
      Set attributes = new TreeSet(attrMap.keySet());
      subSection.setHeader(headerText);

      for (String attribute : attributes) {
        String attributeValue = attrMap.get(attribute);
        subSection.addData(attribute, attributeValue);
      }
    }
  }
  /**
   * Export the cache configuration in XML format.
   *
   * @param member
   *          Member for which to write the configuration
   * @param group
   *          Group or groups for which to write the configuration
   * @return Results of the attempt to write the configuration
   */
  @CliCommand(value = { CliStrings.EXPORT_CONFIG }, help = CliStrings.EXPORT_CONFIG__HELP)
  @CliMetaData(interceptor = "com.gemstone.gemfire.management.internal.cli.commands.ConfigCommands$Interceptor", relatedTopic = {CliStrings.TOPIC_GEMFIRE_CONFIG})
  public Result exportConfig(
      @CliOption(key = { CliStrings.EXPORT_CONFIG__MEMBER },
                 optionContext = ConverterHint.ALL_MEMBER_IDNAME,
                 help = CliStrings.EXPORT_CONFIG__MEMBER__HELP)
      @CliMetaData (valueSeparator = ",")
                  String member,
      @CliOption(key = { CliStrings.EXPORT_CONFIG__GROUP },
                 optionContext = ConverterHint.MEMBERGROUP,
                 help = CliStrings.EXPORT_CONFIG__GROUP__HELP)
      @CliMetaData (valueSeparator = ",")
                  String group,
      @CliOption(key = { CliStrings.EXPORT_CONFIG__DIR },
                 help = CliStrings.EXPORT_CONFIG__DIR__HELP)
                  String dir) {
    InfoResultData infoData = ResultBuilder.createInfoResultData();

    Set targetMembers;
    try {
      targetMembers = CliUtil.findAllMatchingMembers(group, member);
    } catch (CommandResultException crex) {
      return crex.getResult();
    }

    try {
      ResultCollector rc = CliUtil.executeFunction(this.exportConfigFunction, null, targetMembers);
      List results = CliFunctionResult.cleanResults((List) rc.getResult());

      for (CliFunctionResult result : results) {
        if (result.getThrowable() != null) {
          infoData.addLine(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, result.getMemberIdOrName(), result
              .getThrowable()));
        } else if (result.isSuccessful()) {
          String cacheFileName = result.getMemberIdOrName() + "-cache.xml";
          String propsFileName = result.getMemberIdOrName() + "-gf.properties";
          String[] fileContent = (String[]) result.getSerializables();
          infoData.addAsFile(cacheFileName, fileContent[0], "Downloading Cache XML file: {0}", false);
          infoData.addAsFile(propsFileName, fileContent[1], "Downloading properties file: {0}", false);
        }
      }
      return ResultBuilder.buildResult(infoData);
    } catch (VirtualMachineError e) {
      SystemFailure.initiateFailure(e);
      throw e;
    } catch (Throwable th) {
      SystemFailure.checkFailure();
      th.printStackTrace(System.err);
      return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, th.getClass()
          .getName()
          + ": " + th.getMessage()));
    }
  }


  @CliCommand(value = { CliStrings.ALTER_RUNTIME_CONFIG }, help = CliStrings.ALTER_RUNTIME_CONFIG__HELP)
  @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEMFIRE_CONFIG})
  public Result alterRuntimeConfig(
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__MEMBER},
      optionContext = ConverterHint.ALL_MEMBER_IDNAME,
      help = CliStrings.ALTER_RUNTIME_CONFIG__MEMBER__HELP) String memberNameOrId,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__GROUP},
      optionContext = ConverterHint.MEMBERGROUP,
      help = CliStrings.ALTER_RUNTIME_CONFIG__MEMBER__HELP) String group,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__DISK__SPACE__LIMIT},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__DISK__SPACE__LIMIT__HELP) Integer archiveDiskSpaceLimit,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__FILE__SIZE__LIMIT},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__FILE__SIZE__LIMIT__HELP) Integer archiveFileSizeLimit,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__LOG__DISK__SPACE__LIMIT},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__LOG__DISK__SPACE__LIMIT__HELP) Integer logDiskSpaceLimit,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__LOG__FILE__SIZE__LIMIT},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__LOG__FILE__SIZE__LIMIT__HELP) Integer logFileSizeLimit,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__LOG__LEVEL},
      optionContext = ConverterHint.LOG_LEVEL,
      help = CliStrings.ALTER_RUNTIME_CONFIG__LOG__LEVEL__HELP ) String logLevel,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__ARCHIVE__FILE},
      help = CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__ARCHIVE__FILE__HELP) String statisticArchiveFile,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__SAMPLE__RATE},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__SAMPLE__RATE__HELP) Integer statisticSampleRate,
      @CliOption (key = {CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__SAMPLING__ENABLED},
      unspecifiedDefaultValue=CliMetaData.ANNOTATION_NULL_VALUE,
      help = CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__SAMPLING__ENABLED__HELP) Boolean statisticSamplingEnabled) {

    Map runTimeAttributes = new HashMap();
    Set targetMembers = new HashSet();
    try {

      targetMembers = CliUtil.findAllMatchingMembers(group, memberNameOrId);

      if (archiveDiskSpaceLimit != null) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__DISK__SPACE__LIMIT, archiveDiskSpaceLimit.toString());
      }

      if (archiveFileSizeLimit != null) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__ARCHIVE__FILE__SIZE__LIMIT, archiveFileSizeLimit.toString());
      }

      if (logDiskSpaceLimit != null) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__LOG__DISK__SPACE__LIMIT, logDiskSpaceLimit.toString());
      }

      if (logFileSizeLimit != null) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__LOG__FILE__SIZE__LIMIT, logFileSizeLimit.toString());
      }

      if (logLevel != null && !logLevel.isEmpty()) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__LOG__LEVEL, logLevel);
      }

      if (statisticArchiveFile != null && !statisticArchiveFile.isEmpty()) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__ARCHIVE__FILE, statisticArchiveFile);
      }

      if (statisticSampleRate != null) {
        runTimeAttributes.put(CliStrings.ALTER_RUNTIME_CONFIG__STATISTIC__SAMPLE__RATE, statisticSampleRate.toString());
      }

      if (statisticSamplingEnabled != null) {
        runTimeAttributes.put(DistributionConfig.STATISTIC_SAMPLING_ENABLED_NAME, statisticSamplingEnabled.toString());
      }

      if (!runTimeAttributes.isEmpty()) {
        ResultCollector rc = CliUtil.executeFunction(alterRunTimeConfigFunction, runTimeAttributes, targetMembers);
        List results = CliFunctionResult.cleanResults((List) rc.getResult());
        CompositeResultData crd = ResultBuilder.createCompositeResultData();
        TabularResultData tabularData = crd.addSection().addTable();
        Set successfulMembers = new TreeSet();
        Set errorMessages = new TreeSet();

        //Map> errorsToMemberMap = new HashMap>();

        for (CliFunctionResult result : results) {
          if (result.getThrowable() != null) {
            //errorsToMemberMap.put(result.getMemberIdOrName(), result.getThrowable().getMessage());
            errorMessages.add(result.getThrowable().getMessage());
            //errorsToMemberMap.put(result.getThrowable().getMessage(), result.getMemberIdOrName());
          } else {
            successfulMembers.add(result.getMemberIdOrName());
          }
        }
        final String lineSeparator = System.getProperty("line.separator");
        if (!successfulMembers.isEmpty()) {
          StringBuilder successMessageBuilder = new StringBuilder();

          successMessageBuilder.append(CliStrings.ALTER_RUNTIME_CONFIG__SUCCESS__MESSAGE);
          successMessageBuilder.append(lineSeparator);

          for (String member : successfulMembers) {
            successMessageBuilder.append(member);
            successMessageBuilder.append(lineSeparator);
          }
          return ResultBuilder.createInfoResult(successMessageBuilder.toString());

        } else {
          StringBuilder errorMessageBuilder = new StringBuilder();
          errorMessageBuilder.append("Following errors occurred while altering runtime config");
          errorMessageBuilder.append(lineSeparator);

          for (String errorMessage : errorMessages){
            errorMessageBuilder.append(errorMessage);
            errorMessageBuilder.append(lineSeparator);
          }

          return ResultBuilder.createUserErrorResult(errorMessageBuilder.toString());
        }
      } else {
        return ResultBuilder.createUserErrorResult(CliStrings.ALTER_RUNTIME_CONFIG__RELEVANT__OPTION__MESSAGE);
      }
    } catch (CommandResultException crex) {
      return crex.getResult();
    }catch (CacheClosedException e) {
      return ResultBuilder.createGemFireErrorResult(e.getMessage());
    } catch (FunctionInvocationTargetException e) {
      return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, CliStrings.ALTER_RUNTIME_CONFIG));
    } catch (Exception e) {
      return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass(), e.getMessage()));
    }
  }
  @CliAvailabilityIndicator({ CliStrings.DESCRIBE_CONFIG, CliStrings.EXPORT_CONFIG, CliStrings.ALTER_RUNTIME_CONFIG})
  public boolean configCommandsAvailable() {
    boolean isAvailable = true; // always available on server
    if (CliUtil.isGfshVM()) { // in gfsh check if connected
      isAvailable = getGfsh() != null && getGfsh().isConnectedAndReady();
    }
    return isAvailable;
  }

  /**
   * Interceptor used by gfsh to intercept execution of export config command at "shell".
   */
  public static class Interceptor extends AbstractCliAroundInterceptor {
    private String saveDirString;

    @Override
    public Result preExecution(GfshParseResult parseResult) {
      Map paramValueMap = parseResult.getParamValueStrings();
      String dir = paramValueMap.get("dir");
      dir = (dir == null) ? null : dir.trim();

      File saveDirFile = new File(".");
      if (dir != null && !dir.isEmpty()) {
        saveDirFile = new File(dir);
        if (saveDirFile.exists()) {
          if (!saveDirFile.isDirectory())
            return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__NOT_A_DIRECTORY, dir));
        } else if (!saveDirFile.mkdirs()) {
          return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__CANNOT_CREATE_DIR, dir));
        }
      }
      try {
        if (!saveDirFile.canWrite()) {
          return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__NOT_WRITEABLE, saveDirFile
              .getCanonicalPath()));
        }
      } catch (IOException ioex) {
        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__NOT_WRITEABLE, saveDirFile
            .getName()));
      }

      saveDirString = saveDirFile.getAbsolutePath();
      return ResultBuilder.createInfoResult("OK");
    }

    @Override
    public Result postExecution(GfshParseResult parseResult, Result commandResult) {
      if (commandResult.hasIncomingFiles()) {
        try {
          commandResult.saveIncomingFiles(saveDirString);
        } catch (IOException ioex) {
          getGfsh().logSevere("Unable to export config", ioex);
        }
      }

      return commandResult;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy