https://pulumi.com logo
Title
f

fresh-honey-15888

09/14/2021, 4:42 PM
Is there a recommended solution for templating? I'm submitting jobs to Nomad and want to embed secrets in the jobspecs. I thought I could do something like: `const drone = new nomad.Job("lightsoutgames-drone", { jobspec: eval("`" + readFileSync("lightsoutgames-drone.nomad").toString() + "`") })`` then I can treat the jobspec like a template string and embed secrets in it. The
eval
trick works in the REPL but fails in Pulumi. I can cut-and-paste the error, but first wanted to make sure this wasn't a terrible idea. FWIW, this is just a personal setup, so I'm not super worried about how robust this is.
s

steep-toddler-94095

09/14/2021, 5:41 PM
i think this should work
const drone = new nomad.Job("lightsoutgames-drone", { jobspec: readFileSync("lightsoutgames-drone.nomad").toString() })
f

fresh-honey-15888

09/14/2021, 6:14 PM
That would just embed the raw file content. It wouldn't evaluate snippets like ``DRONE_GITEA_CLIENT_SECRET = "${config.requireSecret('lightsoutgamesDroneGiteaClientSecret')}"`` in the jobspec.
s

steep-toddler-94095

09/14/2021, 6:28 PM
oh right. i suppose one option is to paste the file contents as a templated string with the backticks and replace the env vars with JS/TS vars
f

fresh-honey-15888

09/14/2021, 7:05 PM
FWIW, the error I get is: error: Running program 'C:\Users\Nolan\projects\infrastructure' failed with an unhandled exception: SyntaxError: Unexpected identifier at Object.<anonymous> (C:\Users\Nolan\projects\infrastructure\index.ts:42:128) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Module.m._compile (C:\Users\Nolan\projects\infrastructure\node_modules\ts-node\src\index.ts:439:23) at Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Object.require.extensions.<computed> [as .ts] (C:\Users\Nolan\projects\infrastructure\node_modules\ts-node\src\index.ts:442:12) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:94:18) at Object.<anonymous> (C:\Users\Nolan\projects\infrastructure\node_modules\@pulumi\pulumi\cmd\run\run.js:219:31) Right after
toString()
. And again, `eval("``"+"${1+2}"+"``")` works in the REPL, so this should work for quick-and-dirty templating I'd think.
s

steep-toddler-94095

09/14/2021, 8:38 PM
does that eval work in the repl with readFileSync? maybe the file has special characters that mess up the eval?