You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dlab.apache.org by lf...@apache.org on 2020/06/09 14:36:10 UTC

[incubator-dlab] 02/03: [DLAB-54]: added script to create custom notebook image

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

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

commit ac6e528cc1f8d2a393df46bd1a86dfb8451f8541
Author: leonidfrolov <fr...@gmail.com>
AuthorDate: Tue Jun 9 15:59:52 2020 +0300

    [DLAB-54]: added script to create custom notebook image
---
 .../scripts/gcp/common_create_notebook_image.py    | 90 ++++++++++++++++++++++
 1 file changed, 90 insertions(+)

diff --git a/infrastructure-provisioning/src/general/scripts/gcp/common_create_notebook_image.py b/infrastructure-provisioning/src/general/scripts/gcp/common_create_notebook_image.py
new file mode 100644
index 0000000..2359e27
--- /dev/null
+++ b/infrastructure-provisioning/src/general/scripts/gcp/common_create_notebook_image.py
@@ -0,0 +1,90 @@
+#!/usr/bin/python
+
+# *****************************************************************************
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# ******************************************************************************
+
+import dlab.fab
+import dlab.actions_lib
+import dlab.meta_lib
+import sys
+import json
+import uuid
+import os
+
+
+if __name__ == "__main__":
+    try:
+        image_conf = dict()
+        try:
+            image_conf['exploratory_name'] = (os.environ['exploratory_name']).replace('_', '-').lower()
+        except:
+            image_conf['exploratory_name'] = ''
+        image_conf['service_base_name'] = os.environ['conf_service_base_name'] = dlab.fab.replace_multi_symbols(
+            os.environ['conf_service_base_name'][:20], '-', True)
+        image_conf['endpoint_name'] = (os.environ['endpoint_name']).replace('_', '-').lower()
+        image_conf['endpoint_tag'] = image_conf['endpoint_name']
+        image_conf['project_name'] = os.environ['project_name']
+        image_conf['project_tag'] = os.environ['project_name']
+        image_conf['instance_name'] = os.environ['notebook_instance_name']
+        image_conf['instance_tag'] = '{}-tag'.format(image_conf['service_base_name'])
+        image_conf['application'] = os.environ['application']
+        image_conf['image_name'] = os.environ['notebook_image_name']
+        image_conf['expected_primary_image_name'] = '{}-{}-{}-{}-primary-{}'.format(
+            image_conf['service_base_name'], image_conf['project_name'], image_conf['endpoint_name'],
+            os.environ['application'], image_conf['image_name'])
+        image_conf['expected_secondary_image_name'] = '{}-{}-{}-{}-secondary-{}'.format(
+            image_conf['service_base_name'], image_conf['project_name'], image_conf['endpoint_name'],
+            os.environ['application'], image_conf['image_name'])
+        image_conf['image_labels'] = {"sbn": image_conf['service_base_name'],
+                                           "endpoint_tag": image_conf['endpoint_tag'],
+                                           "project_tag": image_conf['project_tag'],
+                                           "image": image_conf['image_name'],
+                                           os.environ['conf_billing_tag_key']: os.environ['conf_billing_tag_value']}
+        image_conf['instance_name'] = '{0}-{1}-{2}-nb-{3}'.format(image_conf['service_base_name'],
+                                                                       image_conf['project_name'],
+                                                                       image_conf['endpoint_name'],
+                                                                       image_conf['exploratory_name'])
+        image_conf['zone'] = os.environ['gcp_zone']
+        print('[CREATING IMAGE]')
+        primary_image_id = GCPMeta.get_image_by_name(image_conf['expected_primary_image_name'])
+        if primary_image_id == '':
+            image_id_list = GCPActions.create_image_from_instance_disks(
+                image_conf['expected_primary_image_name'], image_conf['expected_secondary_image_name'],
+                image_conf['instance_name'], image_conf['zone'], image_conf['image_labels'])
+            if image_id_list and image_id_list[0] != '':
+                print("Image of primary disk was successfully created. It's ID is {}".format(image_id_list[0]))
+            else:
+                print("Looks like another image creating operation for your template have been started a "
+                      "moment ago.")
+            if image_id_list and image_id_list[1] != '':
+                print("Image of secondary disk was successfully created. It's ID is {}".format(image_id_list[1]))
+
+            with open("/root/result.json", 'w') as result:
+                res = {"primary_image_name": image_conf['expected_primary_image_name'],
+                       "secondary_image_name": image_conf['expected_secondary_image_name'],
+                       "project_name": image_conf['project_name'],
+                       "application": image_conf['application'],
+                       "status": "created",
+                       "Action": "Create image from notebook"}
+                result.write(json.dumps(res))
+    except Exception as err:
+        dlab.fab.append_result("Failed to create image from notebook", str(err))
+        sys.exit(1)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@dlab.apache.org
For additional commands, e-mail: commits-help@dlab.apache.org