2018-03-29 23:50:58 -04:00
|
|
|
package googlecomputeimport
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2018-03-29 23:50:58 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-02 19:16:13 -04:00
|
|
|
"golang.org/x/oauth2/jwt"
|
2018-03-29 23:50:58 -04:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/storage/v1"
|
|
|
|
|
2019-01-22 12:34:01 -05:00
|
|
|
"github.com/hashicorp/packer/builder/googlecompute"
|
2018-03-29 23:50:58 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/post-processor/compress"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2019-01-21 18:09:29 -05:00
|
|
|
AccountFile string `mapstructure:"account_file"`
|
|
|
|
ProjectId string `mapstructure:"project_id"`
|
|
|
|
|
|
|
|
Bucket string `mapstructure:"bucket"`
|
|
|
|
GCSObjectName string `mapstructure:"gcs_object_name"`
|
|
|
|
ImageDescription string `mapstructure:"image_description"`
|
|
|
|
ImageFamily string `mapstructure:"image_family"`
|
|
|
|
ImageGuestOsFeatures []string `mapstructure:"image_guest_os_features"`
|
|
|
|
ImageLabels map[string]string `mapstructure:"image_labels"`
|
|
|
|
ImageName string `mapstructure:"image_name"`
|
|
|
|
SkipClean bool `mapstructure:"skip_clean"`
|
2019-09-20 19:52:35 -04:00
|
|
|
VaultGCPOauthEngine string `mapstructure:"vault_gcp_oauth_engine"`
|
2018-03-29 23:50:58 -04:00
|
|
|
|
2019-07-02 19:16:13 -04:00
|
|
|
Account *jwt.Config
|
2019-01-22 09:16:58 -05:00
|
|
|
ctx interpolate.Context
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"gcs_object_name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
errs := new(packer.MultiError)
|
|
|
|
|
2018-03-29 23:50:58 -04:00
|
|
|
// Set defaults
|
|
|
|
if p.config.GCSObjectName == "" {
|
|
|
|
p.config.GCSObjectName = "packer-import-{{timestamp}}.tar.gz"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check and render gcs_object_name
|
|
|
|
if err = interpolate.Validate(p.config.GCSObjectName, &p.config.ctx); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing gcs_object_name template: %s", err))
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
if p.config.AccountFile != "" {
|
2019-07-02 19:16:13 -04:00
|
|
|
cfg, err := googlecompute.ProcessAccountFile(p.config.AccountFile)
|
|
|
|
if err != nil {
|
2019-01-22 09:16:58 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
2019-07-02 19:16:13 -04:00
|
|
|
p.config.Account = cfg
|
2019-01-22 09:16:58 -05:00
|
|
|
}
|
|
|
|
|
2019-09-20 19:52:35 -04:00
|
|
|
if p.config.AccountFile != "" && p.config.VaultGCPOauthEngine != "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("May set either account_file or "+
|
|
|
|
"vault_gcp_oauth_engine, but not both."))
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:50:58 -04:00
|
|
|
templates := map[string]*string{
|
2019-01-22 09:16:58 -05:00
|
|
|
"bucket": &p.config.Bucket,
|
|
|
|
"image_name": &p.config.ImageName,
|
|
|
|
"project_id": &p.config.ProjectId,
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
for key, ptr := range templates {
|
|
|
|
if *ptr == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("%s must be set", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2019-09-20 19:52:35 -04:00
|
|
|
client, err := googlecompute.NewClientGCE(p.config.Account, p.config.VaultGCPOauthEngine)
|
2019-01-22 09:16:58 -05:00
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2019-01-22 09:16:58 -05:00
|
|
|
}
|
2018-03-29 23:50:58 -04:00
|
|
|
|
|
|
|
if artifact.BuilderId() != compress.BuilderId {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"incompatible artifact type: %s\nCan only import from Compress post-processor artifacts",
|
|
|
|
artifact.BuilderId())
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.config.GCSObjectName, err = interpolate.Render(p.config.GCSObjectName, &p.config.ctx)
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, fmt.Errorf("Error rendering gcs_object_name template: %s", err)
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
rawImageGcsPath, err := UploadToBucket(client, ui, artifact, p.config.Bucket, p.config.GCSObjectName)
|
2018-03-29 23:50:58 -04:00
|
|
|
if err != nil {
|
2019-04-03 15:05:38 -04:00
|
|
|
return nil, false, false, err
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
gceImageArtifact, err := CreateGceImage(client, ui, p.config.ProjectId, rawImageGcsPath, p.config.ImageName, p.config.ImageDescription, p.config.ImageFamily, p.config.ImageLabels, p.config.ImageGuestOsFeatures)
|
2018-03-29 23:50:58 -04:00
|
|
|
if err != nil {
|
2019-04-03 15:05:38 -04:00
|
|
|
return nil, false, false, err
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 22:44:30 -04:00
|
|
|
if !p.config.SkipClean {
|
2019-01-22 09:16:58 -05:00
|
|
|
err = DeleteFromBucket(client, ui, p.config.Bucket, p.config.GCSObjectName)
|
2018-07-02 22:44:30 -04:00
|
|
|
if err != nil {
|
2019-04-03 15:05:38 -04:00
|
|
|
return nil, false, false, err
|
2018-07-02 22:44:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:05:38 -04:00
|
|
|
return gceImageArtifact, false, false, nil
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
func UploadToBucket(client *http.Client, ui packer.Ui, artifact packer.Artifact, bucket string, gcsObjectName string) (string, error) {
|
2018-03-29 23:50:58 -04:00
|
|
|
service, err := storage.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Looking for tar.gz file in list of artifacts...")
|
|
|
|
source := ""
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
ui.Say(fmt.Sprintf("Found artifact %v...", path))
|
|
|
|
if strings.HasSuffix(path, ".tar.gz") {
|
|
|
|
source = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == "" {
|
2019-01-22 09:16:58 -05:00
|
|
|
return "", fmt.Errorf("No tar.gz file found in list of artifacts")
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
artifactFile, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error opening %v", source)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Uploading file %v to GCS bucket %v/%v...", source, bucket, gcsObjectName))
|
|
|
|
storageObject, err := service.Objects.Insert(bucket, &storage.Object{Name: gcsObjectName}).Media(artifactFile).Do()
|
|
|
|
if err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Failed to upload: %v", storageObject))
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
return storageObject.SelfLink, nil
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
func CreateGceImage(client *http.Client, ui packer.Ui, project string, rawImageURL string, imageName string, imageDescription string, imageFamily string, imageLabels map[string]string, imageGuestOsFeatures []string) (packer.Artifact, error) {
|
2018-03-29 23:50:58 -04:00
|
|
|
service, err := compute.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:09:29 -05:00
|
|
|
// Build up the imageFeatures
|
|
|
|
imageFeatures := make([]*compute.GuestOsFeature, len(imageGuestOsFeatures))
|
|
|
|
for _, v := range imageGuestOsFeatures {
|
|
|
|
imageFeatures = append(imageFeatures, &compute.GuestOsFeature{
|
|
|
|
Type: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:50:58 -04:00
|
|
|
gceImage := &compute.Image{
|
2019-01-21 18:09:29 -05:00
|
|
|
Description: imageDescription,
|
|
|
|
Family: imageFamily,
|
|
|
|
GuestOsFeatures: imageFeatures,
|
|
|
|
Labels: imageLabels,
|
|
|
|
Name: imageName,
|
|
|
|
RawDisk: &compute.ImageRawDisk{Source: rawImageURL},
|
|
|
|
SourceType: "RAW",
|
2018-03-29 23:50:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating GCE image %v...", imageName))
|
|
|
|
op, err := service.Images.Insert(project, gceImage).Do()
|
|
|
|
if err != nil {
|
|
|
|
ui.Say("Error creating GCE image")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for GCE image creation operation to complete...")
|
|
|
|
for op.Status != "DONE" {
|
|
|
|
op, err = service.GlobalOperations.Get(project, op.Name).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if image creation operation has an error
|
|
|
|
if op.Error != nil {
|
|
|
|
var imageError string
|
|
|
|
for _, error := range op.Error.Errors {
|
|
|
|
imageError += error.Message
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("failed to create GCE image %s: %s", imageName, imageError)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Artifact{paths: []string{op.TargetLink}}, nil
|
|
|
|
}
|
2018-07-02 22:44:30 -04:00
|
|
|
|
2019-01-22 09:16:58 -05:00
|
|
|
func DeleteFromBucket(client *http.Client, ui packer.Ui, bucket string, gcsObjectName string) error {
|
2018-07-02 22:44:30 -04:00
|
|
|
service, err := storage.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Deleting import source from GCS %s/%s...", bucket, gcsObjectName))
|
|
|
|
err = service.Objects.Delete(bucket, gcsObjectName).Do()
|
|
|
|
if err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Failed to delete: %v/%v", bucket, gcsObjectName))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|