From dccb6aec43ead43b90174973f3e2b9a18e9ae811 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Vonheiden?= <bjoern.vonheiden@hotmail.de>
Date: Tue, 22 Sep 2020 15:12:04 +0200
Subject: [PATCH] Enable reset with cli parameters in run uc py

Use the parameters -res and -reso to reset the kubernetes cluster.
With -reso only the cluster is resetted in nothing else is executed.
With -res the cluster gets resetted before execution of the usecase.
---
 execution/run_uc.py | 64 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)

diff --git a/execution/run_uc.py b/execution/run_uc.py
index ca911d817..25c463be8 100644
--- a/execution/run_uc.py
+++ b/execution/run_uc.py
@@ -4,6 +4,7 @@ from kubernetes.stream import stream
 import logging  # logging
 from os import path  # path utilities
 import subprocess  # execute bash commands
+import sys # for exit of program
 import time  # process sleep
 import yaml  # convert from file to yaml object
 
@@ -64,6 +65,14 @@ def load_variables():
                         metavar='EXECUTION_MINUTES',
                         help='Duration in minutes subexperiments should be \
                                 executed for')
+    parser.add_argument('--reset', '-res',
+                        dest='reset',
+                        action="store_true",
+                        help='Resets the environment before execution')
+    parser.add_argument('--reset-only', '-reso',
+                        dest='reset_only',
+                        action="store_true",
+                        help='Only resets the environment. Ignores all other parameters')
 
     args = parser.parse_args()
     print(args)
@@ -287,10 +296,17 @@ def run_evaluation_script():
 
 
 def delete_resource(obj, del_func):
+    """
+    Helper function to delete kuberentes resources.
+    First tries to delete with the kuberentes object.
+    Then it uses the dict representation of yaml to delete the object.
+    :param obj: Either kubernetes resource object or yaml as a dict.
+    :param del_func: The function that need to be executed for deletion
+    """
     try:
         del_func(obj.metadata.name, 'default')
     except Exception as e:
-        logging.info('Error deleting resource with api object, try with dict.')
+        logging.debug('Error deleting resource with api object, try with dict.')
         try:
             del_func(obj['metadata']['name'], 'default')
         except Exception as e:
@@ -462,43 +478,61 @@ def stop_lag_exporter():
     print(output)
     return
 
-# def start():
-#
-#
-# def stop():
-#
+
+def reset_cluster(wg, app_svc, app_svc_monitor, app_jmx, app_deploy, topics):
+    """
+    Stop the applications, delete topics, reset zookeeper and stop lag exporter.
+    """
+    stop_applications(wg, app_svc, app_svc_monitor, app_jmx, app_deploy)
+    print('---------------------')
+    delete_topics(topics)
+    print('---------------------')
+    reset_zookeeper()
+    print('---------------------')
+    stop_lag_exporter()
 
 
 def main():
     load_variables()
     print('---------------------')
+
+    wg, app_svc, app_svc_monitor, app_jmx, app_deploy = load_yaml_files()
+    print('---------------------')
+
     initialize_kubernetes_api()
     print('---------------------')
+
     topics = [('input', args.partitions),
               ('output', args.partitions),
               ('aggregation-feedback', args.partitions),
               ('configuration', 1)]
+
+    if args.reset_only:
+        print('Reset only cluster')
+        reset_cluster(wg, app_svc, app_svc_monitor, app_jmx, app_deploy, topics)
+        sys.exit()
+    if args.reset:
+        print('Reset cluster before execution')
+        reset_cluster(wg, app_svc, app_svc_monitor, app_jmx, app_deploy, topics)
+        print('---------------------')
+
     create_topics(topics)
     print('---------------------')
-    wg, app_svc, app_svc_monitor, app_jmx, app_deploy = load_yaml_files()
-    print('---------------------')
+
     wg = start_workload_generator(wg)
     print('---------------------')
+
     app_svc, app_svc_monitor, app_jmx, app_deploy = start_application(
         app_svc,
         app_svc_monitor,
         app_jmx,
         app_deploy)
     print('---------------------')
+
     wait_execution()
     print('---------------------')
-    stop_applications(wg, app_svc, app_svc_monitor, app_jmx, app_deploy)
-    print('---------------------')
-    delete_topics(topics)
-    print('---------------------')
-    reset_zookeeper()
-    print('---------------------')
-    stop_lag_exporter()
+
+    reset_cluster(wg, app_svc, app_svc_monitor, app_jmx, app_deploy, topics)
 
 
 if __name__ == '__main__':
-- 
GitLab