Not able to check if Post installation scripts are completed after reboot with Packer

39 views
Skip to first unread message

Subhashini Radhakrishnan

unread,
Dec 4, 2018, 8:10:15 PM12/4/18
to Packer
While provisioning, I install a software which needs to complete a part of installation on rebooting the machine.

So after I run my powershell script to install the software, I issue windows_restart with a restart check command.

"provisioners": [
    {
      "type": "powershell",
      "elevated_user": "myuse",
      "elevated_password": "{{ user `admin_pass` }}",
      "script": "./scripts/install_software.ps1"
   },
   {
      "type": "windows-restart",
      "restart_check_command": "powershell -command \"& {Get-Process | Where-Object { $_ProcessName -ne 'msiexec' }}\""
      "restart_timeout": "30m"
    },
]


So, I am asking packer to assume the restart is completed only when there is no misexec process is running. After which I run some clean up scripts. 

my post installation step installs some drivers and it takes atleast 20 mins to finish.

but the instance gets terminated even before running the cleanupscripts. Any clue?

TIA

Alvaro Miranda Aguilera

unread,
Dec 5, 2018, 3:41:05 PM12/5/18
to packe...@googlegroups.com
the command will only check if the restart happened.

if the command can be run, then will continue.

So you should separate your logic, from the restart and the command.

- first command
- restart
- next command


and in the next command do wait while that process is running or something.




--
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/43549505-00aa-46a9-b794-19275297aaeb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Alvaro

Subhashini Radhakrishnan

unread,
Dec 5, 2018, 6:27:25 PM12/5/18
to Packer
Hi Alvaro, Thank you for the reply!

I have nothing to check until the restart is completed.

Packer check if the restart is success by checking if winlogon service is running(if I m not wrong)

After that I also want to wait until the msiexe process is finished. 
So I assume the below command meant, the restart is considered as completed only when no msiexec process is running. AMI I Right? 

Jordan Borean

unread,
Dec 5, 2018, 8:25:01 PM12/5/18
to Packer
You would have to have a loop in your command to wait until your Get-Process call returns nothing. Right now Packer is just running the command to see if it can and then moves on regardless of the output.

Alvaro Miranda Aguilera

unread,
Dec 6, 2018, 7:34:28 AM12/6/18
to packe...@googlegroups.com
well

packer need that to confirm machine is up and running again

a script like the example in the documentation is you need for the restart part

Subha

unread,
Dec 6, 2018, 1:53:15 PM12/6/18
to packe...@googlegroups.com
Hi Alvaro,

Thank you, Which Script are you referring to? I dont see any script in the documentation. https://www.packer.io/docs/provisioners/windows-restart.html#restart_check_command 
It just uses a powershell command.
Can you please give me more specific. Thank you and sorry if its to much to ask. I am a newbie here. 

Thanks for your help



--
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.

Subha

unread,
Dec 6, 2018, 2:03:35 PM12/6/18
to packe...@googlegroups.com
I also tried to add the below script as a seperate powershell script to run after the windoes-restart. But the Instance gets terminated before this script finishes. 

{
      "type": "windows-restart",
      "restart_check_command": "powershell -command \"& {Get-Process | Where-Object { $_.Name -ne 'msiexec'};}\"",
      "restart_timeout": "30m"
    },
    {
      "script": "./scripts/restart_check.ps1",
      "type": "powershell"
    }



"restart_Checks.ps1"
----------------------------------
#############First check if msiexec is running, if not wait until it starts.#############
while ($true)
{
    if ((Get-Process -Name msiexec -ErrorAction SilentlyContinue) -eq $null)
    {

        Write-Host -ForegroundColor Red "MSI Process Is Not Running"
    
    }
    else
    {
        write-host -ForegroundColor Green "MSI Process Is Running"
        break;
    }

    Start-Sleep -Seconds 10
}

#########Once msiexc is started running, wait until it gets finished.#############
  while ($true)
 {
   $isInstallerRunning=""
   Write-Host "$(Get-Date)Checking for post installation completion..."
   $isInstallerRunning = Get-Process | Where-Object { $_.Name -like "msiexec" }
   Write-Host "installreRunning Result = $isInstallerRunning"
    if (-not ([string]::IsNullOrEmpty($isInstallerRunning)))
    {
      Write-Host "$(Get-Date) Restart is still in progress with post installation steps ..."
      Start-Sleep -Seconds 10
    }
    else
    {
      Write-Host "$(Get-Date) msiexec is not running anymore"
       break;
    }
 }

Subha

unread,
Dec 6, 2018, 2:46:34 PM12/6/18
to packe...@googlegroups.com
I just ran the above and this how it ends.. Any help is much appreciated. Thanks. 

==> amazon-ebs: Restarting Machine
==> amazon-ebs: Waiting for machine to restart...
    amazon-ebs: A system shutdown is in progress.(1115)
    amazon-ebs: WIN-R6G2K20J120 restarted.
    amazon-ebs: #< CLIXML
    amazon-ebs: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>
==> amazon-ebs: Machine successfully restarted, moving on
==> amazon-ebs: Provisioning with Powershell...
==> amazon-ebs: Provisioning with powershell script: ./scripts/restart_check.ps1
    amazon-ebs: 12/06/2018 19:38:44 Starting the Clean up ...
    amazon-ebs: MSI Process Is Running
    amazon-ebs: 12/06/2018 19:38:44Checking for post installation completion...
    amazon-ebs: installreRunning Result = System.Diagnostics.Process (msiexec)
    amazon-ebs: 12/06/2018 19:38:44 Restart is still in progress with post installation steps ...
    amazon-ebs: 12/06/2018 19:38:54Checking for post installation completion...
    amazon-ebs: installreRunning Result = System.Diagnostics.Process (msiexec)
    amazon-ebs: 12/06/2018 19:38:54 Restart is still in progress with post installation steps ...
    amazon-ebs: 12/06/2018 19:39:04Checking for post installation completion...
    amazon-ebs: installreRunning Result = System.Diagnostics.Process (msiexec)
    amazon-ebs: 12/06/2018 19:39:04 Restart is still in progress with post installation steps ...
    amazon-ebs: 12/06/2018 19:39:14Checking for post installation completion...
    amazon-ebs: installreRunning Result = System.Diagnostics.Process (msiexec)
    amazon-ebs: 12/06/2018 19:39:14 Restart is still in progress with post installation steps ...
    amazon-ebs: 12/06/2018 19:39:24Checking for post installation completion...
    amazon-ebs: installreRunning Result = System.Diagnostics.Process (msiexec)
    amazon-ebs: 12/06/2018 19:39:24 Restart is still in progress with post installation steps ...
    amazon-ebs: 12/06/2018 19:39:25 Finished Cleaning up
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 1. Allowed exit codes are: [0]

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Script exited with non-zero exit status: 1. Allowed exit codes are: [0]

Reply all
Reply to author
Forward
0 new messages