|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2024 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +#define FLUX_SHELL_PLUGIN_NAME "env-expand" |
| 12 | + |
| 13 | +#if HAVE_CONFIG_H |
| 14 | +#include "config.h" |
| 15 | +#endif |
| 16 | + |
| 17 | +#include <jansson.h> |
| 18 | + |
| 19 | +#include <flux/shell.h> |
| 20 | + |
| 21 | +#include "builtins.h" |
| 22 | + |
| 23 | +static int env_expand (flux_plugin_t *p, |
| 24 | + const char *topic, |
| 25 | + flux_plugin_arg_t *args, |
| 26 | + void *data) |
| 27 | +{ |
| 28 | + json_t *to_expand = NULL; |
| 29 | + json_t *value; |
| 30 | + void *tmp; |
| 31 | + const char *key; |
| 32 | + flux_shell_t *shell = flux_plugin_get_shell (p); |
| 33 | + |
| 34 | + if (!(shell = flux_plugin_get_shell (p))) |
| 35 | + return shell_log_errno ("unable to get shell handle"); |
| 36 | + if (flux_shell_getopt_unpack (shell, "env-expand", "o", &to_expand) != 1) |
| 37 | + return 0; |
| 38 | + json_object_foreach_safe (to_expand, tmp, key, value) { |
| 39 | + const char *s = json_string_value (value); |
| 40 | + char *result; |
| 41 | + if (s == NULL) { |
| 42 | + shell_log_error ("invalid value for env var %s", key); |
| 43 | + continue; |
| 44 | + } |
| 45 | + result = flux_shell_mustache_render (shell, s); |
| 46 | + |
| 47 | + /* If mustache render was successful, then set it for the job and |
| 48 | + * remove the key from the env-expand object internally, so it isn't |
| 49 | + * expanded again in task_env_expand(): |
| 50 | + */ |
| 51 | + if (result && !strstr (result, "{{")) { |
| 52 | + if (flux_shell_setenvf (shell, 1, key, "%s", result) < 0) |
| 53 | + shell_log_errno ("failed to set %s=%s", key, result); |
| 54 | + else |
| 55 | + (void) json_object_del (to_expand, key); |
| 56 | + } |
| 57 | + free (result); |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +/* Per-task environment variable mustache substitution. |
| 63 | + * N.B.: Only templates that were not fully rendered by env_expand() above |
| 64 | + * should remain in the `env-expand` shell options object. |
| 65 | + */ |
| 66 | +static int task_env_expand (flux_plugin_t *p, |
| 67 | + const char *topic, |
| 68 | + flux_plugin_arg_t *args, |
| 69 | + void *data) |
| 70 | +{ |
| 71 | + json_t *to_expand = NULL; |
| 72 | + json_t *value; |
| 73 | + const char *key; |
| 74 | + flux_shell_t *shell; |
| 75 | + flux_shell_task_t *task; |
| 76 | + flux_cmd_t *cmd; |
| 77 | + |
| 78 | + if (!(shell = flux_plugin_get_shell (p))) |
| 79 | + return shell_log_errno ("unable to get shell handle"); |
| 80 | + if (flux_shell_getopt_unpack (shell, "env-expand", "o", &to_expand) != 1) |
| 81 | + return 0; |
| 82 | + |
| 83 | + if (!(task = flux_shell_current_task (shell)) |
| 84 | + || !(cmd = flux_shell_task_cmd (task))) |
| 85 | + return -1; |
| 86 | + |
| 87 | + json_object_foreach (to_expand, key, value) { |
| 88 | + const char *s = json_string_value (value); |
| 89 | + char *result; |
| 90 | + if (s == NULL) { |
| 91 | + shell_log_error ("invalid value for env var %s", key); |
| 92 | + continue; |
| 93 | + } |
| 94 | + if (!(result = flux_shell_mustache_render (shell, s))) { |
| 95 | + shell_log_errno ("failed to expand env var %s=%s", key, s); |
| 96 | + continue; |
| 97 | + } |
| 98 | + if (flux_cmd_setenvf (cmd, 1, key, "%s", result) < 0) |
| 99 | + shell_log_errno ("failed to set %s=%s", key, result); |
| 100 | + free (result); |
| 101 | + } |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +struct shell_builtin builtin_env_expand = { |
| 106 | + .name = FLUX_SHELL_PLUGIN_NAME, |
| 107 | + .init = env_expand, |
| 108 | + .task_init = task_env_expand, |
| 109 | +}; |
| 110 | + |
| 111 | +/* |
| 112 | + * vi:tabstop=4 shiftwidth=4 expandtab |
| 113 | + */ |
0 commit comments