Skip to content

Commit

Permalink
Convert license tools to use unified cli entrypoint (#85919)
Browse files Browse the repository at this point in the history
The license tools exists separate from the normal cli tools since they
are not part of the Elasticsearch distribution. However, they still
utilize the Elasticsearch cli infrastructure. This commit adds a common
cli base script that calls the cli launcher and converts each of the
license tools to use it.

relates #85758
  • Loading branch information
rjernst authored Apr 18, 2022
1 parent a672936 commit 7a99254
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 122 deletions.
38 changes: 1 addition & 37 deletions x-pack/license-tools/bin/key-pair-generator
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.

CDPATH=""
SCRIPT="$0"

# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
# Drop everything prior to ->
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

# determine license home
LICENSE_HOME=`dirname "$SCRIPT"`/..

# make LICENSE_HOME absolute
LICENSE_HOME=`cd "$LICENSE_HOME"; pwd`

# setup classpath
LICENSE_CLASSPATH=$LICENSE_CLASSPATH:$LICENSE_HOME/lib/*

if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA=$JAVA_HOME/bin/java
else
JAVA=`which java`
fi

exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool "$@"

source "`dirname "$0"`"/license-cli
47 changes: 47 additions & 0 deletions x-pack/license-tools/bin/license-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.

CDPATH=""
SCRIPT="$0"

# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
# Drop everything prior to ->
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

# determine license home
LICENSE_HOME=`dirname "$SCRIPT"`/..
# make LICENSE_HOME absolute
LICENSE_HOME=`cd "$LICENSE_HOME"; pwd`

# setup classpath
LICENSE_CLASSPATH=$LICENSE_CLASSPATH:$LICENSE_HOME/lib/*

if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA=$JAVA_HOME/bin/java
else
JAVA=`which java`
fi

JAVA_OPTS="$JAVA_OPTS -Xmx64m -Xms16m"
CLI_NAME=$(basename $0)

exec \
"$JAVA" \
$JAVA_OPTS \
-Dcli.name="$CLI_NAME" \
-Des.path.home="`pwd`" \
-cp "$LICENSE_CLASSPATH" \
org.elasticsearch.launcher.CliToolLauncher \
"$@"
37 changes: 1 addition & 36 deletions x-pack/license-tools/bin/license-generator
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.

CDPATH=""
SCRIPT="$0"

# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
# Drop everything prior to ->
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

# determine license home
LICENSE_HOME=`dirname "$SCRIPT"`/..

# make LICENSE_HOME absolute
LICENSE_HOME=`cd "$LICENSE_HOME"; pwd`

# setup classpath
LICENSE_CLASSPATH=$LICENSE_CLASSPATH:$LICENSE_HOME/lib/*

if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA=$JAVA_HOME/bin/java
else
JAVA=`which java`
fi

exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseGeneratorTool "$@"
source "`dirname "$0"`"/license-cli
38 changes: 1 addition & 37 deletions x-pack/license-tools/bin/verify-license
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.

CDPATH=""
SCRIPT="$0"

# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
# Drop everything prior to ->
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

# determine license home
LICENSE_HOME=`dirname "$SCRIPT"`/..

# make LICENSE_HOME absolute
LICENSE_HOME=`cd "$LICENSE_HOME"; pwd`

# setup classpath
LICENSE_CLASSPATH=$LICENSE_CLASSPATH:$LICENSE_HOME/lib/*

if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA=$JAVA_HOME/bin/java
else
JAVA=`which java`
fi

exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseVerificationTool "$@"

source "`dirname "$0"`"/license-cli
1 change: 1 addition & 0 deletions x-pack/license-tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'elasticsearch.build'
dependencies {
api project(':x-pack:plugin:core')
api project(':server')
api project(':distribution:tools:cli-launcher')
testImplementation project(':test:framework')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ public KeyPairGeneratorTool() {
this.privateKeyPathOption = parser.accepts("privateKeyPath", "private key path").withRequiredArg().required();
}

public static void main(String[] args) throws Exception {
exit(new KeyPairGeneratorTool().main(args, Terminal.DEFAULT));
}

@Override
protected void printAdditionalHelp(Terminal terminal) {
terminal.println("This tool generates and saves a key pair to the provided publicKeyPath");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.license.licensor.tools;

import org.elasticsearch.cli.CliToolProvider;
import org.elasticsearch.cli.Command;

public class KeyPairGeneratorToolProvider implements CliToolProvider {
@Override
public String name() {
return "key-pair-generator";
}

@Override
public Command create() {
return new KeyPairGeneratorTool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public LicenseGeneratorTool() {
licenseFileOption = parser.accepts("licenseFile", "license json spec file").withRequiredArg();
}

public static void main(String[] args) throws Exception {
exit(new LicenseGeneratorTool().main(args, Terminal.DEFAULT));
}

@Override
protected void printAdditionalHelp(Terminal terminal) {
terminal.println("This tool generate elasticsearch license(s) for the provided");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.license.licensor.tools;

import org.elasticsearch.cli.CliToolProvider;
import org.elasticsearch.cli.Command;

public class LicenseGeneratorToolProvider implements CliToolProvider {
@Override
public String name() {
return "license-generator";
}

@Override
public Command create() {
return new LicenseGeneratorTool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public LicenseVerificationTool() {
licenseFileOption = parser.accepts("licenseFile", "license json spec file").withRequiredArg();
}

public static void main(String[] args) throws Exception {
exit(new LicenseVerificationTool().main(args, Terminal.DEFAULT));
}

@Override
protected void execute(Terminal terminal, OptionSet options) throws Exception {
Path publicKeyPath = parsePath(publicKeyPathOption.value(options));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.license.licensor.tools;

import org.elasticsearch.cli.CliToolProvider;
import org.elasticsearch.cli.Command;

public class LicenseVerificationToolProvider implements CliToolProvider {
@Override
public String name() {
return "verify-license";
}

@Override
public Command create() {
return new LicenseVerificationTool();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.elasticsearch.license.licensor.tools.LicenseGeneratorToolProvider
org.elasticsearch.license.licensor.tools.LicenseVerificationToolProvider
org.elasticsearch.license.licensor.tools.KeyPairGeneratorToolProvider

0 comments on commit 7a99254

Please sign in to comment.