2019-01-23 08:04:05 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2019-01-23 08:04:05 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-01-28 06:30:15 -05:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
openapi "github.com/hyperonecom/h1-client-go"
|
2019-01-23 08:04:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderID = "hyperone.builder"
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
client *openapi.APIClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
config, warnings, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
b.config = *config
|
|
|
|
|
|
|
|
cfg := openapi.NewConfiguration()
|
|
|
|
cfg.AddDefaultHeader("x-auth-token", b.config.Token)
|
|
|
|
if b.config.Project != "" {
|
|
|
|
cfg.AddDefaultHeader("x-project", b.config.Project)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:03:44 -05:00
|
|
|
if b.config.APIURL != "" {
|
|
|
|
cfg.BasePath = b.config.APIURL
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
prefer := fmt.Sprintf("respond-async,wait=%d", int(b.config.StateTimeout.Seconds()))
|
|
|
|
cfg.AddDefaultHeader("Prefer", prefer)
|
|
|
|
|
|
|
|
b.client = openapi.NewAPIClient(cfg)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
type wrappedCommandTemplate struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2019-01-28 06:30:15 -05:00
|
|
|
wrappedCommand := func(command string) (string, error) {
|
|
|
|
ctx := b.config.ctx
|
|
|
|
ctx.Data = &wrappedCommandTemplate{Command: command}
|
|
|
|
return interpolate.Render(b.config.ChrootCommandWrapper, &ctx)
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
state := &multistep.BasicStateBag{}
|
|
|
|
state.Put("config", &b.config)
|
|
|
|
state.Put("client", b.client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2019-01-28 06:30:15 -05:00
|
|
|
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&stepCreateSSHKey{},
|
|
|
|
&stepCreateVM{},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: getPublicIP,
|
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
|
|
|
},
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ChrootDisk {
|
|
|
|
steps = append(steps,
|
|
|
|
&stepPrepareDevice{},
|
|
|
|
&stepPreMountCommands{},
|
|
|
|
&stepMountChroot{},
|
|
|
|
&stepPostMountCommands{},
|
|
|
|
&stepMountExtra{},
|
|
|
|
&stepCopyFiles{},
|
|
|
|
&stepChrootProvision{},
|
|
|
|
&stepStopVM{},
|
|
|
|
&stepDetachDisk{},
|
|
|
|
&stepCreateVMFromDisk{},
|
|
|
|
&stepCreateImage{},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
steps = append(steps,
|
|
|
|
&common.StepProvision{},
|
|
|
|
&stepStopVM{},
|
|
|
|
&stepCreateImage{},
|
|
|
|
)
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
|
|
|
imageID: state.Get("image_id").(string),
|
|
|
|
imageName: state.Get("image_name").(string),
|
|
|
|
client: b.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|