builder/docker: ability to disable pull
This commit is contained in:
parent
0287cdd614
commit
8ec68031d0
@ -11,6 +11,7 @@ type Config struct {
|
|||||||
|
|
||||||
ExportPath string `mapstructure:"export_path"`
|
ExportPath string `mapstructure:"export_path"`
|
||||||
Image string
|
Image string
|
||||||
|
Pull bool
|
||||||
|
|
||||||
tpl *packer.ConfigTemplate
|
tpl *packer.ConfigTemplate
|
||||||
}
|
}
|
||||||
@ -27,6 +28,19 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default Pull if it wasn't set
|
||||||
|
hasPull := false
|
||||||
|
for _, k := range md.Keys {
|
||||||
|
if k == "Pull" {
|
||||||
|
hasPull = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasPull {
|
||||||
|
c.Pull = true
|
||||||
|
}
|
||||||
|
|
||||||
errs := common.CheckUnusedConfig(md)
|
errs := common.CheckUnusedConfig(md)
|
||||||
|
|
||||||
templates := map[string]*string{
|
templates := map[string]*string{
|
||||||
|
@ -23,28 +23,36 @@ func testConfigStruct(t *testing.T) *Config {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testConfigErr(t *testing.T, warns []string, err error) {
|
||||||
|
if len(warns) > 0 {
|
||||||
|
t.Fatalf("bad: %#v", warns)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConfigOk(t *testing.T, warns []string, err error) {
|
||||||
|
if len(warns) > 0 {
|
||||||
|
t.Fatalf("bad: %#v", warns)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("bad: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigPrepare_exportPath(t *testing.T) {
|
func TestConfigPrepare_exportPath(t *testing.T) {
|
||||||
raw := testConfig()
|
raw := testConfig()
|
||||||
|
|
||||||
// No export path
|
// No export path
|
||||||
delete(raw, "export_path")
|
delete(raw, "export_path")
|
||||||
_, warns, errs := NewConfig(raw)
|
_, warns, errs := NewConfig(raw)
|
||||||
if len(warns) > 0 {
|
testConfigErr(t, warns, errs)
|
||||||
t.Fatalf("bad: %#v", warns)
|
|
||||||
}
|
|
||||||
if errs == nil {
|
|
||||||
t.Fatal("should error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Good export path
|
// Good export path
|
||||||
raw["export_path"] = "good"
|
raw["export_path"] = "good"
|
||||||
_, warns, errs = NewConfig(raw)
|
_, warns, errs = NewConfig(raw)
|
||||||
if len(warns) > 0 {
|
testConfigOk(t, warns, errs)
|
||||||
t.Fatalf("bad: %#v", warns)
|
|
||||||
}
|
|
||||||
if errs != nil {
|
|
||||||
t.Fatalf("bad: %s", errs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigPrepare_image(t *testing.T) {
|
func TestConfigPrepare_image(t *testing.T) {
|
||||||
@ -53,20 +61,30 @@ func TestConfigPrepare_image(t *testing.T) {
|
|||||||
// No image
|
// No image
|
||||||
delete(raw, "image")
|
delete(raw, "image")
|
||||||
_, warns, errs := NewConfig(raw)
|
_, warns, errs := NewConfig(raw)
|
||||||
if len(warns) > 0 {
|
testConfigErr(t, warns, errs)
|
||||||
t.Fatalf("bad: %#v", warns)
|
|
||||||
}
|
|
||||||
if errs == nil {
|
|
||||||
t.Fatal("should error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Good image
|
// Good image
|
||||||
raw["image"] = "path"
|
raw["image"] = "path"
|
||||||
_, warns, errs = NewConfig(raw)
|
_, warns, errs = NewConfig(raw)
|
||||||
if len(warns) > 0 {
|
testConfigOk(t, warns, errs)
|
||||||
t.Fatalf("bad: %#v", warns)
|
|
||||||
}
|
}
|
||||||
if errs != nil {
|
|
||||||
t.Fatalf("bad: %s", errs)
|
func TestConfigPrepare_pull(t *testing.T) {
|
||||||
|
raw := testConfig()
|
||||||
|
|
||||||
|
// No pull set
|
||||||
|
delete(raw, "pull")
|
||||||
|
c, warns, errs := NewConfig(raw)
|
||||||
|
testConfigOk(t, warns, errs)
|
||||||
|
if !c.Pull {
|
||||||
|
t.Fatal("should pull by default")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pull set
|
||||||
|
raw["pull"] = false
|
||||||
|
c, warns, errs = NewConfig(raw)
|
||||||
|
testConfigOk(t, warns, errs)
|
||||||
|
if c.Pull {
|
||||||
|
t.Fatal("should not pull")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepPull struct{}
|
type StepPull struct{}
|
||||||
@ -13,6 +14,11 @@ func (s *StepPull) Run(state multistep.StateBag) multistep.StepAction {
|
|||||||
driver := state.Get("driver").(Driver)
|
driver := state.Get("driver").(Driver)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
if !config.Pull {
|
||||||
|
log.Println("Pull disabled, won't docker pull")
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image))
|
ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image))
|
||||||
if err := driver.Pull(config.Image); err != nil {
|
if err := driver.Pull(config.Image); err != nil {
|
||||||
err := fmt.Errorf("Error pulling Docker image: %s", err)
|
err := fmt.Errorf("Error pulling Docker image: %s", err)
|
||||||
|
@ -50,3 +50,24 @@ func TestStepPull_error(t *testing.T) {
|
|||||||
t.Fatal("should have error")
|
t.Fatal("should have error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStepPull_noPull(t *testing.T) {
|
||||||
|
state := testState(t)
|
||||||
|
step := new(StepPull)
|
||||||
|
defer step.Cleanup(state)
|
||||||
|
|
||||||
|
config := state.Get("config").(*Config)
|
||||||
|
config.Pull = false
|
||||||
|
|
||||||
|
driver := state.Get("driver").(*MockDriver)
|
||||||
|
|
||||||
|
// run the step
|
||||||
|
if action := step.Run(state); action != multistep.ActionContinue {
|
||||||
|
t.Fatalf("bad action: %#v", action)
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify we did the right thing
|
||||||
|
if driver.PullCalled {
|
||||||
|
t.Fatal("shouldn't have pulled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user