mirror of https://github.com/hashicorp/packer
commit
b2ad92f414
@ -0,0 +1,36 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FileArtifact struct {
|
||||
filename string
|
||||
}
|
||||
|
||||
func (*FileArtifact) BuilderId() string {
|
||||
return BuilderId
|
||||
}
|
||||
|
||||
func (a *FileArtifact) Files() []string {
|
||||
return []string{a.filename}
|
||||
}
|
||||
|
||||
func (a *FileArtifact) Id() string {
|
||||
return "File"
|
||||
}
|
||||
|
||||
func (a *FileArtifact) String() string {
|
||||
return fmt.Sprintf("Stored file: %s", a.filename)
|
||||
}
|
||||
|
||||
func (a *FileArtifact) State(name string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *FileArtifact) Destroy() error {
|
||||
log.Printf("Deleting %s", a.filename)
|
||||
return os.Remove(a.filename)
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
func TestNullArtifact(t *testing.T) {
|
||||
var _ packer.Artifact = new(FileArtifact)
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package file
|
||||
|
||||
/*
|
||||
The File builder creates an artifact from a file. Because it does not require
|
||||
any virutalization or network resources, it's very fast and useful for testing.
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
const BuilderId = "packer.file"
|
||||
|
||||
type Builder struct {
|
||||
config *Config
|
||||
runner multistep.Runner
|
||||
}
|
||||
|
||||
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
||||
c, warnings, errs := NewConfig(raws...)
|
||||
if errs != nil {
|
||||
return warnings, errs
|
||||
}
|
||||
b.config = c
|
||||
|
||||
return warnings, nil
|
||||
}
|
||||
|
||||
// Run is where the actual build should take place. It takes a Build and a Ui.
|
||||
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
||||
artifact := new(FileArtifact)
|
||||
|
||||
if b.config.Source != "" {
|
||||
source, err := os.Open(b.config.Source)
|
||||
defer source.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create will truncate an existing file
|
||||
target, err := os.Create(b.config.Target)
|
||||
defer target.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ui.Say(fmt.Sprintf("Copying %s to %s", source.Name(), target.Name()))
|
||||
bytes, err := io.Copy(target, source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ui.Say(fmt.Sprintf("Copied %d bytes", bytes))
|
||||
artifact.filename = target.Name()
|
||||
} else {
|
||||
// We're going to write Contents; if it's empty we'll just create an
|
||||
// empty file.
|
||||
err := ioutil.WriteFile(b.config.Target, []byte(b.config.Content), 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
artifact.filename = b.config.Target
|
||||
}
|
||||
|
||||
return artifact, nil
|
||||
}
|
||||
|
||||
// Cancel cancels a possibly running Builder. This should block until
|
||||
// the builder actually cancels and cleans up after itself.
|
||||
func (b *Builder) Cancel() {
|
||||
b.runner.Cancel()
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
builderT "github.com/mitchellh/packer/helper/builder/testing"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
func TestBuilder_implBuilder(t *testing.T) {
|
||||
var _ packer.Builder = new(Builder)
|
||||
}
|
||||
|
||||
func TestBuilderFileAcc_content(t *testing.T) {
|
||||
builderT.Test(t, builderT.TestCase{
|
||||
Builder: &Builder{},
|
||||
Template: fileContentTest,
|
||||
Check: checkContent,
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuilderFileAcc_copy(t *testing.T) {
|
||||
builderT.Test(t, builderT.TestCase{
|
||||
Builder: &Builder{},
|
||||
Template: fileCopyTest,
|
||||
Check: checkCopy,
|
||||
})
|
||||
}
|
||||
|
||||
func checkContent(artifacts []packer.Artifact) error {
|
||||
content, err := ioutil.ReadFile("contentTest.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contentString := string(content)
|
||||
if contentString != "hello world!" {
|
||||
return fmt.Errorf("Unexpected file contents: %s", contentString)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkCopy(artifacts []packer.Artifact) error {
|
||||
content, err := ioutil.ReadFile("copyTest.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contentString := string(content)
|
||||
if contentString != "Hello world.\n" {
|
||||
return fmt.Errorf("Unexpected file contents: %s", contentString)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const fileContentTest = `
|
||||
{
|
||||
"builders": [
|
||||
{
|
||||
"type":"test",
|
||||
"target":"contentTest.txt",
|
||||
"content":"hello world!"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
|
||||
const fileCopyTest = `
|
||||
{
|
||||
"builders": [
|
||||
{
|
||||
"type":"test",
|
||||
"target":"copyTest.txt",
|
||||
"source":"test-fixtures/artifact.txt"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
@ -0,0 +1,56 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/packer/common"
|
||||
"github.com/mitchellh/packer/helper/config"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"github.com/mitchellh/packer/template/interpolate"
|
||||
)
|
||||
|
||||
var ErrTargetRequired = fmt.Errorf("target required")
|
||||
var ErrContentSourceConflict = fmt.Errorf("Cannot specify source file AND content")
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
|
||||
Source string `mapstructure:"source"`
|
||||
Target string `mapstructure:"target"`
|
||||
Content string `mapstructure:"content"`
|
||||
}
|
||||
|
||||
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
||||
c := new(Config)
|
||||
warnings := []string{}
|
||||
|
||||
err := config.Decode(c, &config.DecodeOpts{
|
||||
Interpolate: true,
|
||||
InterpolateFilter: &interpolate.RenderFilter{
|
||||
Exclude: []string{},
|
||||
},
|
||||
}, raws...)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
var errs *packer.MultiError
|
||||
|
||||
if c.Target == "" {
|
||||
errs = packer.MultiErrorAppend(errs, ErrTargetRequired)
|
||||
}
|
||||
|
||||
if c.Content == "" && c.Source == "" {
|
||||
warnings = append(warnings, "Both source file and contents are blank; target will have no content")
|
||||
}
|
||||
|
||||
if c.Content != "" && c.Source != "" {
|
||||
errs = packer.MultiErrorAppend(errs, ErrContentSourceConflict)
|
||||
}
|
||||
|
||||
if errs != nil && len(errs.Errors) > 0 {
|
||||
return nil, warnings, errs
|
||||
}
|
||||
|
||||
return c, warnings, nil
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testConfig() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"source": "src.txt",
|
||||
"target": "dst.txt",
|
||||
"content": "Hello, world!",
|
||||
}
|
||||
}
|
||||
|
||||
func TestContentSourceConflict(t *testing.T) {
|
||||
raw := testConfig()
|
||||
|
||||
_, _, errs := NewConfig(raw)
|
||||
if !strings.Contains(errs.Error(), ErrContentSourceConflict.Error()) {
|
||||
t.Errorf("Expected config error: %s", ErrContentSourceConflict.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoFilename(t *testing.T) {
|
||||
raw := testConfig()
|
||||
|
||||
delete(raw, "filename")
|
||||
_, _, errs := NewConfig(raw)
|
||||
if errs == nil {
|
||||
t.Errorf("Expected config error: %s", ErrTargetRequired.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoContent(t *testing.T) {
|
||||
raw := testConfig()
|
||||
|
||||
delete(raw, "content")
|
||||
delete(raw, "source")
|
||||
_, warns, _ := NewConfig(raw)
|
||||
|
||||
if len(warns) == 0 {
|
||||
t.Error("Expected config warning without any content")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
Hello world.
|
||||
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/builder/file"
|
||||
"github.com/mitchellh/packer/packer/plugin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server, err := plugin.Server()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server.RegisterBuilder(new(file.Builder))
|
||||
server.Serve()
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Vasiliy Tolstov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@ -1,197 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"compress/flate"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/biogo/hts/bgzf"
|
||||
"github.com/klauspost/pgzip"
|
||||
"github.com/pierrec/lz4"
|
||||
)
|
||||
|
||||
type Compressor struct {
|
||||
r *os.File
|
||||
w *os.File
|
||||
sr int64
|
||||
sw int64
|
||||
}
|
||||
|
||||
func (c *Compressor) Close() error {
|
||||
var err error
|
||||
|
||||
fi, _ := c.w.Stat()
|
||||
c.sw = fi.Size()
|
||||
if err = c.w.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi, _ = c.r.Stat()
|
||||
c.sr = fi.Size()
|
||||
if err = c.r.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCompressor(src, dst string) (*Compressor, error) {
|
||||
r, err := os.Open(src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w, err := os.Create(dst)
|
||||
if err != nil {
|
||||
r.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Compressor{r: r, w: w}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
||||
var resw testing.BenchmarkResult
|
||||
var resr testing.BenchmarkResult
|
||||
|
||||
c, err := NewCompressor("/tmp/image.r", "/tmp/image.w")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resw = testing.Benchmark(c.BenchmarkGZIPWriter)
|
||||
c.w.Seek(0, 0)
|
||||
resr = testing.Benchmark(c.BenchmarkGZIPReader)
|
||||
c.Close()
|
||||
fmt.Printf("gzip:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw)
|
||||
|
||||
c, err = NewCompressor("/tmp/image.r", "/tmp/image.w")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resw = testing.Benchmark(c.BenchmarkBGZFWriter)
|
||||
c.w.Seek(0, 0)
|
||||
resr = testing.Benchmark(c.BenchmarkBGZFReader)
|
||||
c.Close()
|
||||
fmt.Printf("bgzf:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw)
|
||||
|
||||
c, err = NewCompressor("/tmp/image.r", "/tmp/image.w")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resw = testing.Benchmark(c.BenchmarkPGZIPWriter)
|
||||
c.w.Seek(0, 0)
|
||||
resr = testing.Benchmark(c.BenchmarkPGZIPReader)
|
||||
c.Close()
|
||||
fmt.Printf("pgzip:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw)
|
||||
|
||||
c, err = NewCompressor("/tmp/image.r", "/tmp/image.w")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resw = testing.Benchmark(c.BenchmarkLZ4Writer)
|
||||
c.w.Seek(0, 0)
|
||||
resr = testing.Benchmark(c.BenchmarkLZ4Reader)
|
||||
c.Close()
|
||||
fmt.Printf("lz4:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw)
|
||||
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkGZIPWriter(b *testing.B) {
|
||||
cw, _ := gzip.NewWriterLevel(c.w, flate.BestSpeed)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(cw, c.r)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
cw.Close()
|
||||
c.w.Sync()
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkGZIPReader(b *testing.B) {
|
||||
cr, _ := gzip.NewReader(c.w)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(ioutil.Discard, cr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkBGZFWriter(b *testing.B) {
|
||||
cw, _ := bgzf.NewWriterLevel(c.w, flate.BestSpeed, runtime.NumCPU())
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(cw, c.r)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
c.w.Sync()
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkBGZFReader(b *testing.B) {
|
||||
cr, _ := bgzf.NewReader(c.w, 0)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(ioutil.Discard, cr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkPGZIPWriter(b *testing.B) {
|
||||
cw, _ := pgzip.NewWriterLevel(c.w, flate.BestSpeed)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(cw, c.r)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
cw.Close()
|
||||
c.w.Sync()
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkPGZIPReader(b *testing.B) {
|
||||
cr, _ := pgzip.NewReader(c.w)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(ioutil.Discard, cr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkLZ4Writer(b *testing.B) {
|
||||
cw := lz4.NewWriter(c.w)
|
||||
// cw.Header.HighCompression = true
|
||||
cw.Header.NoChecksum = true
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(cw, c.r)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
cw.Close()
|
||||
c.w.Sync()
|
||||
}
|
||||
|
||||
func (c *Compressor) BenchmarkLZ4Reader(b *testing.B) {
|
||||
cr := lz4.NewReader(c.w)
|
||||
b.ResetTimer()
|
||||
|
||||
_, err := io.Copy(ioutil.Discard, cr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,95 @@
|
||||
package compress
|
||||
|
||||
import ()
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/packer/builder/file"
|
||||
env "github.com/mitchellh/packer/helper/builder/testing"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"github.com/mitchellh/packer/template"
|
||||
)
|
||||
|
||||
func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
|
||||
// Create fake UI and Cache
|
||||
ui := packer.TestUi(t)
|
||||
cache := &packer.FileCache{CacheDir: os.TempDir()}
|
||||
|
||||
// Create config for file builder
|
||||
const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
|
||||
tpl, err := template.Parse(strings.NewReader(fileConfig))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
|
||||
}
|
||||
|
||||
// Prepare the file builder
|
||||
builder := file.Builder{}
|
||||
warnings, err := builder.Prepare(tpl.Builders["file"].Config)
|
||||
if len(warnings) > 0 {
|
||||
for _, warn := range warnings {
|
||||
return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
|
||||
}
|
||||
|
||||
// Run the file builder
|
||||
artifact, err := builder.Run(ui, nil, cache)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
|
||||
}
|
||||
|
||||
return ui, artifact, err
|
||||
}
|
||||
|
||||
func TestSimpleCompress(t *testing.T) {
|
||||
if os.Getenv(env.TestEnvVar) == "" {
|
||||
t.Skip(fmt.Sprintf(
|
||||
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
|
||||
}
|
||||
|
||||
ui, artifact, err := setup(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Error bootstrapping test: %s", err)
|
||||
}
|
||||
if artifact != nil {
|
||||
defer artifact.Destroy()
|
||||
}
|
||||
|
||||
tpl, err := template.Parse(strings.NewReader(simpleTestCase))
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to parse test config: %s", err)
|
||||
}
|
||||
|
||||
compressor := PostProcessor{}
|
||||
compressor.Configure(tpl.PostProcessors[0][0].Config)
|
||||
artifactOut, _, err := compressor.PostProcess(ui, artifact)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to compress artifact: %s", err)
|
||||
}
|
||||
// Cleanup after the test completes
|
||||
defer artifactOut.Destroy()
|
||||
|
||||
// Verify things look good
|
||||
fi, err := os.Stat("package.tar.gz")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to read archive: %s", err)
|
||||
}
|
||||
if fi.IsDir() {
|
||||
t.Error("Archive should not be a directory")
|
||||
}
|
||||
}
|
||||
|
||||
const simpleTestCase = `
|
||||
{
|
||||
"post-processors": [
|
||||
{
|
||||
"type": "compress",
|
||||
"output": "package.tar.gz"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
|
||||
Loading…
Reference in new issue