Skip to content

Commit

Permalink
ssh: improve debug logging
Browse files Browse the repository at this point in the history
When trying to figure out the exact behaviour of the ssh communicator
when copying directories, I found myself wondering what each log was
conveying.

To make it clearer what's happening on each step, we add some more logs
during the copy steps, and make the `creating directory' log a bit more
explicit, so we understand what's happening.
  • Loading branch information
lbajolet-hashicorp committed Apr 28, 2023
1 parent 3028bde commit 097c924
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions sdk-internals/communicator/ssh/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,9 @@ func (c *comm) sftpUploadDirSession(dst string, src string, excl []string) error
sftpFunc := func(client *sftp.Client) error {
rootDst := dst
if src[len(src)-1] != '/' {
log.Printf("[DEBUG] No trailing slash, creating the source directory name")
rootDst = filepath.Join(dst, filepath.Base(src))
srcBase := filepath.Base(src)
log.Printf("[DEBUG] sftp: No trailing slash, creating directory %s/%s", dst, srcBase)
rootDst = filepath.Join(dst, srcBase)
}
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -545,9 +546,12 @@ func (c *comm) sftpUploadDirSession(dst string, src string, excl []string) error
// to the sftp server
finalDst = filepath.ToSlash(finalDst)

log.Printf("[DEBUG] sftp: uploading %q to %q", relSrc, finalDst)

// Skip the creation of the target destination directory since
// it should exist and we might not even own it
if finalDst == dst {
log.Printf("[DEBUG] sftp: skipping creation of %q", dst)
return nil
}

Expand Down Expand Up @@ -689,12 +693,13 @@ func (c *comm) scpUploadDirSession(dst string, src string, excl []string) error
}

if src[len(src)-1] != '/' {
log.Printf("[DEBUG] No trailing slash, creating the source directory name")
srcBase := filepath.Base(src)
log.Printf("[DEBUG] scp: No trailing slash, creating directory %s/%s", dst, srcBase)
fi, err := os.Stat(src)
if err != nil {
return err
}
return scpUploadDirProtocol(filepath.Base(src), w, r, uploadEntries, fi)
return scpUploadDirProtocol(srcBase, w, r, uploadEntries, fi)
} else {
// Trailing slash, so only upload the contents
return uploadEntries()
Expand Down Expand Up @@ -961,6 +966,8 @@ func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() er
}

func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error {
log.Printf("[DEBUG] scp: uploading directory %s", root)

for _, fi := range fs {
realPath := filepath.Join(root, fi.Name())

Expand Down

0 comments on commit 097c924

Please sign in to comment.