2019-08-09 03:00:23 -04:00
|
|
|
package jdcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2019-08-09 03:00:23 -04:00
|
|
|
)
|
|
|
|
|
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-08-09 03:00:23 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BUILDER_ID,
|
2019-08-09 03:00:23 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, fmt.Errorf("[ERROR] Failed in decoding JSON->mapstructure")
|
2019-08-09 03:00:23 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
errs := &packersdk.MultiError{}
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.JDCloudCredentialConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.JDCloudInstanceSpecConfig.Prepare(&b.config.ctx)...)
|
2019-08-09 03:00:23 -04:00
|
|
|
if errs != nil && len(errs.Errors) != 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs
|
2019-08-09 03:00:23 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 17:03:11 -05:00
|
|
|
packersdk.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey)
|
2019-08-09 03:00:23 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-08-09 03:00:23 -04:00
|
|
|
}
|
|
|
|
|
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-08-09 03:00:23 -04:00
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2019-12-19 14:17:04 -05:00
|
|
|
state.Put("config", &b.config)
|
2019-08-09 03:00:23 -04:00
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
|
|
|
|
&stepValidateParameters{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepConfigCredentials{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepCreateJDCloudInstance{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
CredentialConfig: &b.config.JDCloudCredentialConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.JDCloudInstanceSpecConfig.Comm,
|
|
|
|
SSHConfig: b.config.JDCloudInstanceSpecConfig.Comm.SSHConfigFunc(),
|
|
|
|
Host: instanceHost,
|
|
|
|
},
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
2019-08-09 03:00:23 -04:00
|
|
|
|
|
|
|
&stepStopJDCloudInstance{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepCreateJDCloudImage{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-08-09 03:00:23 -04:00
|
|
|
b.runner.Run(ctx, state)
|
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2020-01-30 05:27:58 -05:00
|
|
|
ImageId: b.config.ArtifactId,
|
|
|
|
RegionID: b.config.RegionId,
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-08-09 03:00:23 -04:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|