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"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/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
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2019-01-23 08:04:05 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
type wrappedCommandTemplate struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2019-01-28 06:30:15 -05:00
|
|
|
wrappedCommand := func(command string) (string, error) {
|
2019-03-29 11:50:02 -04:00
|
|
|
ictx := b.config.ctx
|
|
|
|
ictx.Data = &wrappedCommandTemplate{Command: command}
|
|
|
|
return interpolate.Render(b.config.ChrootCommandWrapper, &ictx)
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
|
|
|
|
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,
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
2019-01-28 06:30:15 -05:00
|
|
|
&stepStopVM{},
|
|
|
|
&stepCreateImage{},
|
|
|
|
)
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.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,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|