packer-cn/builder/oracle/oci/driver_oci.go

216 lines
6.2 KiB
Go
Raw Normal View History

package oci
import (
2018-04-11 05:20:40 -04:00
"context"
"errors"
"fmt"
2018-04-11 05:20:40 -04:00
"time"
2018-04-11 05:20:40 -04:00
core "github.com/oracle/oci-go-sdk/core"
)
// driverOCI implements the Driver interface and communicates with Oracle
// OCI.
type driverOCI struct {
2018-04-11 05:20:40 -04:00
computeClient core.ComputeClient
vcnClient core.VirtualNetworkClient
cfg *Config
2018-06-26 05:05:56 -04:00
context context.Context
}
2018-04-11 05:20:40 -04:00
// NewDriverOCI Creates a new driverOCI with a connected compute client and a connected vcn client.
func NewDriverOCI(cfg *Config) (Driver, error) {
2018-04-11 05:20:40 -04:00
coreClient, err := core.NewComputeClientWithConfigurationProvider(cfg.ConfigProvider)
if err != nil {
return nil, err
}
2018-04-11 05:20:40 -04:00
vcnClient, err := core.NewVirtualNetworkClientWithConfigurationProvider(cfg.ConfigProvider)
if err != nil {
return nil, err
}
return &driverOCI{
computeClient: coreClient,
vcnClient: vcnClient,
cfg: cfg,
}, nil
}
// CreateInstance creates a new compute instance.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) CreateInstance(ctx context.Context, publicKey string) (string, error) {
2018-04-11 05:20:40 -04:00
metadata := map[string]string{
"ssh_authorized_keys": publicKey,
}
2018-07-17 11:41:19 -04:00
if d.cfg.Metadata != nil {
for key, value := range d.cfg.Metadata {
metadata[key] = value
}
}
if d.cfg.UserData != "" {
2018-04-11 05:20:40 -04:00
metadata["user_data"] = d.cfg.UserData
}
2018-04-11 05:20:40 -04:00
instanceDetails := core.LaunchInstanceDetails{
2018-04-11 05:20:40 -04:00
AvailabilityDomain: &d.cfg.AvailabilityDomain,
CompartmentId: &d.cfg.CompartmentID,
ImageId: &d.cfg.BaseImageID,
Shape: &d.cfg.Shape,
SubnetId: &d.cfg.SubnetID,
Metadata: metadata,
}
// When empty, the default display name is used.
if d.cfg.InstanceName != "" {
instanceDetails.DisplayName = &d.cfg.InstanceName
}
instance, err := d.computeClient.LaunchInstance(context.TODO(), core.LaunchInstanceRequest{LaunchInstanceDetails: instanceDetails})
2018-04-11 05:20:40 -04:00
if err != nil {
return "", err
}
2018-04-11 05:20:40 -04:00
return *instance.Id, nil
}
// CreateImage creates a new custom image.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) CreateImage(ctx context.Context, id string) (core.Image, error) {
res, err := d.computeClient.CreateImage(ctx, core.CreateImageRequest{CreateImageDetails: core.CreateImageDetails{
2018-04-11 05:20:40 -04:00
CompartmentId: &d.cfg.CompartmentID,
InstanceId: &id,
DisplayName: &d.cfg.ImageName,
}})
if err != nil {
2018-04-11 05:20:40 -04:00
return core.Image{}, err
}
2018-04-11 05:20:40 -04:00
return res.Image, nil
}
// DeleteImage deletes a custom image.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) DeleteImage(ctx context.Context, id string) error {
_, err := d.computeClient.DeleteImage(ctx, core.DeleteImageRequest{ImageId: &id})
2018-04-11 05:20:40 -04:00
return err
}
// GetInstanceIP returns the public or private IP corresponding to the given instance id.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) GetInstanceIP(ctx context.Context, id string) (string, error) {
vnics, err := d.computeClient.ListVnicAttachments(ctx, core.ListVnicAttachmentsRequest{
2018-04-11 05:20:40 -04:00
InstanceId: &id,
CompartmentId: &d.cfg.CompartmentID,
})
if err != nil {
return "", err
}
2018-04-11 05:20:40 -04:00
if len(vnics.Items) == 0 {
return "", errors.New("instance has zero VNICs")
}
2018-06-26 05:05:56 -04:00
vnic, err := d.vcnClient.GetVnic(ctx, core.GetVnicRequest{VnicId: vnics.Items[0].VnicId})
if err != nil {
return "", fmt.Errorf("Error getting VNIC details: %s", err)
}
if d.cfg.UsePrivateIP {
2018-04-11 05:20:40 -04:00
return *vnic.PrivateIp, nil
}
2018-04-11 05:20:40 -04:00
if vnic.PublicIp == nil {
return "", fmt.Errorf("Error getting VNIC Public Ip for: %s", id)
}
return *vnic.PublicIp, nil
}
2018-06-26 05:05:56 -04:00
func (d *driverOCI) GetInstanceInitialCredentials(ctx context.Context, id string) (string, string, error) {
credentials, err := d.computeClient.GetWindowsInstanceInitialCredentials(ctx, core.GetWindowsInstanceInitialCredentialsRequest{
InstanceId: &id,
})
if err != nil {
return "", "", err
}
return *credentials.InstanceCredentials.Username, *credentials.InstanceCredentials.Password, err
}
// TerminateInstance terminates a compute instance.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) TerminateInstance(ctx context.Context, id string) error {
_, err := d.computeClient.TerminateInstance(ctx, core.TerminateInstanceRequest{
2018-04-11 05:20:40 -04:00
InstanceId: &id,
})
return err
}
// WaitForImageCreation waits for a provisioning custom image to reach the
// "AVAILABLE" state.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) WaitForImageCreation(ctx context.Context, id string) error {
2018-04-11 05:20:40 -04:00
return waitForResourceToReachState(
func(string) (string, error) {
2018-06-26 05:05:56 -04:00
image, err := d.computeClient.GetImage(ctx, core.GetImageRequest{ImageId: &id})
2018-04-11 05:20:40 -04:00
if err != nil {
return "", err
}
return string(image.LifecycleState), nil
},
id,
[]string{"PROVISIONING"},
"AVAILABLE",
2018-04-11 05:20:40 -04:00
0, //Unlimited Retries
5*time.Second, //5 second wait between retries
)
}
// WaitForInstanceState waits for an instance to reach the a given terminal
// state.
2018-06-26 05:05:56 -04:00
func (d *driverOCI) WaitForInstanceState(ctx context.Context, id string, waitStates []string, terminalState string) error {
2018-04-11 05:20:40 -04:00
return waitForResourceToReachState(
func(string) (string, error) {
2018-06-26 05:05:56 -04:00
instance, err := d.computeClient.GetInstance(ctx, core.GetInstanceRequest{InstanceId: &id})
2018-04-11 05:20:40 -04:00
if err != nil {
return "", err
}
return string(instance.LifecycleState), nil
},
id,
waitStates,
terminalState,
2018-04-11 05:20:40 -04:00
0, //Unlimited Retries
5*time.Second, //5 second wait between retries
)
}
2018-04-11 05:20:40 -04:00
// WaitForResourceToReachState checks the response of a request through a
// polled get and waits until the desired state or until the max retried has
// been reached.
func waitForResourceToReachState(getResourceState func(string) (string, error), id string, waitStates []string, terminalState string, maxRetries int, waitDuration time.Duration) error {
for i := 0; maxRetries == 0 || i < maxRetries; i++ {
state, err := getResourceState(id)
if err != nil {
return err
}
if stringSliceContains(waitStates, state) {
time.Sleep(waitDuration)
continue
} else if state == terminalState {
return nil
}
return fmt.Errorf("Unexpected resource state %q, expecting a waiting state %s or terminal state %q ", state, waitStates, terminalState)
}
return fmt.Errorf("Maximum number of retries (%d) exceeded; resource did not reach state %q", maxRetries, terminalState)
}
// stringSliceContains loops through a slice of strings returning a boolean
// based on whether a given value is contained in the slice.
func stringSliceContains(slice []string, value string) bool {
for _, elem := range slice {
if elem == value {
return true
}
}
return false
}