stub out step_snapshot

This commit is contained in:
Megan Marsh 2018-01-16 16:55:39 -08:00 committed by Matthew Hooker
parent 007e8f7c14
commit 8aa716cd4c
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
3 changed files with 61 additions and 49 deletions

View File

@ -29,6 +29,7 @@ func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAc
log.Printf("Error creating IP Reservation: %s", err)
return multistep.ActionHalt
}
state.Put("instance_ip", ipRes.IP)
log.Printf("debug: ipRes is %#v", ipRes)
return multistep.ActionContinue
}

View File

@ -1,65 +1,37 @@
package classic
import (
"bytes"
"fmt"
"text/template"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
type instanceOptions struct {
Username string
IdentityDomain string
SshKey string
Shape string
ImageList string
InstanceIP string
}
var instanceTemplate = template.Must(template.New("instanceRequestBody").Parse(`
{
"instances": [{
"shape": "{{.Shape}}",
"sshkeys": ["/Compute-{{.IdentityDomain}}/{{Username}}/{{.SshKey}}"],
"name": "Compute-{{.IdentityDomain}}/{{Username}}/packer-instance",
"label": "packer-instance",
"imagelist": "/Compute-{{.IdentityDomain}}/{{Username}}/{{.ImageList}}",
"networking": {
"eth0": {
"nat": "ipreservation:/Compute-{{.IdentityDomain}}/{{Username}}/{{.InstanceIP}}"
}
}
}]
}
`))
type stepCreateInstance struct{}
func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
const endpoint_path = "/launchplan/" // POST
func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
ui.Say("Creating Instance...")
// generate launch plan definition for this instance
var buffer bytes.Buffer
err = instanceTemplate.Execute(&buffer, instanceOptions{
Username: config.Username,
IdentityDomain: config.IdentityDomain,
SshKey: config.SshKey,
Shape: config.Shape,
ImageList: config.ImageList,
})
if err != nil {
fmt.Printf("Error creating launch plan definition: %s", err)
return "", err
// get variables from state
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
client := state.Get("client").(*compute.ComputeClient)
sshPublicKey := state.Get("publicKey").(string)
// get instances client
instanceClient := client.Instances()
// Instances Input
input := &compute.CreateInstanceInput{
Name: config.ImageName,
Shape: config.Shape,
ImageList: config.ImageList,
SSHKeys: []string{},
Attributes: map[string]interface{}{},
}
// for API docs see
// https://docs.oracle.com/en/cloud/iaas/compute-iaas-cloud/stcsa/op-launchplan--post.html
instanceID, err := client.CreateInstance(publicKey)
instanceInfo, err := instanceClient.CreateInstance(input)
if err != nil {
err = fmt.Errorf("Problem creating instance: %s", err)
ui.Error(err.Error())
@ -67,7 +39,7 @@ func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAc
return multistep.ActionHalt
}
state.Put("instance_id", instanceID)
state.Put("instance_id", instanceInfo.ID)
ui.Say(fmt.Sprintf("Created instance (%s).", instanceID))
}

View File

@ -0,0 +1,39 @@
package classic
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
type stepSnapshot struct{}
func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
// get variables from state
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating Snapshot...")
config := state.Get("config").(Config)
client := state.Get("client").(*compute.ComputeClient)
instanceID := state.Get("instance_id").(string)
// get instances client
snapshotClient := client.SnapshotsClient()
// Instances Input
createSnapshotInput := &CreateSnapshotInput{
Instance: instanceID,
MachineImage: config.ImageName,
}
snap, err := snapshotClient.CreateSnapshot(input)
if err != nil {
err = fmt.Errorf("Problem creating snapshot: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Created snapshot (%s).", snap.Name))
}