2016-01-11 06:22:41 -05:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
2017-07-16 02:26:48 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-01-11 06:22:41 -05:00
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "packer.cloudstack"
|
|
|
|
|
|
|
|
// Builder represents the CloudStack builder.
|
|
|
|
type Builder struct {
|
|
|
|
config *Config
|
|
|
|
runner multistep.Runner
|
|
|
|
ui packer.Ui
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare implements the packer.Builder interface.
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
config, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
b.config = config
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the packer.Builder interface.
|
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
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2016-01-11 06:22:41 -05:00
|
|
|
b.ui = ui
|
|
|
|
|
|
|
|
// Create a CloudStack API client.
|
|
|
|
client := cloudstack.NewAsyncClient(
|
|
|
|
b.config.APIURL,
|
|
|
|
b.config.APIKey,
|
|
|
|
b.config.SecretKey,
|
|
|
|
!b.config.SSLNoVerify,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set the time to wait before timing out
|
|
|
|
client.AsyncTimeout(int64(b.config.AsyncTimeout.Seconds()))
|
|
|
|
|
|
|
|
// Some CloudStack service providers only allow HTTP GET calls.
|
|
|
|
client.HTTPGETOnly = b.config.HTTPGetOnly
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&stepPrepareConfig{},
|
2017-07-06 05:59:02 -04:00
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
2017-07-16 02:26:48 -04:00
|
|
|
&stepKeypair{
|
2018-08-28 11:47:19 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName),
|
2017-07-16 02:26:48 -04:00
|
|
|
},
|
2017-07-18 03:08:11 -04:00
|
|
|
&stepCreateSecurityGroup{},
|
2017-07-06 05:59:02 -04:00
|
|
|
&stepCreateInstance{
|
2017-07-18 15:05:27 -04:00
|
|
|
Ctx: b.config.ctx,
|
2017-07-26 15:34:11 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
2017-07-06 05:59:02 -04:00
|
|
|
},
|
2016-01-11 06:22:41 -05:00
|
|
|
&stepSetupNetworking{},
|
|
|
|
&communicator.StepConnect{
|
2018-08-22 11:02:23 -04:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2017-07-17 03:05:12 -04:00
|
|
|
SSHPort: commPort,
|
|
|
|
WinRMPort: commPort,
|
2016-01-11 06:22:41 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2016-01-11 06:22:41 -05:00
|
|
|
&stepShutdownInstance{},
|
|
|
|
&stepCreateTemplate{},
|
|
|
|
}
|
|
|
|
|
2017-08-31 13:43:00 -04:00
|
|
|
// Configure the runner and run the steps.
|
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2016-01-11 06:22:41 -05:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
2017-07-06 17:31:13 -04:00
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
ui.Error(rawErr.(error).Error())
|
|
|
|
return nil, rawErr.(error)
|
2016-01-11 06:22:41 -05:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:36:53 -04:00
|
|
|
// If there was no template created, just return
|
|
|
|
if _, ok := state.GetOk("template"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-01-11 06:22:41 -05:00
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &Artifact{
|
|
|
|
client: client,
|
|
|
|
config: b.config,
|
2017-07-06 17:31:13 -04:00
|
|
|
template: state.Get("template").(*cloudstack.CreateTemplateResponse),
|
2016-01-11 06:22:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel the step runner.
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
b.ui.Say("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|