General Waters
General Waters

Reputation: 1139

Add executable to Windows PATH from .ebextensions and then call executable in subsequent command

I have a Windows Elastic Beanstalk instance. I have the following commands.config in my project's .ebextensions

commands:
  00-install-choco:
    command: |
        powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"
  01-set-choco-path:
    command: |
        SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin

These two commands are working. However, I also have a container-commands.config file which needs to call upon choco, but it keeps failing due to 'choco' is not recognized as an internal or external command

container_commands:
  01-install nssm:
    command: |
        choco install nssm -y

How do I get subsequent commands to pick up the updated PATH? Is there a more appropriate approach to installing binaries and setting them to Window's PATH during ESB instance creation?

I attempted to follow https://aws.amazon.com/blogs/developer/using-nuget-and-chocolatey-package-managers-in-aws-cloudformation-and-aws-elastic-beanstalk/

files:
  c:/tools/ewmp.cmd:
    content: |
      @ECHO OFF
      FOR /F "tokens=3,*" %%a IN ('REG QUERY "HKLMSystemCurrentControlSetControlSession ManagerEnvironment" /v PATH') DO PATH %%a%%b
      %*
commands:
  01-install-choco:
    command: powershell -NoProfile -ExecutionPolicy unrestricted -Command "Invoke-Expression ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
    waitAfterCompletion: 0
  02-install-nssm:
    command: c:/tools/ewmp choco install nssm -y
    waitAfterCompletion: 0

But even that gives me similar errors.

-----------------------Command Output-----------------------
ERROR: Invalid key name.
Type "REG QUERY /?" for usage.
'choco' is not recognized as an internal or external command,
operable program or batch file.
------------------------------------------------------------

Upvotes: 1

Views: 593

Answers (2)

DanH
DanH

Reputation: 1

There's definitely an escaping issue in the documentation, using that code as-is didn't work for me. Here is code that works:

files:
  c:\\tools\\ewmp.cmd:
    content: |
      @ECHO OFF
      FOR /F "tokens=3,*" %%a IN ('REG QUERY "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PATH') DO PATH %%a%%b
      %*
commands:
  00_create_temp_dir:
    command: powershell.exe -Command "if (-not (Test-Path C:\\temp)) { New-Item -Path C:\\temp -ItemType Directory }"
    ignoreErrors: false
    waitAfterCompletion: 0
  01_install_chocolatey:
    command: powershell -NoProfile -ExecutionPolicy unrestricted -Command "Invoke-Expression ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
    waitAfterCompletion: 0
    ignoreErrors: false
  02_install_ffmpeg:
    command: c:\\tools\\ewmp choco install ffmpeg -y > C:\\temp\\ffmpeg_install.log 2>&1
    waitAfterCompletion: 0
    ignoreErrors: false
...

Although I used double-backslashes, I did test with forward slashes and it worked in most cases. I don't have a complete test matrix for that but I'll say it's flexible in that regard.

Upvotes: 0

Gary Ewan Park
Gary Ewan Park

Reputation: 19021

I am not a user of Elastic Beanstalk, so I can't speak to whether this will work or not, but according to the documentation it is necessary to provide an additional command to update to PATH variable that is used:

The Chocolatey installer and the packages it installs may modify the machine’s PATH environment variable. This adds complexity since subsequent commands after these installations are executed in the same session, which does not have the updated PATH. To overcome this, we utilize a command file to set the session’s PATH to that of the machine before it executes our command.

files:
  c:/tools/ewmp.cmd:
    content: |
      @ECHO OFF
      FOR /F "tokens=3,*" %%a IN ('REG QUERY "HKLMSystemCurrentControlSetControlSession ManagerEnvironment" /v PATH') DO PATH %%a%%b
      %*
commands:
  00-install-choco:
    command: powershell -NoProfile -ExecutionPolicy unrestricted -Command "Invoke-Expression ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
container_commands:
  01-install nssm:
    command: |
      c:toolsewmp choco install nssm -y

NOTE: The one thing I am not clear on, based on the documentation, is the usage of c:toolsewmp rather than c:/tools/ewmp in the command section. I don't know if this is an escaping issue in the code that is being rendered to the browser in the documentation, or whether this is intentional. You may need to play with this to see what works.

Upvotes: 1

Related Questions