0

When trying to install Chocolatey for the first time using Chef and the Chocolatey cookbook I get the message on my chef-zero provisioning run:

WARNING: It's very likely you will need to close and reopen your shell

When it comes time to install the apps I've listed in my attributes file for the Chocolatey cookbook, it fails because the command "choco" cannot be found. When I provision a second time everything works fine.

I've tried to restart powershell within the Chocolatey cookbook but that fails. I was wondering if anyone else has ran into this and if there is a work around.

Here is the output of the STDERR:

==> default:     STDOUT:
==> default:     STDERR: C:\Users\vagrant\AppData\Local\Temp\chef-script20160425-2280-k20yic.ps1 : The
==> default:
==> default:     term 'choco' is not recognized as the name of a cmdlet, function, script file,
==> default:
==> default:     or operable program. Check the spelling of the name, or if a path was
==> default:
==> default:     included, verify that the path is correct and try again.
==> default:
==> default:         + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep
==> default:
==> default:        tion
==> default:
==> default:         + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
==> default:
==> default:        n,chef-script20160425-2280-k20yic.ps1
==> default:     ---- End output of "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None -File "C:/Users/vagrant/AppData/Local/Temp/chef-script20160425-2280-k20yic.ps1" ----
==> default:     Ran "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None -File "C:/Users/vagrant/AppData/Local/Temp/chef-script20160425-2280-k20yic.ps1" returned 1
Kryten
  • 595
  • 3
  • 10
  • 25
  • It is common to bundle chocolatey into your golden image. The reason it fails is because the chocolatey directory isn't part of the $PATH. See this question: http://stackoverflow.com/questions/6284517/how-can-you-use-a-chef-recipe-to-set-an-environment-variable and this question: http://stackoverflow.com/a/17851541/1626687 – spuder Apr 25 '16 at 18:02

1 Answers1

-1

As the warning says, you need to restart either the shell or another process to pick up global modifications to $PATH.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Well, I tried to put a powershell_script resource at the end of the Chocolatey default.rb that sources $profile but that still doesn't work. – Kryten Apr 25 '16 at 20:27
  • Indeed, that has no effect on the parent process. – coderanger Apr 25 '16 at 20:46
  • Any ideas on how to force the process to pick up global modifications? As it is now I have to use the full path to chocolatey on my first chef run. Any code you can give me to fix this would be appreciated. Thanks. – Kryten Apr 25 '16 at 21:08
  • Yes, it will pick up global modifications when you restart it. Just like the warning says. – coderanger Apr 25 '16 at 22:36
  • Chef can't really restart itself, it would have to be at the level of whatever is running Chef. If that's something like `knife winrm` then you run that command again. If it's human in a console, they restart the console and re-run Chef. This is something very difficult to automate, which is why someone pointed out before that it is often best to put Chocolately in your base image before Chef touches the box. – coderanger Apr 25 '16 at 22:44