2016-01-11 12:22:41 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
2019-03-22 14:53:28 +01:00
|
|
|
"context"
|
2017-07-16 08:26:48 +02:00
|
|
|
"fmt"
|
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-01-11 12:22:41 +01:00
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "packer.cloudstack"
|
|
|
|
|
|
|
|
// Builder represents the CloudStack builder.
|
|
|
|
type Builder struct {
|
2019-12-17 11:25:56 +01:00
|
|
|
config Config
|
2016-01-11 12:22:41 +01:00
|
|
|
runner multistep.Runner
|
|
|
|
ui packer.Ui
|
|
|
|
}
|
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 11:25:56 +01:00
|
|
|
errs := b.config.Prepare(raws...)
|
2016-01-11 12:22:41 +01:00
|
|
|
if errs != nil {
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, nil, errs
|
2016-01-11 12:22:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, nil, nil
|
2016-01-11 12:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the packer.Builder interface.
|
2019-03-22 14:53:28 +01:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2016-01-11 12:22:41 +01: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)
|
2019-12-19 17:01:55 +01:00
|
|
|
state.Put("config", &b.config)
|
2016-01-11 12:22:41 +01:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&stepPrepareConfig{},
|
2017-07-06 11:59:02 +02:00
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2017-07-06 11:59:02 +02:00
|
|
|
},
|
2017-07-16 08:26:48 +02:00
|
|
|
&stepKeypair{
|
2018-08-28 17:47:19 +02:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName),
|
2017-07-16 08:26:48 +02:00
|
|
|
},
|
2017-07-18 09:08:11 +02:00
|
|
|
&stepCreateSecurityGroup{},
|
2017-07-06 11:59:02 +02:00
|
|
|
&stepCreateInstance{
|
2017-07-18 21:05:27 +02:00
|
|
|
Ctx: b.config.ctx,
|
2017-07-26 21:34:11 +02:00
|
|
|
Debug: b.config.PackerDebug,
|
2017-07-06 11:59:02 +02:00
|
|
|
},
|
2016-01-11 12:22:41 +01:00
|
|
|
&stepSetupNetworking{},
|
2019-09-12 20:35:14 -03:00
|
|
|
&stepDetachIso{},
|
2016-01-11 12:22:41 +01:00
|
|
|
&communicator.StepConnect{
|
2018-08-22 17:02:23 +02:00
|
|
|
Config: &b.config.Comm,
|
2020-01-30 15:22:22 -08:00
|
|
|
Host: communicator.CommHost(b.config.Comm.Host(), "ipaddress"),
|
2018-08-22 17:02:23 +02:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2017-07-17 09:05:12 +02:00
|
|
|
SSHPort: commPort,
|
|
|
|
WinRMPort: commPort,
|
2016-01-11 12:22:41 +01:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 11:03:23 -07:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2016-01-11 12:22:41 +01:00
|
|
|
&stepShutdownInstance{},
|
|
|
|
&stepCreateTemplate{},
|
|
|
|
}
|
|
|
|
|
2017-08-31 23:13:00 +05:30
|
|
|
// Configure the runner and run the steps.
|
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 14:53:28 +01:00
|
|
|
b.runner.Run(ctx, state)
|
2016-01-11 12:22:41 +01:00
|
|
|
|
2017-07-06 23:31:13 +02: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 12:22:41 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 13:36:53 -07:00
|
|
|
// If there was no template created, just return
|
|
|
|
if _, ok := state.GetOk("template"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:22:41 +01:00
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &Artifact{
|
2020-01-30 11:27:58 +01:00
|
|
|
client: client,
|
|
|
|
config: &b.config,
|
|
|
|
template: state.Get("template").(*cloudstack.CreateTemplateResponse),
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2016-01-11 12:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|