Skip to content

Commit

Permalink
pass metalinter checks
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed May 26, 2016
1 parent 0a178b0 commit 81441c6
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 186 deletions.
193 changes: 79 additions & 114 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,9 @@ import (
"path/filepath"
"syscall"

"golang.org/x/crypto/ssh"

"github.com/seletskiy/hierr"
"github.com/theairkit/runcmd"
)

type archiveReceiverNode struct {
node distributedLockNode
command runcmd.CmdWorker
}

type archiveReceivers struct {
stdin io.WriteCloser
nodes []archiveReceiverNode
}

func (receivers *archiveReceivers) wait() error {
err := receivers.stdin.Close()
if err != nil {
return hierr.Errorf(
err,
`can't close archive stream`,
)
}

for _, receiver := range receivers.nodes {
err := receiver.command.Wait()
if err != nil {
if sshErr, ok := err.(*ssh.ExitError); ok {
return fmt.Errorf(
`%s failed to receive archive, `+
`remote command exited with non-zero code: %d`,
receiver.node.String(),
sshErr.Waitmsg.ExitStatus(),
)
} else {
return hierr.Errorf(
err,
`%s failed to receive archive, unexpected error`,
receiver.node.String(),
)
}
}
}

return nil
}

func startArchiveReceivers(
lockedNodes *distributedLock,
args map[string]interface{},
Expand Down Expand Up @@ -150,99 +105,109 @@ func archiveFilesToWriter(target io.Writer, files []string) error {

archive := tar.NewWriter(target)
for fileIndex, fileName := range files {
fileInfo, err := os.Stat(fileName)

if err != nil {
return hierr.Errorf(
err,
`can't stat file for archiving: '%s`, fileName,
)
}

// avoid tar warnings about leading slash
tarFileName := fileName
if tarFileName[0] == '/' {
tarFileName = tarFileName[1:]

fileName, err = filepath.Rel(workDir, fileName)
if err != nil {
return hierr.Errorf(
err,
`can't make relative path from: '%s'`,
fileName,
)
}
}

header := &tar.Header{
Name: tarFileName,
Mode: int64(fileInfo.Sys().(*syscall.Stat_t).Mode),
Size: fileInfo.Size(),

Uid: int(fileInfo.Sys().(*syscall.Stat_t).Uid),
Gid: int(fileInfo.Sys().(*syscall.Stat_t).Gid),

ModTime: fileInfo.ModTime(),
}

logger.Infof(
"(%d/%d) sending file: '%s'",
fileIndex+1,
len(files),
fileName,
)

debugf(
hierr.Errorf(
fmt.Sprintf(
"size: %d bytes; mode: %o; uid/gid: %d/%d; modtime: %s",
header.Size,
header.Mode,
header.Uid,
header.Gid,
header.ModTime,
),
`local file: %s; remote file: %s`,
fileName,
tarFileName,
).Error(),
writeFileToArchive(fileName, archive, workDir)
}

debugf("closing archive stream, %d files sent", len(files))

err = archive.Close()
if err != nil {
return hierr.Errorf(
err,
`can't close tar stream`,
)
}

return nil
}

err = archive.WriteHeader(header)
func writeFileToArchive(
fileName string,
archive *tar.Writer,
workDir string,
) error {
fileInfo, err := os.Stat(fileName)

if err != nil {
return hierr.Errorf(
err,
`can't write tar header for fileName: '%s'`, fileName,
)
}
if err != nil {
return hierr.Errorf(
err,
`can't stat file for archiving: '%s`, fileName,
)
}

fileToArchive, err := os.Open(fileName)
if err != nil {
return hierr.Errorf(
err,
`can't open fileName for reading: '%s'`,
fileName,
)
}
// avoid tar warnings about leading slash
tarFileName := fileName
if tarFileName[0] == '/' {
tarFileName = tarFileName[1:]

_, err = io.Copy(archive, fileToArchive)
fileName, err = filepath.Rel(workDir, fileName)
if err != nil {
return hierr.Errorf(
err,
`can't copy file to the archive: '%s'`,
`can't make relative path from: '%s'`,
fileName,
)
}
}

debugf("closing archive stream, %d files sent", len(files))
header := &tar.Header{
Name: tarFileName,
Mode: int64(fileInfo.Sys().(*syscall.Stat_t).Mode),
Size: fileInfo.Size(),

Uid: int(fileInfo.Sys().(*syscall.Stat_t).Uid),
Gid: int(fileInfo.Sys().(*syscall.Stat_t).Gid),

ModTime: fileInfo.ModTime(),
}

debugf(
hierr.Errorf(
fmt.Sprintf(
"size: %d bytes; mode: %o; uid/gid: %d/%d; modtime: %s",
header.Size,
header.Mode,
header.Uid,
header.Gid,
header.ModTime,
),
`local file: %s; remote file: %s`,
fileName,
tarFileName,
).Error(),
)

err = archive.WriteHeader(header)

err = archive.Close()
if err != nil {
return hierr.Errorf(
err,
`can't close tar stream`,
`can't write tar header for fileName: '%s'`, fileName,
)
}

fileToArchive, err := os.Open(fileName)
if err != nil {
return hierr.Errorf(
err,
`can't open fileName for reading: '%s'`,
fileName,
)
}

_, err = io.Copy(archive, fileToArchive)
if err != nil {
return hierr.Errorf(
err,
`can't copy file to the archive: '%s'`,
fileName,
)
}

Expand Down
8 changes: 8 additions & 0 deletions archive_receiver_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/theairkit/runcmd"

type archiveReceiverNode struct {
node distributedLockNode
command runcmd.CmdWorker
}
46 changes: 46 additions & 0 deletions archive_receivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"io"

"github.com/seletskiy/hierr"
"golang.org/x/crypto/ssh"
)

type archiveReceivers struct {
stdin io.WriteCloser
nodes []archiveReceiverNode
}

func (receivers *archiveReceivers) wait() error {
err := receivers.stdin.Close()
if err != nil {
return hierr.Errorf(
err,
`can't close archive stream`,
)
}

for _, receiver := range receivers.nodes {
err := receiver.command.Wait()
if err != nil {
if sshErr, ok := err.(*ssh.ExitError); ok {
return fmt.Errorf(
`%s failed to receive archive, `+
`remote command exited with non-zero code: %d`,
receiver.node.String(),
sshErr.Waitmsg.ExitStatus(),
)
}

return hierr.Errorf(
err,
`%s failed to receive archive, unexpected error`,
receiver.node.String(),
)
}
}

return nil
}
43 changes: 0 additions & 43 deletions concurrency.go

This file was deleted.

Loading

0 comments on commit 81441c6

Please sign in to comment.