You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kibble.apache.org by tu...@apache.org on 2020/11/14 17:48:50 UTC
[kibble] branch main updated: Refactor setup scripts as kibble cli
command (#91)
This is an automated email from the ASF dual-hosted git repository.
turbaszek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/kibble.git
The following commit(s) were added to refs/heads/main by this push:
new 7d70dd5 Refactor setup scripts as kibble cli command (#91)
7d70dd5 is described below
commit 7d70dd5644454876ce3403d945ae823449eb788e
Author: Sharvil Kekre <sh...@gmail.com>
AuthorDate: Sat Nov 14 09:48:42 2020 -0800
Refactor setup scripts as kibble cli command (#91)
---
docker-compose-dev.yaml | 2 +-
kibble/__main__.py | 69 +++++++++++-
kibble/{setup/setup.py => cli/setup_command.py} | 136 ++++++------------------
setup.py | 1 +
4 files changed, 103 insertions(+), 105 deletions(-)
diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml
index 7744b99..3e67c36 100644
--- a/docker-compose-dev.yaml
+++ b/docker-compose-dev.yaml
@@ -7,7 +7,7 @@ services:
build:
context: .
dockerfile: Dockerfile.dev
- command: bash -c "python kibble/setup/setup.py --autoadmin --skiponexist"
+ command: bash -c "kibble setup --autoadmin --skiponexist"
volumes:
- .:/kibble/
depends_on:
diff --git a/kibble/__main__.py b/kibble/__main__.py
index aea89fd..b63d27c 100644
--- a/kibble/__main__.py
+++ b/kibble/__main__.py
@@ -15,9 +15,76 @@
# specific language governing permissions and limitations
# under the License.
+import click
+
+from kibble.cli import setup_command
+from kibble.version import version as kibble_version
+
+from kibble.configuration import conf
+
+
+@click.group()
+def cli():
+ """A simple command line tool for kibble"""
+
+
+@cli.command("version", short_help="displays the current kibble version")
+def version():
+ click.echo(kibble_version)
+
+
+@cli.command("setup", short_help="starts the setup process for kibble")
+@click.option(
+ "-u",
+ "--uri",
+ default=conf.get("elasticsearch", "conn_uri"),
+ help="connection uri for ElasticSearch",
+)
+@click.option(
+ "-d",
+ "--dbname",
+ default=conf.get("elasticsearch", "dbname"),
+ help="elasticsearch database prefix",
+)
+@click.option(
+ "-s",
+ "--shards",
+ default=conf.get("elasticsearch", "shards"),
+ help="number of ES shards",
+)
+@click.option(
+ "-r",
+ "--replicas",
+ default=conf.get("elasticsearch", "replicas"),
+ help="number of replicas for ES",
+)
+@click.option(
+ "-m", "--mailhost", default=conf.get("mail", "mailhost"), help="mail server host"
+)
+@click.option("-a", "--autoadmin", default=False, help="generate generic admin account")
+@click.option("-k", "--skiponexist", default=True, help="skip DB creation if DBs exist")
+def setup(
+ uri: str,
+ dbname: str,
+ shards: str,
+ replicas: str,
+ mailhost: str,
+ autoadmin: bool,
+ skiponexist: bool,
+):
+ setup_command.do_setup(
+ uri=uri,
+ dbname=dbname,
+ shards=shards,
+ replicas=replicas,
+ mailhost=mailhost,
+ autoadmin=autoadmin,
+ skiponexist=skiponexist,
+ )
+
def main():
- print("Hello to kibble!")
+ cli()
if __name__ == "__main__":
diff --git a/kibble/setup/setup.py b/kibble/cli/setup_command.py
similarity index 66%
rename from kibble/setup/setup.py
rename to kibble/cli/setup_command.py
index 79d67a9..842701c 100644
--- a/kibble/setup/setup.py
+++ b/kibble/cli/setup_command.py
@@ -15,12 +15,12 @@
# specific language governing permissions and limitations
# under the License.
-import sys
import os
-import argparse
+import sys
import logging
from getpass import getpass
+import click
import tenacity
import bcrypt
import json
@@ -28,63 +28,15 @@ from elasticsearch import Elasticsearch
from kibble.configuration import conf
-
KIBBLE_VERSION = conf.get("api", "version")
-KIBBLE_DB_VERSION = conf.get("api", "database") # database revision
+KIBBLE_DB_VERSION = conf.get("api", "database")
-if sys.version_info <= (3, 3):
- print("This script requires Python 3.4 or higher")
- sys.exit(-1)
-
-# Arguments for non-interactive setups like docker
-def get_parser():
- arg_parser = argparse.ArgumentParser()
- arg_parser.add_argument(
- "-e",
- "--conn-uri",
- help="Pre-defined connection uri for ElasticSearch.",
- default=conf.get("elasticsearch", "conn_uri"),
- )
- arg_parser.add_argument(
- "-d",
- "--dbname",
- help="Pre-defined Database prefix. Default: kibble",
- default=conf.get("elasticsearch", "dbname"),
- )
- arg_parser.add_argument(
- "-s",
- "--shards",
- help="Predefined number of ES shards, Default: 5",
- default=conf.get("elasticsearch", "shards"),
- )
- arg_parser.add_argument(
- "-r",
- "--replicas",
- help="Predefined number of replicas for ES. Default: 1",
- default=conf.get("elasticsearch", "replicas"),
- )
- arg_parser.add_argument(
- "-m",
- "--mailhost",
- help="Pre-defined mail server host. Default: localhost:25",
- default=conf.get("mail", "mailhost"),
- )
- arg_parser.add_argument(
- "-a",
- "--autoadmin",
- action="store_true",
- help="Generate generic admin account. Default: False",
- default=False,
- )
- arg_parser.add_argument(
- "-k",
- "--skiponexist",
- action="store_true",
- help="Skip DB creation if DBs exist. Defaul: True",
- default=True,
- )
- return arg_parser
+def get_user_input(msg: str, secure: bool = False):
+ value = None
+ while not value:
+ value = getpass(msg) if secure else input(msg)
+ return value
def create_es_index(
@@ -102,12 +54,13 @@ def create_es_index(
logging.getLogger("elasticsearch").setLevel(logging.ERROR)
mappings_json = os.path.join(
- os.path.dirname(os.path.realpath(__file__)), "mappings.json"
+ os.path.dirname(os.path.realpath(__file__)), "../setup/mappings.json"
)
with open(mappings_json, "r") as f:
mappings = json.load(f)
es = Elasticsearch([conn_uri], max_retries=5, retry_on_timeout=True)
+ print(es.info())
es_version = es.info()["version"]["number"]
es6 = int(es_version.split(".")[0]) >= 6
@@ -187,7 +140,7 @@ def create_es_index(
es.indices.create(
index=iname, body={"mappings": mappings["mappings"], "settings": settings}
)
- print(f"Indices created!")
+ print(f"Indices created!\n")
print()
salt = bcrypt.gensalt()
@@ -212,49 +165,30 @@ def create_es_index(
print("Account created!")
-def get_user_input(msg: str, secure: bool = False):
- value = None
- while not value:
- value = getpass(msg) if secure else input(msg)
- return value
-
-
-def print_configuration(args):
- print(
- "Configuring Apache Kibble elasticsearch instance with the following arguments:"
- )
- print(f"- conn_uri: {args.conn_uri}")
- print(f"- dbname: {args.dbname}")
- print(f"- shards: {int(args.shards)}")
- print(f"- replicas: {int(args.replicas)}")
- print()
-
-
-def main():
- """
- The main Kibble setup logic. Using users input we create:
- - Elasticsearch indexes used by Apache Kibble app
- - Configuration yaml file
- """
- parser = get_parser()
- args = parser.parse_args()
-
- print("Welcome to the Apache Kibble setup script!")
- print_configuration(args)
+def do_setup(
+ uri: str,
+ dbname: str,
+ shards: str,
+ replicas: str,
+ mailhost: str,
+ autoadmin: bool,
+ skiponexist: bool,
+):
+ click.echo("Welcome to the Apache Kibble setup script!")
admin_name = "admin@kibble"
admin_pass = "kibbleAdmin"
- if not args.autoadmin:
+ if not autoadmin:
admin_name = get_user_input(
- "Enter an email address for the administrator account:"
+ "Enter an email address for the administrator account: "
)
admin_pass = get_user_input(
- "Enter a password for the administrator account:", secure=True
+ "Enter a password for the administrator account: ", secure=True
)
# Create Elasticsearch index
# Retry in case ES is not yet up
- print(f"Elasticsearch: {args.conn_uri}")
+ click.echo(f"Elasticsearch: {uri}")
for attempt in tenacity.Retrying(
retry=tenacity.retry_if_exception_type(exception_types=Exception),
wait=tenacity.wait_fixed(10),
@@ -262,19 +196,15 @@ def main():
reraise=True,
):
with attempt:
- print("Trying to create ES index...")
+ click.echo("Trying to create ES index...")
create_es_index(
- conn_uri=args.conn_uri,
- dbname=args.dbname,
- shards=int(args.shards),
- replicas=int(args.replicas),
+ conn_uri=uri,
+ dbname=dbname,
+ shards=int(shards),
+ replicas=int(replicas),
admin_name=admin_name,
admin_pass=admin_pass,
- skiponexist=args.skiponexist,
+ skiponexist=skiponexist,
)
- print()
- print("All done, Kibble should...work now :)")
-
-
-if __name__ == "__main__":
- main()
+ click.echo()
+ click.echo("All done, Kibble should...work now :)")
diff --git a/setup.py b/setup.py
index dfa5ebd..568b316 100644
--- a/setup.py
+++ b/setup.py
@@ -33,6 +33,7 @@ DEVEL_REQUIREMENTS = ["black==20.8b1", "pre-commit==2.7.1", "pytest==6.1.1"]
INSTALL_REQUIREMENTS = [
"bcrypt==3.2.0",
"certifi==2020.6.20",
+ "click==7.1.2",
"elasticsearch==7.9.1",
"gunicorn==20.0.4",
"psutil==5.7.3",