Catch error of Null Builder with Shell Provisioner

27 views
Skip to first unread message

Becky

unread,
Apr 13, 2017, 3:49:03 AM4/13/17
to Packer

Hi, 


I am using packer 1.0.0 on Windows 10.

 

Run.bat

 

Packer build –only mybuilder –var-file=variables_file.json

Echo %errorlevel%  

<<This always show 0 even if my shell scripts exited with returned code 1. 

Is this expected behavior, if so then how can one catch the errors of SHELL and take action in run.bat file ?>>

 

variables_file.json

 

{

    "builders": [{

        "type": "null",

        "name": " mybuilder",

        "ssh_host": "{{user ` host`}}",

        "ssh_username": "{{user ` username`}}",

        "ssh_password": "{{user ` password`}}",

        "ssh_timeout" : "{{user `esxi_ssh_timeout`}}"

    }],

    "provisioners": [{

        "type": "shell",

        "only": ["mybuilder"],

        "scripts": [

            "script/testESXi.sh"

        ]

    }

    ]

}

 

testESXi.sh

 

#!/bin/sh

 

exit 1;


Regards,

Becky

Alvaro Miranda Aguilera

unread,
Apr 14, 2017, 4:47:55 AM4/14/17
to packe...@googlegroups.com
Hello i see test.sh but you mention run.bat, so I assume you run packer in windows, and want to capture the error?


lets go on stages

Can you run the packer in debug mode so we can confirm the exit code of that part is fine?

set PACKER_LOG=1

then run the build

share a gist (gist.github.com)

and if you can run in your run.bat something like this after the packer run: 
if %errorlevel% neq 0 exit /b %errorlevel%

Should help.

Thanks
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/82f04172-898b-4dd3-8184-f0202aa9bf36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Becky

unread,
Apr 15, 2017, 12:55:19 AM4/15/17
to Packer
Hi Alvaro,

I was able to figure out the issue. It seems PACKER works fine.

Issue was in my script in highlighted line

WORNG:
packer build -only=BUILDER-TEST-ESXI -var-file=%VARIABLES_FILE_PATH1% %VARIABLES_FILE_PATH2% >> test.log  2>&1

CORRECT:
packer build -only=BUILDER-TEST-ESXI -var-file=%VARIABLES_FILE_PATH1% %VARIABLES_FILE_PATH2% >> test.log


Here is the working script in which i am able to catch the error...

@echo off
REM setlocal EnableDelayedExpansion
cls
SET "APP_HOME_DIR=%~dp0"
SET "APP_BIN_DIR=%~dp0bin"
SET "PACKER_HOME=;%APP_BIN_DIR%\packer"
SET "VARIABLES_FILE_PATH1=%APP_HOME_DIR%template\variablesTest.json"
SET "VARIABLES_FILE_PATH2=%APP_HOME_DIR%template\connectToESXiTest.json"
SET "PATH=%PATH%%PACKER_HOME%"

echo Validating Template
packer validate -var-file=%VARIABLES_FILE_PATH1% %VARIABLES_FILE_PATH2%

echo Packer Build
packer build -only=BUILDER-TEST-ESXI -var-file=%VARIABLES_FILE_PATH1% %VARIABLES_FILE_PATH2% >> test.log
IF %ERRORLEVEL% NEQ 0 echo Error: Seems packers exited with non-zero exit code

Thanks,
Becky
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