diff --git a/contents/common.py b/contents/common.py index 34b9f6f..254dcfa 100644 --- a/contents/common.py +++ b/contents/common.py @@ -563,3 +563,72 @@ def delete_pod(data): if e.status != 404: log.exception("Unknown error:") return None + + +def handle_rundeck_environment_variables(name, namespace, container): + + # reduce environment variable array for just rundeck variables + rundeck_variables = dict(filter(lambda x: x[0].startswith('RD_'), + os.environ.items() + ) + ) + + # get variables of temporarily + rundeck_files_variables = dict(filter(lambda x: x[0].startswith('RD_FILE_'), + rundeck_variables.items() + ) + ) + + destination_path = "/tmp" + + if 'RD_NODE_FILE_COPY_DESTINATION_DIR' in os.environ: + destination_path = os.environ.get('RD_NODE_FILE_COPY_DESTINATION_DIR') + + # copy uploaded files to the pod + files_copied = [] + for file_key, file_value in rundeck_files_variables.items(): + if not file_key.endswith('_SHA') and not file_key.endswith('_FILENAME'): + source_file = file_value + destination_file_name = rundeck_variables[file_key.replace('RD_FILE', 'RD_OPTION')] + full_path = destination_path + "/" + destination_file_name + + try: + log.debug("coping script from %s to %s", source_file, full_path) + copy_file( + name=name, + namespace=namespace, + container=container, + source_file=source_file, + destination_path=destination_path, + destination_file_name=destination_file_name + ) + + finally: + rundeck_variables[file_key] = full_path + files_copied.append(full_path) + + + converted_variables = ['env'] + for key, value in rundeck_variables.items(): + converted_variables.append(key + '=' + value) + + return converted_variables, files_copied + + +def clean_up_temporary_files(name, namespace, container, files): + rm_command = ["rm"] + files + + log.debug("removing file %s", rm_command) + resp = run_command( + name=name, + namespace=namespace, + container=container, + command=rm_command + ) + + if resp.peek_stdout(): + log.debug(resp.read_stdout()) + + if resp.peek_stderr(): + log.debug(resp.read_stderr()) + sys.exit(1) diff --git a/contents/pods-node-executor.py b/contents/pods-node-executor.py index 19b6621..075fb99 100644 --- a/contents/pods-node-executor.py +++ b/contents/pods-node-executor.py @@ -41,18 +41,29 @@ def main(): log.debug("Command: %s ", command) - # calling exec and wait for response. - exec_command = [ - shell, - '-c', - command] + environments_variables, temporary_files = common.handle_rundeck_environment_variables( + name=name, + namespace=namespace, + container=container + ) + + exec_command = environments_variables + [shell, '-c', command] + # calling exec and wait for response. resp, error = common.run_interactive_command(name, namespace, container, exec_command) if error: log.error("error running script") sys.exit(1) + if len(temporary_files) > 0: + common.clean_up_temporary_files( + name=name, + namespace=namespace, + container=container, + files=temporary_files + ) + if __name__ == '__main__': main() diff --git a/contents/pods-run-script.py b/contents/pods-run-script.py index 00d379c..bfa9294 100644 --- a/contents/pods-run-script.py +++ b/contents/pods-run-script.py @@ -118,8 +118,14 @@ def main(): print(resp.read_stderr()) sys.exit(1) + environments_variables, temporary_files = common.handle_rundeck_environment_variables( + name=name, + namespace=namespace, + container=container + ) + # calling exec and wait for response. - exec_command = invocation.split(" ") + exec_command = environments_variables + invocation.split(" ") exec_command.append(full_path) if 'RD_CONFIG_ARGUMENTS' in os.environ: @@ -143,21 +149,13 @@ def main(): log.info("POD deleted") sys.exit(1) - rm_command = ["rm", full_path] - - log.debug("removing file %s", rm_command) - resp = common.run_command(name=name, - namespace=namespace, - container=container, - command=rm_command - ) - - if resp.peek_stdout(): - log.debug(resp.read_stdout()) - - if resp.peek_stderr(): - log.debug(resp.read_stderr()) - sys.exit(1) + temporary_files.append(full_path) + common.clean_up_temporary_files( + name=name, + namespace=namespace, + container=container, + files=temporary_files + ) if __name__ == '__main__':