matti / terraform-shell-resource

Run (exec) a command in shell and capture the output (stdout, stderr) and status code (exit status)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bourne shell

krzysztofantczak opened this issue · comments

Hey,

Its reaally naive implementation which uses bourne shell, but it works for simple use cases - as a replacement for read.rb.

#!/bin/sh

files=`cat $(1:-/dev/stdin) | grep -Eo '"[^"]+"' | grep -v '"[a-z]' | sed s/\"//g`
exitcode=$(cat `echo "$files" | head -n1` | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\\\n/g')
stderr=$(cat `echo "$files" | head -n2 | tail -n1` | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\\\n/g')
stdout=$(cat `echo "$files" | tail -n1` | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\\\n/g')

echo "{\"stdout\":\"$stdout\", \"stderr\":\"$stderr\", \"exitstatus\": \"$exitcode\"}"

TF output:

module.openssl_rand_key.null_resource.shell (local-exec): Executing: ["/bin/sh" "-c" "openssl rand -hex 16 2>\"/home/x/tests/.terraform/modules/93db91de164d6f0180919202e57234c3/matti-terraform-shell-resource-e9cb317/stderr.9163502827087229919\" >\"/home/x/tests/.terraform/modules/93db91de164d6f0180919202e57234c3/matti-terraform-shell-resource-e9cb317/stdout.9163502827087229919\"; echo $? >\"/home/x/tests/.terraform/modules/93db91de164d6f0180919202e57234c3/matti-terraform-shell-resource-e9cb317/exitstatus.9163502827087229919\""]
module.openssl_rand_key.null_resource.shell: Creation complete after 0s (ID: 9163502827087229919)
module.openssl_rand_key.data.external.read: Refreshing state...
module.openssl_rand_key.null_resource.contents: Creating...
  triggers.%:          "" => "3"
  triggers.exitstatus: "" => "0"
  triggers.stderr:     "" => ""
  triggers.stdout:     "" => "5d236a3185dd33b5ac46895edf038e23"

Hope it helps someone.

Thanks, I think I figured how to skip ruby/bash all together - have to try it out hopefully today

Cool. Btw. what bugs the hell out of me. Because i'm not so much familiar with terraform yet. Any idea if its possible to apply this kind of module to remote-exec? My use case is to grab cluster token from logfile on master instance and pass it to worker instances.

@krzysztofantczak I think I got it working, see the new version I pushed (0.4.1 or master) - let me know if that works and then I can close this.

Yes I believe remote-exec could be done, do you have something to share what you are doing currently and what's the problem?

Actually, i got my issue resolved with local-exec and ssh'ing back to my remote instance to get the data i need, thanks ;-)

Okay, closing this now when the new implementation is there.