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

74 lines
1.9 KiB
Go
Raw Normal View History

2018-01-12 19:06:03 -05:00
package classic
import (
2018-01-16 15:09:42 -05:00
"bytes"
2018-01-12 19:18:55 -05:00
"fmt"
2018-01-12 19:48:35 -05:00
"text/template"
2018-01-12 19:18:55 -05:00
2018-01-12 19:06:03 -05:00
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
2018-01-12 19:48:35 -05:00
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}}"
}
}
}]
}
`))
2018-01-12 19:18:55 -05:00
type stepCreateInstance struct{}
2018-01-12 19:06:03 -05:00
func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
2018-01-12 19:48:35 -05:00
config := state.Get("config").(Config)
2018-01-12 19:06:03 -05:00
const endpoint_path = "/launchplan/" // POST
2018-01-12 19:48:35 -05:00
ui.Say("Creating Instance...")
// generate launch plan definition for this instance
2018-01-16 15:09:42 -05:00
var buffer bytes.Buffer
2018-01-12 19:48:35 -05:00
err = instanceTemplate.Execute(&buffer, instanceOptions{
Username: config.Username,
IdentityDomain: config.IdentityDomain,
SshKey: config.SshKey,
Shape: config.Shape,
ImageList: config.ImageList,
})
2018-01-12 19:51:59 -05:00
if err != nil {
fmt.Printf("Error creating launch plan definition: %s", err)
return "", err
}
2018-01-16 15:09:42 -05:00
// for API docs see
// https://docs.oracle.com/en/cloud/iaas/compute-iaas-cloud/stcsa/op-launchplan--post.html
2018-01-12 19:18:55 -05:00
instanceID, err := client.CreateInstance(publicKey)
if err != nil {
err = fmt.Errorf("Problem creating instance: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("instance_id", instanceID)
ui.Say(fmt.Sprintf("Created instance (%s).", instanceID))
2018-01-12 19:06:03 -05:00
}