2015-12-22 09:56:33 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-12-22 09:56:33 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2015-12-22 09:56:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepWaitInit struct {
|
|
|
|
WaitTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepWaitInit) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
waitDone := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
ui.Say("Waiting for container to finish init...")
|
|
|
|
err = s.waitForInit(state, cancel)
|
|
|
|
waitDone <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("Waiting for container to finish init, up to timeout: %s", s.WaitTimeout)
|
|
|
|
timeout := time.After(s.WaitTimeout)
|
|
|
|
WaitLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-waitDone:
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error waiting for container to finish init: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Container finished init!")
|
|
|
|
break WaitLoop
|
|
|
|
case <-timeout:
|
|
|
|
err := fmt.Errorf("Timeout waiting for container to finish init.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
close(cancel)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
close(cancel)
|
|
|
|
log.Println("Interrupt detected, quitting waiting for container to finish init.")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepWaitInit) Cleanup(multistep.StateBag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepWaitInit) waitForInit(state multistep.StateBag, cancel <-chan struct{}) error {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
mountPath := state.Get("mount_path").(string)
|
|
|
|
wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-cancel:
|
|
|
|
log.Println("Cancelled. Exiting loop.")
|
|
|
|
return errors.New("Wait cancelled")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
comm := &LxcAttachCommunicator{
|
|
|
|
ContainerName: config.ContainerName,
|
2017-10-30 21:48:43 -04:00
|
|
|
AttachOptions: config.AttachOptions,
|
2015-12-22 09:56:33 -05:00
|
|
|
RootFs: mountPath,
|
|
|
|
CmdWrapper: wrappedCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
runlevel, _ := comm.CheckInit()
|
|
|
|
currentRunlevel := "unknown"
|
|
|
|
if arr := strings.Split(runlevel, " "); len(arr) >= 2 {
|
|
|
|
currentRunlevel = arr[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Current runlevel in container: '%s'", runlevel)
|
|
|
|
|
|
|
|
targetRunlevel := fmt.Sprintf("%d", config.TargetRunlevel)
|
|
|
|
if currentRunlevel == targetRunlevel {
|
|
|
|
log.Printf("Container finished init.")
|
|
|
|
break
|
2016-05-14 03:13:49 -04:00
|
|
|
} else if currentRunlevel > targetRunlevel {
|
2016-05-16 01:59:39 -04:00
|
|
|
log.Printf("Expected Runlevel %s, Got Runlevel %s, continuing", targetRunlevel, currentRunlevel)
|
2016-05-14 03:13:49 -04:00
|
|
|
break
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|