|
| 1 | +import fnmatch |
| 2 | +import io |
| 3 | +import json |
| 4 | +import os |
| 5 | +import yaml |
| 6 | +import zipfile |
| 7 | +from pathlib import Path |
| 8 | +from typing import List |
| 9 | +from aztk.spark import models |
| 10 | +from aztk.utils import constants, file_utils, secure_utils |
| 11 | +from aztk.error import InvalidCustomScriptError |
| 12 | + |
| 13 | +ROOT_PATH = constants.ROOT_PATH |
| 14 | + |
| 15 | +# Constants for node data |
| 16 | +NODE_SCRIPT_FOLDER = "node_scripts" |
| 17 | +CUSTOM_SCRIPT_FOLDER = "custom-scripts" |
| 18 | +CUSTOM_SCRIPT_METADATA_FILE = "custom-scripts.yaml" |
| 19 | +PLUGIN_FOLDER = "plugins" |
| 20 | + |
| 21 | + |
| 22 | +class NodeData: |
| 23 | + """ |
| 24 | + Class made to bundle data to be uploaded to the node as a zip |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, cluster_config: models.ClusterConfiguration): |
| 28 | + self.zip_path = os.path.join(ROOT_PATH, "tmp/node-scripts.zip") |
| 29 | + self.cluster_config = cluster_config |
| 30 | + file_utils.ensure_dir(self.zip_path) |
| 31 | + self.zipf = zipfile.ZipFile(self.zip_path, "w", zipfile.ZIP_DEFLATED) |
| 32 | + |
| 33 | + def add_core(self): |
| 34 | + self._add_node_scripts() |
| 35 | + self._add_custom_scripts() |
| 36 | + self._add_plugins() |
| 37 | + self._add_spark_configuration() |
| 38 | + self._add_user_conf() |
| 39 | + self.add_file(os.path.join(constants.ROOT_PATH, 'aztk', 'utils', 'command_builder.py'), '', binary=False) |
| 40 | + return self |
| 41 | + |
| 42 | + def done(self): |
| 43 | + self.zipf.close() |
| 44 | + return self |
| 45 | + |
| 46 | + def add_file(self, file: str, zip_dir: str, binary: bool = True): |
| 47 | + if not file: |
| 48 | + return |
| 49 | + if isinstance(file, (str, bytes)): |
| 50 | + full_file_path = Path(file) |
| 51 | + with io.open(file, 'r') as f: |
| 52 | + if binary: |
| 53 | + self.zipf.write(file, os.path.join(zip_dir, full_file_path.name)) |
| 54 | + else: |
| 55 | + self.zipf.writestr(os.path.join(zip_dir, full_file_path.name), f.read().replace('\r\n', '\n')) |
| 56 | + elif isinstance(file, models.File): |
| 57 | + self.zipf.writestr(os.path.join(zip_dir, file.name), file.payload.getvalue()) |
| 58 | + |
| 59 | + def add_files(self, file_paths: List[str], zip_dir, binary: bool = True): |
| 60 | + """ |
| 61 | + Add a list of local files to the node data |
| 62 | + """ |
| 63 | + for file in file_paths: |
| 64 | + self.add_file(file, zip_dir, binary) |
| 65 | + |
| 66 | + def add_dir(self, path: str, exclude: List[str] = []): |
| 67 | + """ |
| 68 | + Zip all the files in the given directory into the zip file handler |
| 69 | + """ |
| 70 | + for base, _, files in os.walk(path): |
| 71 | + relative_folder = os.path.relpath(base, path) |
| 72 | + for file in files: |
| 73 | + if self._includeFile(file, exclude): |
| 74 | + with io.open(os.path.join(base, file), 'r') as f: |
| 75 | + self.zipf.writestr(os.path.join(relative_folder, file), f.read().replace('\r\n', '\n')) |
| 76 | + |
| 77 | + def _add_custom_scripts(self): |
| 78 | + data = [] |
| 79 | + if not self.cluster_config.custom_scripts: |
| 80 | + return |
| 81 | + |
| 82 | + for index, custom_script in enumerate(self.cluster_config.custom_scripts): |
| 83 | + if isinstance(custom_script.script, (str, bytes)): |
| 84 | + new_file_name = str(index) + '_' + os.path.basename(custom_script.script) |
| 85 | + data.append(dict(script=new_file_name, runOn=str(custom_script.run_on))) |
| 86 | + try: |
| 87 | + with io.open(custom_script.script, 'r') as f: |
| 88 | + self.zipf.writestr( |
| 89 | + os.path.join(CUSTOM_SCRIPT_FOLDER, new_file_name), |
| 90 | + f.read().replace('\r\n', '\n')) |
| 91 | + except FileNotFoundError: |
| 92 | + raise InvalidCustomScriptError("Custom script '{0}' doesn't exists.".format(custom_script.script)) |
| 93 | + elif isinstance(custom_script.script, models.File): |
| 94 | + new_file_name = str(index) + '_' + custom_script.script.name |
| 95 | + self.zipf.writestr(os.path.join('custom-scripts', new_file_name), custom_script.script.payload.getvalue()) |
| 96 | + |
| 97 | + self.zipf.writestr( |
| 98 | + os.path.join(CUSTOM_SCRIPT_FOLDER, CUSTOM_SCRIPT_METADATA_FILE), yaml.dump(data, default_flow_style=False)) |
| 99 | + |
| 100 | + def _add_spark_configuration(self): |
| 101 | + spark_configuration = self.cluster_config.spark_configuration |
| 102 | + if not spark_configuration: |
| 103 | + return |
| 104 | + self.add_files( |
| 105 | + [ |
| 106 | + spark_configuration.spark_defaults_conf, spark_configuration.spark_env_sh, |
| 107 | + spark_configuration.core_site_xml |
| 108 | + ], |
| 109 | + 'conf', |
| 110 | + binary=False) |
| 111 | + |
| 112 | + # add ssh keys for passwordless ssh |
| 113 | + self.zipf.writestr( 'id_rsa.pub', spark_configuration.ssh_key_pair['pub_key']) |
| 114 | + self.zipf.writestr( 'id_rsa', spark_configuration.ssh_key_pair['priv_key']) |
| 115 | + |
| 116 | + if spark_configuration.jars: |
| 117 | + for jar in spark_configuration.jars: |
| 118 | + self.add_file(jar, 'jars', binary=True) |
| 119 | + |
| 120 | + def _add_user_conf(self): |
| 121 | + user_conf = self.cluster_config.user_configuration |
| 122 | + if not user_conf: |
| 123 | + return |
| 124 | + encrypted_aes_session_key, cipher_aes_nonce, tag, ciphertext = secure_utils.encrypt_password( |
| 125 | + self.cluster_config.spark_configuration.ssh_key_pair['pub_key'], user_conf.password) |
| 126 | + user_conf = yaml.dump({ |
| 127 | + 'username': user_conf.username, |
| 128 | + 'password': ciphertext, |
| 129 | + 'ssh-key': user_conf.ssh_key, |
| 130 | + 'aes_session_key': encrypted_aes_session_key, |
| 131 | + 'cipher_aes_nonce': cipher_aes_nonce, |
| 132 | + 'tag': tag, |
| 133 | + 'cluster_id': self.cluster_config.cluster_id |
| 134 | + }) |
| 135 | + self.zipf.writestr('user.yaml', user_conf) |
| 136 | + |
| 137 | + def _add_plugins(self): |
| 138 | + if not self.cluster_config.plugins: |
| 139 | + return |
| 140 | + |
| 141 | + data = [] |
| 142 | + for plugin in self.cluster_config.plugins: |
| 143 | + for file in plugin.files: |
| 144 | + zipf = self.zipf.writestr('plugins/{0}/{1}'.format(plugin.name, file.target), file.content()) |
| 145 | + if plugin.execute: |
| 146 | + data.append(dict( |
| 147 | + name=plugin.name, |
| 148 | + execute='{0}/{1}'.format(plugin.name, plugin.execute), |
| 149 | + args=plugin.args, |
| 150 | + env=plugin.env, |
| 151 | + runOn=plugin.run_on.value, |
| 152 | + )) |
| 153 | + |
| 154 | + self.zipf.writestr(os.path.join('plugins', 'plugins-manifest.yaml'), yaml.dump(data)) |
| 155 | + return zipf |
| 156 | + |
| 157 | + def _add_node_scripts(self): |
| 158 | + self.add_dir(os.path.join(ROOT_PATH, NODE_SCRIPT_FOLDER), exclude=['*.pyc']) |
| 159 | + |
| 160 | + def _includeFile(self, filename: str, exclude: List[str] = []) -> bool: |
| 161 | + for pattern in exclude: |
| 162 | + if fnmatch.fnmatch(filename, pattern): |
| 163 | + return False |
| 164 | + |
| 165 | + return True |
0 commit comments