From f62903a35e7d932a6d6b5c3b9a8cfa3f5dbe8352 Mon Sep 17 00:00:00 2001 From: greenmath Date: Fri, 2 Jun 2023 09:25:35 +0000 Subject: [PATCH 1/3] add function to handle Rundeck environment variables and options --- contents/common.py | 67 ++++++++++++++++++++++++++++++++++ contents/pods-node-executor.py | 19 +++++++--- contents/pods-run-script.py | 28 ++++++-------- 3 files changed, 93 insertions(+), 21 deletions(-) diff --git a/contents/common.py b/contents/common.py index 34b9f6f..4fddc9f 100644 --- a/contents/common.py +++ b/contents/common.py @@ -563,3 +563,70 @@ 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, + namespace, + container, + source_file, + destination_path, + 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..7e015d5 100644 --- a/contents/pods-node-executor.py +++ b/contents/pods-node-executor.py @@ -41,18 +41,27 @@ 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, + namespace, + 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..f37e481 100644 --- a/contents/pods-run-script.py +++ b/contents/pods-run-script.py @@ -118,8 +118,13 @@ def main(): print(resp.read_stderr()) sys.exit(1) + environments_variables, temporary_files = common.handle_rundeck_environment_variables(name, + namespace, + 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 +148,12 @@ 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__': From d15d74365cd60728773f9f606c29ebd07e622e98 Mon Sep 17 00:00:00 2001 From: greenmath Date: Fri, 2 Jun 2023 12:00:57 +0000 Subject: [PATCH 2/3] formating --- contents/common.py | 26 ++++++++++++++------------ contents/pods-node-executor.py | 20 +++++++++++--------- contents/pods-run-script.py | 20 +++++++++++--------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/contents/common.py b/contents/common.py index 4fddc9f..254dcfa 100644 --- a/contents/common.py +++ b/contents/common.py @@ -594,13 +594,14 @@ def handle_rundeck_environment_variables(name, namespace, container): try: log.debug("coping script from %s to %s", source_file, full_path) - copy_file(name, - namespace, - container, - source_file, - destination_path, - destination_file_name - ) + 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 @@ -618,11 +619,12 @@ 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 - ) + resp = run_command( + name=name, + namespace=namespace, + container=container, + command=rm_command + ) if resp.peek_stdout(): log.debug(resp.read_stdout()) diff --git a/contents/pods-node-executor.py b/contents/pods-node-executor.py index 7e015d5..2cf9ecf 100644 --- a/contents/pods-node-executor.py +++ b/contents/pods-node-executor.py @@ -41,10 +41,11 @@ def main(): log.debug("Command: %s ", command) - environments_variables, temporary_files = common.handle_rundeck_environment_variables(name, - namespace, - container - ) + environments_variables, temporary_files = common.handle_rundeck_environment_variables( + name=name, + namespace=namespace, + container=container + ) exec_command = environments_variables + [shell, '-c', command] @@ -56,11 +57,12 @@ def main(): sys.exit(1) if len(temporary_files) > 0: - common.clean_up_temporary_files(name=name, - namespace=namespace, - container=container, - files=temporary_files - ) + common.clean_up_temporary_files( + name=name, + namespace=namespace, + container=container, + files=temporary_files + ) if __name__ == '__main__': diff --git a/contents/pods-run-script.py b/contents/pods-run-script.py index f37e481..bfa9294 100644 --- a/contents/pods-run-script.py +++ b/contents/pods-run-script.py @@ -118,10 +118,11 @@ def main(): print(resp.read_stderr()) sys.exit(1) - environments_variables, temporary_files = common.handle_rundeck_environment_variables(name, - namespace, - container - ) + environments_variables, temporary_files = common.handle_rundeck_environment_variables( + name=name, + namespace=namespace, + container=container + ) # calling exec and wait for response. exec_command = environments_variables + invocation.split(" ") @@ -149,11 +150,12 @@ def main(): sys.exit(1) temporary_files.append(full_path) - common.clean_up_temporary_files(name=name, - namespace=namespace, - container=container, - files=temporary_files - ) + common.clean_up_temporary_files( + name=name, + namespace=namespace, + container=container, + files=temporary_files + ) if __name__ == '__main__': From 5c647ec3ac5952cd9b7e42b214f1611629f5ed58 Mon Sep 17 00:00:00 2001 From: greenmath Date: Wed, 16 Aug 2023 08:01:55 +0000 Subject: [PATCH 3/3] small indentation fix --- contents/pods-node-executor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contents/pods-node-executor.py b/contents/pods-node-executor.py index 2cf9ecf..075fb99 100644 --- a/contents/pods-node-executor.py +++ b/contents/pods-node-executor.py @@ -57,12 +57,12 @@ def main(): sys.exit(1) if len(temporary_files) > 0: - common.clean_up_temporary_files( - name=name, - namespace=namespace, - container=container, - files=temporary_files - ) + common.clean_up_temporary_files( + name=name, + namespace=namespace, + container=container, + files=temporary_files + ) if __name__ == '__main__':