Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions builder/openstack/access_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ package openstack

import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/utils/v2/openstack/clientconfig"
"github.com/hashicorp/go-cleanhttp"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
Expand Down Expand Up @@ -135,6 +136,10 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
c.ClientKeyFile = os.Getenv("OS_KEY")
}

return nil
}

func (c *AccessConfig) Authenticate(ctx context.Context) error {
clientOpts := new(clientconfig.ClientOpts)

// If a cloud entry was given, base AuthOptions on a clouds.yaml file.
Expand All @@ -143,7 +148,7 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {

cloud, err := clientconfig.GetCloudFromYAML(clientOpts)
if err != nil {
return []error{err}
return err
}

if c.Region == "" && cloud.RegionName != "" {
Expand All @@ -166,7 +171,7 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {

ao, err := clientconfig.AuthOptions(clientOpts)
if err != nil {
return []error{err}
return err
}

// Make sure we reauth as needed
Expand Down Expand Up @@ -198,15 +203,15 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
// Build the client itself
client, err := openstack.NewClient(ao.IdentityEndpoint)
if err != nil {
return []error{err}
return err
}

tls_config := &tls.Config{}

if c.CACertFile != "" {
caCert, err := os.ReadFile(c.CACertFile)
if err != nil {
return []error{err}
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
Expand All @@ -222,7 +227,7 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
cert, err := tls.LoadX509KeyPair(c.ClientCertFile, c.ClientKeyFile)
if err != nil {
return []error{err}
return err
}

tls_config.Certificates = []tls.Certificate{cert}
Expand All @@ -233,9 +238,9 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
client.HTTPClient.Transport = transport

// Auth
err = openstack.Authenticate(client, *ao)
err = openstack.Authenticate(ctx, client, *ao)
if err != nil {
return []error{err}
return err
}

c.osClient = client
Expand All @@ -259,7 +264,7 @@ func (c *AccessConfig) computeV2Client() (*gophercloud.ServiceClient, error) {
}

func (c *AccessConfig) imageV2Client() (*gophercloud.ServiceClient, error) {
return openstack.NewImageServiceV2(c.osClient, gophercloud.EndpointOpts{
return openstack.NewImageV2(c.osClient, gophercloud.EndpointOpts{
Region: c.Region,
Availability: c.getEndpointType(),
})
Expand Down
7 changes: 4 additions & 3 deletions builder/openstack/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package openstack

import (
"context"
"fmt"
"log"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"

registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
)
Expand Down Expand Up @@ -74,5 +75,5 @@ func (a *Artifact) State(name string) any {

func (a *Artifact) Destroy() error {
log.Printf("Destroying image: %s", a.ImageId)
return images.Delete(a.Client, a.ImageId).ExtractErr()
return images.Delete(context.TODO(), a.Client, a.ImageId).ExtractErr()
}
7 changes: 7 additions & 0 deletions builder/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}

func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {

err := b.config.AccessConfig.Authenticate(ctx)
if err != nil {
return nil, fmt.Errorf("Error initializing compute client: %s", err)
}

if b.config.PackerDebug {
b.config.enableDebug(ui)
}
Expand Down Expand Up @@ -158,6 +164,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Host: CommHost(
ctx,
b.config.RunConfig.Comm.Host(),
computeClient,
b.config.SSHInterface,
Expand Down
2 changes: 1 addition & 1 deletion builder/openstack/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion builder/openstack/image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"strings"

imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
imageservice "github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)

Expand Down
39 changes: 20 additions & 19 deletions builder/openstack/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
package openstack

import (
"context"
"fmt"
"log"
"net"

"github.com/google/uuid"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/gophercloud/pagination"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/attachinterfaces"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
"github.com/gophercloud/gophercloud/v2/pagination"
)

// CheckFloatingIP gets a floating IP by its ID and checks if it is already
// associated with any internal interface.
// It returns floating IP if it can be used.
func CheckFloatingIP(client *gophercloud.ServiceClient, id string) (*floatingips.FloatingIP, error) {
floatingIP, err := floatingips.Get(client, id).Extract()
func CheckFloatingIP(ctx context.Context, client *gophercloud.ServiceClient, id string) (*floatingips.FloatingIP, error) {
floatingIP, err := floatingips.Get(ctx, client, id).Extract()
if err != nil {
return nil, err
}
Expand All @@ -36,13 +37,13 @@ func CheckFloatingIP(client *gophercloud.ServiceClient, id string) (*floatingips

// FindFreeFloatingIP returns free unassociated floating IP.
// It will return first floating IP if there are many.
func FindFreeFloatingIP(client *gophercloud.ServiceClient) (*floatingips.FloatingIP, error) {
func FindFreeFloatingIP(ctx context.Context, client *gophercloud.ServiceClient) (*floatingips.FloatingIP, error) {
var freeFloatingIP *floatingips.FloatingIP

pager := floatingips.List(client, floatingips.ListOpts{
Status: "DOWN",
})
err := pager.EachPage(func(page pagination.Page) (bool, error) {
err := pager.EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
candidates, err := floatingips.ExtractFloatingIPs(page)
if err != nil {
return false, err // stop and throw error out
Expand Down Expand Up @@ -72,11 +73,11 @@ func FindFreeFloatingIP(client *gophercloud.ServiceClient) (*floatingips.Floatin
// GetInstancePortID returns internal port of the instance that can be used for
// the association of a floating IP.
// It will return an ID of a first port if there are many.
func GetInstancePortID(client *gophercloud.ServiceClient, id string, instance_float_net string) (string, error) {
func GetInstancePortID(ctx context.Context, client *gophercloud.ServiceClient, id string, instance_float_net string) (string, error) {

selected_interface := 0

interfacesPage, err := attachinterfaces.List(client, id).AllPages()
interfacesPage, err := attachinterfaces.List(client, id).AllPages(ctx)
if err != nil {
return "", err
}
Expand All @@ -102,9 +103,9 @@ func GetInstancePortID(client *gophercloud.ServiceClient, id string, instance_fl

// CheckFloatingIPNetwork checks provided network reference and returns a valid
// Networking service ID.
func CheckFloatingIPNetwork(client *gophercloud.ServiceClient, networkRef string) (string, error) {
func CheckFloatingIPNetwork(ctx context.Context, client *gophercloud.ServiceClient, networkRef string) (string, error) {
if _, err := uuid.Parse(networkRef); err != nil {
return GetFloatingIPNetworkIDByName(client, networkRef)
return GetFloatingIPNetworkIDByName(ctx, client, networkRef)
}

return networkRef, nil
Expand All @@ -117,12 +118,12 @@ type ExternalNetwork struct {
}

// GetFloatingIPNetworkIDByName searches for the external network ID by the provided name.
func GetFloatingIPNetworkIDByName(client *gophercloud.ServiceClient, networkName string) (string, error) {
func GetFloatingIPNetworkIDByName(ctx context.Context, client *gophercloud.ServiceClient, networkName string) (string, error) {
var externalNetworks []ExternalNetwork

allPages, err := networks.List(client, networks.ListOpts{
Name: networkName,
}).AllPages()
}).AllPages(ctx)
if err != nil {
return "", err
}
Expand All @@ -143,8 +144,8 @@ func GetFloatingIPNetworkIDByName(client *gophercloud.ServiceClient, networkName
}

// DiscoverProvisioningNetwork finds the first network whose subnet matches the given network ranges.
func DiscoverProvisioningNetwork(client *gophercloud.ServiceClient, cidrs []string) (string, error) {
allPages, err := subnets.List(client, subnets.ListOpts{}).AllPages()
func DiscoverProvisioningNetwork(ctx context.Context, client *gophercloud.ServiceClient, cidrs []string) (string, error) {
allPages, err := subnets.List(client, subnets.ListOpts{}).AllPages(ctx)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion builder/openstack/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
"fmt"

"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-sdk/uuid"
Expand Down
2 changes: 1 addition & 1 deletion builder/openstack/run_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"testing"

"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/mitchellh/mapstructure"
)
Expand Down
22 changes: 12 additions & 10 deletions builder/openstack/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package openstack

import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
Expand All @@ -37,12 +39,12 @@ type StateChangeConf struct {

// ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch
// an openstack server.
func ServerStateRefreshFunc(
func ServerStateRefreshFunc(ctx context.Context,
client *gophercloud.ServiceClient, instanceID string) StateRefreshFunc {
return func() (interface{}, string, int, error) {
serverNew, err := servers.Get(client, instanceID).Extract()
serverNew, err := servers.Get(ctx, client, instanceID).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
log.Printf("[INFO] 404 on ServerStateRefresh, returning DELETED")
return nil, "DELETED", 0, nil
}
Expand Down Expand Up @@ -96,7 +98,7 @@ func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
}
}

func DeleteServer(state multistep.StateBag, instance string) error {
func DeleteServer(ctx context.Context, state multistep.StateBag, instance string) error {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)

Expand All @@ -113,16 +115,16 @@ func DeleteServer(state multistep.StateBag, instance string) error {
ui.Say(fmt.Sprintf("Terminating the source server: %s ...", instance))
for {
if config.ForceDelete {
err = servers.ForceDelete(computeClient, instance).ExtractErr()
err = servers.ForceDelete(ctx, computeClient, instance).ExtractErr()
} else {
err = servers.Delete(computeClient, instance).ExtractErr()
err = servers.Delete(ctx, computeClient, instance).ExtractErr()
}

if err == nil {
break
}

if _, ok := err.(gophercloud.ErrDefault500); !ok {
if gophercloud.ResponseCodeIs(err, http.StatusInternalServerError) {
err = fmt.Errorf("Error terminating server, may still be around: %s", err)
return err
}
Expand All @@ -139,7 +141,7 @@ func DeleteServer(state multistep.StateBag, instance string) error {

stateChange := StateChangeConf{
Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED", "SHUTOFF", "STOPPED"},
Refresh: ServerStateRefreshFunc(computeClient, instance),
Refresh: ServerStateRefreshFunc(ctx, computeClient, instance),
Target: []string{"DELETED"},
}

Expand Down
10 changes: 6 additions & 4 deletions builder/openstack/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
package openstack

import (
"context"
"errors"
"log"
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/hashicorp/packer-plugin-sdk/multistep"
)

// CommHost looks up the host for the communicator.
func CommHost(
ctx context.Context,
host string,
client *gophercloud.ServiceClient,
sshinterface string,
Expand Down Expand Up @@ -54,7 +56,7 @@ func CommHost(
return addr, nil
}

s, err := servers.Get(client, s.ID).Extract()
s, err := servers.Get(ctx, client, s.ID).Extract()
if err != nil {
return "", err
}
Expand Down
Loading