mirror of https://github.com/hashicorp/packer
parent
2ef4210a98
commit
3622a669dc
@ -0,0 +1,3 @@
|
||||
* 1.9.6 => GNU tar format
|
||||
* 1.10.3 w/ patch => GNU tar format
|
||||
* 1.10.3 w/o patch => Posix tar format
|
||||
@ -0,0 +1,9 @@
|
||||
// +build !go1.10
|
||||
|
||||
package compress
|
||||
|
||||
import "archive/tar"
|
||||
|
||||
func setHeaderFormat(header *tar.Header) {
|
||||
// no-op
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
// +build go1.10
|
||||
|
||||
package compress
|
||||
|
||||
import "archive/tar"
|
||||
|
||||
func setHeaderFormat(header *tar.Header) {
|
||||
// We have to set the Format explicitly for the googlecompute-import
|
||||
// post-processor. Google Cloud only allows importing GNU tar format.
|
||||
header.Format = tar.FormatGNU
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package googlecomputeimport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const BuilderId = "packer.post-processor.googlecompute-import"
|
||||
|
||||
type Artifact struct {
|
||||
paths []string
|
||||
}
|
||||
|
||||
func (*Artifact) BuilderId() string {
|
||||
return BuilderId
|
||||
}
|
||||
|
||||
func (*Artifact) Id() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *Artifact) Files() []string {
|
||||
pathsCopy := make([]string, len(a.paths))
|
||||
copy(pathsCopy, a.paths)
|
||||
return pathsCopy
|
||||
}
|
||||
|
||||
func (a *Artifact) String() string {
|
||||
return fmt.Sprintf("Exported artifacts in: %s", a.paths)
|
||||
}
|
||||
|
||||
func (*Artifact) State(name string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Artifact) Destroy() error {
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package googlecomputeimport
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
func TestArtifact_ImplementsArtifact(t *testing.T) {
|
||||
var raw interface{}
|
||||
raw = &Artifact{}
|
||||
if _, ok := raw.(packer.Artifact); !ok {
|
||||
t.Fatalf("Artifact should be a Artifact")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,235 @@
|
||||
package googlecomputeimport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/api/compute/v1"
|
||||
"google.golang.org/api/storage/v1"
|
||||
|
||||
"github.com/hashicorp/packer/builder/googlecompute"
|
||||
"github.com/hashicorp/packer/common"
|
||||
"github.com/hashicorp/packer/helper/config"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/hashicorp/packer/post-processor/compress"
|
||||
"github.com/hashicorp/packer/template/interpolate"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/jwt"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
|
||||
Bucket string `mapstructure:"bucket"`
|
||||
GCSObjectName string `mapstructure:"gcs_object_name"`
|
||||
ImageDescription string `mapstructure:"image_description"`
|
||||
ImageFamily string `mapstructure:"image_family"`
|
||||
ImageLabels map[string]string `mapstructure:"image_labels"`
|
||||
ImageName string `mapstructure:"image_name"`
|
||||
ProjectId string `mapstructure:"project_id"`
|
||||
AccountFile string `mapstructure:"account_file"`
|
||||
KeepOriginalImage bool `mapstructure:"keep_input_artifact"`
|
||||
|
||||
ctx interpolate.Context
|
||||
}
|
||||
|
||||
type PostProcessor struct {
|
||||
config Config
|
||||
runner multistep.Runner
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if p.config.GCSObjectName == "" {
|
||||
p.config.GCSObjectName = "packer-import-{{timestamp}}.tar.gz"
|
||||
}
|
||||
|
||||
errs := new(packer.MultiError)
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
templates := map[string]*string{
|
||||
"bucket": &p.config.Bucket,
|
||||
"image_name": &p.config.ImageName,
|
||||
"project_id": &p.config.ProjectId,
|
||||
"account_file": &p.config.AccountFile,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
||||
var err error
|
||||
|
||||
if artifact.BuilderId() != compress.BuilderId {
|
||||
err = fmt.Errorf(
|
||||
"incompatible artifact type: %s\nCan only import from Compress post-processor artifacts",
|
||||
artifact.BuilderId())
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
p.config.GCSObjectName, err = interpolate.Render(p.config.GCSObjectName, &p.config.ctx)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("Error rendering gcs_object_name template: %s", err)
|
||||
}
|
||||
|
||||
rawImageGcsPath, err := UploadToBucket(p.config.AccountFile, ui, artifact, p.config.Bucket, p.config.GCSObjectName)
|
||||
if err != nil {
|
||||
return nil, p.config.KeepOriginalImage, err
|
||||
}
|
||||
|
||||
gceImageArtifact, err := CreateGceImage(p.config.AccountFile, ui, p.config.ProjectId, rawImageGcsPath, p.config.ImageName, p.config.ImageDescription, p.config.ImageFamily, p.config.ImageLabels)
|
||||
if err != nil {
|
||||
return nil, p.config.KeepOriginalImage, err
|
||||
}
|
||||
|
||||
return gceImageArtifact, p.config.KeepOriginalImage, nil
|
||||
}
|
||||
|
||||
func UploadToBucket(accountFile string, ui packer.Ui, artifact packer.Artifact, bucket string, gcsObjectName string) (string, error) {
|
||||
var client *http.Client
|
||||
var account googlecompute.AccountFile
|
||||
|
||||
err := googlecompute.ProcessAccountFile(&account, accountFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var DriverScopes = []string{"https://www.googleapis.com/auth/devstorage.full_control"}
|
||||
conf := jwt.Config{
|
||||
Email: account.ClientEmail,
|
||||
PrivateKey: []byte(account.PrivateKey),
|
||||
Scopes: DriverScopes,
|
||||
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
||||
}
|
||||
|
||||
client = conf.Client(oauth2.NoContext)
|
||||
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 == "" {
|
||||
return "", fmt.Errorf("No tar.gz file found in list of articats")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return "https://storage.googleapis.com/" + bucket + "/" + gcsObjectName, nil
|
||||
}
|
||||
|
||||
func CreateGceImage(accountFile string, ui packer.Ui, project string, rawImageURL string, imageName string, imageDescription string, imageFamily string, imageLabels map[string]string) (packer.Artifact, error) {
|
||||
var client *http.Client
|
||||
var account googlecompute.AccountFile
|
||||
|
||||
err := googlecompute.ProcessAccountFile(&account, accountFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"}
|
||||
conf := jwt.Config{
|
||||
Email: account.ClientEmail,
|
||||
PrivateKey: []byte(account.PrivateKey),
|
||||
Scopes: DriverScopes,
|
||||
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
||||
}
|
||||
|
||||
client = conf.Client(oauth2.NoContext)
|
||||
|
||||
service, err := compute.New(client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gceImage := &compute.Image{
|
||||
Name: imageName,
|
||||
Description: imageDescription,
|
||||
Family: imageFamily,
|
||||
Labels: imageLabels,
|
||||
RawDisk: &compute.ImageRawDisk{Source: rawImageURL},
|
||||
SourceType: "RAW",
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.8
|
||||
|
||||
package gensupport
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SetGetBody sets the GetBody field of req to f.
|
||||
func SetGetBody(req *http.Request, f func() (io.ReadCloser, error)) {
|
||||
req.GetBody = f
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !go1.8
|
||||
|
||||
package gensupport
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SetGetBody(req *http.Request, f func() (io.ReadCloser, error)) {}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,145 @@
|
||||
---
|
||||
description: |
|
||||
The Google Compute Image Import post-processor takes a compressed raw disk
|
||||
image and imports it to a GCE image available to Google Compute Engine.
|
||||
|
||||
layout: docs
|
||||
page_title: 'Google Compute Image Import - Post-Processors'
|
||||
sidebar_current: 'docs-post-processors-googlecompute-import'
|
||||
---
|
||||
|
||||
# Google Compute Image Import Post-Processor
|
||||
|
||||
Type: `googlecompute-import`
|
||||
|
||||
The Google Compute Image Import post-processor takes a compressed raw disk
|
||||
image and imports it to a GCE image available to Google Compute Engine.
|
||||
|
||||
~> This post-processor is for advanced users. Please ensure you read the [GCE import documentation](https://cloud.google.com/compute/docs/images/import-existing-image) before using this post-processor.
|
||||
|
||||
## How Does it Work?
|
||||
|
||||
The import process operates by uploading a temporary copy of the compressed raw disk image
|
||||
to a GCS bucket, and calling an import task in GCP on the raw disk file. Once completed, a
|
||||
GCE image is created containing the converted virtual machine. The temporary raw disk image
|
||||
copy in GCS can be discarded after the import is complete.
|
||||
|
||||
Google Cloud has very specific requirements for images being imported. Please see the
|
||||
[GCE import documentation](https://cloud.google.com/compute/docs/images/import-existing-image)
|
||||
for details.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Required
|
||||
|
||||
- `account_file` (string) - The JSON file containing your account credentials.
|
||||
|
||||
- `bucket` (string) - The name of the GCS bucket where the raw disk image
|
||||
will be uploaded.
|
||||
|
||||
- `image_name` (string) - The unique name of the resulting image.
|
||||
|
||||
- `project_id` (string) - The project ID where the GCS bucket exists and
|
||||
where the GCE image is stored.
|
||||
|
||||
### Optional
|
||||
|
||||
- `gcs_object_name` (string) - The name of the GCS object in `bucket` where the RAW disk image will be copied for import. Defaults to "packer-import-{{timestamp}}.tar.gz".
|
||||
|
||||
- `image_description` (string) - The description of the resulting image.
|
||||
|
||||
- `image_family` (string) - The name of the image family to which the resulting image belongs.
|
||||
|
||||
- `image_labels` (object of key/value strings) - Key/value pair labels to apply to the created image.
|
||||
|
||||
- `keep_input_artifact` (boolean) - if true, do not delete the compressed RAW disk image. Defaults to false.
|
||||
|
||||
|
||||
## Basic Example
|
||||
|
||||
Here is a basic example. This assumes that the builder has produced an compressed
|
||||
raw disk image artifact for us to work with, and that the GCS bucket has been created.
|
||||
|
||||
``` json
|
||||
{
|
||||
"type": "googlecompute-import",
|
||||
"account_file": "account.json",
|
||||
"project_id": "my-project",
|
||||
"bucket": "my-bucket",
|
||||
"image_name": "my-gce-image"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## QEMU Builder Example
|
||||
|
||||
Here is a complete example for building a Fedora 28 server GCE image. For this example
|
||||
packer was run from a CentOS 7 server with KVM installed. The CentOS 7 server was running
|
||||
in GCE with the nested hypervisor feature enabled.
|
||||
|
||||
```
|
||||
$ packer build -var serial=$(tty) build.json
|
||||
```
|
||||
|
||||
``` json
|
||||
{
|
||||
"variables": {
|
||||
"serial": ""
|
||||
},
|
||||
"builders": [
|
||||
{
|
||||
"type": "qemu",
|
||||
"accelerator": "kvm",
|
||||
"communicator": "none",
|
||||
"boot_command": ["<tab> console=ttyS0,115200n8 inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/fedora-28-ks.cfg rd.live.check=0<enter><wait>"],
|
||||
"disk_size": "15000",
|
||||
"format": "raw",
|
||||
"iso_checksum_type": "sha256",
|
||||
"iso_checksum": "ea1efdc692356b3346326f82e2f468903e8da59324fdee8b10eac4fea83f23fe",
|
||||
"iso_url": "https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/28/Server/x86_64/iso/Fedora-Server-netinst-x86_64-28-1.1.iso",
|
||||
"headless": "true",
|
||||
"http_directory": "http",
|
||||
"http_port_max": "10089",
|
||||
"http_port_min": "10082",
|
||||
"output_directory": "output",
|
||||
"shutdown_timeout": "30m",
|
||||
"vm_name": "disk.raw",
|
||||
"qemu_binary": "/usr/libexec/qemu-kvm",
|
||||
"qemuargs": [
|
||||
[
|
||||
"-m", "1024"
|
||||
],
|
||||
[
|
||||
"-cpu", "host"
|
||||
],
|
||||
[
|
||||
"-chardev", "tty,id=pts,path={{user `serial`}}"
|
||||
],
|
||||
[
|
||||
"-device", "isa-serial,chardev=pts"
|
||||
],
|
||||
[
|
||||
"-device", "virtio-net,netdev=user.0"
|
||||
]
|
||||
]
|
||||
}
|
||||
],
|
||||
"post-processors": [
|
||||
[
|
||||
{
|
||||
"type": "compress",
|
||||
"output": "output/disk.raw.tar.gz"
|
||||
},
|
||||
{
|
||||
"type": "googlecompute-import",
|
||||
"project_id": "my-project",
|
||||
"account_file": "account.json",
|
||||
"bucket": "my-bucket",
|
||||
"image_name": "fedora28-server-{{timestamp}}",
|
||||
"image_description": "Fedora 28 Server",
|
||||
"image_family": "fedora28-server"
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
```
|
||||
Loading…
Reference in new issue