Skip to content

Commit

Permalink
add logs to print command when it fails (#28)
Browse files Browse the repository at this point in the history
* add logs
  • Loading branch information
gnmahanth authored Jan 19, 2023
1 parent 88a773c commit bc50743
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"os/exec"
"strings"

log "github.com/sirupsen/logrus"
)

// New instantiates a new Docker runtime object
Expand Down Expand Up @@ -88,7 +90,7 @@ func (d Docker) ExtractFileSystem(imageTarPath string, outputTarPath string, ima
if err != nil {
return err
}
containerId := strings.TrimSpace(string(containerOutput.Bytes()))
containerId := strings.TrimSpace(containerOutput.String())
_, err = runCommand(exec.Command("docker", "export", strings.TrimSpace(containerId), "-o", outputTarPath), "docker export: "+string(containerId))
if err != nil {
return err
Expand All @@ -106,28 +108,30 @@ func (d Docker) ExtractFileSystem(imageTarPath string, outputTarPath string, ima

// ExtractFileSystemContainer Extract the file system of an existing container to tar
func (d Docker) ExtractFileSystemContainer(containerId string, namespace string, outputTarPath string, socketPath string) error {
_, err := runCommand(exec.Command("docker", "export", strings.TrimSpace(containerId), "-o", outputTarPath), "docker export: "+string(containerId))
cmd := exec.Command("docker", "export", strings.TrimSpace(containerId), "-o", outputTarPath)
_, err := runCommand(cmd, "docker export: "+string(containerId))
if err != nil {
return err
}
return nil
}



// ExtractFileSystemContainer Extract the file system of an existing container to tar
func (d Docker) GetFileSystemPathsForContainer(containerId string, namespace string) ([]byte, error) {
return exec.Command("docker", "inspect", strings.TrimSpace(containerId), "|", "jq" , "-r" , "'map([.Name, .GraphDriver.Data.MergedDir]) | .[] | \"\\(.[0])\t\\(.[1])\"'").Output()
return exec.Command("docker", "inspect", strings.TrimSpace(containerId), "|", "jq", "-r", "'map([.Name, .GraphDriver.Data.MergedDir]) | .[] | \"\\(.[0])\t\\(.[1])\"'").Output()
}

// operation is prepended to error message in case of error: optional
func runCommand(cmd *exec.Cmd, operation string) (*bytes.Buffer, error) {

var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
errorOnRun := cmd.Run()
if errorOnRun != nil {
log.Error("cmd: %s", cmd.String())
log.Error(errorOnRun)
return nil, errors.New(operation + fmt.Sprint(errorOnRun) + ": " + stderr.String())
}
return &out, nil
Expand Down

0 comments on commit bc50743

Please sign in to comment.