2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2020-09-25 07:16:53 -04:00
|
|
|
"bytes"
|
2014-05-05 12:44:25 -04:00
|
|
|
"crypto/tls"
|
2017-05-24 03:28:13 -04:00
|
|
|
"crypto/x509"
|
2017-01-17 17:38:49 -05:00
|
|
|
"fmt"
|
2020-09-25 07:16:53 -04:00
|
|
|
"io"
|
2017-05-24 14:05:59 -04:00
|
|
|
"io/ioutil"
|
2020-09-25 07:16:53 -04:00
|
|
|
"net/http"
|
2017-01-17 17:38:49 -05:00
|
|
|
"os"
|
2017-05-24 14:05:59 -04:00
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack"
|
2017-01-17 17:38:49 -05:00
|
|
|
"github.com/gophercloud/utils/openstack/clientconfig"
|
2017-05-24 14:05:59 -04:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccessConfig is for common configuration related to openstack access
|
|
|
|
type AccessConfig struct {
|
2019-06-20 09:24:10 -04:00
|
|
|
// The username or id used to connect to the OpenStack service. If not
|
|
|
|
// specified, Packer will use the environment variable OS_USERNAME or
|
|
|
|
// OS_USERID, if set. This is not required if using access token or
|
2019-06-06 10:29:25 -04:00
|
|
|
// application credential instead of password, or if using cloud.yaml.
|
2019-06-20 09:24:10 -04:00
|
|
|
Username string `mapstructure:"username" required:"true"`
|
|
|
|
// Sets username
|
|
|
|
UserID string `mapstructure:"user_id"`
|
|
|
|
// The password used to connect to the OpenStack service. If not specified,
|
|
|
|
// Packer will use the environment variables OS_PASSWORD, if set. This is
|
|
|
|
// not required if using access token or application credential instead of
|
|
|
|
// password, or if using cloud.yaml.
|
2019-06-06 10:29:25 -04:00
|
|
|
Password string `mapstructure:"password" required:"true"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The URL to the OpenStack Identity service. If not specified, Packer will
|
|
|
|
// use the environment variables OS_AUTH_URL, if set. This is not required
|
|
|
|
// if using cloud.yaml.
|
2019-06-06 10:29:25 -04:00
|
|
|
IdentityEndpoint string `mapstructure:"identity_endpoint" required:"true"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The tenant ID or name to boot the instance into. Some OpenStack
|
|
|
|
// installations require this. If not specified, Packer will use the
|
|
|
|
// environment variable OS_TENANT_NAME or OS_TENANT_ID, if set. Tenant is
|
|
|
|
// also called Project in later versions of OpenStack.
|
2019-06-06 10:29:25 -04:00
|
|
|
TenantID string `mapstructure:"tenant_id" required:"false"`
|
|
|
|
TenantName string `mapstructure:"tenant_name"`
|
|
|
|
DomainID string `mapstructure:"domain_id"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The Domain name or ID you are authenticating with. OpenStack
|
|
|
|
// installations require this if identity v3 is used. Packer will use the
|
|
|
|
// environment variable OS_DOMAIN_NAME or OS_DOMAIN_ID, if set.
|
2019-06-06 10:29:25 -04:00
|
|
|
DomainName string `mapstructure:"domain_name" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Whether or not the connection to OpenStack can be done over an insecure
|
|
|
|
// connection. By default this is false.
|
2019-06-06 10:29:25 -04:00
|
|
|
Insecure bool `mapstructure:"insecure" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The name of the region, such as "DFW", in which to launch the server to
|
|
|
|
// create the image. If not specified, Packer will use the environment
|
|
|
|
// variable OS_REGION_NAME, if set.
|
2019-06-06 10:29:25 -04:00
|
|
|
Region string `mapstructure:"region" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The endpoint type to use. Can be any of "internal", "internalURL",
|
|
|
|
// "admin", "adminURL", "public", and "publicURL". By default this is
|
|
|
|
// "public".
|
2019-06-06 10:29:25 -04:00
|
|
|
EndpointType string `mapstructure:"endpoint_type" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Custom CA certificate file path. If omitted the OS_CACERT environment
|
|
|
|
// variable can be used.
|
2019-06-06 10:29:25 -04:00
|
|
|
CACertFile string `mapstructure:"cacert" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Client certificate file path for SSL client authentication. If omitted
|
|
|
|
// the OS_CERT environment variable can be used.
|
2019-06-06 10:29:25 -04:00
|
|
|
ClientCertFile string `mapstructure:"cert" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Client private key file path for SSL client authentication. If omitted
|
|
|
|
// the OS_KEY environment variable can be used.
|
2019-06-06 10:29:25 -04:00
|
|
|
ClientKeyFile string `mapstructure:"key" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// the token (id) to use with token based authorization. Packer will use
|
|
|
|
// the environment variable OS_TOKEN, if set.
|
2019-06-06 10:29:25 -04:00
|
|
|
Token string `mapstructure:"token" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The application credential name to use with application credential based
|
|
|
|
// authorization. Packer will use the environment variable
|
|
|
|
// OS_APPLICATION_CREDENTIAL_NAME, if set.
|
2019-06-06 10:29:25 -04:00
|
|
|
ApplicationCredentialName string `mapstructure:"application_credential_name" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The application credential id to use with application credential based
|
|
|
|
// authorization. Packer will use the environment variable
|
|
|
|
// OS_APPLICATION_CREDENTIAL_ID, if set.
|
2019-06-06 10:29:25 -04:00
|
|
|
ApplicationCredentialID string `mapstructure:"application_credential_id" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// The application credential secret to use with application credential
|
|
|
|
// based authorization. Packer will use the environment variable
|
|
|
|
// OS_APPLICATION_CREDENTIAL_SECRET, if set.
|
2019-05-28 11:50:58 -04:00
|
|
|
ApplicationCredentialSecret string `mapstructure:"application_credential_secret" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// An entry in a `clouds.yaml` file. See the OpenStack os-client-config
|
|
|
|
// [documentation](https://docs.openstack.org/os-client-config/latest/user/configuration.html)
|
|
|
|
// for more information about `clouds.yaml` files. If omitted, the
|
|
|
|
// `OS_CLOUD` environment variable is used.
|
2019-06-06 10:29:25 -04:00
|
|
|
Cloud string `mapstructure:"cloud" required:"false"`
|
2015-06-12 00:16:43 -04:00
|
|
|
|
|
|
|
osClient *gophercloud.ProviderClient
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
if c.EndpointType != "internal" && c.EndpointType != "internalURL" &&
|
|
|
|
c.EndpointType != "admin" && c.EndpointType != "adminURL" &&
|
|
|
|
c.EndpointType != "public" && c.EndpointType != "publicURL" &&
|
|
|
|
c.EndpointType != "" {
|
|
|
|
return []error{fmt.Errorf("Invalid endpoint type provided")}
|
2014-01-06 16:41:30 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:33:52 -04:00
|
|
|
// Legacy RackSpace stuff. We're keeping this around to keep things BC.
|
|
|
|
if c.Password == "" {
|
|
|
|
c.Password = os.Getenv("SDK_PASSWORD")
|
|
|
|
}
|
|
|
|
if c.Region == "" {
|
|
|
|
c.Region = os.Getenv("SDK_REGION")
|
|
|
|
}
|
|
|
|
if c.TenantName == "" {
|
|
|
|
c.TenantName = os.Getenv("SDK_PROJECT")
|
|
|
|
}
|
|
|
|
if c.Username == "" {
|
|
|
|
c.Username = os.Getenv("SDK_USERNAME")
|
|
|
|
}
|
2017-01-17 17:38:49 -05:00
|
|
|
// End RackSpace
|
|
|
|
|
|
|
|
if c.Cloud == "" {
|
|
|
|
c.Cloud = os.Getenv("OS_CLOUD")
|
|
|
|
}
|
|
|
|
if c.Region == "" {
|
|
|
|
c.Region = os.Getenv("OS_REGION_NAME")
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:21:16 -04:00
|
|
|
if c.CACertFile == "" {
|
|
|
|
c.CACertFile = os.Getenv("OS_CACERT")
|
|
|
|
}
|
2017-05-23 21:23:12 -04:00
|
|
|
if c.ClientCertFile == "" {
|
|
|
|
c.ClientCertFile = os.Getenv("OS_CERT")
|
|
|
|
}
|
|
|
|
if c.ClientKeyFile == "" {
|
|
|
|
c.ClientKeyFile = os.Getenv("OS_KEY")
|
|
|
|
}
|
2015-06-12 00:33:52 -04:00
|
|
|
|
2017-01-17 17:38:49 -05:00
|
|
|
clientOpts := new(clientconfig.ClientOpts)
|
|
|
|
|
|
|
|
// If a cloud entry was given, base AuthOptions on a clouds.yaml file.
|
|
|
|
if c.Cloud != "" {
|
|
|
|
clientOpts.Cloud = c.Cloud
|
|
|
|
|
|
|
|
cloud, err := clientconfig.GetCloudFromYAML(clientOpts)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Region == "" && cloud.RegionName != "" {
|
|
|
|
c.Region = cloud.RegionName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
authInfo := &clientconfig.AuthInfo{
|
|
|
|
AuthURL: c.IdentityEndpoint,
|
|
|
|
DomainID: c.DomainID,
|
|
|
|
DomainName: c.DomainName,
|
|
|
|
Password: c.Password,
|
|
|
|
ProjectID: c.TenantID,
|
|
|
|
ProjectName: c.TenantName,
|
|
|
|
Token: c.Token,
|
|
|
|
Username: c.Username,
|
|
|
|
UserID: c.UserID,
|
|
|
|
}
|
|
|
|
clientOpts.AuthInfo = authInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
ao, err := clientconfig.AuthOptions(clientOpts)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
2014-05-05 12:44:25 -04:00
|
|
|
|
2016-12-06 20:36:10 -05:00
|
|
|
// Make sure we reauth as needed
|
|
|
|
ao.AllowReauth = true
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// Override values if we have them in our config
|
|
|
|
overrides := []struct {
|
|
|
|
From, To *string
|
|
|
|
}{
|
|
|
|
{&c.Username, &ao.Username},
|
|
|
|
{&c.UserID, &ao.UserID},
|
|
|
|
{&c.Password, &ao.Password},
|
|
|
|
{&c.IdentityEndpoint, &ao.IdentityEndpoint},
|
|
|
|
{&c.TenantID, &ao.TenantID},
|
|
|
|
{&c.TenantName, &ao.TenantName},
|
|
|
|
{&c.DomainID, &ao.DomainID},
|
|
|
|
{&c.DomainName, &ao.DomainName},
|
2017-01-17 17:38:49 -05:00
|
|
|
{&c.Token, &ao.TokenID},
|
2019-02-09 22:51:08 -05:00
|
|
|
{&c.ApplicationCredentialName, &ao.ApplicationCredentialName},
|
|
|
|
{&c.ApplicationCredentialID, &ao.ApplicationCredentialID},
|
|
|
|
{&c.ApplicationCredentialSecret, &ao.ApplicationCredentialSecret},
|
2015-06-12 00:16:43 -04:00
|
|
|
}
|
|
|
|
for _, s := range overrides {
|
|
|
|
if *s.From != "" {
|
|
|
|
*s.To = *s.From
|
2013-11-19 12:03:35 -05:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the client itself
|
|
|
|
client, err := openstack.NewClient(ao.IdentityEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
2013-11-19 12:03:35 -05:00
|
|
|
|
2017-05-23 21:23:12 -04:00
|
|
|
tls_config := &tls.Config{}
|
|
|
|
|
2017-05-24 01:21:16 -04:00
|
|
|
if c.CACertFile != "" {
|
|
|
|
caCert, err := ioutil.ReadFile(c.CACertFile)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
tls_config.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
2019-06-20 09:24:10 -04:00
|
|
|
// If we have insecure set, then create a custom HTTP client that ignores
|
|
|
|
// SSL errors.
|
2015-06-12 00:16:43 -04:00
|
|
|
if c.Insecure {
|
2017-05-23 21:23:12 -04:00
|
|
|
tls_config.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(c.ClientCertFile, c.ClientKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
tls_config.Certificates = []tls.Certificate{cert}
|
2014-05-05 12:44:25 -04:00
|
|
|
}
|
2017-05-24 03:19:30 -04:00
|
|
|
|
2017-05-24 14:05:59 -04:00
|
|
|
transport := cleanhttp.DefaultTransport()
|
2017-05-24 03:19:30 -04:00
|
|
|
transport.TLSClientConfig = tls_config
|
2017-05-24 04:57:21 -04:00
|
|
|
client.HTTPClient.Transport = transport
|
2014-05-05 12:44:25 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// Auth
|
2017-01-17 17:38:49 -05:00
|
|
|
err = openstack.Authenticate(client, *ao)
|
2015-06-12 00:16:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
2013-11-19 12:03:35 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
c.osClient = client
|
|
|
|
return nil
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func (c *AccessConfig) enableDebug(ui packersdk.Ui) {
|
2020-09-25 07:16:53 -04:00
|
|
|
c.osClient.HTTPClient = http.Client{
|
|
|
|
Transport: &DebugRoundTripper{
|
|
|
|
ui: ui,
|
|
|
|
rt: c.osClient.HTTPClient.Transport,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
func (c *AccessConfig) computeV2Client() (*gophercloud.ServiceClient, error) {
|
|
|
|
return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.Region,
|
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
2013-09-01 16:22:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:21:20 -05:00
|
|
|
func (c *AccessConfig) imageV2Client() (*gophercloud.ServiceClient, error) {
|
|
|
|
return openstack.NewImageServiceV2(c.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.Region,
|
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-24 06:20:22 -04:00
|
|
|
func (c *AccessConfig) blockStorageV3Client() (*gophercloud.ServiceClient, error) {
|
|
|
|
return openstack.NewBlockStorageV3(c.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.Region,
|
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-12 04:38:54 -04:00
|
|
|
func (c *AccessConfig) networkV2Client() (*gophercloud.ServiceClient, error) {
|
|
|
|
return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.Region,
|
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
func (c *AccessConfig) getEndpointType() gophercloud.Availability {
|
|
|
|
if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
|
|
|
|
return gophercloud.AvailabilityInternal
|
2014-08-15 14:19:04 -04:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
|
|
|
|
return gophercloud.AvailabilityAdmin
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
return gophercloud.AvailabilityPublic
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
2020-09-25 07:16:53 -04:00
|
|
|
|
|
|
|
type DebugRoundTripper struct {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui packersdk.Ui
|
2020-09-25 07:16:53 -04:00
|
|
|
rt http.RoundTripper
|
|
|
|
numReauthAttempts int
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip performs a round-trip HTTP request and logs relevant information about it.
|
|
|
|
func (drt *DebugRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
|
|
defer func() {
|
|
|
|
if request.Body != nil {
|
|
|
|
request.Body.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var response *http.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
response, err = drt.rt.RoundTrip(request)
|
|
|
|
if response == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode == http.StatusUnauthorized {
|
|
|
|
if drt.numReauthAttempts == 3 {
|
|
|
|
return response, fmt.Errorf("Tried to re-authenticate 3 times with no success.")
|
|
|
|
}
|
|
|
|
drt.numReauthAttempts++
|
|
|
|
}
|
|
|
|
|
|
|
|
drt.DebugMessage(fmt.Sprintf("Request %s %s %d", request.Method, request.URL, response.StatusCode))
|
|
|
|
|
|
|
|
if response.StatusCode >= 400 {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
body, _ := ioutil.ReadAll(io.TeeReader(response.Body, buf))
|
|
|
|
drt.DebugMessage(fmt.Sprintf("Response Error: %+v\n", string(body)))
|
|
|
|
bufWithClose := ioutil.NopCloser(buf)
|
|
|
|
response.Body = bufWithClose
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drt *DebugRoundTripper) DebugMessage(message string) {
|
|
|
|
drt.ui.Message(fmt.Sprintf("[DEBUG] %s", message))
|
|
|
|
}
|