You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kibble.apache.org by GitBox <gi...@apache.org> on 2020/11/13 01:29:54 UTC

[GitHub] [kibble] skekre98 commented on a change in pull request #91: Kibble initial cli

skekre98 commented on a change in pull request #91:
URL: https://github.com/apache/kibble/pull/91#discussion_r522551736



##########
File path: kibble/__main__.py
##########
@@ -15,9 +15,252 @@
 # specific language governing permissions and limitations
 # under the License.
 
+import sys
+import os
+import argparse
+import logging
+import click
+from getpass import getpass
+
+import tenacity
+import bcrypt
+import json
+from elasticsearch import Elasticsearch
+
+from kibble.version import version as kv
+from kibble.configuration import conf
+
+KIBBLE_VERSION = conf.get("api", "version")
+KIBBLE_DB_VERSION = conf.get("api", "database")
+
+
+def get_user_input(msg: str, secure: bool = False):
+    value = None
+    while not value:
+        value = getpass(msg) if secure else input(msg)
+    return value
+
+
+@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(kv)
+
+
+def create_es_index(
+    conn_uri: str,
+    dbname: str,
+    shards: int,
+    replicas: int,
+    admin_name: str,
+    admin_pass: str,
+    skiponexist: bool,
+):
+    """Creates Elasticsearch index used by Kibble"""
+
+    # elasticsearch logs lots of warnings on retries/connection failure
+    logging.getLogger("elasticsearch").setLevel(logging.ERROR)
+
+    mappings_json = os.path.join(
+        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
+    es7 = int(es_version.split(".")[0]) >= 7
+
+    if not es6:
+        print(
+            f"New Kibble installations require ElasticSearch 6.x or newer! "
+            f"You appear to be running {es_version}!"
+        )
+        sys.exit(-1)
+
+    # If ES >= 7, _doc is invalid and mapping should be rooted
+    if es7:
+        mappings["mappings"] = mappings["mappings"]["_doc"]
+
+    # Check if index already exists
+    if es.indices.exists(dbname + "_api"):
+        # Skip this is DB exists and -k added
+        if skiponexist:
+            print("DB prefix exists, but --skiponexist used, skipping this step.")
+            return
+        print("Error: ElasticSearch DB prefix '%s' already exists!" % dbname)
+        sys.exit(-1)
+
+    types = [
+        "api",
+        # ci_*: CI service stats
+        "ci_build",
+        "ci_queue",
+        # code_* + evolution + file_history: git repo stats
+        "code_commit",
+        "code_commit_unique",
+        "code_modification",
+        "evolution",
+        "file_history",
+        # forum_*: forum stats (SO, Discourse, Askbot etc)
+        "forum_post",
+        "forum_topic",
+        # GitHub stats
+        "ghstats",
+        # im_*: Instant messaging stats
+        "im_stats",
+        "im_ops",
+        "im_msg",
+        "issue",
+        "logstats",
+        # email, mail*: Email statitics
+        "email",
+        "mailstats",
+        "mailtop",
+        # organisation, view, source, publish: UI Org DB
+        "organisation",
+        "view",
+        "publish",
+        "source",
+        # stats: Miscellaneous stats
+        "stats",
+        # social_*: Twitter, Mastodon, Facebook etc
+        "social_follow",
+        "social_followers",
+        "social_follower",
+        "social_person",
+        # uisession, useraccount, message: UI user DB
+        "uisession",
+        "useraccount",
+        "message",
+        # person: contributor DB
+        "person",
+    ]
+
+    for t in types:
+        iname = f"{dbname}_{t}"
+        print(f"Creating index {iname}")
+
+        settings = {"number_of_shards": shards, "number_of_replicas": replicas}
+        es.indices.create(
+            index=iname, body={"mappings": mappings["mappings"], "settings": settings}
+        )
+    print(f"Indices created!")
+    print()
+
+    salt = bcrypt.gensalt()
+    pwd = bcrypt.hashpw(admin_pass.encode("utf-8"), salt).decode("ascii")
+    print("Creating administrator account")
+    doc = {
+        "email": admin_name,  # Username (email)
+        "password": pwd,  # Hashed password
+        "displayName": "Administrator",  # Display Name
+        "organisations": [],  # Orgs user belongs to (default is none)
+        "ownerships": [],  # Orgs user owns (default is none)
+        "defaultOrganisation": None,  # Default org for user
+        "verified": True,  # Account verified via email?
+        "userlevel": "admin",  # User level (user/admin)
+    }
+    dbdoc = {
+        "apiversion": KIBBLE_VERSION,  # Log current API version
+        "dbversion": KIBBLE_DB_VERSION,  # Log the database revision we accept (might change!)
+    }
+    es.index(index=dbname + "_useraccount", doc_type="_doc", id=admin_name, body=doc)
+    es.index(index=dbname + "_api", doc_type="_doc", id="current", body=dbdoc)
+    print("Account created!")
+
+
+@cli.command("setup", short_help="starts the setup process for kibble")
+@click.option(
+    "-u",
+    "--uri",
+    default=conf.get("elasticsearch", "conn_uri"),
+    help="Pre-defined connection uri for ElasticSearch.",
+)
+@click.option(
+    "-d",
+    "--dbname",
+    default=conf.get("elasticsearch", "dbname"),
+    help="Pre-defined Database prefix.",
+)
+@click.option(
+    "-s",
+    "--shards",
+    default=conf.get("elasticsearch", "shards"),
+    help="Predefined number of ES shards.",
+)
+@click.option(
+    "-r",
+    "--replicas",
+    default=conf.get("elasticsearch", "replicas"),
+    help="Predefined number of replicas for ES.",
+)
+@click.option(
+    "-m",
+    "--mailhost",
+    default=conf.get("mail", "mailhost"),
+    help="Pre-defined 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,
+):

Review comment:
       That definitely sounds a lot cleaner. Will refactor current implementation.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org