Local-Shell doesn't when building from a windows machine

1,162 views
Skip to first unread message

James Elmes

unread,
Jul 10, 2017, 12:41:19 AM7/10/17
to Packer
It seems the post-processors LOCAL-SHELL won't run windows commands out of the box (example below), it tries to run as a linux command as seen in the logs. Does anyone know what i need to change to get this working for windows? I read somewhere its using the "execute-command" option.. any help appreciated

  "post-processors": [
    {
    "type":"shell-local",
    "inline":"ovftool/ovftool.exe -tt=ova vi://root:pass...@10.1.1.13/VMIMAGE ovaexport1.ova"


2017/07/05 21:12:19 ui: ==> Windows2012R2v1-2017-07 (shell-local): Post processing with local shell script: C:\Users\ADMINI~1.ISS\AppData\Local\Temp\packer-shell486898063
2017/07/05 21:12:19 packer.exe: 2017/07/05 21:12:19 starting local command: chmod +x "C:\Users\ADMINI~1.ISS\AppData\Local\Temp\packer-shell486898063"; 

regards

James

Rickard von Essen

unread,
Jul 10, 2017, 1:54:19 AM7/10/17
to packe...@googlegroups.com
See https://github.com/hashicorp/packer/issues/4140
> --
> This mailing list is governed under the HashiCorp Community Guidelines -
> https://www.hashicorp.com/community-guidelines.html. Behavior in violation
> of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/packer/issues
> IRC: #packer-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups
> "Packer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to packer-tool...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/packer-tool/724d2823-9f46-46bd-926e-4f8d371d75f8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

James Elmes

unread,
Jul 10, 2017, 3:29:35 AM7/10/17
to Packer
that doesn't help unless you can explain the below to me, where is {{.Script}} predefined, and what would the below do? I just don't understand what the below achieves / how it works

> "post-processors": [
>     {
>       "type": "shell-local",
>       "execute_command": "Powershell.exe Move-Item {{.Script}} {{.Script}}.ps1; Powershell.exe -File {{.Script}}.ps1",
>       "inline": ["write-output HIIIIIIIII"]
>     }

Rickard von Essen

unread,
Jul 10, 2017, 4:57:10 AM7/10/17
to packe...@googlegroups.com
I know very little about Windows, but the problem seems to be that the
shell-local assumes a Posix shell (and also does shell). To workaround
this you can override the default "execute_command" (according to
Megan Marsh).

{
"type":"shell-local",
"execute_command": "Powershell.exe Move-Item {{.Script}}
{{.Script}}.ps1; Powershell.exe -File {{.Script}}.ps1",
"inline": ["ovftool/ovftool.exe -tt=ova
vi://root:pass...@10.1.1.13/VMIMAGE ovaexport1.ova"]
}

This will result in that the following command will be executed:
- A temp file will be created with content of <inline> (which should
be an array).
- The script is moved to a new name with extension ps1 since
Powershell seems to require this.
- The script is executed.

Run the null builder against a running machine or a Vagrant box and try it out.
> --
> This mailing list is governed under the HashiCorp Community Guidelines -
> https://www.hashicorp.com/community-guidelines.html. Behavior in violation
> of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/packer/issues
> IRC: #packer-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups
> "Packer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to packer-tool...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/packer-tool/91c5102f-1939-4881-9a1c-8829326c76d2%40googlegroups.com.

Alexander Lehmann

unread,
Jul 10, 2017, 5:31:14 AM7/10/17
to Packer
You can either provide a local script file or the inline string to the command, the execute_command is expanded with this filename for the .Script variable.

The result should execute something like:

Powershell.exe Move-Item \temp\script \temp\script.ps1; Powershell.exe -File \temp\script.ps1

where \temp\script contains your command
Message has been deleted

James Elmes

unread,
Jul 10, 2017, 7:35:33 AM7/10/17
to Packer
I have tried this but it doesn't work, just "error running script"

the ovftool command needs to be run in CMD.EXE i believe, the below is what it looks like

C:\PACKER\packer_config>ovftool -tt=ova vi://root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport1.ova
Opening OVA target: ovaexport1.ova
Writing OVA package: ovaexport1.ova
Disk progress: 2%

I have tried the below

  "type":"shell-local", 
  "execute_command": "cmd.exe {{.Script}}.cmd", 
  "inline": ["ovftool.exe -tt=ova vi://root:root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"] 
 
AND

  "type":"shell-local", 
  "execute_command": "Powershell.exe Move-Item {{.Script}} {{.Script}}.ps1; cmd.exe {{.Script}}.cmd", 
  "inline": ["ovftool.exe -tt=ova vi://root:root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"] 

AND

  "type":"shell-local", 
  "execute_command": "cmd.exe /c {{.Script}}", 
  "script": "ovftool/ovftool_command.cmd"

I get the below result, furthermore i never actually see this file get created.. which me be part of the puzzle..

==> null: Running post-processor: shell-local
==> null (shell-local): Post processing with local shell script: C:\Users\ADMINI~1.ISS\AppData\Local\Temp\packer-shell35
9109543
Build 'null' errored: 1 error(s) occurred:

OR 

==> null: Running post-processor: shell-local
==> null (shell-local): Post processing with local shell script: scripts/ovftool_command.cmd
Build 'null' errored: 1 error(s) occurred:

* Post-processor failed: Error executing script: scripts/ovftool_command.cmd
any idea how to run a .CMD guys?


Alexander Lehmann

unread,
Jul 10, 2017, 11:53:53 AM7/10/17
to Packer
I tried a few things with inline, but I couldn't get that to work.

When using a local .cmd file, calling the script works with the following entry:

    {

      "type":"shell-local",
      "execute_command": "cmd.exe /c {{.Script}}",
      "script": "createovf.cmd"
    }

and the script is in the packer dir .\createovf.cmd

"\Program Files (x86)\VMware\VMware Workstation\OVFTool\ovftool.exe" source/vmware-iso/packer-centos-7-x86_64.vmx export-vmware-ovf



On Monday, July 10, 2017 at 1:35:33 PM UTC+2, James Elmes wrote:
I have tried this but it doesn't work, just "error running script"

the ovftool command needs to be run in CMD.EXE i believe, the below is what it looks like

C:\PACKER\packer_config>ovftool -tt=ova vi://root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport1.ova
Opening OVA target: ovaexport1.ova
Writing OVA package: ovaexport1.ova
Disk progress: 2%

I have tried the below

  "type":"shell-local", 
  "execute_command": "cmd.exe {{.Script}}.cmd", 
  "inline": ["ovftool.exe -tt=ova vi://root:root:password@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"] 
 
AND

  "type":"shell-local", 
  "execute_command": "Powershell.exe Move-Item {{.Script}} {{.Script}}.ps1; cmd.exe {{.Script}}.cmd", 
  "inline": ["ovftool.exe -tt=ova vi://root:root:password@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"] 

James Elmes

unread,
Jul 11, 2017, 5:56:40 AM7/11/17
to Packer
Thanks for responding Alex, i have tried the above as well but cannot get even the simplest of commands to run, even the below as mentioned in https://github.com/hashicorp/packer/issues/4140

I really need to get this working, is there anyone else i can reach out to?

> "post-processors": [
>     {
>       "type": "shell-local",
>       "execute_command": "Powershell.exe Move-Item {{.Script}} {{.Script}}.ps1; Powershell.exe -File {{.Script}}.ps1",
>       "inline": ["write-output HIIIIIIIII"]
>     }

Rickard von Essen

unread,
Jul 11, 2017, 6:31:50 AM7/11/17
to packe...@googlegroups.com
One quick workaround is to install Cygwin and run Packer in that. Or
just wrap the Packer build in a script that run ovftool after packer.
>>> vi://root:root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"]
>>>
>>> AND
>>>
>>> "type":"shell-local",
>>> "execute_command": "Powershell.exe Move-Item {{.Script}}
>>> {{.Script}}.ps1; cmd.exe {{.Script}}.cmd",
>>> "inline": ["ovftool.exe -tt=ova
>>> vi://root:root:pass...@10.1.1.11/Windows2012R2v1-2017-07 ovaexport12.ova"]
>>>
>>> AND
>>>
>>> "type":"shell-local",
>>> "execute_command": "cmd.exe /c {{.Script}}",
>>> "script": "ovftool/ovftool_command.cmd"
>>>
>>> I get the below result, furthermore i never actually see this file get
>>> created.. which me be part of the puzzle..
>>>
>>> ==> null: Running post-processor: shell-local
>>> ==> null (shell-local): Post processing with local shell script:
>>> C:\Users\ADMINI~1.ISS\AppData\Local\Temp\packer-shell35
>>> 9109543
>>> Build 'null' errored: 1 error(s) occurred:
>>>
>>> OR
>>>
>>> ==> null: Running post-processor: shell-local
>>> ==> null (shell-local): Post processing with local shell script:
>>> scripts/ovftool_command.cmd
>>> Build 'null' errored: 1 error(s) occurred:
>>>
>>> * Post-processor failed: Error executing script:
>>> scripts/ovftool_command.cmd
>>> any idea how to run a .CMD guys?
>>>
>>>
> --
> This mailing list is governed under the HashiCorp Community Guidelines -
> https://www.hashicorp.com/community-guidelines.html. Behavior in violation
> of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/packer/issues
> IRC: #packer-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups
> "Packer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to packer-tool...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/packer-tool/e81d5874-df76-4666-9aa8-9b77b4e70b23%40googlegroups.com.

Alexander Lehmann

unread,
Jul 11, 2017, 7:02:51 AM7/11/17
to Packer
I wonder if the command works in my setup due to an installation of cmder which includes git for windows, which in turn includes sh and bash.

Could try to install either cygwin or cmder (http://cmder.net/) to check if the operation depends on sh?

James Elmes

unread,
Jul 12, 2017, 7:25:47 AM7/12/17
to Packer
thanks again Alex, i didn't really want to increase the moving parts involved with the packer process but i may give this a go.

the other option is to just run it with the run command like the below. 

$env:PACKER_ESXI_VNC_PROBE_TIMEOUT="30s"
$env:PACKER_LOG="1"
$env:PACKER_LOG_PATH="packerlog.txt"
Start-Process c:\packer\packer_config\packer.exe -ArgumentList "build -var-file esxi_variables.json windowsonesxi4.json" -Wait -NoNewWindow
ovftool.exe -tt=ova vi://root:pass...@10.122.104.11/Windows2012R2v1-2017-07 ovaexport1112.ova

ill let you know how this goes

regards

James

Megan Marsh

unread,
Jul 14, 2017, 1:32:55 PM7/14/17
to Packer
Hi James,

Coming back online after a couple days away so apologies if you've already figured this out and I'm being redundant.  

The operation definitely depends on sh being installed, so installing that will very likely solve your issue. 

{{.Script}} is a Packer internal variable that references the location of the script in the tmp folder on the guest vm. This is implied in the docs, but it could probably be clearer that you can actually use {{.Script}} in your packer template as a way of referring to that script's absolute path.

I'm guessing the reason the code Rickard pasted isn't working for you "out of the box" is because you don't have shell installed and you didn't apply the diff from my referenced comment beforehand (https://github.com/hashicorp/packer/compare/shell_4140)? As you can see in there, Packer currently hardcodes the command as assuming you've got shell: `localCmd := exec.Command("sh", "-c", cmd.Command)`.

Hope this helps! We've definitely got some kinks to work out on Windows.

James Elmes

unread,
Jul 18, 2017, 4:06:28 AM7/18/17
to Packer
Thanks for your reply and explaining {{.script}. 

I don't sh, I have just installed CMDER but not sure what to do next to be honest.. not being a linux guru

do i have to run the packer commands from this? if so wouldn't they be completely different? i currently run using powershell

Example:
$env:PACKER_ESXI_VNC_PROBE_TIMEOUT="30s"
$env:PACKER_LOG="1"
$env:PACKER_LOG_PATH="packerlog.txt"
Start-Process c:\packer\packer_config\packer.exe -ArgumentList "build -var-file esxi_variables.json windowsonesxi4.json" -Wait -NoNewWindow

Alvaro Miranda Aguilera

unread,
Jul 18, 2017, 5:38:19 AM7/18/17
to packe...@googlegroups.com
Hello

You need to override the command argument

default is:

execute_command = ["/bin/sh", "-c", "{{.Command}}"]

use something you would use to run the command in the command line.




Alexander Lehmann

unread,
Jul 18, 2017, 10:17:16 AM7/18/17
to Packer
It should work to run a powershell script with the execute_command which renames the file to .ps1 and runs it.

You can either run packer from the cmder screen or you have to put the dir where the sh.exe command is present is included in the search path in Powershell.

James Elmes

unread,
Jul 18, 2017, 7:38:46 PM7/18/17
to Packer
alright i got a bit further now after adding the sh.exe to the same location from where im running packer.

now im getting the below, doesn't seem to matter what script i run but here is the result:

2017/07/19 09:04:46 packer.exe: 2017/07/19 09:04:44 starting local command: cmd.exe /c scripts/copy.cmd
2017/07/19 09:04:46 [INFO] (telemetry) ending shell-local
2017/07/19 09:04:46 [INFO] (telemetry) found error: Erroneous exit code 3221225781 while executing script: scripts/copy.cmd
Please see output above for more information.
2017/07/19 09:04:46 Deleting original artifact for build 'null'
2017/07/19 09:04:46 ui error: Build 'null' errored: 1 error(s) occurred:
* Post-processor failed: Erroneous exit code 3221225781 while executing script: scripts/copy.cmd
2017/07/19 09:04:46 Builds completed. Waiting on interrupt barrier...
2017/07/19 09:04:46 machine readable: error-count []string{"1"}
2017/07/19 09:04:46 ui error: 
==> Some builds didn't complete successfully and had errors:
2017/07/19 09:04:46 machine readable: null,error []string{"1 error(s) occurred:\n\n* Post-processor failed: Erroneous exit code 3221225781 while executing script: scripts/copy.cmd\n\nPlease see output above for more information."}
2017/07/19 09:04:46 ui error: --> null: 1 error(s) occurred:
* Post-processor failed: Erroneous exit code 3221225781 while executing script: scripts/copy.cmd
Please see output above for more information.
2017/07/19 09:04:46 ui: 


It is just a simple .CMD command to copy a file, when i run in CMD it works fine as below.
C:\PACKER\OPTUSgit\scripts>copy.cmd
C:\PACKER\OPTUSgit\scripts>cmd.exe /c copy c:\tools\winscp.ini c:\temp
        1 file(s) copied.

Below is my test template, just encase i am doing something wrong but i doubt it:
{

"builders":[{
    "type": "null",
    "communicator":"winrm",
    "winrm_host": "10.1.1.10",
     "winrm_username":"jimmy",
     "winrm_password":"password",
     "winrm_port":"5985",
     "winrm_timeout":"24h"

}],

"provisioners": [

    {
    "type": "windows-shell",
    "inline": "shutdown /s /t 1 /f /d p:4:1 /c \"Packer Shutdown Stage 1\""
  
  }
],
"post-processors":[
      "type":"shell-local", 
      "execute_command": "cmd.exe /c {{.Script}}", 
      "script": "scripts/copy.cmd"



any further assistance would be appreciated..

Alvaro Miranda Aguilera

unread,
Jul 19, 2017, 3:51:20 AM7/19/17
to packe...@googlegroups.com
Hello

So packer expect exit code 0 to assume all went fine.

It seems when you run the command it exit with error code 3221225781

If I do a script file  a.cmd
copy a b

and I run it like

cmd.exe /c a.cmd

Z:\t>copy a b
        1 file(s) copied.

It gives me:

Z:\t>echo %errorlevel%
0

Can you run something like that?

file a need to exits

Alvaro


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/6c9563e2-d2ae-4928-8739-4575acf764f5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Alexander Lehmann

unread,
Jul 19, 2017, 6:47:42 AM7/19/17
to Packer
Yes, you can probably fix the script by adding exit 0 as the last line
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages