2013-07-02 22:11:30 -04:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os"
|
2013-07-13 20:28:56 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2013-07-02 22:11:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
// The local path of the file to upload.
|
|
|
|
Source string
|
|
|
|
|
|
|
|
// The remote path where the local file will be uploaded to.
|
|
|
|
Destination string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2013-07-13 20:28:56 -04:00
|
|
|
var md mapstructure.Metadata
|
|
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
|
|
|
Metadata: &md,
|
|
|
|
Result: &p.config,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
for _, raw := range raws {
|
2013-07-13 20:28:56 -04:00
|
|
|
err := decoder.Decode(raw)
|
|
|
|
if err != nil {
|
2013-07-02 22:11:30 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
// Accumulate any errors
|
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
// Unused keys are errors
|
|
|
|
if len(md.Unused) > 0 {
|
|
|
|
sort.Strings(md.Unused)
|
|
|
|
for _, unused := range md.Unused {
|
|
|
|
if unused != "type" && !strings.HasPrefix(unused, "packer_") {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Unknown configuration key: %s", unused))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:11:30 -04:00
|
|
|
|
|
|
|
if _, err := os.Stat(p.config.Source); err != nil {
|
2013-07-04 15:50:00 -04:00
|
|
|
errs = append(errs,
|
|
|
|
fmt.Errorf("Bad source '%s': %s", p.config.Source, err))
|
2013-07-02 22:11:30 -04:00
|
|
|
}
|
|
|
|
|
2013-07-04 15:50:00 -04:00
|
|
|
if p.config.Destination == "" {
|
2013-07-02 22:11:30 -04:00
|
|
|
errs = append(errs, errors.New("Destination must be specified."))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
ui.Say(fmt.Sprintf("Uploading %s => %s", p.config.Source, p.config.Destination))
|
|
|
|
f, err := os.Open(p.config.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
2013-07-17 21:17:46 -04:00
|
|
|
err = comm.Upload(p.config.Destination, f)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Upload failed: %s", err))
|
|
|
|
}
|
|
|
|
return err
|
2013-07-02 22:11:30 -04:00
|
|
|
}
|