2018-09-06 01:44:31 -04:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreatePVMaster struct {
|
2018-10-26 02:56:18 -04:00
|
|
|
Name string
|
|
|
|
VolumeName string
|
|
|
|
SecurityListKey string
|
2018-09-06 01:44:31 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreatePVMaster) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-06 01:44:31 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
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)
|
2018-10-26 02:56:18 -04:00
|
|
|
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{
|
2018-10-22 19:39:04 -04:00
|
|
|
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{
|
|
|
|
{
|
2018-10-22 19:39:04 -04:00
|
|
|
Volume: s.VolumeName,
|
2018-09-06 01:44:31 -04:00
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
BootOrder: []int{1},
|
|
|
|
Attributes: config.attribs,
|
2018-10-26 02:56:18 -04:00
|
|
|
}
|
|
|
|
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)
|
2018-09-07 19:18:47 -04:00
|
|
|
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) {
|
2018-10-26 02:56:18 -04:00
|
|
|
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").(packer.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{
|
2018-10-22 19:39:04 -04:00
|
|
|
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
|
|
|
}
|