cleanup
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
p4u1 2023-11-20 21:31:28 +01:00
parent 6217bf213f
commit a94dc125f4
1 changed files with 114 additions and 121 deletions

View File

@ -107,12 +107,22 @@ func parseSrcAndDst(src, dst string) (srcPath string, dstPath string, service st
return "", "", "", false, errServiceMissing
}
type CopyMode int
const (
CopyModeFileToFile = CopyMode(iota)
CopyModeFileToFileRename
CopyModeFileToDir
CopyModeDirToDir
CopyModeFilesToDir
)
// copyToContainer works with one of the following:
//
// <file> -> <dst_dir> = <dst_dir>/<file>
// <file> -> <dst_dir>/<file> = <dst_dir>/<file> (overrides the file)
// <src_dir> -> <dst_dir> = <dst_dir>/<src_dir>/<contents_of_src_dir>
// <src_dir>/ -> <dst_dir> = <dst_dir>/<contents_of_src_dir>
// <file> + <dst_dir> = <dst_dir>/<file>
// <file> + <dst_dir>/<file> = <dst_dir>/<file> (overrides the file)
// <src_dir> + <dst_dir> = <dst_dir>/<src_dir>/<contents_of_src_dir>
// <src_dir>/ + <dst_dir> = <dst_dir>/<contents_of_src_dir>
func copyToContainer(cl *dockerClient.Client, containerID, srcPath, dstPath string) error {
srcStat, err := os.Stat(srcPath)
if err != nil {
@ -199,15 +209,83 @@ func copyToContainer(cl *dockerClient.Client, containerID, srcPath, dstPath stri
return nil
}
type CopyMode int
func copyFromContainer(cl *dockerClient.Client, containerID, srcPath, dstPath string) error {
srcStat, err := cl.ContainerStatPath(context.Background(), containerID, srcPath)
if err != nil {
if errdefs.IsNotFound(err) {
return fmt.Errorf("remote: %s does not exist", srcPath)
} else {
return fmt.Errorf("remote path: %s", err)
}
}
const (
CopyModeFileToFile = CopyMode(iota)
CopyModeFileToFileRename
CopyModeFileToDir
CopyModeDirToDir
CopyModeFilesToDir
)
dstStat, err := os.Stat(dstPath)
dstExists := true
var dstMode os.FileMode
if err != nil {
if os.IsNotExist(err) {
dstExists = false
} else {
return fmt.Errorf("remote path: %s", err)
}
} else {
dstMode = dstStat.Mode()
}
mode, err := copyMode(srcPath, dstPath, srcStat.Mode, dstMode, dstExists)
if err != nil {
return err
}
moveDstDir := ""
moveDstFile := ""
switch mode {
case CopyModeFileToFile:
// Remove the file component from the path, since docker can only copy
// to a directoy.
dstPath, _ = path.Split(dstPath)
case CopyModeFileToFileRename:
// Copy the file to the temp directory and move it to its dstPath
// afterwards.
moveDstFile = dstPath
dstPath = "/tmp"
case CopyModeFilesToDir:
// Copy the directory to the temp directory and move it to its
// dstPath afterwards.
moveDstDir = path.Join(dstPath, "/")
dstPath = "/tmp"
// Make sure the temp directory always gets removed
defer os.Remove(path.Join("/tmp"))
}
content, _, err := cl.CopyFromContainer(context.Background(), containerID, srcPath)
if err != nil {
return fmt.Errorf("copy: %s", err)
}
defer content.Close()
if err := archive.Untar(content, dstPath, &archive.TarOptions{
NoOverwriteDirNonDir: true,
Compression: archive.Gzip,
NoLchown: true,
}); err != nil {
return fmt.Errorf("untar: %s", err)
}
if moveDstFile != "" {
_, srcFile := path.Split(strings.TrimSuffix(srcPath, "/"))
if err := moveFile(path.Join("/tmp", srcFile), moveDstFile); err != nil {
return err
}
}
if moveDstDir != "" {
_, srcDir := path.Split(strings.TrimSuffix(srcPath, "/"))
if err := moveDir(path.Join("/tmp", srcDir), moveDstDir); err != nil {
return err
}
}
return nil
}
var (
ErrCopyDirToFile = fmt.Errorf("can't copy dir to file")
@ -241,136 +319,51 @@ func copyMode(srcPath, dstPath string, srcMode os.FileMode, dstMode os.FileMode,
return CopyModeFileToFile, nil
}
func copyFromContainer(cl *dockerClient.Client, containerID, srcPath, dstPath string) error {
srcStat, err := cl.ContainerStatPath(context.Background(), containerID, srcPath)
if err != nil {
if errdefs.IsNotFound(err) {
return fmt.Errorf("remote: %s does not exist", srcPath)
} else {
return fmt.Errorf("remote path: %s", err)
}
}
dstStat, err := os.Stat(dstPath)
dstExists := true
var dstMode os.FileMode
if err != nil {
if os.IsNotExist(err) {
dstExists = false
} else {
return fmt.Errorf("remote path: %s", err)
}
} else {
dstMode = dstStat.Mode()
}
mode, err := copyMode(srcPath, dstPath, srcStat.Mode, dstMode, dstExists)
if err != nil {
return err
}
log.Println(mode)
moveDir := ""
moveFile := ""
switch mode {
case CopyModeDirToDir:
// Add the src directory to the destination path
// _, srcDir := path.Split(srcPath)
// dstPath = path.Join(dstPath, srcDir)
//
// if err := os.MkdirAll(dstPath, os.ModePerm); err != nil {
// return fmt.Errorf("create local directory: %s", err)
// }
case CopyModeFileToFile:
// Remove the file component from the path, since docker can only copy
// to a directoy.
dstPath, _ = path.Split(dstPath)
case CopyModeFileToFileRename:
// Copy the file to the temp directory and move it to its dstPath
// afterwards.
moveFile = dstPath
dstPath = "/tmp"
// _, srcFile := path.Split(srcPath)
// _, dstFile := path.Split(dstPath)
// log.Println(srcFile)
// log.Println(dstFile)
// tarOpts.RebaseNames = map[string]string{
// srcFile: dstFile,
// }
case CopyModeFilesToDir:
moveDir = path.Join(dstPath, "/")
dstPath = "/tmp"
}
content, _, err := cl.CopyFromContainer(context.Background(), containerID, srcPath)
if err != nil {
return fmt.Errorf("copy: %s", err)
}
defer content.Close()
if err := archive.Untar(content, dstPath, &archive.TarOptions{
NoOverwriteDirNonDir: true,
Compression: archive.Gzip,
NoLchown: true,
}); err != nil {
return fmt.Errorf("untar: %s", err)
}
if moveFile != "" {
_, srcFile := path.Split(strings.TrimSuffix(srcPath, "/"))
log.Println(moveFile)
err := MoveFile(path.Join("/tmp", srcFile), moveFile)
// moveDir moves all files from a source path to the destination path recursively.
func moveDir(sourcePath, destPath string) error {
return filepath.Walk(sourcePath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
}
if moveDir != "" {
_, srcDir := path.Split(strings.TrimSuffix(srcPath, "/"))
dir := path.Join("/tmp", srcDir)
err := filepath.Walk(dir,
func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
newPath := path.Join(destPath, strings.TrimPrefix(p, sourcePath))
if info.IsDir() {
err := os.Mkdir(newPath, info.Mode())
if err != nil {
if os.IsExist(err) {
return nil
}
newPath := path.Join(moveDir, strings.TrimPrefix(p, dir))
if info.IsDir() {
err := os.Mkdir(newPath, info.Mode())
if err != nil {
if os.IsExist(err) {
return nil
}
return err
}
}
if info.Mode().IsRegular() {
return MoveFile(p, newPath)
}
return nil
})
if err != nil {
return err
return err
}
}
}
return nil
if info.Mode().IsRegular() {
return moveFile(p, newPath)
}
return nil
})
}
func MoveFile(sourcePath, destPath string) error {
// moveFile moves a file from a source path to a destination path.
func moveFile(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("Couldn't open source file: %s", err)
return err
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("Couldn't open dest file: %s", err)
return err
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("Writing to output file failed: %s", err)
return err
}
// The copy was successful, so now delete the original file
// Remove file after succesfull copy.
err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("Failed removing original file: %s", err)
return err
}
return nil
}