Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to handle Rundeck environment variables and options for execution in pod #161

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions contents/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 16 additions & 5 deletions contents/pods-node-executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
30 changes: 14 additions & 16 deletions contents/pods-run-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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__':
Expand Down