2013-12-13 00:38:34 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2019-04-08 11:57:27 -04:00
|
|
|
"context"
|
2016-09-25 22:46:52 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2013-12-13 00:38:34 -05:00
|
|
|
"fmt"
|
2013-12-13 00:52:20 -05:00
|
|
|
"log"
|
2013-12-13 00:38:34 -05:00
|
|
|
"net/http"
|
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"
|
2016-09-25 22:46:52 -04:00
|
|
|
"time"
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2018-04-27 14:24:08 -04:00
|
|
|
compute "google.golang.org/api/compute/v1"
|
2016-12-02 16:22:36 -05:00
|
|
|
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2018-04-04 22:24:21 -04:00
|
|
|
"github.com/hashicorp/packer/helper/useragent"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-12-21 14:22:24 -05:00
|
|
|
|
2015-04-17 15:12:39 -04:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"golang.org/x/oauth2/jwt"
|
2013-12-13 00:38:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// driverGCE is a Driver implementation that actually talks to GCE.
|
|
|
|
// Create an instance using NewDriverGCE.
|
|
|
|
type driverGCE struct {
|
|
|
|
projectId string
|
|
|
|
service *compute.Service
|
|
|
|
ui packer.Ui
|
|
|
|
}
|
|
|
|
|
2014-11-17 13:06:22 -05:00
|
|
|
var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"}
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
func NewClientGCE(a *AccountFile) (*http.Client, error) {
|
2014-11-17 13:06:22 -05:00
|
|
|
var err error
|
|
|
|
|
2015-04-17 15:12:39 -04:00
|
|
|
var client *http.Client
|
|
|
|
|
2014-11-17 13:06:22 -05:00
|
|
|
// Auth with AccountFile first if provided
|
|
|
|
if a.PrivateKey != "" {
|
|
|
|
log.Printf("[INFO] Requesting Google token via AccountFile...")
|
|
|
|
log.Printf("[INFO] -- Email: %s", a.ClientEmail)
|
|
|
|
log.Printf("[INFO] -- Scopes: %s", DriverScopes)
|
|
|
|
log.Printf("[INFO] -- Private Key Length: %d", len(a.PrivateKey))
|
|
|
|
|
2015-04-17 15:12:39 -04:00
|
|
|
conf := jwt.Config{
|
|
|
|
Email: a.ClientEmail,
|
|
|
|
PrivateKey: []byte(a.PrivateKey),
|
|
|
|
Scopes: DriverScopes,
|
|
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate an http.Client. The following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of
|
|
|
|
// your service account.
|
|
|
|
client = conf.Client(oauth2.NoContext)
|
2014-11-17 13:06:22 -05:00
|
|
|
} else {
|
2016-08-26 21:12:55 -04:00
|
|
|
log.Printf("[INFO] Requesting Google token via GCE API Default Client Token Source...")
|
|
|
|
client, err = google.DefaultClient(oauth2.NoContext, DriverScopes...)
|
|
|
|
// 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.)
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 13:06:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDriverGCE(ui packer.Ui, p string, a *AccountFile) (Driver, error) {
|
|
|
|
client, err := NewClientGCE(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-17 15:12:39 -04:00
|
|
|
log.Printf("[INFO] Instantiating GCE client...")
|
|
|
|
service, err := compute.New(client)
|
2013-12-13 00:38:34 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-04 22:24:21 -04:00
|
|
|
// Set UserAgent
|
|
|
|
service.UserAgent = useragent.String()
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
return &driverGCE{
|
2014-09-05 12:47:20 -04:00
|
|
|
projectId: p,
|
2013-12-13 00:38:34 -05:00
|
|
|
service: service,
|
|
|
|
ui: ui,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:51:39 -05:00
|
|
|
func (d *driverGCE) CreateImage(name, description, family, zone, disk string, image_labels map[string]string, image_licenses []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{
|
2013-12-13 22:03:10 -05:00
|
|
|
Description: description,
|
|
|
|
Name: name,
|
2016-05-06 07:43:39 -04:00
|
|
|
Family: family,
|
2017-09-18 13:13:21 -04:00
|
|
|
Labels: image_labels,
|
2017-12-18 15:51:39 -05:00
|
|
|
Licenses: image_licenses,
|
2014-11-24 11:36:14 -05:00
|
|
|
SourceDisk: fmt.Sprintf("%s%s/zones/%s/disks/%s", d.service.BasePath, d.projectId, zone, disk),
|
|
|
|
SourceType: "RAW",
|
2013-12-13 22:03:10 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2013-12-13 22:03:10 -05:00
|
|
|
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()
|
2013-12-13 22:03:10 -05:00
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else {
|
2016-05-24 20:13:36 -04:00
|
|
|
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
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
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
|
2016-11-13 10:53:45 -05:00
|
|
|
image, err = d.GetImageFromProject(d.projectId, name, false)
|
2016-05-24 20:13:36 -04:00
|
|
|
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
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
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
|
2016-05-24 20:13:36 -04:00
|
|
|
close(imageCh)
|
|
|
|
}()
|
2013-12-13 22:03:10 -05:00
|
|
|
}
|
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
return imageCh, errCh
|
2013-12-13 22:03:10 -05:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:07:10 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
func (d *driverGCE) GetImage(name string, fromFamily bool) (*Image, error) {
|
2018-08-31 16:13:19 -04:00
|
|
|
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",
|
|
|
|
"ubuntu-os-cloud",
|
|
|
|
"windows-cloud",
|
|
|
|
"windows-sql-cloud",
|
|
|
|
"gce-uefi-images",
|
|
|
|
"gce-nvme",
|
|
|
|
// misc
|
|
|
|
"google-containers",
|
|
|
|
"opensuse-cloud",
|
|
|
|
}
|
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 {
|
2016-11-13 10:53:45 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
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{
|
2016-03-11 03:51:12 -05:00
|
|
|
Licenses: image.Licenses,
|
|
|
|
Name: image.Name,
|
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
|
|
|
ProjectId: project,
|
2016-03-11 03:51:12 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:01:28 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
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
|
|
|
|
}
|
2016-03-11 03:51:12 -05:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
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 {
|
2016-11-13 10:53:45 -05:00
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
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
|
|
|
|
|
2017-11-07 00:07:56 -05:00
|
|
|
networkId, subnetworkId, err := getNetworking(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-10 23:31:46 -05:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:43:04 -04:00
|
|
|
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",
|
|
|
|
}
|
2015-12-24 10:19:29 -05:00
|
|
|
|
2016-08-02 16:43:04 -04:00
|
|
|
// 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
|
2015-12-24 10:19:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// Build up the metadata
|
|
|
|
metadata := make([]*compute.MetadataItems, len(c.Metadata))
|
|
|
|
for k, v := range c.Metadata {
|
2016-02-10 23:31:46 -05:00
|
|
|
vCopy := v
|
2013-12-13 00:38:34 -05:00
|
|
|
metadata = append(metadata, &compute.MetadataItems{
|
|
|
|
Key: k,
|
2016-02-10 23:31:46 -05:00
|
|
|
Value: &vCopy,
|
2013-12-13 00:38:34 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-17 10:09:34 -04:00
|
|
|
var guestAccelerators []*compute.AcceleratorConfig
|
|
|
|
if c.AcceleratorCount > 0 {
|
|
|
|
ac := &compute.AcceleratorConfig{
|
|
|
|
AcceleratorCount: c.AcceleratorCount,
|
|
|
|
AcceleratorType: c.AcceleratorType,
|
|
|
|
}
|
|
|
|
guestAccelerators = append(guestAccelerators, ac)
|
|
|
|
}
|
|
|
|
|
2018-03-07 18:35:01 -05:00
|
|
|
// 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
|
2018-02-21 12:22:39 -05:00
|
|
|
}
|
|
|
|
if c.ServiceAccountEmail != "" {
|
|
|
|
serviceAccount.Email = c.ServiceAccountEmail
|
2018-03-07 18:35:01 -05:00
|
|
|
serviceAccount.Scopes = c.Scopes
|
2018-02-21 12:22:39 -05:00
|
|
|
}
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// Create the instance information
|
|
|
|
instance := compute.Instance{
|
|
|
|
Description: c.Description,
|
2014-04-03 18:18:58 -04:00
|
|
|
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,
|
2014-11-24 11:36:14 -05:00
|
|
|
AutoDelete: false,
|
2014-04-03 18:18:58 -04:00
|
|
|
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,
|
2014-08-07 15:34:08 -04:00
|
|
|
DiskSizeGb: c.DiskSizeGb,
|
2015-10-13 20:18:26 -04:00
|
|
|
DiskType: fmt.Sprintf("zones/%s/diskTypes/%s", zone.Name, c.DiskType),
|
2014-04-03 18:18:58 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-17 10:09:34 -04:00
|
|
|
GuestAccelerators: guestAccelerators,
|
2017-09-06 04:58:08 -04:00
|
|
|
Labels: c.Labels,
|
2017-07-17 10:09:34 -04:00
|
|
|
MachineType: machineType.SelfLink,
|
2013-12-13 00:38:34 -05:00
|
|
|
Metadata: &compute.Metadata{
|
|
|
|
Items: metadata,
|
|
|
|
},
|
2018-08-21 04:09:30 -04:00
|
|
|
MinCpuPlatform: c.MinCpuPlatform,
|
|
|
|
Name: c.Name,
|
2013-12-13 00:38:34 -05:00
|
|
|
NetworkInterfaces: []*compute.NetworkInterface{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2016-08-02 16:43:04 -04:00
|
|
|
AccessConfigs: []*compute.AccessConfig{accessconfig},
|
2017-09-13 17:15:39 -04:00
|
|
|
Network: networkId,
|
|
|
|
Subnetwork: subnetworkId,
|
2013-12-13 00:38:34 -05:00
|
|
|
},
|
|
|
|
},
|
2015-12-04 15:13:35 -05:00
|
|
|
Scheduling: &compute.Scheduling{
|
2017-02-10 13:58:57 -05:00
|
|
|
OnHostMaintenance: c.OnHostMaintenance,
|
|
|
|
Preemptible: c.Preemptible,
|
2015-12-04 15:13:35 -05:00
|
|
|
},
|
2013-12-13 00:38:34 -05:00
|
|
|
ServiceAccounts: []*compute.ServiceAccount{
|
2018-02-21 12:22:39 -05:00
|
|
|
serviceAccount,
|
2013-12-13 00:38:34 -05:00
|
|
|
},
|
|
|
|
Tags: &compute.Tags{
|
|
|
|
Items: c.Tags,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
d.ui.Message("Requesting instance creation...")
|
|
|
|
op, err := d.service.Instances.Insert(d.projectId, zone.Name, &instance).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error, 1)
|
2013-12-13 01:23:00 -05:00
|
|
|
go waitForState(errCh, "DONE", d.refreshZoneOp(zone.Name, op))
|
2013-12-13 00:38:34 -05:00
|
|
|
return errCh, nil
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:46:52 -04:00
|
|
|
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()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:01:28 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 22:03:10 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:23:00 -05:00
|
|
|
func (d *driverGCE) refreshZoneOp(zone string, op *compute.Operation) stateRefreshFunc {
|
2013-12-13 00:38:34 -05:00
|
|
|
return func() (string, error) {
|
2013-12-13 01:23:00 -05:00
|
|
|
newOp, err := d.service.ZoneOperations.Get(d.projectId, zone, op.Name).Do()
|
2013-12-13 00:38:34 -05:00
|
|
|
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.
|
2016-05-24 20:13:36 -04:00
|
|
|
func waitForState(errCh chan<- error, target string, refresh stateRefreshFunc) error {
|
2019-04-08 11:57:27 -04:00
|
|
|
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 {
|
2013-12-13 00:38:34 -05:00
|
|
|
state, err := refresh()
|
|
|
|
if err != nil {
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
2019-04-08 11:57:27 -04:00
|
|
|
if state == target {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("retrying for state %s, got %s", target, state)
|
2016-05-24 20:13:36 -04:00
|
|
|
})
|
|
|
|
errCh <- err
|
|
|
|
return err
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|