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

Make it easier to run multiline script #558

Open
cowwoc opened this issue Nov 14, 2024 · 3 comments
Open

Make it easier to run multiline script #558

cowwoc opened this issue Nov 14, 2024 · 3 comments
Labels
kind/enhancement Improvements or new features

Comments

@cowwoc
Copy link

cowwoc commented Nov 14, 2024

Hello!

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

Please make it easier to run multiline scripts that are declared in a string variable. Currently, we need to do something like this:

create: "PULUMI_COMMAND_STDOUT=true sh -c '`echo \"${script}\" > ~/script.sh && chmod u+x ~/script.sh && ~/script.sh'"
@cowwoc cowwoc added kind/enhancement Improvements or new features needs-triage Needs attention from the triage team labels Nov 14, 2024
@mjeffryes
Copy link
Member

Thanks for the suggestion @cowwoc! I can think of a few other workarounds for packaging a multiline script into a single command (bash functions, stuffing '; ' at the end of every line but the last, etc.) but I understand that it would be much nicer if you didn't any packaging like that.

One challenge with just executing a multiline string right now is that Command is actually somewhat agnostic about the shell that it's running in. You could use bash, Tcsh, powershell, or something more obscure, but that means that we probably can't actually just run a multiline script since every shell would probably need a different approach to packaging it as a single command to run.

What might be doable would be to run a list of commands in order (but as completely separate processes). That would require you to pass a list of strings rather than one multi line string, but would be easier than the cat into the file, then run the file approach you're using. It wouldn't be quite like running a script though, since you could not set variables in one line and then use them later in another. Another challenge with this approach would be deciding what to do when one of the commands fails; how to represent all the results in state, etc.

Given all these challenges, I'm not sure this is something we could move on anytime soon, but I'd be very open to more suggestions about how we could make your use case better in light of all the nuance above!

@mjeffryes mjeffryes removed the needs-triage Needs attention from the triage team label Nov 14, 2024
@cowwoc
Copy link
Author

cowwoc commented Nov 15, 2024

One challenge with just executing a multiline string right now is that Command is actually somewhat agnostic about the shell that it's running in. You could use bash, Tcsh, powershell, or something more obscure, but that means that we probably can't actually just run a multiline script since every shell would probably need a different approach to packaging it as a single command to run.

Maybe I'm missing something but... You already have remote.CopyToRemote. You could write the string into a file, upload it, chmod u+x on it, and then execute it. It would be up to the user to add whatever sha-bang header is necessary for their script.

Would this work?

@mjeffryes mjeffryes added the needs-triage Needs attention from the triage team label Nov 27, 2024
@mjeffryes
Copy link
Member

I don't think so; I can't run chmod u+x because I might not be on a *nix host. You as the user could do that since you know the target:

  1. Put your script in a StringAsset(Assets & Archives and copy to the host with CopyToRemote
  2. Use remote.Command to run chmod u+x script.sh && ./script.sh

I think the two options for general support that would be most reliable would be:

  1. A shorthand for executing multiple independent commands (like I outlined in my first command)
  2. Some helper functions that would bundle a multiline string into a single command for a specific shell. eg. command.createBashMultilineCommand("...") that would do something equivalent of what you're writing by hand now.

@mjeffryes mjeffryes added awaiting-feedback Blocked on input from the author and removed needs-triage Needs attention from the triage team awaiting-feedback Blocked on input from the author labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

2 participants