hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.6k stars 9.54k forks source link

local-exec should allow choice of shell #2974

Closed talarczykco closed 7 years ago

talarczykco commented 9 years ago

As a Linux developer working in a Windows environment, I am comfortable with bash and can work with cmd.exe when I have to. But Terraform dictates which shell executes local-exec directives based on platform alone, regardless of whether I know other shells are installed on my teams' workstations.

Can Terraform allow me to use my choice of shell?

My use case is this: I need to write templates of non-trivial length to a local Chef repo before provisioning my resource. The local repository is uploaded to the resource, where Chef Zero uses it to complete provisioning. The templates in question are Chef environment, client.rb, and node JSON files - the largest of which is 2.5K in length.

Redirecting this with /bin/sh is trivial. Redirecting this with cmd has been an exercise in futility, since cmd considers any newline to be end-of-command. Quoting does not work sanely with cmd. Forward slashes do not work sanely with cmd (at least PowerShell can convert them to backslashes; cmd just considers a forward slash to be an option to the command.) Escaping newlines, if even possible with cmd, would tie me to Windows, which I don't want to be. I want to continue to develop from my Mac and Linux workstations.

I have tried writing batch files that run cross-platform, which works once you get past the initial OS detection phase. But I'm still faced with forward/backward slash problems that I can't add a conditional for in main.tf. So I chdir into a scripts/ directory, but I'm still facing path problems and having to add -state to my terraform output commands (which incidentally I can't script since the tfstate file isn't written at the time I try to access it.)

If I know my workstation has sh from, say, an Msysgit or ChefDK installation, shouldn't I be able to use it? Once I get to the point where I'm putting command = "sh -c 'do-stuff'" in my local-exec directives, I declare madness ;) I'm open to alternatives, but I've tried a few.

talarczykco commented 9 years ago

I have preliminary code committed to my fork to add an "interpreter" argument to local-exec and could spend some amount of time contributing a more fully fleshed out solution; I may need the fork even if it's not accepted for upstream use. That said, I might need a hand figuring out how to add an optional argument since the validator is still telling me its unrecognized.

Preskton commented 8 years ago

Bump this, but from the other side - as primarily a Windows person that prefers PowerShell over cmd.exe, it would be super neat if I could select my shell then pass in a config object to match that shell. For example, with PowerShell, I might choose to just pass an inline script, a base-64 encoded script, a path to a file, etc.

jsalbright commented 7 years ago

Bump. We really need this guys...

fractos commented 7 years ago

Bumping this... just ran smack into it when otherwise happily running Terraform under git-bash on Windows and trying to do the actions in #8090

talarczykco commented 7 years ago

Submitted PR #15166. I no longer have a need for this, but I'm happy to make this contribution and help with minor testing/edits. See link for example usage.

talarczykco commented 7 years ago

PR #15166 merged into master by @apparentlymart this morning. Thanks Martin!

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.