qemu: allow to use live iso without cache

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Vasiliy Tolstov 2016-01-25 09:01:37 +00:00
parent cd4a70f846
commit d96283e475
3 changed files with 79 additions and 5 deletions

View File

@ -83,6 +83,7 @@ type Config struct {
common.ISOConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
ISOSkipCache bool `mapstructure:"iso_skip_cache"`
Accelerator string `mapstructure:"accelerator"` Accelerator string `mapstructure:"accelerator"`
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
DiskInterface string `mapstructure:"disk_interface"` DiskInterface string `mapstructure:"disk_interface"`
@ -221,6 +222,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
var errs *packer.MultiError var errs *packer.MultiError
warnings := make([]string, 0) warnings := make([]string, 0)
if b.config.ISOSkipCache {
b.config.ISOChecksumType = "none"
}
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx) isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...) warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...) 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" steprun.Message = "Starting VM, booting disk image"
} }
steps := []multistep.Step{ steps := []multistep.Step{}
&common.StepDownload{ if !b.config.ISOSkipCache {
steps = append(steps, &common.StepDownload{
Checksum: b.config.ISOChecksum, Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType, ChecksumType: b.config.ISOChecksumType,
Description: "ISO", Description: "ISO",
@ -336,7 +342,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
TargetPath: b.config.TargetPath, TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls, 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{ &common.StepCreateFloppy{
Files: b.config.FloppyFiles, 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(common.StepProvision),
new(stepShutdown), new(stepShutdown),
new(stepConvertDisk), new(stepConvertDisk),
} )
// Setup the state bag // Setup the state bag
state := new(multistep.BasicStateBag) 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,10 +173,13 @@ builder.
to force the HTTP server to be on one port, make this minimum and maximum 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. 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 - `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 download. By default will go in the packer cache, with a hash of the
original filename as its name. original filename as its name.
- `iso_urls` (array of strings) - Multiple URLs for the ISO to download. - `iso_urls` (array of strings) - Multiple URLs for the ISO to download.
Packer will try these in order. If anything goes wrong attempting to Packer will try these in order. If anything goes wrong attempting to
download or while downloading a single URL, it will move on to the next. All download or while downloading a single URL, it will move on to the next. All