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

98 lines
2.4 KiB
Go
Raw Normal View History

2018-09-06 01:44:31 -04:00
package classic
import (
"context"
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
2018-09-06 01:44:31 -04:00
)
type stepCreatePVMaster struct {
Name string
VolumeName string
SecurityListKey string
2018-09-06 01:44:31 -04:00
}
func (s *stepCreatePVMaster) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
2018-09-06 01:44:31 -04:00
ui.Say("Creating master 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-09-06 01:44:31 -04:00
Shape: config.Shape,
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.VolumeName,
2018-09-06 01:44:31 -04:00
Index: 1,
},
},
BootOrder: []int{1},
Attributes: config.attribs,
}
if config.Comm.Type == "ssh" {
input.SSHKeys = []string{config.Comm.SSHKeyPairName}
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
}
state.Put("master_instance_info", instanceInfo)
state.Put("master_instance_id", instanceInfo.ID)
ui.Message(fmt.Sprintf("Created master instance: %s.", instanceInfo.Name))
2018-09-06 01:44:31 -04:00
return multistep.ActionContinue
}
func (s *stepCreatePVMaster) Cleanup(state multistep.StateBag) {
if _, deleted := state.GetOk("master_instance_deleted"); deleted {
2018-09-06 03:05:47 -04:00
return
}
instanceID, ok := state.GetOk("master_instance_id")
if !ok {
return
}
// terminate instance
ui := state.Get("ui").(packersdk.Ui)
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
2018-09-06 03:05:47 -04:00
ui.Say("Terminating builder instance...")
instanceClient := client.Instances()
input := &compute.DeleteInstanceInput{
Name: s.Name,
2018-09-06 03:05:47 -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 master instance.")
2018-09-06 01:44:31 -04:00
}