@@ -2,21 +2,23 @@ package upcloud
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "time"
78
8- "github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
9- "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
109 "github.com/hashicorp/hcl/v2/hcldec"
1110 "github.com/hashicorp/packer-plugin-sdk/communicator"
1211 "github.com/hashicorp/packer-plugin-sdk/multistep"
1312 "github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
1413 "github.com/hashicorp/packer-plugin-sdk/packer"
1514 "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
15+
16+ "github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
17+ "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1618)
1719
1820const (
19- BuilderId = "upcloud.builder"
21+ BuilderID = "upcloud.builder"
2022 defaultTimeout time.Duration = 1 * time .Hour
2123)
2224
@@ -28,7 +30,7 @@ type Builder struct {
2830
2931func (b * Builder ) ConfigSpec () hcldec.ObjectSpec { return b .config .FlatMapstructure ().HCL2Spec () }
3032
31- func (b * Builder ) Prepare (raws ... interface {}) (generatedVars [] string , warnings []string , err error ) {
33+ func (b * Builder ) Prepare (raws ... interface {}) (generatedVars , warnings []string , err error ) {
3234 warnings , errs := b .config .Prepare (raws ... )
3335 if errs != nil {
3436 return nil , warnings , errs
@@ -65,43 +67,32 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
6567 generatedData := & packerbuilderdata.GeneratedData {State : state }
6668
6769 // Build the steps
68- steps := []multistep.Step {
69- & StepCreateSSHKey {
70- Debug : b .config .PackerDebug ,
71- DebugKeyPath : fmt .Sprintf ("ssh_key-%s.pem" , b .config .PackerBuildName ),
72- },
73- & StepCreateServer {
74- Config : & b .config ,
75- GeneratedData : generatedData ,
76- },
77- b .communicatorStep (),
78- & commonsteps.StepProvision {},
79- & commonsteps.StepCleanupTempKeys {
80- Comm : & b .config .Comm ,
81- },
82- & StepTeardownServer {},
83- & StepCreateTemplate {
84- Config : & b .config ,
85- GeneratedData : generatedData ,
86- },
87- }
70+ steps := b .buildSteps (generatedData )
8871
8972 // Run
9073 b .runner = commonsteps .NewRunner (steps , b .config .PackerConfig , ui )
9174 b .runner .Run (ctx , state )
9275
9376 // If there was an error, return that
9477 if err , ok := state .GetOk ("error" ); ok {
95- return nil , err .(error )
78+ if errVal , ok := err .(error ); ok {
79+ return nil , errVal
80+ }
81+ return nil , fmt .Errorf ("unknown error type: %T" , err )
9682 }
9783
9884 templates , ok := state .GetOk ("templates" )
9985 if ! ok {
100- return nil , fmt .Errorf ("No template found in state, the build was probably cancelled" )
86+ return nil , errors .New ("no template found in state, the build was probably cancelled" )
87+ }
88+
89+ templatesVal , ok := templates .([]* upcloud.Storage )
90+ if ! ok {
91+ return nil , fmt .Errorf ("templates is not of expected type []*upcloud.Storage, got %T" , templates )
10192 }
10293
10394 artifact := & Artifact {
104- Templates : templates .([] * upcloud. Storage ) ,
95+ Templates : templatesVal ,
10596 config : & b .config ,
10697 driver : b .driver ,
10798 StateData : map [string ]interface {}{
@@ -116,9 +107,33 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
116107 return artifact , nil
117108}
118109
110+ // buildSteps creates and returns the sequence of steps for the build process.
111+ func (b * Builder ) buildSteps (generatedData * packerbuilderdata.GeneratedData ) []multistep.Step {
112+ return []multistep.Step {
113+ & StepCreateSSHKey {
114+ Debug : b .config .PackerDebug ,
115+ DebugKeyPath : fmt .Sprintf ("ssh_key-%s.pem" , b .config .PackerBuildName ),
116+ },
117+ & StepCreateServer {
118+ Config : & b .config ,
119+ GeneratedData : generatedData ,
120+ },
121+ b .communicatorStep (),
122+ & commonsteps.StepProvision {},
123+ & commonsteps.StepCleanupTempKeys {
124+ Comm : & b .config .Comm ,
125+ },
126+ & StepTeardownServer {},
127+ & StepCreateTemplate {
128+ Config : & b .config ,
129+ GeneratedData : generatedData ,
130+ },
131+ }
132+ }
133+
119134// CommunicatorStep returns step based on communicator type
120135// We currently support only SSH communicator but 'none' type
121- // can also be used for e.g. testing purposes
136+ // can also be used for e.g. testing purposes.
122137func (b * Builder ) communicatorStep () multistep.Step {
123138 switch b .config .Comm .Type {
124139 case "none" :
0 commit comments