Packer Windows build script not waiting for install to finish before moving on

971 views
Skip to first unread message

winderr...@gmail.com

unread,
Jun 20, 2018, 10:59:29 AM6/20/18
to Packer
Hi there,
We're trying to build a windows image. The script looks like this:

```
"provisioners": [
    {
      "type": "powershell",
      "inline": [
        "Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))",
        "refreshenv"
      ]
    },
    {
      "type": "powershell",
      "inline": [
        "Write-Host \"Installing applications\"",
        "Start-Process choco -PassThru -ArgumentList 'install -y awscli 7zip.install' -NoNewWindow -Wait",
        "cat C:\\ProgramData\\chocolatey\\logs\\chocolatey.log"
      ]
    },
    {
      "type": "windows-shell",
      "inline": [
        "aws s3 cp ...",
      ]
    },
```

choco is installed, and the installation of the awscli starts, but the packer process continues before the process has finished installing. The offending logs look like this:

```
amazon-ebs: Ensuring chocolatey commands are on the path
    amazon-ebs: Ensuring chocolatey.nupkg is in the lib folder
    amazon-ebs: Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
==> amazon-ebs: Provisioning with Powershell...
==> amazon-ebs: Provisioning with powershell script: /var/folders/pw/wy4vnj7n48g5lmq6m94xwl880000gn/T/packer-powershell-provisioner725332409
    amazon-ebs: Installing applications
    amazon-ebs:
    amazon-ebs: Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    amazon-ebs: -------  ------    -----      -----     ------     --  -- -----------
    amazon-ebs:      70       6     1016       3712       0.08   3476   0 choco
    amazon-ebs: Chocolatey v0.10.11
    amazon-ebs: Installing the following packages:
    amazon-ebs: awscli;7zip.install
    amazon-ebs: By installing you accept licenses for the packages.
    amazon-ebs: Progress: Downloading awscli 1.15.41... 100%
    amazon-ebs:
    amazon-ebs: awscli v1.15.41 [Approved]
    amazon-ebs: awscli package files install completed. Performing other installation steps.
    amazon-ebs: Downloading awscli 64 bit
    amazon-ebs: Progress: 100% - Completed download of C:\Users\admin\AppData\Local\Temp\chocolatey\awscli\1.15.41\AWSCLI64-1.15.41.msi (10.39 MB).
    amazon-ebs: Download of AWSCLI64-1.15.41.msi (10.39 MB) completed.
==> amazon-ebs: Provisioning with windows-shell...
==> amazon-ebs: Provisioning with shell script: /var/folders/pw/wy4vnj7n48g5lmq6m94xwl880000gn/T/packer-windows-shell-provisioner359973109
    amazon-ebs:
    amazon-ebs: 'aws' is not recognized as an internal or external command,
    amazon-ebs: operable program or batch file.
    amazon-ebs: 
```

You can see that it has continued before the choco process has finished installing everything and the subsequent aws call fails.

These commands work when you run them manually. I've tried running it as a windows-shell provisioner rather than a powershell one and I see the same result. I've tried various configurations of Start-Process (e.g. returning a variable and waiting on that, etc.) and can't find a consistent way of fixing this.

Does anyone have any experience of working with choco and packer? Is there anything obvious that I'm doing wrong?

Thanks,
Phil

t3e...@gmail.com

unread,
Jun 26, 2018, 5:43:01 AM6/26/18
to Packer
I Phil, 

I was having a similar issue when I was trying to install a number of application during the build process. At first I had them being called inline as you have however it was not waiting for them to finish. What I ended up doing was moving each individual install to it's own ps1 script, copied each one up to the C:\ then excited them one by one. 

e.g. 

* Script called Install_AWSCLI64.ps1

Start-Process -FilePath C:\installfiles\AWSCLI64.msi -ArgumentList /quiet -wait

* Packer JSON looks like 

   {
      "type": "file",
      "source": "/packer/installfiles/AWSCLI64.msi",
      "destination": "C:\\installfiles"
    },
    {
      "type": "powershell",
      "script": "/packer/scripts/install_AWSCLI64.ps1"
    },


This dose mean the JSON file is adding additional lines of code however, I am not seeing to much difference in performance.

winderr...@gmail.com

unread,
Jul 17, 2018, 6:26:22 AM7/17/18
to Packer
Hi again, thanks for the response.

I tried what you suggested and unfortunately, it doesn't help. The crucial thing that seems to cause this is the `choco install` command. It doesn't seem to happen with other commands. I have tested manually on a win10 machine and it works as expected. Also, interestingly, the packer commands do work when you use a windows server instance (inline or as files).

I will raise a bug but it is hard to recreate due to the use of a custom win10 image.

Phil

j.r.haw...@googlemail.com

unread,
Jul 18, 2018, 8:53:14 AM7/18/18
to Packer
I have not had this specific issue but I know from experience with MSIs that they get passed to the windows module installer service and can still be installing after msiexec has returned an exit code to the operating system.

I don't know much about chocolatey but if the particular packages you are installing are actually wrappers around .MSI files, I can imagine this could happen.

As for workarounds you can stick in a Start-Sleep -Seconds <something big> or perhaps nicer would be to monitor for the presence of a file that gets installed by the last choco package you install.

Hope this helps,

Jon
Reply all
Reply to author
Forward
0 new messages