Skip to content

Commit

Permalink
Uses Options class to build the options supported by tool
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 11, 2023
1 parent 6c6e80f commit 5c675e5
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 additions & 40 deletions src/main/java/org/opensearch/security/tools/democonfig/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
* This class installs demo configuration for security plugin
*/
Expand Down Expand Up @@ -57,9 +64,14 @@ public class Installer {

static File RPM_DEB_OPENSEARCH_HOME = new File("/usr/share/opensearch");

private final Options options;

private final HelpFormatter formatter = new HelpFormatter();

private Installer() {
this.OS = System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch");
FILE_EXTENSION = OS.toLowerCase().contains("win") ? ".bat" : ".sh";
options = new Options();
}

public static Installer getInstance() {
Expand All @@ -84,9 +96,42 @@ public void installDemoConfiguration(String[] options) {

public static void main(String[] options) {
Installer installer = Installer.getInstance();
installer.buildOptions();
installer.installDemoConfiguration(options);
}

/**
* Builds options supported by this tool
*/
void buildOptions() {
options.addOption("h", "show-help", false, "Shows help for this tool.");
options.addOption("y", "answer-yes-to-all-prompts", false, "Confirm all installation dialogues automatically.");
options.addOption(
"i",
"initialize-security",
false,
"Initialize Security plugin with default configuration (default is to ask if -y is not given)."
);
options.addOption(
"c",
"enable-cluster-mode",
false,
"Enable cluster mode by binding to all network interfaces (default is to ask if -y is not given)."
);
options.addOption(
"s",
"skip-updates-when-already-configured",
false,
"Skip updates if config is already applied to opensearch.yml."
);
options.addOption(
"t",
"test-execution-environment",
false,
"Set the execution environment to `test` to skip password validation. Should be used only for testing. (default is set to `demo`)"
);
}

/**
* Prints headers that indicate the start of script execution
*/
Expand All @@ -97,52 +142,37 @@ static void printScriptHeaders() {

/**
* Reads the options passed to the script
* @param options an array of strings containing options passed to the script
* @param args an array of strings containing options passed to the script
*/
void readOptions(String[] options) {
void readOptions(String[] args) {
// set script execution dir
SCRIPT_DIR = options[0];

for (int i = 1; i < options.length; i++) {
switch (options[i]) {
case "-y":
assumeyes = true;
break;
case "-i":
initsecurity = true;
break;
case "-c":
cluster_mode = true;
break;
case "-s":
skip_updates = 0;
break;
case "-t":
environment = ExecutionEnvironment.TEST;
break;
case "-h":
case "-?":
showHelp();
return;
default:
System.out.println("Invalid option: " + options[i]);
SCRIPT_DIR = args[0];

CommandLineParser parser = new DefaultParser();
try {
CommandLine line = parser.parse(options, args);

if (line.hasOption("h")) {
showHelp();
return;
}
assumeyes = line.hasOption("y");
initsecurity = line.hasOption("i");
cluster_mode = line.hasOption("c");
skip_updates = line.hasOption("s") ? 0 : -1;
environment = line.hasOption("t") ? ExecutionEnvironment.TEST : environment;

} catch (ParseException exp) {
System.out.println("ERR: Parsing failed. Reason: " + exp.getMessage());
System.exit(-1);
}
}

/**
* Prints the help menu when -h option is passed
*/
void showHelp() {
System.out.println("install_demo_configuration.sh [-y] [-i] [-c]");
System.out.println(" -h show help");
System.out.println(" -y confirm all installation dialogues automatically");
System.out.println(" -i initialize Security plugin with default configuration (default is to ask if -y is not given)");
System.out.println(" -c enable cluster mode by binding to all network interfaces (default is to ask if -y is not given)");
System.out.println(" -s skip updates if config is already applied to opensearch.yml");
System.out.println(
" -t set the execution environment to `test` to skip password validation. Should be used only for testing. (default is set to `demo`)"
);
formatter.printHelp("install_demo_configuration.sh", options, true);
System.exit(0);
}

Expand Down Expand Up @@ -394,8 +424,4 @@ void finishScriptExecution() {
System.out.println(e.getMessage());
}
}

public static void resetInstance() {
instance = null;
}
}

0 comments on commit 5c675e5

Please sign in to comment.