michalslowikowski00 commented on a change in pull request #48:
URL: https://github.com/apache/kibble/pull/48#discussion_r502935736



##########
File path: setup/setup.py
##########
@@ -259,60 +206,133 @@ def createIndex():
         '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 = adminName, 
body = doc)
-    es.index(index=dbname+'_api', doc_type = '_doc', id = 'current', body = 
dbdoc)
+    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!")
 
-try:
-    import logging
-    # elasticsearch logs lots of warnings on retries/connection failure
-    logging.getLogger("elasticsearch").setLevel(logging.ERROR)
-    createIndex()
-    
-     
-except Exception as e:
-    print("Index creation failed: %s" % e)
-    sys.exit(1)
 
-kibble_yaml = '../api/yaml/kibble.yaml'
+def get_kibble_yaml() -> str:
+    """Resolve path to kibble config yaml"""
+    kibble_yaml = os.path.join(
+        os.path.dirname(os.path.realpath(__file__)),
+        os.pardir,
+        "api",
+        "yaml",
+        "kibble.yaml"
+    )
+    if os.path.exists(kibble_yaml):
+        print(f"{kibble_yaml} already exists! Writing to {kibble_yaml}.tmp 
instead")
+        kibble_yaml = kibble_yaml + ".tmp"
+    return kibble_yaml
 
-if os.path.exists(kibble_yaml):
-    print("%s already exists! Writing to %s.tmp instead" % (kibble_yaml, 
kibble_yaml))
-    kibble_yaml = kibble_yaml + ".tmp"
-    
 
-print("Writing Kibble config (%s)" % kibble_yaml)
+def save_config(
+    mlserver: str,
+    hostname: str,
+    port: int,
+    dbname: str,
+):
+    """Save kibble config to yaml file"""
+    if ":" in mlserver:
+        try:
+            mailhost, mailport = mlserver.split(":")
+        except ValueError:
+            raise ValueError("mailhost argument must be in form of `host:port` 
or `host`")
+    else:
+        mailhost = mlserver
+        mailport = 25
 
-m = mlserver.split(':')
-if len(m) == 1:
-    m.append(25)
-    
-myconfig = {
-    'api': {
-        'version': KIBBLE_VERSION,
-        'database': KIBBLE_DB_VERSION
-    },
-    'elasticsearch': {
-        'host': hostname,
-        'port': port,
-        'ssl': False,
-        'dbname': dbname
-    },
-    'mail': {
-        'mailhost': m[0],
-        'mailport': m[1],
-        'sender': 'Kibble <[email protected]>'
-    },
-    'accounts': {
-        'allowSignup': True,
-        'verify': True
+    config = {
+        'api': {
+            'version': KIBBLE_VERSION,
+            'database': KIBBLE_DB_VERSION
+        },
+        'elasticsearch': {
+            'host': hostname,
+            'port': port,
+            'ssl': False,
+            'dbname': dbname
+        },
+        'mail': {
+            'mailhost': mailhost,
+            'mailport': int(mailport),
+            'sender': 'Kibble <[email protected]>'
+        },
+        'accounts': {
+            'allowSignup': True,
+            'verify': True
+        }
     }
-}
 
-with open(kibble_yaml, "w") as f:
-    f.write(yaml.dump(myconfig, default_flow_style = False))
-    f.close()
+    kibble_yaml = get_kibble_yaml()
+    print(f"Writing Kibble config to {kibble_yaml}")
+    with open(kibble_yaml, "w") as f:
+        f.write(yaml.dump(config, default_flow_style = False))
+        f.close()
+
+
+def get_user_input(msg: str):
+    value = None
+    while not value:
+        value = input(msg)
+    return value
+
+
+def print_configuration(args):
+    print("Configuring Apache Kibble elasticsearch instance with the following 
arguments:")
+    print(f"- hostname: {args.hostname}")
+    print(f"- port: {int(args.port)}")
+    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)
+
+    admin_name = "admin@kibble"
+    admin_pass = "kibbleAdmin"
+    if not args.autoadmin:
+        admin_name = get_user_input("Enter an email address for the 
adminstrator account:")

Review comment:
       ```suggestion
           admin_name = get_user_input("Enter an email address for the 
administrator account:")
   ```




----------------------------------------------------------------
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:
[email protected]


Reply via email to