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"
|
2019-08-09 03:00:23 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
errs := &packer.MultiError{}
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.JDCloudCredentialConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.JDCloudInstanceSpecConfig.Prepare(&b.config.ctx)...)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey)
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-08-09 03:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
&common.StepProvision{},
|
|
|
|
|
|
|
|
&stepStopJDCloudInstance{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepCreateJDCloudImage{
|
|
|
|
InstanceSpecConfig: &b.config.JDCloudInstanceSpecConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
|
|
|
b.runner.Run(ctx, state)
|
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
|
|
|
ImageId: b.config.ArtifactId,
|
|
|
|
RegionID: b.config.RegionId,
|
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|