Merge pull request #3105 from vtolstov/iso_cache

qemu: allow to use live iso without cache
This commit is contained in:
Chris Bednarski 2016-01-25 21:03:27 -08:00
commit 4aa59e4999
3 changed files with 79 additions and 5 deletions

View File

@ -83,6 +83,7 @@ type Config struct {
common.ISOConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
ISOSkipCache bool `mapstructure:"iso_skip_cache"`
Accelerator string `mapstructure:"accelerator"`
BootCommand []string `mapstructure:"boot_command"`
DiskInterface string `mapstructure:"disk_interface"`
@ -221,6 +222,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
var errs *packer.MultiError
warnings := make([]string, 0)
if b.config.ISOSkipCache {
b.config.ISOChecksumType = "none"
}
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)
@ -326,8 +331,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steprun.Message = "Starting VM, booting disk image"
}
steps := []multistep.Step{
&common.StepDownload{
steps := []multistep.Step{}
if !b.config.ISOSkipCache {
steps = append(steps, &common.StepDownload{
Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType,
Description: "ISO",
@ -336,7 +342,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls,
},
new(stepPrepareOutputDir),
)
} else {
steps = append(steps, &stepSetISO{
ResultKey: "iso_path",
Url: b.config.ISOUrls,
},
)
}
steps = append(steps, new(stepPrepareOutputDir),
&common.StepCreateFloppy{
Files: b.config.FloppyFiles,
},
@ -362,7 +377,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
new(common.StepProvision),
new(stepShutdown),
new(stepConvertDisk),
}
)
// Setup the state bag
state := new(multistep.BasicStateBag)

View File

@ -0,0 +1,56 @@
package qemu
import (
"fmt"
"net/http"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
// This step set iso_patch to available url
type stepSetISO struct {
ResultKey string
Url []string
}
func (s *stepSetISO) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
iso_path := ""
for _, url := range s.Url {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
continue
}
req.Header.Set("User-Agent", "Packer")
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
res, err := httpClient.Do(req)
if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {
if res.Header.Get("Accept-Ranges") == "bytes" {
iso_path = url
}
}
}
if iso_path == "" {
err := fmt.Errorf("No byte serving support. The HTTP server must support Accept-Ranges=bytes")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put(s.ResultKey, iso_path)
return multistep.ActionContinue
}
func (s *stepSetISO) Cleanup(state multistep.StateBag) {}

View File

@ -173,6 +173,9 @@ builder.
to force the HTTP server to be on one port, make this minimum and maximum
port the same. By default the values are 8000 and 9000, respectively.
- `iso_skip_cache` (boolean) - Use iso from provided url. Qemu must support
curl block device.
- `iso_target_path` (string) - The path where the iso should be saved after
download. By default will go in the packer cache, with a hash of the
original filename as its name.