This is an automated email from the ASF dual-hosted git repository.

omartushevskyi pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git


The following commit(s) were added to refs/heads/develop by this push:
     new 2c899b4  [DLAB-552]: Fix notebook stopping/termination fails on GCP
     new 038fb9d  Merge pull request #60 from ioleksandr/DLAB-552
2c899b4 is described below

commit 2c899b41344b65b7104c63b356c005ad5e79fbec
Author: Oleksandr_Isniuk <[email protected]>
AuthorDate: Tue Mar 26 11:43:10 2019 +0200

    [DLAB-552]: Fix notebook stopping/termination fails on GCP
---
 .../src/general/lib/gcp/meta_lib.py                | 28 ++++++++++++++++------
 .../general/scripts/gcp/common_stop_notebook.py    | 17 ++++++-------
 .../scripts/gcp/common_terminate_notebook.py       | 14 +++++++----
 3 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/infrastructure-provisioning/src/general/lib/gcp/meta_lib.py 
b/infrastructure-provisioning/src/general/lib/gcp/meta_lib.py
index 76dcecd..b122245 100644
--- a/infrastructure-provisioning/src/general/lib/gcp/meta_lib.py
+++ b/infrastructure-provisioning/src/general/lib/gcp/meta_lib.py
@@ -391,6 +391,24 @@ class GCPMeta:
             traceback.print_exc(file=sys.stdout)
             return ''
 
+    def get_list_instances_by_label(self, zone, filter_string=''):
+        try:
+            if not filter_string:
+                raise Exception("There are no filter_string was added for list 
instances by label")
+            else:
+                request = self.service.instances().list(project=self.project, 
zone=zone,
+                                                        
filter='labels.notebook_name eq {}'.format(filter_string))
+            result = request.execute()
+            return result
+        except Exception as err:
+            logging.info("Error with getting list instances by label: " + 
str(err) + "\n Traceback: " + traceback.print_exc(
+                file=sys.stdout))
+            append_result(str({"error": "Error with getting list instances by 
label",
+                               "error_message": str(err) + "\n Traceback: " + 
traceback.print_exc(file=sys.stdout)}))
+            traceback.print_exc(file=sys.stdout)
+            return ''
+
+
     def get_list_images(self, filter_string=''):
         try:
             if not filter_string:
@@ -641,14 +659,10 @@ class GCPMeta:
                 private_list_ip.append(GCPMeta().get_private_ip_address(
                 instance_id))
             elif conf_type == 'computational_resource':
-                instance_list = GCPMeta().get_list_instances(
-                    os.environ['gcp_zone'])
+                instance_list = GCPMeta().get_list_instances_by_label(
+                    os.environ['gcp_zone'], instance_id)
                 for instance in instance_list.get('items'):
-                    if instance.get('labels') != None:
-                        if instance.get('labels').get('name') == instance_id:
-                            private_list_ip.append(
-                                instance.get('networkInterfaces')[0].get(
-                                    'networkIP'))
+                    
private_list_ip.append(instance.get('networkInterfaces')[0].get('networkIP'))
             return private_list_ip
         except Exception as err:
             logging.info(
diff --git 
a/infrastructure-provisioning/src/general/scripts/gcp/common_stop_notebook.py 
b/infrastructure-provisioning/src/general/scripts/gcp/common_stop_notebook.py
index 8d4b2fd..5f95b3e 100644
--- 
a/infrastructure-provisioning/src/general/scripts/gcp/common_stop_notebook.py
+++ 
b/infrastructure-provisioning/src/general/scripts/gcp/common_stop_notebook.py
@@ -58,17 +58,18 @@ def stop_notebook(instance_name, bucket_name, region, zone, 
ssh_user, key_path,
         sys.exit(1)
 
     print("Stopping data engine cluster")
-    cluster_list = []
     try:
-        for vm in GCPMeta().get_list_instances(zone)['items']:
-            try:
-                if instance_name == vm['labels']['notebook_name']:
-                    if 'master' == vm['labels']["type"]:
-                        cluster_list.append(vm['labels']["name"])
+        clusters_list = GCPMeta().get_list_instances_by_label(zone, 
instance_name)
+        if clusters_list.get('items'):
+            for vm in clusters_list['items']:
+                try:
                     GCPActions().stop_instance(vm['name'], zone)
                     print("Instance {} has been stopped".format(vm['name']))
-            except:
-                pass
+                except:
+                    pass
+        else:
+            print("There are no data engine clusters to terminate.")
+
     except Exception as err:
         print('Error: {0}'.format(err))
         sys.exit(1)
diff --git 
a/infrastructure-provisioning/src/general/scripts/gcp/common_terminate_notebook.py
 
b/infrastructure-provisioning/src/general/scripts/gcp/common_terminate_notebook.py
index 776c5c7..a8144b9 100644
--- 
a/infrastructure-provisioning/src/general/scripts/gcp/common_terminate_notebook.py
+++ 
b/infrastructure-provisioning/src/general/scripts/gcp/common_terminate_notebook.py
@@ -52,13 +52,17 @@ def terminate_nb(instance_name, bucket_name, region, zone, 
user_name):
 
     print("Terminating data engine cluster")
     try:
-        for vm in GCPMeta().get_list_instances(zone)['items']:
-            try:
-                if instance_name == vm['labels']['notebook_name']:
+        clusters_list = GCPMeta().get_list_instances_by_label(zone, 
instance_name)
+        if clusters_list.get('items'):
+            for vm in clusters_list['items']:
+                try:
                     GCPActions().remove_instance(vm['name'], zone)
                     print("Instance {} has been terminated".format(vm['name']))
-            except:
-                pass
+                except:
+                    pass
+        else:
+            print("There are no data engine clusters to terminate.")
+
     except Exception as err:
         print('Error: {0}'.format(err))
         sys.exit(1)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to