2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2015-10-30 04:23:30 -04:00
|
|
|
"bytes"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-10-30 04:23:30 -04:00
|
|
|
"time"
|
2017-03-28 21:02:51 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const port string = "13000"
|
|
|
|
|
|
|
|
type StepPollingInstalation struct {
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepPollingInstalation) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
errorMsg := "Error polling VM: %s"
|
|
|
|
vmIp := state.Get("ip").(string)
|
|
|
|
|
|
|
|
ui.Say("Start polling VM to check the installation is complete...")
|
|
|
|
host := "'" + vmIp + "'," + port
|
|
|
|
|
|
|
|
var blockBuffer bytes.Buffer
|
|
|
|
blockBuffer.WriteString("Invoke-Command -scriptblock {function foo(){try{$client=New-Object System.Net.Sockets.TcpClient(")
|
|
|
|
blockBuffer.WriteString(host)
|
|
|
|
blockBuffer.WriteString(") -ErrorAction SilentlyContinue;if($client -eq $null){return $false}}catch{return $false}return $true} foo}")
|
|
|
|
|
|
|
|
count := 60
|
|
|
|
var duration time.Duration = 20
|
|
|
|
sleepTime := time.Second * duration
|
|
|
|
|
|
|
|
var res string
|
|
|
|
|
|
|
|
for count > 0 {
|
2015-10-30 04:23:30 -04:00
|
|
|
log.Println(fmt.Sprintf("Connecting vm (%s)...", host))
|
2015-06-21 07:36:07 -04:00
|
|
|
cmd := exec.Command("powershell", blockBuffer.String())
|
|
|
|
cmdOut, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
res = strings.TrimSpace(string(cmdOut))
|
|
|
|
|
|
|
|
if res != "False" {
|
|
|
|
ui.Say("Signal was received from the VM")
|
|
|
|
// Sleep before starting provision
|
2015-10-30 04:23:30 -04:00
|
|
|
time.Sleep(time.Second * 30)
|
|
|
|
break
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(fmt.Sprintf("Slipping for more %v seconds...", uint(duration)))
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
count--
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
err := fmt.Errorf(errorMsg, "a signal from vm was not received in a given time period ")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("The installation complete")
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepPollingInstalation) Cleanup(state multistep.StateBag) {
|
|
|
|
|
|
|
|
}
|