packer-cn/builder/googlecompute/driver_gce.go

769 lines
21 KiB
Go
Raw Normal View History

package googlecompute
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
2013-12-13 00:52:20 -05:00
"log"
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
"strings"
"time"
compute "google.golang.org/api/compute/v1"
2020-09-20 10:18:37 -04:00
"google.golang.org/api/option"
oslogin "google.golang.org/api/oslogin/v1"
"github.com/hashicorp/packer/builder/googlecompute/version"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/retry"
"github.com/hashicorp/packer/packer-plugin-sdk/useragent"
vaultapi "github.com/hashicorp/vault/api"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
// driverGCE is a Driver implementation that actually talks to GCE.
// Create an instance using NewDriverGCE.
type driverGCE struct {
projectId string
service *compute.Service
osLoginService *oslogin.Service
ui packer.Ui
}
2020-10-01 15:39:06 -04:00
type GCEDriverConfig struct {
Ui packer.Ui
ProjectId string
Account *ServiceAccount
ImpersonateServiceAccountName string
VaultOauthEngineName string
}
var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"}
// Define a TokenSource that gets tokens from Vault
type OauthTokenSource struct {
Path string
}
func (ots OauthTokenSource) Token() (*oauth2.Token, error) {
log.Printf("Retrieving Oauth token from Vault...")
vaultConfig := vaultapi.DefaultConfig()
cli, err := vaultapi.NewClient(vaultConfig)
if err != nil {
return nil, fmt.Errorf("%s\n", err)
}
resp, err := cli.Logical().Read(ots.Path)
if err != nil {
return nil, fmt.Errorf("Error reading vault resp: %s", err)
}
if resp == nil {
return nil, fmt.Errorf("Vault Oauth Engine does not exist at the given path.")
}
token, ok := resp.Data["token"]
if !ok {
return nil, fmt.Errorf("ERROR, token was not present in response body")
}
at := token.(string)
log.Printf("Retrieved Oauth token from Vault")
return &oauth2.Token{
AccessToken: at,
Expiry: time.Now().Add(time.Minute * time.Duration(60)),
}, nil
}
2020-10-01 15:39:06 -04:00
func NewClientOptionGoogle(account *ServiceAccount, vaultOauth string, impersonatesa string) (option.ClientOption, error) {
var err error
2020-09-20 10:31:45 -04:00
var opts option.ClientOption
if vaultOauth != "" {
// Auth with Vault Oauth
log.Printf("Using Vault to generate Oauth token.")
ts := OauthTokenSource{vaultOauth}
2020-09-20 10:31:45 -04:00
opts = option.WithTokenSource(ts)
2020-10-01 15:39:06 -04:00
} else if impersonatesa != "" {
opts = option.ImpersonateCredentials(impersonatesa)
} else if account != nil && account.jwt != nil && len(account.jwt.PrivateKey) > 0 {
// Auth with AccountFile if provided
log.Printf("[INFO] Requesting Google token via account_file...")
2020-09-20 10:18:37 -04:00
log.Printf("[INFO] -- Email: %s", account.jwt.Email)
log.Printf("[INFO] -- Scopes: %s", DriverScopes)
2020-09-20 10:18:37 -04:00
log.Printf("[INFO] -- Private Key Length: %d", len(account.jwt.PrivateKey))
2020-09-20 10:31:45 -04:00
opts = option.WithCredentialsJSON(account.jsonKey)
} else {
log.Printf("[INFO] Requesting Google token via GCE API Default Client Token Source...")
2020-09-20 10:18:37 -04:00
ts, err := google.DefaultTokenSource(context.TODO(), "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return nil, err
}
2020-09-20 10:31:45 -04:00
opts = option.WithTokenSource(ts)
// The DefaultClient uses the DefaultTokenSource of the google lib.
// The DefaultTokenSource uses the "Application Default Credentials"
// It looks for credentials in the following places, preferring the first location found:
// 1. A JSON file whose path is specified by the
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
// 2. A JSON file in a location known to the gcloud command-line tool.
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
// 3. On Google App Engine it uses the appengine.AccessToken function.
// 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
// credentials from the metadata server.
// (In this final case any provided scopes are ignored.)
}
if err != nil {
return nil, err
}
2020-09-20 10:18:37 -04:00
return opts, nil
}
2020-10-01 15:39:06 -04:00
func NewDriverGCE(config GCEDriverConfig) (Driver, error) {
opts, err := NewClientOptionGoogle(config.Account, config.VaultOauthEngineName, config.ImpersonateServiceAccountName)
if err != nil {
return nil, err
}
log.Printf("[INFO] Instantiating GCE client...")
2020-09-20 10:31:45 -04:00
service, err := compute.NewService(context.TODO(), opts)
if err != nil {
return nil, err
}
2020-09-20 10:18:37 -04:00
log.Printf("[INFO] Instantiating OS Login client...")
2020-09-20 10:31:45 -04:00
osLoginService, err := oslogin.NewService(context.TODO(), opts)
if err != nil {
return nil, err
}
// Set UserAgent
service.UserAgent = useragent.String(version.GCEPluginVersion.FormattedVersion())
return &driverGCE{
2020-10-01 15:39:06 -04:00
projectId: config.ProjectId,
service: service,
osLoginService: osLoginService,
2020-10-01 15:39:06 -04:00
ui: config.Ui,
}, nil
}
func (d *driverGCE) CreateImage(name, description, family, zone, disk string, image_labels map[string]string, image_licenses []string, image_encryption_key *compute.CustomerEncryptionKey, imageStorageLocations []string) (<-chan *Image, <-chan error) {
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
gce_image := &compute.Image{
Description: description,
Name: name,
Family: family,
Labels: image_labels,
Licenses: image_licenses,
ImageEncryptionKey: image_encryption_key,
SourceDisk: fmt.Sprintf("%s%s/zones/%s/disks/%s", d.service.BasePath, d.projectId, zone, disk),
SourceType: "RAW",
StorageLocations: imageStorageLocations,
}
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
imageCh := make(chan *Image, 1)
errCh := make(chan error, 1)
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
op, err := d.service.Images.Insert(d.projectId, gce_image).Do()
if err != nil {
errCh <- err
} else {
go func() {
err = waitForState(errCh, "DONE", d.refreshGlobalOp(op))
if err != nil {
close(imageCh)
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
errCh <- err
return
}
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
var image *Image
image, err = d.GetImageFromProject(d.projectId, name, false)
if err != nil {
close(imageCh)
errCh <- err
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
return
}
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
imageCh <- image
close(imageCh)
}()
}
return imageCh, errCh
}
func (d *driverGCE) DeleteImage(name string) <-chan error {
errCh := make(chan error, 1)
op, err := d.service.Images.Delete(d.projectId, name).Do()
if err != nil {
errCh <- err
} else {
go waitForState(errCh, "DONE", d.refreshGlobalOp(op))
}
return errCh
}
2013-12-13 01:34:47 -05:00
func (d *driverGCE) DeleteInstance(zone, name string) (<-chan error, error) {
op, err := d.service.Instances.Delete(d.projectId, zone, name).Do()
if err != nil {
return nil, err
}
errCh := make(chan error, 1)
go waitForState(errCh, "DONE", d.refreshZoneOp(zone, op))
return errCh, nil
}
func (d *driverGCE) DeleteDisk(zone, name string) (<-chan error, error) {
op, err := d.service.Disks.Delete(d.projectId, zone, name).Do()
if err != nil {
return nil, err
}
errCh := make(chan error, 1)
go waitForState(errCh, "DONE", d.refreshZoneOp(zone, op))
return errCh, nil
}
func (d *driverGCE) GetImage(name string, fromFamily bool) (*Image, error) {
projects := []string{
d.projectId,
// Public projects, drawn from
// https://cloud.google.com/compute/docs/images
"centos-cloud",
"cos-cloud",
"coreos-cloud",
"debian-cloud",
"rhel-cloud",
"rhel-sap-cloud",
"suse-cloud",
"suse-sap-cloud",
2019-07-31 12:38:35 -04:00
"suse-byos-cloud",
"ubuntu-os-cloud",
"windows-cloud",
"windows-sql-cloud",
"gce-uefi-images",
"gce-nvme",
// misc
"google-containers",
"opensuse-cloud",
}
return d.GetImageFromProjects(projects, name, fromFamily)
}
func (d *driverGCE) GetImageFromProjects(projects []string, name string, fromFamily bool) (*Image, error) {
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
var errs error
for _, project := range projects {
image, err := d.GetImageFromProject(project, name, fromFamily)
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
if err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
if image != nil {
return image, nil
}
}
return nil, fmt.Errorf(
"Could not find image, %s, in projects, %s: %s", name,
projects, errs)
}
func (d *driverGCE) GetImageFromProject(project, name string, fromFamily bool) (*Image, error) {
var (
image *compute.Image
err error
)
if fromFamily {
image, err = d.service.Images.GetFromFamily(project, name).Do()
} else {
image, err = d.service.Images.Get(project, name).Do()
}
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
if err != nil {
return nil, err
} else if image == nil || image.SelfLink == "" {
return nil, fmt.Errorf("Image, %s, could not be found in project: %s", name, project)
} else {
return &Image{
GuestOsFeatures: image.GuestOsFeatures,
Licenses: image.Licenses,
Name: image.Name,
ProjectId: project,
SelfLink: image.SelfLink,
SizeGb: image.DiskSizeGb,
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
}, nil
}
}
func (d *driverGCE) GetInstanceMetadata(zone, name, key string) (string, error) {
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
return "", err
}
for _, item := range instance.Metadata.Items {
if item.Key == key {
return *item.Value, nil
}
}
return "", fmt.Errorf("Instance metadata key, %s, not found.", key)
}
func (d *driverGCE) GetNatIP(zone, name string) (string, error) {
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
return "", err
}
for _, ni := range instance.NetworkInterfaces {
if ni.AccessConfigs == nil {
continue
}
for _, ac := range ni.AccessConfigs {
if ac.NatIP != "" {
return ac.NatIP, nil
}
}
}
return "", nil
}
func (d *driverGCE) GetInternalIP(zone, name string) (string, error) {
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
return "", err
}
for _, ni := range instance.NetworkInterfaces {
if ni.NetworkIP == "" {
continue
}
return ni.NetworkIP, nil
}
return "", nil
}
func (d *driverGCE) GetSerialPortOutput(zone, name string) (string, error) {
output, err := d.service.Instances.GetSerialPortOutput(d.projectId, zone, name).Do()
if err != nil {
return "", err
}
return output.Contents, nil
}
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
func (d *driverGCE) ImageExists(name string) bool {
_, err := d.GetImageFromProject(d.projectId, name, false)
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
// The API may return an error for reasons other than the image not
// existing, but this heuristic is sufficient for now.
return err == nil
}
func (d *driverGCE) RunInstance(c *InstanceConfig) (<-chan error, error) {
// Get the zone
d.ui.Message(fmt.Sprintf("Loading zone: %s", c.Zone))
zone, err := d.service.Zones.Get(d.projectId, c.Zone).Do()
if err != nil {
return nil, err
}
// Get the machine type
d.ui.Message(fmt.Sprintf("Loading machine type: %s", c.MachineType))
machineType, err := d.service.MachineTypes.Get(
d.projectId, zone.Name, c.MachineType).Do()
if err != nil {
return nil, err
}
// TODO(mitchellh): deprecation warnings
networkId, subnetworkId, err := getNetworking(c)
if err != nil {
return nil, err
}
var accessconfig *compute.AccessConfig
// Use external IP if OmitExternalIP isn't set
if !c.OmitExternalIP {
accessconfig = &compute.AccessConfig{
Name: "AccessConfig created by Packer",
Type: "ONE_TO_ONE_NAT",
}
// If given a static IP, use it
if c.Address != "" {
region_url := strings.Split(zone.Region, "/")
region := region_url[len(region_url)-1]
address, err := d.service.Addresses.Get(d.projectId, region, c.Address).Do()
if err != nil {
return nil, err
}
accessconfig.NatIP = address.Address
}
}
// Build up the metadata
metadata := make([]*compute.MetadataItems, len(c.Metadata))
for k, v := range c.Metadata {
vCopy := v
metadata = append(metadata, &compute.MetadataItems{
Key: k,
Value: &vCopy,
})
}
var guestAccelerators []*compute.AcceleratorConfig
if c.AcceleratorCount > 0 {
ac := &compute.AcceleratorConfig{
AcceleratorCount: c.AcceleratorCount,
AcceleratorType: c.AcceleratorType,
}
guestAccelerators = append(guestAccelerators, ac)
}
// Configure the instance's service account. If the user has set
// disable_default_service_account, then the default service account
// will not be used. If they also do not set service_account_email, then
// the instance will be created with no service account or scopes.
serviceAccount := &compute.ServiceAccount{}
if !c.DisableDefaultServiceAccount {
serviceAccount.Email = "default"
serviceAccount.Scopes = c.Scopes
}
if c.ServiceAccountEmail != "" {
serviceAccount.Email = c.ServiceAccountEmail
serviceAccount.Scopes = c.Scopes
}
// Create the instance information
instance := compute.Instance{
Description: c.Description,
Disks: []*compute.AttachedDisk{
2016-11-01 17:08:04 -04:00
{
2014-04-26 14:12:43 -04:00
Type: "PERSISTENT",
Mode: "READ_WRITE",
Kind: "compute#attachedDisk",
Boot: true,
AutoDelete: false,
InitializeParams: &compute.AttachedDiskInitializeParams{
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
SourceImage: c.Image.SelfLink,
DiskSizeGb: c.DiskSizeGb,
DiskType: fmt.Sprintf("zones/%s/diskTypes/%s", zone.Name, c.DiskType),
},
},
},
GuestAccelerators: guestAccelerators,
Labels: c.Labels,
MachineType: machineType.SelfLink,
Metadata: &compute.Metadata{
Items: metadata,
},
2018-08-21 04:09:30 -04:00
MinCpuPlatform: c.MinCpuPlatform,
Name: c.Name,
NetworkInterfaces: []*compute.NetworkInterface{
2016-11-01 17:08:04 -04:00
{
AccessConfigs: []*compute.AccessConfig{accessconfig},
Network: networkId,
Subnetwork: subnetworkId,
},
},
Scheduling: &compute.Scheduling{
OnHostMaintenance: c.OnHostMaintenance,
Preemptible: c.Preemptible,
},
ServiceAccounts: []*compute.ServiceAccount{
serviceAccount,
},
Tags: &compute.Tags{
Items: c.Tags,
},
}
// Shielded VMs configuration. If the user has set at least one of the
// options, the shielded VM configuration will reflect that. If they
// don't set any of the options the settings will default to the ones
// of the source compute image which is used for creating the virtual
// machine.
shieldedInstanceConfig := &compute.ShieldedInstanceConfig{
EnableSecureBoot: c.EnableSecureBoot,
EnableVtpm: c.EnableVtpm,
EnableIntegrityMonitoring: c.EnableIntegrityMonitoring,
}
shieldedUiMessage := ""
if c.EnableSecureBoot || c.EnableVtpm || c.EnableIntegrityMonitoring {
instance.ShieldedInstanceConfig = shieldedInstanceConfig
shieldedUiMessage = " Shielded VM"
}
d.ui.Message(fmt.Sprintf("Requesting%s instance creation...", shieldedUiMessage))
op, err := d.service.Instances.Insert(d.projectId, zone.Name, &instance).Do()
if err != nil {
return nil, err
}
errCh := make(chan error, 1)
go waitForState(errCh, "DONE", d.refreshZoneOp(zone.Name, op))
return errCh, nil
}
func (d *driverGCE) CreateOrResetWindowsPassword(instance, zone string, c *WindowsPasswordConfig) (<-chan error, error) {
errCh := make(chan error, 1)
go d.createWindowsPassword(errCh, instance, zone, c)
return errCh, nil
}
func (d *driverGCE) createWindowsPassword(errCh chan<- error, name, zone string, c *WindowsPasswordConfig) {
data, err := json.Marshal(c)
if err != nil {
errCh <- err
return
}
dCopy := string(data)
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
errCh <- err
return
}
instance.Metadata.Items = append(instance.Metadata.Items, &compute.MetadataItems{Key: "windows-keys", Value: &dCopy})
op, err := d.service.Instances.SetMetadata(d.projectId, zone, name, &compute.Metadata{
Fingerprint: instance.Metadata.Fingerprint,
Items: instance.Metadata.Items,
}).Do()
if err != nil {
errCh <- err
return
}
newErrCh := make(chan error, 1)
go waitForState(newErrCh, "DONE", d.refreshZoneOp(zone, op))
select {
case err = <-newErrCh:
case <-time.After(time.Second * 30):
err = errors.New("time out while waiting for instance to create")
}
if err != nil {
errCh <- err
return
}
timeout := time.Now().Add(time.Minute * 3)
hash := sha1.New()
random := rand.Reader
for time.Now().Before(timeout) {
if passwordResponses, err := d.getPasswordResponses(zone, name); err == nil {
for _, response := range passwordResponses {
if response.Modulus == c.Modulus {
decodedPassword, err := base64.StdEncoding.DecodeString(response.EncryptedPassword)
if err != nil {
errCh <- err
return
}
password, err := rsa.DecryptOAEP(hash, random, c.key, decodedPassword, nil)
if err != nil {
errCh <- err
return
}
c.password = string(password)
errCh <- nil
return
}
}
}
time.Sleep(2 * time.Second)
}
err = errors.New("Could not retrieve password. Timed out.")
errCh <- err
return
}
func (d *driverGCE) getPasswordResponses(zone, instance string) ([]windowsPasswordResponse, error) {
output, err := d.service.Instances.GetSerialPortOutput(d.projectId, zone, instance).Port(4).Do()
if err != nil {
return nil, err
}
responses := strings.Split(output.Contents, "\n")
passwordResponses := make([]windowsPasswordResponse, 0, len(responses))
for _, response := range responses {
var passwordResponse windowsPasswordResponse
if err := json.Unmarshal([]byte(response), &passwordResponse); err != nil {
continue
}
passwordResponses = append(passwordResponses, passwordResponse)
}
return passwordResponses, nil
}
func (d *driverGCE) ImportOSLoginSSHKey(user, sshPublicKey string) (*oslogin.LoginProfile, error) {
parent := fmt.Sprintf("users/%s", user)
resp, err := d.osLoginService.Users.ImportSshPublicKey(parent, &oslogin.SshPublicKey{
Key: sshPublicKey,
}).Do()
if err != nil {
return nil, err
}
return resp.LoginProfile, nil
}
func (d *driverGCE) DeleteOSLoginSSHKey(user, fingerprint string) error {
name := fmt.Sprintf("users/%s/sshPublicKeys/%s", user, fingerprint)
_, err := d.osLoginService.Users.SshPublicKeys.Delete(name).Do()
if err != nil {
return err
}
return nil
}
func (d *driverGCE) WaitForInstance(state, zone, name string) <-chan error {
errCh := make(chan error, 1)
go waitForState(errCh, state, d.refreshInstanceState(zone, name))
return errCh
}
func (d *driverGCE) refreshInstanceState(zone, name string) stateRefreshFunc {
return func() (string, error) {
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
return "", err
}
return instance.Status, nil
}
}
func (d *driverGCE) refreshGlobalOp(op *compute.Operation) stateRefreshFunc {
return func() (string, error) {
newOp, err := d.service.GlobalOperations.Get(d.projectId, op.Name).Do()
if err != nil {
return "", err
}
// If the op is done, check for errors
err = nil
if newOp.Status == "DONE" {
if newOp.Error != nil {
for _, e := range newOp.Error.Errors {
err = packer.MultiErrorAppend(err, fmt.Errorf(e.Message))
}
}
}
return newOp.Status, err
}
}
func (d *driverGCE) refreshZoneOp(zone string, op *compute.Operation) stateRefreshFunc {
return func() (string, error) {
newOp, err := d.service.ZoneOperations.Get(d.projectId, zone, op.Name).Do()
if err != nil {
return "", err
}
// If the op is done, check for errors
err = nil
if newOp.Status == "DONE" {
if newOp.Error != nil {
for _, e := range newOp.Error.Errors {
err = packer.MultiErrorAppend(err, fmt.Errorf(e.Message))
}
}
}
return newOp.Status, err
}
}
// used in conjunction with waitForState.
type stateRefreshFunc func() (string, error)
// waitForState will spin in a loop forever waiting for state to
// reach a certain target.
func waitForState(errCh chan<- error, target string, refresh stateRefreshFunc) error {
ctx := context.TODO()
err := retry.Config{
RetryDelay: (&retry.Backoff{InitialBackoff: 2 * time.Second, MaxBackoff: 2 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error {
state, err := refresh()
if err != nil {
return err
}
if state == target {
return nil
}
return fmt.Errorf("retrying for state %s, got %s", target, state)
})
errCh <- err
return err
}
Amend commit author for license pass $ make test find: -executable: unknown primary or operator find: -executable: unknown primary or operator ==> Checking that only certain files are executable... Check passed. ok github.com/hashicorp/packer 0.098s ok github.com/hashicorp/packer/builder/alicloud/ecs 70.102s ? github.com/hashicorp/packer/builder/alicloud/version [no test files] ok github.com/hashicorp/packer/builder/amazon/chroot 0.076s ok github.com/hashicorp/packer/builder/amazon/common 0.052s ? github.com/hashicorp/packer/builder/amazon/common/awserrors [no test files] ? github.com/hashicorp/packer/builder/amazon/common/ssm [no test files] ok github.com/hashicorp/packer/builder/amazon/ebs 0.053s ? github.com/hashicorp/packer/builder/amazon/ebs/acceptance [no test files] ok github.com/hashicorp/packer/builder/amazon/ebssurrogate 0.057s ok github.com/hashicorp/packer/builder/amazon/ebsvolume 0.052s ok github.com/hashicorp/packer/builder/amazon/instance 0.092s ? github.com/hashicorp/packer/builder/amazon/version [no test files] ok github.com/hashicorp/packer/builder/azure/arm 8.929s ok github.com/hashicorp/packer/builder/azure/chroot 0.071s ok github.com/hashicorp/packer/builder/azure/common 0.032s ok github.com/hashicorp/packer/builder/azure/common/client 2.768s ? github.com/hashicorp/packer/builder/azure/common/constants [no test files] ? github.com/hashicorp/packer/builder/azure/common/lin [no test files] ? github.com/hashicorp/packer/builder/azure/common/logutil [no test files] ok github.com/hashicorp/packer/builder/azure/common/template 0.038s ok github.com/hashicorp/packer/builder/azure/dtl 1.508s ok github.com/hashicorp/packer/builder/azure/pkcs12 0.250s ok github.com/hashicorp/packer/builder/azure/pkcs12/rc2 0.018s ? github.com/hashicorp/packer/builder/azure/version [no test files] ok github.com/hashicorp/packer/builder/cloudstack 0.074s ? github.com/hashicorp/packer/builder/cloudstack/version [no test files] ok github.com/hashicorp/packer/builder/digitalocean 0.078s ? github.com/hashicorp/packer/builder/digitalocean/version [no test files] ok github.com/hashicorp/packer/builder/docker 0.054s ? github.com/hashicorp/packer/builder/docker/version [no test files] ok github.com/hashicorp/packer/builder/file 0.037s ? github.com/hashicorp/packer/builder/file/version [no test files] ok github.com/hashicorp/packer/builder/googlecompute 7.982s ? github.com/hashicorp/packer/builder/googlecompute/version [no test files] ok github.com/hashicorp/packer/builder/hcloud 0.037s ? github.com/hashicorp/packer/builder/hcloud/version [no test files] ok github.com/hashicorp/packer/builder/hyperone 0.031s ? github.com/hashicorp/packer/builder/hyperone/version [no test files] ok github.com/hashicorp/packer/builder/hyperv/common 0.042s ok github.com/hashicorp/packer/builder/hyperv/common/powershell 0.017s ok github.com/hashicorp/packer/builder/hyperv/common/powershell/hyperv 0.027s ok github.com/hashicorp/packer/builder/hyperv/iso 0.193s ? github.com/hashicorp/packer/builder/hyperv/version [no test files] ok github.com/hashicorp/packer/builder/hyperv/vmcx 0.160s ok github.com/hashicorp/packer/builder/jdcloud 0.038s ? github.com/hashicorp/packer/builder/jdcloud/version [no test files] ok github.com/hashicorp/packer/builder/linode 0.074s ? github.com/hashicorp/packer/builder/linode/version [no test files] ok github.com/hashicorp/packer/builder/lxc 0.038s ? github.com/hashicorp/packer/builder/lxc/version [no test files] ok github.com/hashicorp/packer/builder/lxd 0.033s ? github.com/hashicorp/packer/builder/lxd/version [no test files] ok github.com/hashicorp/packer/builder/ncloud 0.038s ? github.com/hashicorp/packer/builder/ncloud/version [no test files] ok github.com/hashicorp/packer/builder/null 0.036s ? github.com/hashicorp/packer/builder/null/version [no test files] ok github.com/hashicorp/packer/builder/oneandone 0.038s ? github.com/hashicorp/packer/builder/oneandone/version [no test files] ok github.com/hashicorp/packer/builder/openstack 0.048s ? github.com/hashicorp/packer/builder/openstack/version [no test files] ok github.com/hashicorp/packer/builder/oracle/classic 0.055s ? github.com/hashicorp/packer/builder/oracle/common [no test files] ok github.com/hashicorp/packer/builder/oracle/oci 6.349s ? github.com/hashicorp/packer/builder/oracle/version [no test files] ok github.com/hashicorp/packer/builder/osc/bsu 0.045s ok github.com/hashicorp/packer/builder/osc/bsusurrogate 0.043s ok github.com/hashicorp/packer/builder/osc/bsuvolume 0.048s ok github.com/hashicorp/packer/builder/osc/chroot 0.035s ok github.com/hashicorp/packer/builder/osc/common 0.030s ok github.com/hashicorp/packer/builder/osc/common/retry 0.018s ? github.com/hashicorp/packer/builder/osc/version [no test files] ok github.com/hashicorp/packer/builder/parallels/common 1.546s ok github.com/hashicorp/packer/builder/parallels/iso 0.047s ok github.com/hashicorp/packer/builder/parallels/pvm 0.044s ? github.com/hashicorp/packer/builder/parallels/version [no test files] ok github.com/hashicorp/packer/builder/profitbricks 0.046s ? github.com/hashicorp/packer/builder/profitbricks/version [no test files] ? github.com/hashicorp/packer/builder/proxmox [no test files] ? github.com/hashicorp/packer/builder/proxmox/clone [no test files] ok github.com/hashicorp/packer/builder/proxmox/common 0.070s ok github.com/hashicorp/packer/builder/proxmox/iso 0.072s ? github.com/hashicorp/packer/builder/proxmox/version [no test files] ok github.com/hashicorp/packer/builder/qemu 0.088s ? github.com/hashicorp/packer/builder/qemu/version [no test files] ok github.com/hashicorp/packer/builder/scaleway 0.062s ? github.com/hashicorp/packer/builder/scaleway/version [no test files] ok github.com/hashicorp/packer/builder/tencentcloud/cvm 0.037s ? github.com/hashicorp/packer/builder/tencentcloud/version [no test files] ok github.com/hashicorp/packer/builder/triton 0.057s ? github.com/hashicorp/packer/builder/triton/version [no test files] ok github.com/hashicorp/packer/builder/ucloud/common 0.039s ok github.com/hashicorp/packer/builder/ucloud/uhost 0.045s ? github.com/hashicorp/packer/builder/ucloud/version [no test files] ok github.com/hashicorp/packer/builder/vagrant 0.046s ? github.com/hashicorp/packer/builder/vagrant/version [no test files] ok github.com/hashicorp/packer/builder/virtualbox/common 2.546s ok github.com/hashicorp/packer/builder/virtualbox/iso 0.053s ? github.com/hashicorp/packer/builder/virtualbox/iso/acceptance [no test files] ok github.com/hashicorp/packer/builder/virtualbox/ovf 0.043s ? github.com/hashicorp/packer/builder/virtualbox/version [no test files] ? github.com/hashicorp/packer/builder/virtualbox/vm [no test files] ok github.com/hashicorp/packer/builder/vmware/common 5.228s ok github.com/hashicorp/packer/builder/vmware/iso 0.104s ? github.com/hashicorp/packer/builder/vmware/version [no test files] ok github.com/hashicorp/packer/builder/vmware/vmx 0.055s ok github.com/hashicorp/packer/builder/vsphere/clone 0.072s ok github.com/hashicorp/packer/builder/vsphere/common 0.037s ? github.com/hashicorp/packer/builder/vsphere/common/testing [no test files] ok github.com/hashicorp/packer/builder/vsphere/driver 0.443s ? github.com/hashicorp/packer/builder/vsphere/examples/driver [no test files] ok github.com/hashicorp/packer/builder/vsphere/iso 0.063s ? github.com/hashicorp/packer/builder/vsphere/version [no test files] ok github.com/hashicorp/packer/builder/yandex 0.098s ? github.com/hashicorp/packer/builder/yandex/version [no test files] ? github.com/hashicorp/packer/cmd/generate-fixer-deprecations [no test files] ? github.com/hashicorp/packer/cmd/mapstructure-to-hcl2 [no test files] ? github.com/hashicorp/packer/cmd/snippet-extractor [no test files] ? github.com/hashicorp/packer/cmd/ssh-keygen [no test files] ? github.com/hashicorp/packer/cmd/struct-markdown [no test files] ok github.com/hashicorp/packer/command 3.717s ? github.com/hashicorp/packer/command/enumflag [no test files] ok github.com/hashicorp/packer/command/flag-kv 0.019s ok github.com/hashicorp/packer/command/flag-slice 0.018s ok github.com/hashicorp/packer/fix 0.026s ok github.com/hashicorp/packer/hcl2template 0.281s ? github.com/hashicorp/packer/hcl2template/addrs [no test files] ok github.com/hashicorp/packer/hcl2template/function 0.028s ? github.com/hashicorp/packer/hcl2template/internal [no test files] ? github.com/hashicorp/packer/hcl2template/repl [no test files] ok github.com/hashicorp/packer/hcl2template/shim 0.019s ok github.com/hashicorp/packer/helper/builder/testing 0.027s ok github.com/hashicorp/packer/helper/communicator 0.034s ok github.com/hashicorp/packer/helper/communicator/ssh 4.204s ok github.com/hashicorp/packer/helper/communicator/sshkey 2.744s ? github.com/hashicorp/packer/helper/tests [no test files] ? github.com/hashicorp/packer/helper/tests/acc [no test files] ? github.com/hashicorp/packer/helper/wrappedreadline [no test files] ? github.com/hashicorp/packer/helper/wrappedstreams [no test files] ok github.com/hashicorp/packer/packer 0.221s ok github.com/hashicorp/packer/packer/plugin 0.417s ok github.com/hashicorp/packer/packer/rpc 0.059s ok github.com/hashicorp/packer/packer-plugin-sdk/adapter 0.063s ok github.com/hashicorp/packer/packer-plugin-sdk/bootcommand 2.778s ok github.com/hashicorp/packer/packer-plugin-sdk/chroot 0.038s ? github.com/hashicorp/packer/packer-plugin-sdk/common [no test files] ? github.com/hashicorp/packer/packer-plugin-sdk/filelock [no test files] ok github.com/hashicorp/packer/packer-plugin-sdk/guestexec 0.029s ok github.com/hashicorp/packer/packer-plugin-sdk/iochan 0.019s ok github.com/hashicorp/packer/packer-plugin-sdk/json 0.017s [no tests to run] ok github.com/hashicorp/packer/packer-plugin-sdk/multistep 0.126s ok github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps 0.136s ok github.com/hashicorp/packer/packer-plugin-sdk/net 1.041s ok github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata 0.018s ? github.com/hashicorp/packer/packer-plugin-sdk/random [no test files] ok github.com/hashicorp/packer/packer-plugin-sdk/retry 0.021s ok github.com/hashicorp/packer/packer-plugin-sdk/sdk-internals/communicator/none 0.026s ok github.com/hashicorp/packer/packer-plugin-sdk/sdk-internals/communicator/ssh 0.095s ok github.com/hashicorp/packer/packer-plugin-sdk/sdk-internals/communicator/winrm 0.062s ok github.com/hashicorp/packer/packer-plugin-sdk/shell 0.023s ok github.com/hashicorp/packer/packer-plugin-sdk/shell-local 0.045s ok github.com/hashicorp/packer/packer-plugin-sdk/shell-local/localexec 0.031s ok github.com/hashicorp/packer/packer-plugin-sdk/shutdowncommand 0.032s ok github.com/hashicorp/packer/packer-plugin-sdk/template 0.041s ok github.com/hashicorp/packer/packer-plugin-sdk/template/config 0.033s ok github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate 0.032s ok github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate/aws/secretsmanager 0.030s ? github.com/hashicorp/packer/packer-plugin-sdk/tmp [no test files] ok github.com/hashicorp/packer/packer-plugin-sdk/useragent 0.018s ok github.com/hashicorp/packer/packer-plugin-sdk/uuid 0.019s ? github.com/hashicorp/packer/packer-plugin-sdk/version [no test files] ok github.com/hashicorp/packer/plugin/example 0.040s [no tests to run] ? github.com/hashicorp/packer/post-processor/alicloud-import [no test files] ? github.com/hashicorp/packer/post-processor/alicloud-import/version [no test files] ? github.com/hashicorp/packer/post-processor/amazon-import [no test files] ? github.com/hashicorp/packer/post-processor/amazon-import/version [no test files] ? github.com/hashicorp/packer/post-processor/artifice [no test files] ? github.com/hashicorp/packer/post-processor/artifice/version [no test files] ok github.com/hashicorp/packer/post-processor/checksum 0.032s ? github.com/hashicorp/packer/post-processor/checksum/version [no test files] ok github.com/hashicorp/packer/post-processor/compress 0.046s ? github.com/hashicorp/packer/post-processor/compress/version [no test files] ok github.com/hashicorp/packer/post-processor/digitalocean-import 0.038s ? github.com/hashicorp/packer/post-processor/digitalocean-import/version [no test files] ok github.com/hashicorp/packer/post-processor/docker-import 0.036s ? github.com/hashicorp/packer/post-processor/docker-import/version [no test files] ok github.com/hashicorp/packer/post-processor/docker-push 0.037s ? github.com/hashicorp/packer/post-processor/docker-push/version [no test files] ok github.com/hashicorp/packer/post-processor/docker-save 0.038s ? github.com/hashicorp/packer/post-processor/docker-save/version [no test files] ok github.com/hashicorp/packer/post-processor/docker-tag 0.042s ? github.com/hashicorp/packer/post-processor/docker-tag/version [no test files] ? github.com/hashicorp/packer/post-processor/exoscale-import [no test files] ? github.com/hashicorp/packer/post-processor/exoscale-import/version [no test files] ok github.com/hashicorp/packer/post-processor/googlecompute-export 0.044s [no tests to run] ? github.com/hashicorp/packer/post-processor/googlecompute-export/version [no test files] ok github.com/hashicorp/packer/post-processor/googlecompute-import 0.035s ? github.com/hashicorp/packer/post-processor/googlecompute-import/version [no test files] ? github.com/hashicorp/packer/post-processor/manifest [no test files] ? github.com/hashicorp/packer/post-processor/manifest/version [no test files] ok github.com/hashicorp/packer/post-processor/shell-local 0.047s ? github.com/hashicorp/packer/post-processor/shell-local/version [no test files] ? github.com/hashicorp/packer/post-processor/ucloud-import [no test files] ? github.com/hashicorp/packer/post-processor/ucloud-import/version [no test files] ok github.com/hashicorp/packer/post-processor/vagrant 0.045s ? github.com/hashicorp/packer/post-processor/vagrant/version [no test files] ok github.com/hashicorp/packer/post-processor/vagrant-cloud 0.089s ? github.com/hashicorp/packer/post-processor/vagrant-cloud/version [no test files] ok github.com/hashicorp/packer/post-processor/vsphere 0.038s ? github.com/hashicorp/packer/post-processor/vsphere/version [no test files] ok github.com/hashicorp/packer/post-processor/vsphere-template 0.047s ? github.com/hashicorp/packer/post-processor/vsphere-template/version [no test files] ok github.com/hashicorp/packer/post-processor/yandex-export 0.055s ? github.com/hashicorp/packer/post-processor/yandex-export/version [no test files] ok github.com/hashicorp/packer/post-processor/yandex-import 0.052s ? github.com/hashicorp/packer/post-processor/yandex-import/version [no test files] ok github.com/hashicorp/packer/provisioner/ansible 0.469s ? github.com/hashicorp/packer/provisioner/ansible/version [no test files] ok github.com/hashicorp/packer/provisioner/ansible-local 0.055s ? github.com/hashicorp/packer/provisioner/ansible-local/version [no test files] ? github.com/hashicorp/packer/provisioner/azure-dtlartifact [no test files] ? github.com/hashicorp/packer/provisioner/azure-dtlartifact/version [no test files] ? github.com/hashicorp/packer/provisioner/breakpoint [no test files] ? github.com/hashicorp/packer/provisioner/breakpoint/version [no test files] ok github.com/hashicorp/packer/provisioner/chef-client 0.056s ? github.com/hashicorp/packer/provisioner/chef-client/version [no test files] ok github.com/hashicorp/packer/provisioner/chef-solo 0.045s ? github.com/hashicorp/packer/provisioner/chef-solo/version [no test files] ok github.com/hashicorp/packer/provisioner/converge 0.033s ? github.com/hashicorp/packer/provisioner/converge/version [no test files] ok github.com/hashicorp/packer/provisioner/file 0.051s ? github.com/hashicorp/packer/provisioner/file/version [no test files] ok github.com/hashicorp/packer/provisioner/inspec 0.050s ? github.com/hashicorp/packer/provisioner/inspec/version [no test files] ok github.com/hashicorp/packer/provisioner/powershell 2.095s ? github.com/hashicorp/packer/provisioner/powershell/version [no test files] ok github.com/hashicorp/packer/provisioner/puppet-masterless 0.049s ? github.com/hashicorp/packer/provisioner/puppet-masterless/version [no test files] ok github.com/hashicorp/packer/provisioner/puppet-server 0.043s ? github.com/hashicorp/packer/provisioner/puppet-server/version [no test files] ok github.com/hashicorp/packer/provisioner/salt-masterless 0.054s ? github.com/hashicorp/packer/provisioner/salt-masterless/version [no test files] ok github.com/hashicorp/packer/provisioner/shell 0.085s ? github.com/hashicorp/packer/provisioner/shell/version [no test files] ok github.com/hashicorp/packer/provisioner/shell-local 0.071s ? github.com/hashicorp/packer/provisioner/shell-local/version [no test files] ok github.com/hashicorp/packer/provisioner/sleep 0.027s ? github.com/hashicorp/packer/provisioner/sleep/version [no test files] ok github.com/hashicorp/packer/provisioner/windows-restart 0.051s ? github.com/hashicorp/packer/provisioner/windows-restart/version [no test files] ok github.com/hashicorp/packer/provisioner/windows-shell 0.039s ? github.com/hashicorp/packer/provisioner/windows-shell/version [no test files] ? github.com/hashicorp/packer/scripts [no test files] ? github.com/hashicorp/packer/version [no test files]
2020-12-01 09:50:36 -05:00
func (d *driverGCE) AddToInstanceMetadata(zone string, name string, metadata map[string]string) (<-chan error, error) {
instance, err := d.service.Instances.Get(d.projectId, zone, name).Do()
if err != nil {
return nil, err
}
// Build up the metadata
metadataForInstance := make([]*compute.MetadataItems, len(metadata))
for k, v := range metadata {
vCopy := v
metadataForInstance = append(metadataForInstance, &compute.MetadataItems{
Key: k,
Value: &vCopy,
})
}
instance.Metadata.Items = append(instance.Metadata.Items, metadataForInstance...)
op, err := d.service.Instances.SetMetadata(d.projectId, zone, name, &compute.Metadata{
Fingerprint: instance.Metadata.Fingerprint,
Items: instance.Metadata.Items,
}).Do()
if err != nil {
return nil, err
}
newErrCh := make(chan error, 1)
go waitForState(newErrCh, "DONE", d.refreshZoneOp(zone, op))
select {
case err = <-newErrCh:
case <-time.After(time.Second * 30):
err = errors.New("time out while waiting for SSH keys to be added to instance")
}
return newErrCh, err
}