|
| 1 | +""" |
| 2 | +Name: jupyter.py |
| 3 | +Purpose: Installs a jupyter notebook |
| 4 | +Author: PNDA team |
| 5 | +
|
| 6 | +Created: 03/10/2016 |
| 7 | +
|
| 8 | +Copyright (c) 2016 Cisco and/or its affiliates. |
| 9 | +
|
| 10 | +This software is licensed to you under the terms of the Apache License, Version 2.0 (the "License"). |
| 11 | +You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +The code, technical concepts, and all information contained herein, are the property of Cisco Technology, Inc. |
| 14 | +and/or its affiliated entities, under various laws including copyright, international treaties, patent, |
| 15 | +and/or contract. Any use of the material herein must be in accordance with the terms of the License. |
| 16 | +All rights not expressly granted by the License are reserved. |
| 17 | +
|
| 18 | +Unless required by applicable law or agreed to separately in writing, software distributed under the |
| 19 | +License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 20 | +either express or implied. |
| 21 | +""" |
| 22 | + |
| 23 | +# pylint: disable=C0103 |
| 24 | + |
| 25 | +import json |
| 26 | +import os |
| 27 | +import logging |
| 28 | + |
| 29 | +import deployer_utils |
| 30 | +from plugins.base_creator import Creator |
| 31 | + |
| 32 | + |
| 33 | +class JupyterCreator(Creator): |
| 34 | + |
| 35 | + def validate_component(self, component): |
| 36 | + errors = [] |
| 37 | + notebook_found = False |
| 38 | + file_list = component['component_detail'] |
| 39 | + for file_name in file_list: |
| 40 | + if file_name.endswith(r'.ipynb'): |
| 41 | + notebook_found = True |
| 42 | + |
| 43 | + if notebook_found is False: |
| 44 | + errors.append('missing ipynb file') |
| 45 | + |
| 46 | + return errors |
| 47 | + |
| 48 | + def get_component_type(self): |
| 49 | + return 'jupyter' |
| 50 | + |
| 51 | + def destroy_component(self, application_name, create_data): |
| 52 | + logging.debug("destroy_component: %s %s", application_name, json.dumps(create_data)) |
| 53 | + key_file = self._environment['cluster_private_key'] |
| 54 | + root_user = self._environment['cluster_root_user'] |
| 55 | + target_host = self._environment['jupyter_host'] |
| 56 | + deployer_utils.exec_ssh(target_host, root_user, key_file, create_data['delete_commands']) |
| 57 | + |
| 58 | + def start_component(self, application_name, create_data): |
| 59 | + logging.debug("start_component (nothing to do for jupyter): %s %s", application_name, json.dumps(create_data)) |
| 60 | + |
| 61 | + def stop_component(self, application_name, create_data): |
| 62 | + logging.debug("stop_component (nothing to do for jupyter): %s %s", application_name, json.dumps(create_data)) |
| 63 | + |
| 64 | + def create_component(self, staged_component_path, application_name, component, properties): |
| 65 | + logging.debug("create_component: %s %s %s", application_name, json.dumps(component), properties) |
| 66 | + |
| 67 | + key_file = self._environment['cluster_private_key'] |
| 68 | + root_user = self._environment['cluster_root_user'] |
| 69 | + target_host = self._environment['jupyter_host'] |
| 70 | + delete_commands = [] |
| 71 | + |
| 72 | + mkdircommands = [] |
| 73 | + remote_component_tmp_path = '%s/%s/%s' % ('/tmp/%s' % self._namespace, application_name, component['component_name']) |
| 74 | + mkdircommands.append('mkdir -p %s' % remote_component_tmp_path) |
| 75 | + deployer_utils.exec_ssh(target_host, root_user, key_file, mkdircommands) |
| 76 | + |
| 77 | + file_list = component['component_detail'] |
| 78 | + for file_name in file_list: |
| 79 | + if file_name.endswith(r'.ipynb'): |
| 80 | + self._fill_properties('%s/%s' % (staged_component_path, file_name), properties) |
| 81 | + logging.debug('Copying %s/* to %s:%s', staged_component_path, target_host, remote_component_tmp_path) |
| 82 | + os.system("scp -i %s -o StrictHostKeyChecking=no %s/%s %s@%s:%s" % |
| 83 | + (key_file, staged_component_path, file_name, root_user, target_host, remote_component_tmp_path)) |
| 84 | + |
| 85 | + remote_component_install_path = '%s/%s_%s' % (self._environment['jupyter_notebook_directory'], application_name, file_name) |
| 86 | + deployer_utils.exec_ssh( |
| 87 | + target_host, root_user, key_file, |
| 88 | + ['sudo mv %s %s' % (remote_component_tmp_path + '/*.ipynb', remote_component_install_path)]) |
| 89 | + delete_commands.append('sudo rm -rf %s\n' % remote_component_install_path) |
| 90 | + |
| 91 | + logging.debug("uninstall commands: %s", delete_commands) |
| 92 | + return {'delete_commands': delete_commands} |
0 commit comments