packer-cn/builder/oracle/classic/step_create_pv_builder.go

97 lines
2.4 KiB
Go
Raw Normal View History

2018-09-06 01:44:31 -04:00
package classic
import (
"context"
"fmt"
2018-09-07 17:42:22 -04:00
"log"
2018-09-06 01:44:31 -04:00
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/packer"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
2018-09-06 01:44:31 -04:00
)
type stepCreatePVBuilder struct {
Name string
BuilderVolumeName string
SecurityListKey string
2018-09-06 01:44:31 -04:00
}
func (s *stepCreatePVBuilder) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2018-09-06 01:44:31 -04:00
// get variables from state
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating builder instance...")
config := state.Get("config").(*Config)
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
2018-09-06 01:44:31 -04:00
ipAddName := state.Get("ipres_name").(string)
secListName := state.Get(s.SecurityListKey).(string)
2018-09-06 01:44:31 -04:00
// get instances client
instanceClient := client.Instances()
// Instances Input
input := &compute.CreateInstanceInput{
Name: s.Name,
2018-10-24 17:40:32 -04:00
Shape: config.BuilderShape,
2018-09-06 01:44:31 -04:00
Networking: map[string]compute.NetworkingInfo{
2018-10-24 17:59:15 -04:00
"eth0": {
2018-09-06 01:44:31 -04:00
Nat: []string{ipAddName},
SecLists: []string{secListName},
},
},
Storage: []compute.StorageAttachmentInput{
{
Volume: s.BuilderVolumeName,
Index: 1,
2018-09-06 01:44:31 -04:00
},
},
2018-10-24 17:40:32 -04:00
ImageList: config.BuilderImageList,
SSHKeys: []string{config.Comm.SSHKeyPairName},
Entry: *config.BuilderImageListEntry,
2018-09-06 01:44:31 -04:00
}
instanceInfo, err := instanceClient.CreateInstance(input)
if err != nil {
err = fmt.Errorf("Problem creating instance: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
2018-09-07 17:42:22 -04:00
log.Printf("Created instance %s", instanceInfo.Name)
2018-09-06 01:44:31 -04:00
state.Put("builder_instance_info", instanceInfo)
state.Put("builder_instance_id", instanceInfo.ID)
ui.Message(fmt.Sprintf("Created builder instance: %s.", instanceInfo.Name))
2018-09-06 01:44:31 -04:00
return multistep.ActionContinue
}
func (s *stepCreatePVBuilder) Cleanup(state multistep.StateBag) {
instanceID, ok := state.GetOk("builder_instance_id")
if !ok {
return
}
// terminate instance
ui := state.Get("ui").(packer.Ui)
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
2018-09-06 01:44:31 -04:00
ui.Say("Terminating builder instance...")
instanceClient := client.Instances()
input := &compute.DeleteInstanceInput{
Name: s.Name,
2018-09-06 01:44:31 -04:00
ID: instanceID.(string),
}
err := instanceClient.DeleteInstance(input)
if err != nil {
err = fmt.Errorf("Problem destroying instance: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return
}
ui.Say("Terminated builder instance.")
2018-09-06 01:44:31 -04:00
}