first pass at allowing configurable sleep timeouts, profiles, and launch configs

This commit is contained in:
Chris Lundquist 2018-03-20 19:30:46 +00:00
parent b9bcde5a97
commit 4f5e7fe060
2 changed files with 33 additions and 8 deletions

View File

@ -2,7 +2,6 @@ package lxd
import ( import (
"fmt" "fmt"
"time"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
@ -17,8 +16,10 @@ type Config struct {
ContainerName string `mapstructure:"container_name"` ContainerName string `mapstructure:"container_name"`
CommandWrapper string `mapstructure:"command_wrapper"` CommandWrapper string `mapstructure:"command_wrapper"`
Image string `mapstructure:"image"` Image string `mapstructure:"image"`
Profile string `mapstructure:"profile"`
InitSleep string `mapstructure:"init_sleep"`
PublishProperties map[string]string `mapstructure:"publish_properties"` PublishProperties map[string]string `mapstructure:"publish_properties"`
InitTimeout time.Duration LaunchConfig map[string]string `mapstructure:"launch_config"`
ctx interpolate.Context ctx interpolate.Context
} }
@ -54,6 +55,16 @@ func NewConfig(raws ...interface{}) (*Config, error) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("`image` is a required parameter for LXD. Please specify an image by alias or fingerprint. e.g. `ubuntu-daily:x`")) errs = packer.MultiErrorAppend(errs, fmt.Errorf("`image` is a required parameter for LXD. Please specify an image by alias or fingerprint. e.g. `ubuntu-daily:x`"))
} }
if c.Profile == "" {
c.Profile = "default"
}
// Sadly we have to wait a few seconds for /tmp to be intialized and networking
// to finish starting. There isn't a great cross platform to check when things are ready.
if c.InitSleep == "" {
c.InitSleep = "3"
}
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return nil, errs return nil, errs
} }

View File

@ -3,6 +3,7 @@ package lxd
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -17,22 +18,35 @@ func (s *stepLxdLaunch) Run(_ context.Context, state multistep.StateBag) multist
name := config.ContainerName name := config.ContainerName
image := config.Image image := config.Image
profile := fmt.Sprintf("--profile=%s", config.Profile)
args := []string{ launch_args := []string{
"launch", "--ephemeral=false", image, name, "launch", "--ephemeral=false", profile, image, name,
}
for k, v := range config.LaunchConfig {
launch_args = append(launch_args, fmt.Sprintf("--config %s=%s", k, v))
} }
ui.Say("Creating container...") ui.Say("Creating container...")
_, err := LXDCommand(args...) _, err := LXDCommand(launch_args...)
if err != nil { if err != nil {
err := fmt.Errorf("Error creating container: %s", err) err := fmt.Errorf("Error creating container: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
sleep_seconds, err := strconv.Atoi(config.InitSleep)
if err != nil {
err := fmt.Errorf("Error parsing InitSleep into int: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// TODO: Should we check `lxc info <container>` for "Running"? // TODO: Should we check `lxc info <container>` for "Running"?
// We have to do this so /tmp doesn't get cleared and lose our provisioner scripts. // We have to do this so /tmp doesn't get cleared and lose our provisioner scripts.
time.Sleep(1 * time.Second) time.Sleep(time.Duration(sleep_seconds) * time.Second)
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -41,12 +55,12 @@ func (s *stepLxdLaunch) Cleanup(state multistep.StateBag) {
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
args := []string{ cleanup_args := []string{
"delete", "--force", config.ContainerName, "delete", "--force", config.ContainerName,
} }
ui.Say("Unregistering and deleting deleting container...") ui.Say("Unregistering and deleting deleting container...")
if _, err := LXDCommand(args...); err != nil { if _, err := LXDCommand(cleanup_args...); err != nil {
ui.Error(fmt.Sprintf("Error deleting container: %s", err)) ui.Error(fmt.Sprintf("Error deleting container: %s", err))
} }
} }