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

[aws-ec2] shellCommand Windows #10684

Closed
sd3z opened this issue Oct 5, 2020 · 3 comments · Fixed by #10691
Closed

[aws-ec2] shellCommand Windows #10684

sd3z opened this issue Oct 5, 2020 · 3 comments · Fixed by #10691
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort needs-triage This issue or PR still needs to be triaged. p2

Comments

@sd3z
Copy link

sd3z commented Oct 5, 2020

When using the latest Windows AMI and using shellcommand the commands fail to run with 'The system cannot find the file specified'.

Reproduction Steps

ec2.InitCommand.argvCommand(['powershell.exe',` '-command "Set-ExecutionPolicy', 'RemoteSigned -Force"'], { key: "00_executionPolicy", waitAfterCompletion: ec2.InitCommandWaitDuration.none() }),

What did you expect to happen?

The command to run successfully

What actually happened?

The error reported was


----- Error during cfn-init bootstrapping -----
ERROR] Unhandled exception during build: [Error 2] The system cannot find the file specified
Traceback (most recent call last):
  File "cfn-init", line 171, in <module>
  File "cfnbootstrap\construction.pyc", line 129, in build
  File "cfnbootstrap\construction.pyc", line 530, in build
  File "cfnbootstrap\construction.pyc", line 544, in run_config
  File "cfnbootstrap\construction.pyc", line 138, in run_commands
  File "cfnbootstrap\command_tool.pyc", line 108, in apply
  File "cfnbootstrap\util.pyc", line 497, in call
  File "cfnbootstrap\util.pyc", line 482, in call
  File "subprocess.pyc", line 710, in __init__
  File "subprocess.pyc", line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

Environment

  • CLI Version : 1.66
  • Framework Version: 1.66
  • Node.js Version: 10.x
  • OS : Windows 10
  • Language (Version): Typescript 4.0.2

Other

I believe the issue is related to shell command outputting to a array with a single element. According to the documentation here if an array is specified every command and parameter needs to be a new element (no whitespace) but if it is a string (as I think shellCommand prefers), it just needs to be a string, so

This will work

commands:
  00_executionPolicy:
    command: powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force
    waitAfterCompletion: 0

This will work

commands:
  00_executionPolicy:
    command:
      - powershell.exe
      - -command
      - Set-ExecutionPolicy RemoteSigned -Force
    waitAfterCompletion: 0

This will NOT work

commands:
  00_executionPolicy:
    command:
      - powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"
    waitAfterCompletion: 0

This is 🐛 Bug Report

@sd3z sd3z added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 5, 2020
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Oct 5, 2020
@rix0rrr
Copy link
Contributor

rix0rrr commented Oct 5, 2020

I don't understand how the example you gave:

// (A)
ec2.InitCommand.argvCommand(['powershell.exe',` '-command "Set-ExecutionPolicy', 'RemoteSigned -Force"'], { key: "00_executionPolicy", waitAfterCompletion: ec2.InitCommandWaitDuration.none() }),

Would lead to this being rendered:

// (B)
    command:
      - powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"

If anything, it should lead to this being rendered:

// (C)
    command:
      - powershell.exe 
      - -command "Set-ExecutionPolicy
      - RemoteSigned -Force"

And by the way, judging from the quotes I think you probably meant to do this:

ec2.InitCommand.argvCommand([
  'powershell.exe',
  '-command',
  'Set-ExecutionPolicy RemoteSigned -Force'
]);

I agree with you there's a bug in the implementation of shellCommand though.

rix0rrr added a commit that referenced this issue Oct 5, 2020
`InitCommand.shellCommand('abc xyz')` renders to `['abc xyz']` which actually
represents an `argv` command of one element, which is treated
differently. For example, spaces in it are not parsed, which would
be expected for a shell command.

The correct rendering for a shell command is a plain string in the
`command` property.

Fixes #10684.
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p2 labels Oct 5, 2020
@mergify mergify bot closed this as completed in #10691 Oct 5, 2020
mergify bot pushed a commit that referenced this issue Oct 5, 2020
#10691)

`InitCommand.shellCommand('abc xyz')` renders to `['abc xyz']` which actually
represents an `argv` command of one element, which is treated
differently. For example, spaces in it are not parsed, which would
be expected for a shell command.

The correct rendering for a shell command is a plain string in the
`command` property.

Fixes #10684.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Oct 5, 2020

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@sd3z
Copy link
Author

sd3z commented Oct 5, 2020

Oh sorry, you are right on the quotes being a little off.

So to iterate the issue is purely around shellCommand, specifically when I do this

ec2.InitCommand.shellCommand('powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"', { key: "00_executionPolicy", waitAfterCompletion: ec2.InitCommandWaitDuration.none() }),

i get this

    command:
      - powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"

but it should be

    command: powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"

because within the cfn-init scripts, the "subprocess" Python library is used to invoke commands on the Windows command prompt. When a list of strings is specified in the "command" section, the underlying library attempts to run the entire string specified as a single command with no arguments. This will cause Windows to return an error since it attempts to search for a program with the name of 'powershell.exe -command "Set-ExecutionPolicy RemoteSigned -Force"' as opposed to just 'powershell.exe'.

I think argvCommand is good for Windows if it renders to a string too and not string[] with a single element as the same above was happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort needs-triage This issue or PR still needs to be triaged. p2
Projects
None yet
2 participants