Merge branch 'rasa-1064-fix-upload-file-permissions'

This commit is contained in:
Mitchell Hashimoto 2014-09-08 13:20:53 -07:00
commit b6ae00fc37
21 changed files with 83 additions and 51 deletions

View File

@ -60,7 +60,7 @@ func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
return nil return nil
} }
func (c *Communicator) Upload(dst string, r io.Reader) error { func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
dst = filepath.Join(c.Chroot, dst) dst = filepath.Join(c.Chroot, dst)
log.Printf("Uploading to chroot dir: %s", dst) log.Printf("Uploading to chroot dir: %s", dst)
tf, err := ioutil.TempFile("", "packer-amazon-chroot") tf, err := ioutil.TempFile("", "packer-amazon-chroot")

View File

@ -45,5 +45,5 @@ func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src str
} }
defer f.Close() defer f.Close()
return comm.Upload(dst, f) return comm.Upload(dst, f, nil)
} }

View File

@ -57,7 +57,7 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error {
return nil return nil
} }
func (c *Communicator) Upload(dst string, src io.Reader) error { func (c *Communicator) Upload(dst string, src io.Reader, fi *os.FileInfo) error {
// Create a temporary file to store the upload // Create a temporary file to store the upload
tempfile, err := ioutil.TempFile(c.HostDir, "upload") tempfile, err := ioutil.TempFile(c.HostDir, "upload")
if err != nil { if err != nil {

View File

@ -62,7 +62,7 @@ func (s *StepUploadParallelsTools) Run(state multistep.StateBag) multistep.StepA
ui.Say(fmt.Sprintf("Uploading Parallels Tools for '%s' to path: '%s'", ui.Say(fmt.Sprintf("Uploading Parallels Tools for '%s' to path: '%s'",
s.ParallelsToolsFlavor, s.ParallelsToolsGuestPath)) s.ParallelsToolsFlavor, s.ParallelsToolsGuestPath))
if err := comm.Upload(s.ParallelsToolsGuestPath, f); err != nil { if err := comm.Upload(s.ParallelsToolsGuestPath, f, nil); err != nil {
err := fmt.Errorf("Error uploading Parallels Tools: %s", err) err := fmt.Errorf("Error uploading Parallels Tools: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())

View File

@ -33,7 +33,7 @@ func (s *StepUploadVersion) Run(state multistep.StateBag) multistep.StepAction {
ui.Say(fmt.Sprintf("Uploading Parallels version info (%s)", version)) ui.Say(fmt.Sprintf("Uploading Parallels version info (%s)", version))
var data bytes.Buffer var data bytes.Buffer
data.WriteString(version) data.WriteString(version)
if err := comm.Upload(s.Path, &data); err != nil { if err := comm.Upload(s.Path, &data, nil); err != nil {
state.Put("error", fmt.Errorf("Error uploading Parallels version: %s", err)) state.Put("error", fmt.Errorf("Error uploading Parallels version: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -58,7 +58,7 @@ func (s *StepUploadGuestAdditions) Run(state multistep.StateBag) multistep.StepA
} }
ui.Say("Uploading VirtualBox guest additions ISO...") ui.Say("Uploading VirtualBox guest additions ISO...")
if err := comm.Upload(s.GuestAdditionsPath, f); err != nil { if err := comm.Upload(s.GuestAdditionsPath, f, nil); err != nil {
state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err)) state.Put("error", fmt.Errorf("Error uploading guest additions: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -33,7 +33,7 @@ func (s *StepUploadVersion) Run(state multistep.StateBag) multistep.StepAction {
ui.Say(fmt.Sprintf("Uploading VirtualBox version info (%s)", version)) ui.Say(fmt.Sprintf("Uploading VirtualBox version info (%s)", version))
var data bytes.Buffer var data bytes.Buffer
data.WriteString(version) data.WriteString(version)
if err := comm.Upload(s.Path, &data); err != nil { if err := comm.Upload(s.Path, &data, nil); err != nil {
state.Put("error", fmt.Errorf("Error uploading VirtualBox version: %s", err)) state.Put("error", fmt.Errorf("Error uploading VirtualBox version: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -55,7 +55,7 @@ func (c *StepUploadTools) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
if err := comm.Upload(c.ToolsUploadPath, f); err != nil { if err := comm.Upload(c.ToolsUploadPath, f, nil); err != nil {
err := fmt.Errorf("Error uploading VMware Tools: %s", err) err := fmt.Errorf("Error uploading VMware Tools: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())

View File

@ -371,7 +371,7 @@ func (d *ESX5Driver) upload(dst, src string) error {
return err return err
} }
defer f.Close() defer f.Close()
return d.comm.Upload(dst, f) return d.comm.Upload(dst, f, nil)
} }
func (d *ESX5Driver) verifyChecksum(ctype string, hash string, file string) bool { func (d *ESX5Driver) verifyChecksum(ctype string, hash string, file string) bool {

View File

@ -119,7 +119,7 @@ func (c *comm) Start(cmd *packer.RemoteCmd) (err error) {
return return
} }
func (c *comm) Upload(path string, input io.Reader) error { func (c *comm) Upload(path string, input io.Reader, fi *os.FileInfo) error {
// The target directory and file for talking the SCP protocol // The target directory and file for talking the SCP protocol
target_dir := filepath.Dir(path) target_dir := filepath.Dir(path)
target_file := filepath.Base(path) target_file := filepath.Base(path)
@ -130,7 +130,7 @@ func (c *comm) Upload(path string, input io.Reader) error {
target_dir = filepath.ToSlash(target_dir) target_dir = filepath.ToSlash(target_dir)
scpFunc := func(w io.Writer, stdoutR *bufio.Reader) error { scpFunc := func(w io.Writer, stdoutR *bufio.Reader) error {
return scpUploadFile(target_file, input, w, stdoutR) return scpUploadFile(target_file, input, w, stdoutR, fi)
} }
return c.scpSession("scp -vt "+target_dir, scpFunc) return c.scpSession("scp -vt "+target_dir, scpFunc)
@ -156,7 +156,11 @@ func (c *comm) UploadDir(dst string, src string, excl []string) error {
if src[len(src)-1] != '/' { if src[len(src)-1] != '/' {
log.Printf("No trailing slash, creating the source directory name") log.Printf("No trailing slash, creating the source directory name")
return scpUploadDirProtocol(filepath.Base(src), w, r, uploadEntries) fi, err := os.Stat(src)
if err != nil {
return err
}
return scpUploadDirProtocol(filepath.Base(src), w, r, uploadEntries, fi)
} else { } else {
// Trailing slash, so only upload the contents // Trailing slash, so only upload the contents
return uploadEntries() return uploadEntries()
@ -328,7 +332,7 @@ func checkSCPStatus(r *bufio.Reader) error {
return nil return nil
} }
func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) error { func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, fi *os.FileInfo) error {
// Create a temporary file where we can copy the contents of the src // Create a temporary file where we can copy the contents of the src
// so that we can determine the length, since SCP is length-prefixed. // so that we can determine the length, since SCP is length-prefixed.
tf, err := ioutil.TempFile("", "packer-upload") tf, err := ioutil.TempFile("", "packer-upload")
@ -338,30 +342,45 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) erro
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close() defer tf.Close()
log.Println("Copying input data into temporary file so we can read the length") var mode os.FileMode
if _, err := io.Copy(tf, src); err != nil { var size int64
return err
}
// Sync the file so that the contents are definitely on disk, then if fi != nil {
// read the length of it. mode = (*fi).Mode().Perm()
if err := tf.Sync(); err != nil { size = (*fi).Size()
return fmt.Errorf("Error creating temporary file for upload: %s", err) } else {
} mode = 0644
// Seek the file to the beginning so we can re-read all of it log.Println("Copying input data into temporary file so we can read the length")
if _, err := tf.Seek(0, 0); err != nil { if _, err := io.Copy(tf, src); err != nil {
return fmt.Errorf("Error creating temporary file for upload: %s", err) return err
} }
fi, err := tf.Stat() // Sync the file so that the contents are definitely on disk, then
if err != nil { // read the length of it.
return fmt.Errorf("Error creating temporary file for upload: %s", err) if err := tf.Sync(); err != nil {
return fmt.Errorf("Error creating temporary file for upload: %s", err)
}
// Seek the file to the beginning so we can re-read all of it
if _, err := tf.Seek(0, 0); err != nil {
return fmt.Errorf("Error creating temporary file for upload: %s", err)
}
tfi, err := tf.Stat()
if err != nil {
return fmt.Errorf("Error creating temporary file for upload: %s", err)
}
size = tfi.Size()
} }
// Start the protocol // Start the protocol
log.Println("Beginning file upload...") log.Println("Beginning file upload...")
fmt.Fprintln(w, "C0644", fi.Size(), dst)
perms := fmt.Sprintf("C%04o", mode)
fmt.Fprintln(w, perms, size, dst)
if err := checkSCPStatus(r); err != nil { if err := checkSCPStatus(r); err != nil {
return err return err
} }
@ -378,9 +397,14 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) erro
return nil return nil
} }
func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() error) error { func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() error, fi os.FileInfo) error {
log.Printf("SCP: starting directory upload: %s", name) log.Printf("SCP: starting directory upload: %s", name)
fmt.Fprintln(w, "D0755 0", name)
mode := fi.Mode().Perm()
perms := fmt.Sprintf("D%04o 0", mode)
fmt.Fprintln(w, perms, name)
err := checkSCPStatus(r) err := checkSCPStatus(r)
if err != nil { if err != nil {
return err return err
@ -430,7 +454,7 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e
err = func() error { err = func() error {
defer f.Close() defer f.Close()
return scpUploadFile(fi.Name(), f, w, r) return scpUploadFile(fi.Name(), f, w, r, &fi)
}() }()
if err != nil { if err != nil {
@ -454,7 +478,7 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e
} }
return scpUploadDir(realPath, entries, w, r) return scpUploadDir(realPath, entries, w, r)
}) }, fi)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,6 +3,7 @@ package packer
import ( import (
"github.com/mitchellh/iochan" "github.com/mitchellh/iochan"
"io" "io"
"os"
"strings" "strings"
"sync" "sync"
) )
@ -57,7 +58,7 @@ type Communicator interface {
// Upload uploads a file to the machine to the given path with the // Upload uploads a file to the machine to the given path with the
// contents coming from the given reader. This method will block until // contents coming from the given reader. This method will block until
// it completes. // it completes.
Upload(string, io.Reader) error Upload(string, io.Reader, *os.FileInfo) error
// UploadDir uploads the contents of a directory recursively to // UploadDir uploads the contents of a directory recursively to
// the remote path. It also takes an optional slice of paths to // the remote path. It also takes an optional slice of paths to

View File

@ -3,6 +3,7 @@ package packer
import ( import (
"bytes" "bytes"
"io" "io"
"os"
"sync" "sync"
) )
@ -68,7 +69,7 @@ func (c *MockCommunicator) Start(rc *RemoteCmd) error {
return nil return nil
} }
func (c *MockCommunicator) Upload(path string, r io.Reader) error { func (c *MockCommunicator) Upload(path string, r io.Reader, fi *os.FileInfo) error {
c.UploadCalled = true c.UploadCalled = true
c.UploadPath = path c.UploadPath = path

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"log" "log"
"net/rpc" "net/rpc"
"os"
) )
// An implementation of packer.Communicator where the communicator is actually // An implementation of packer.Communicator where the communicator is actually
@ -103,7 +104,7 @@ func (c *communicator) Start(cmd *packer.RemoteCmd) (err error) {
return return
} }
func (c *communicator) Upload(path string, r io.Reader) (err error) { func (c *communicator) Upload(path string, r io.Reader, fi *os.FileInfo) (err error) {
// Pipe the reader through to the connection // Pipe the reader through to the connection
streamId := c.mux.NextId() streamId := c.mux.NextId()
go serveSingleCopy("uploadData", c.mux, streamId, nil, r) go serveSingleCopy("uploadData", c.mux, streamId, nil, r)
@ -233,7 +234,7 @@ func (c *CommunicatorServer) Upload(args *CommunicatorUploadArgs, reply *interfa
} }
defer readerC.Close() defer readerC.Close()
err = c.c.Upload(args.Path, readerC) err = c.c.Upload(args.Path, readerC, nil)
return return
} }

View File

@ -82,7 +82,7 @@ func TestCommunicatorRPC(t *testing.T) {
defer uploadW.Close() defer uploadW.Close()
uploadW.Write([]byte("uploadfoo\n")) uploadW.Write([]byte("uploadfoo\n"))
}() }()
err = remote.Upload("foo", uploadR) err = remote.Upload("foo", uploadR, nil)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -320,7 +320,7 @@ func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst, sr
} }
defer f.Close() defer f.Close()
if err = comm.Upload(dst, f); err != nil { if err = comm.Upload(dst, f, nil); err != nil {
return fmt.Errorf("Error uploading %s: %s", src, err) return fmt.Errorf("Error uploading %s: %s", src, err)
} }
return nil return nil

View File

@ -295,7 +295,7 @@ func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, nodeN
} }
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "client.rb")) remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "client.rb"))
if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString))); err != nil { if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString)), nil); err != nil {
return "", err return "", err
} }
@ -324,7 +324,7 @@ func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string
// Upload the bytes // Upload the bytes
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "first-boot.json")) remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "first-boot.json"))
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes)); err != nil { if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes), nil); err != nil {
return "", err return "", err
} }
@ -453,7 +453,7 @@ func (p *Provisioner) copyValidationKey(ui packer.Ui, comm packer.Communicator,
} }
defer f.Close() defer f.Close()
if err := comm.Upload(remotePath, f); err != nil { if err := comm.Upload(remotePath, f, nil); err != nil {
return err return err
} }

View File

@ -329,7 +329,7 @@ func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst str
} }
defer f.Close() defer f.Close()
return comm.Upload(dst, f) return comm.Upload(dst, f, nil)
} }
func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, localCookbooks []string, rolesPath string, dataBagsPath string, encryptedDataBagSecretPath string, environmentsPath string, chefEnvironment string) (string, error) { func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, localCookbooks []string, rolesPath string, dataBagsPath string, encryptedDataBagSecretPath string, environmentsPath string, chefEnvironment string) (string, error) {
@ -379,7 +379,7 @@ func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, local
} }
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "solo.rb")) remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "solo.rb"))
if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString))); err != nil { if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString)), nil); err != nil {
return "", err return "", err
} }
@ -408,7 +408,7 @@ func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string
// Upload the bytes // Upload the bytes
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "node.json")) remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "node.json"))
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes)); err != nil { if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes), nil); err != nil {
return "", err return "", err
} }

View File

@ -89,7 +89,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
} }
defer f.Close() defer f.Close()
err = comm.Upload(p.config.Destination, f) fi, err := f.Stat()
if err != nil {
return err
}
err = comm.Upload(p.config.Destination, f, &fi)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err)) ui.Error(fmt.Sprintf("Upload failed: %s", err))
} }

View File

@ -301,7 +301,7 @@ func (p *Provisioner) uploadHieraConfig(ui packer.Ui, comm packer.Communicator)
defer f.Close() defer f.Close()
path := fmt.Sprintf("%s/hiera.yaml", p.config.StagingDir) path := fmt.Sprintf("%s/hiera.yaml", p.config.StagingDir)
if err := comm.Upload(path, f); err != nil { if err := comm.Upload(path, f, nil); err != nil {
return "", err return "", err
} }
@ -325,7 +325,7 @@ func (p *Provisioner) uploadManifests(ui packer.Ui, comm packer.Communicator) (s
manifestFilename := filepath.Base(p.config.ManifestFile) manifestFilename := filepath.Base(p.config.ManifestFile)
remoteManifestFile := fmt.Sprintf("%s/%s", remoteManifestsPath, manifestFilename) remoteManifestFile := fmt.Sprintf("%s/%s", remoteManifestsPath, manifestFilename)
if err := comm.Upload(remoteManifestFile, f); err != nil { if err := comm.Upload(remoteManifestFile, f, nil); err != nil {
return "", err return "", err
} }

View File

@ -203,7 +203,7 @@ func uploadMinionConfig(comm packer.Communicator, dst string, src string) error
} }
defer f.Close() defer f.Close()
if err = comm.Upload(dst, f); err != nil { if err = comm.Upload(dst, f, nil); err != nil {
return fmt.Errorf("Error uploading minion config: %s", err) return fmt.Errorf("Error uploading minion config: %s", err)
} }

View File

@ -269,7 +269,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
r = &UnixReader{Reader: r} r = &UnixReader{Reader: r}
} }
if err := comm.Upload(p.config.RemotePath, r); err != nil { if err := comm.Upload(p.config.RemotePath, r, nil); err != nil {
return fmt.Errorf("Error uploading script: %s", err) return fmt.Errorf("Error uploading script: %s", err)
} }