2013-12-13 00:38:34 -05:00
|
|
|
package googlecompute
|
|
|
|
|
2019-04-23 14:49:41 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
compute "google.golang.org/api/compute/v1"
|
2020-07-08 06:30:25 -04:00
|
|
|
oslogin "google.golang.org/api/oslogin/v1"
|
2019-04-23 14:49:41 -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
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// DriverMock is a Driver implementation that is a mocked out so that
|
|
|
|
// it can be used for tests.
|
|
|
|
type DriverMock struct {
|
2020-06-02 13:42:33 -04:00
|
|
|
CreateImageName string
|
|
|
|
CreateImageDesc string
|
|
|
|
CreateImageFamily string
|
|
|
|
CreateImageEncryptionKey *compute.CustomerEncryptionKey
|
|
|
|
CreateImageLabels map[string]string
|
|
|
|
CreateImageLicenses []string
|
|
|
|
CreateImageStorageLocations []string
|
|
|
|
CreateImageZone string
|
|
|
|
CreateImageDisk string
|
|
|
|
CreateImageResultProjectId string
|
|
|
|
CreateImageResultSelfLink string
|
|
|
|
CreateImageResultSizeGb int64
|
|
|
|
CreateImageErrCh <-chan error
|
|
|
|
CreateImageResultCh <-chan *Image
|
2013-12-13 22:03:10 -05:00
|
|
|
|
2013-12-13 22:07:10 -05:00
|
|
|
DeleteImageName string
|
|
|
|
DeleteImageErrCh <-chan error
|
|
|
|
|
2013-12-13 01:34:47 -05:00
|
|
|
DeleteInstanceZone string
|
|
|
|
DeleteInstanceName string
|
|
|
|
DeleteInstanceErrCh <-chan error
|
|
|
|
DeleteInstanceErr error
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
DeleteDiskZone string
|
|
|
|
DeleteDiskName string
|
|
|
|
DeleteDiskErrCh <-chan error
|
|
|
|
DeleteDiskErr error
|
|
|
|
|
2020-02-06 04:27:01 -05:00
|
|
|
GetImageName string
|
|
|
|
GetImageSourceProjects []string
|
|
|
|
GetImageFromFamily bool
|
|
|
|
GetImageResult *Image
|
|
|
|
GetImageErr 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
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
GetImageFromProjectProject string
|
|
|
|
GetImageFromProjectName string
|
|
|
|
GetImageFromProjectFromFamily bool
|
|
|
|
GetImageFromProjectResult *Image
|
|
|
|
GetImageFromProjectErr 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
|
|
|
|
|
|
|
GetInstanceMetadataZone string
|
|
|
|
GetInstanceMetadataName string
|
|
|
|
GetInstanceMetadataKey string
|
|
|
|
GetInstanceMetadataResult string
|
|
|
|
GetInstanceMetadataErr error
|
|
|
|
|
2013-12-13 16:01:28 -05:00
|
|
|
GetNatIPZone string
|
|
|
|
GetNatIPName string
|
|
|
|
GetNatIPResult string
|
|
|
|
GetNatIPErr error
|
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
GetInternalIPZone string
|
|
|
|
GetInternalIPName string
|
|
|
|
GetInternalIPResult string
|
|
|
|
GetInternalIPErr error
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
GetSerialPortOutputZone string
|
|
|
|
GetSerialPortOutputName string
|
|
|
|
GetSerialPortOutputResult string
|
|
|
|
GetSerialPortOutputErr error
|
2015-05-29 17:50:11 -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
|
|
|
ImageExistsName string
|
|
|
|
ImageExistsResult bool
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
RunInstanceConfig *InstanceConfig
|
|
|
|
RunInstanceErrCh <-chan error
|
|
|
|
RunInstanceErr error
|
2013-12-13 16:01:28 -05:00
|
|
|
|
2016-09-25 22:46:52 -04:00
|
|
|
CreateOrResetWindowsPasswordZone string
|
|
|
|
CreateOrResetWindowsPasswordInstance string
|
|
|
|
CreateOrResetWindowsPasswordConfig *WindowsPasswordConfig
|
|
|
|
CreateOrResetWindowsPasswordErr error
|
|
|
|
CreateOrResetWindowsPasswordErrCh <-chan error
|
|
|
|
|
2013-12-13 16:01:28 -05:00
|
|
|
WaitForInstanceState string
|
|
|
|
WaitForInstanceZone string
|
|
|
|
WaitForInstanceName string
|
|
|
|
WaitForInstanceErrCh <-chan error
|
2020-12-01 09:50:36 -05:00
|
|
|
|
|
|
|
AddToInstanceMetadataZone string
|
|
|
|
AddToInstanceMetadataName string
|
|
|
|
AddToInstanceMetadataKVPairs map[string]string
|
|
|
|
AddToInstanceMetadataErrCh <-chan error
|
2020-12-02 06:16:16 -05:00
|
|
|
AddToInstanceMetadataErr error
|
2020-12-09 09:01:51 -05:00
|
|
|
|
|
|
|
OSLoginUserFromGCE string
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:42:33 -04:00
|
|
|
func (d *DriverMock) 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) {
|
2019-05-05 11:12:30 -04:00
|
|
|
d.CreateImageName = name
|
|
|
|
d.CreateImageDesc = description
|
|
|
|
d.CreateImageFamily = family
|
|
|
|
d.CreateImageLabels = image_labels
|
|
|
|
d.CreateImageLicenses = image_licenses
|
2020-06-02 13:42:33 -04:00
|
|
|
d.CreateImageStorageLocations = imageStorageLocations
|
2019-05-05 11:12:30 -04:00
|
|
|
d.CreateImageZone = zone
|
|
|
|
d.CreateImageDisk = disk
|
|
|
|
d.CreateImageEncryptionKey = image_encryption_key
|
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 d.CreateImageResultProjectId == "" {
|
|
|
|
d.CreateImageResultProjectId = "test"
|
|
|
|
}
|
|
|
|
if d.CreateImageResultSelfLink == "" {
|
|
|
|
d.CreateImageResultSelfLink = fmt.Sprintf(
|
|
|
|
"http://content.googleapis.com/compute/v1/%s/global/licenses/test",
|
|
|
|
d.CreateImageResultProjectId)
|
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
|
|
|
if d.CreateImageResultSizeGb == 0 {
|
|
|
|
d.CreateImageResultSizeGb = 10
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
2013-12-13 22:03:10 -05:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
resultCh := d.CreateImageResultCh
|
2013-12-13 22:03:10 -05:00
|
|
|
if resultCh == 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
|
|
|
ch := make(chan *Image, 1)
|
|
|
|
ch <- &Image{
|
2017-09-19 06:40:01 -04:00
|
|
|
Labels: d.CreateImageLabels,
|
2017-12-18 15:51:39 -05:00
|
|
|
Licenses: d.CreateImageLicenses,
|
2019-05-05 11:12:30 -04:00
|
|
|
Name: 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: d.CreateImageResultProjectId,
|
|
|
|
SelfLink: d.CreateImageResultSelfLink,
|
|
|
|
SizeGb: d.CreateImageResultSizeGb,
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
2013-12-13 22:03:10 -05:00
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
errCh := d.CreateImageErrCh
|
|
|
|
if errCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
errCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh, errCh
|
2013-12-13 22:07:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) DeleteImage(name string) <-chan error {
|
|
|
|
d.DeleteImageName = name
|
|
|
|
|
|
|
|
resultCh := d.DeleteImageErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
2013-12-13 22:03:10 -05:00
|
|
|
return resultCh
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:34:47 -05:00
|
|
|
func (d *DriverMock) DeleteInstance(zone, name string) (<-chan error, error) {
|
|
|
|
d.DeleteInstanceZone = zone
|
|
|
|
d.DeleteInstanceName = name
|
|
|
|
|
|
|
|
resultCh := d.DeleteInstanceErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh, d.DeleteInstanceErr
|
|
|
|
}
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
func (d *DriverMock) DeleteDisk(zone, name string) (<-chan error, error) {
|
|
|
|
d.DeleteDiskZone = zone
|
|
|
|
d.DeleteDiskName = name
|
|
|
|
|
|
|
|
resultCh := d.DeleteDiskErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh, d.DeleteDiskErr
|
|
|
|
}
|
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
func (d *DriverMock) GetImage(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
|
|
|
d.GetImageName = name
|
2016-11-13 10:53:45 -05:00
|
|
|
d.GetImageFromFamily = 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
|
|
|
return d.GetImageResult, d.GetImageErr
|
|
|
|
}
|
2020-02-06 04:27:01 -05:00
|
|
|
func (d *DriverMock) GetImageFromProjects(projects []string, name string, fromFamily bool) (*Image, error) {
|
|
|
|
d.GetImageSourceProjects = projects
|
|
|
|
d.GetImageFromProjectName = name
|
|
|
|
d.GetImageFromProjectFromFamily = fromFamily
|
|
|
|
return d.GetImageFromProjectResult, d.GetImageFromProjectErr
|
|
|
|
}
|
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
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
func (d *DriverMock) GetImageFromProject(project, 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
|
|
|
d.GetImageFromProjectProject = project
|
|
|
|
d.GetImageFromProjectName = name
|
2016-11-13 10:53:45 -05:00
|
|
|
d.GetImageFromProjectFromFamily = 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
|
|
|
return d.GetImageFromProjectResult, d.GetImageFromProjectErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) GetInstanceMetadata(zone, name, key string) (string, error) {
|
|
|
|
d.GetInstanceMetadataZone = zone
|
|
|
|
d.GetInstanceMetadataName = name
|
|
|
|
d.GetInstanceMetadataKey = key
|
|
|
|
return d.GetInstanceMetadataResult, d.GetInstanceMetadataErr
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:01:28 -05:00
|
|
|
func (d *DriverMock) GetNatIP(zone, name string) (string, error) {
|
|
|
|
d.GetNatIPZone = zone
|
|
|
|
d.GetNatIPName = name
|
|
|
|
return d.GetNatIPResult, d.GetNatIPErr
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
func (d *DriverMock) GetInternalIP(zone, name string) (string, error) {
|
|
|
|
d.GetInternalIPZone = zone
|
|
|
|
d.GetInternalIPName = name
|
|
|
|
return d.GetInternalIPResult, d.GetInternalIPErr
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
func (d *DriverMock) GetSerialPortOutput(zone, name string) (string, error) {
|
|
|
|
d.GetSerialPortOutputZone = zone
|
|
|
|
d.GetSerialPortOutputName = name
|
|
|
|
return d.GetSerialPortOutputResult, d.GetSerialPortOutputErr
|
|
|
|
}
|
|
|
|
|
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 *DriverMock) ImageExists(name string) bool {
|
|
|
|
d.ImageExistsName = name
|
|
|
|
return d.ImageExistsResult
|
|
|
|
}
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
func (d *DriverMock) RunInstance(c *InstanceConfig) (<-chan error, error) {
|
|
|
|
d.RunInstanceConfig = c
|
|
|
|
|
|
|
|
resultCh := d.RunInstanceErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh, d.RunInstanceErr
|
|
|
|
}
|
2013-12-13 16:01:28 -05:00
|
|
|
|
|
|
|
func (d *DriverMock) WaitForInstance(state, zone, name string) <-chan error {
|
|
|
|
d.WaitForInstanceState = state
|
|
|
|
d.WaitForInstanceZone = zone
|
|
|
|
d.WaitForInstanceName = name
|
|
|
|
|
|
|
|
resultCh := d.WaitForInstanceErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh
|
|
|
|
}
|
2016-09-25 22:46:52 -04:00
|
|
|
|
|
|
|
func (d *DriverMock) GetWindowsPassword() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) CreateOrResetWindowsPassword(instance, zone string, c *WindowsPasswordConfig) (<-chan error, error) {
|
|
|
|
|
|
|
|
d.CreateOrResetWindowsPasswordInstance = instance
|
|
|
|
d.CreateOrResetWindowsPasswordZone = zone
|
|
|
|
d.CreateOrResetWindowsPasswordConfig = c
|
|
|
|
|
|
|
|
c.password = "MOCK_PASSWORD"
|
|
|
|
|
|
|
|
resultCh := d.CreateOrResetWindowsPasswordErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
resultCh = ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCh, d.CreateOrResetWindowsPasswordErr
|
|
|
|
}
|
2020-07-08 06:30:25 -04:00
|
|
|
|
|
|
|
func (d *DriverMock) ImportOSLoginSSHKey(user, key string) (*oslogin.LoginProfile, error) {
|
|
|
|
account := oslogin.PosixAccount{Primary: true, Username: "testing_packer_io"}
|
|
|
|
profile := oslogin.LoginProfile{
|
|
|
|
PosixAccounts: []*oslogin.PosixAccount{&account},
|
|
|
|
}
|
|
|
|
return &profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) DeleteOSLoginSSHKey(user, fingerprint string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-01 09:50:36 -05:00
|
|
|
|
2020-12-02 08:55:00 -05:00
|
|
|
func (d *DriverMock) AddToInstanceMetadata(zone string, name string, metadata map[string]string) error {
|
2020-12-02 06:16:16 -05:00
|
|
|
d.AddToInstanceMetadataZone = zone
|
|
|
|
d.AddToInstanceMetadataName = name
|
2020-12-01 09:50:36 -05:00
|
|
|
d.AddToInstanceMetadataKVPairs = metadata
|
|
|
|
|
|
|
|
resultCh := d.AddToInstanceMetadataErrCh
|
|
|
|
if resultCh == nil {
|
|
|
|
ch := make(chan error)
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
|
2020-12-02 08:55:00 -05:00
|
|
|
return nil
|
2020-12-02 06:16:16 -05:00
|
|
|
}
|
2020-12-09 09:01:51 -05:00
|
|
|
|
|
|
|
func (d *DriverMock) GetOSLoginUserFromGCE() string {
|
|
|
|
return d.OSLoginUserFromGCE
|
|
|
|
}
|