Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
[#255] add cp command
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rad committed Oct 30, 2017
1 parent b495f16 commit 7af44d6
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 17 deletions.
65 changes: 65 additions & 0 deletions dgr/aci-cp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"io/ioutil"
"strconv"
"strings"

"os"

"github.com/blablacar/dgr/dgr/common"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
)

var pathCp = "/cp"

func (aci *Aci) Cp(args []string) error {
logs.WithF(aci.fields).Debug("Copy")
if len(args) != 2 {
return errs.With("cp requires 2 arguments")
}
if args[0][0] == '/' {
args[0] = args[0][1:]
}

cpDir := aci.target + pathCp
if err := os.MkdirAll(cpDir, 0755); err != nil {
return errs.WithEF(err, data.WithField("dir", cpDir), "Failed to create directory")
}
defer func(dir string) {
os.RemoveAll(dir)
}(cpDir)

stashes := strings.Count(args[0], "/")
if err := tarExec(
"xf",
aci.target+pathImageAci,
"--strip-components="+strconv.Itoa(stashes+1),
"-C",
cpDir,
"rootfs/"+args[0],
); err != nil {
return errs.WithE(err, "Failed to extract files from aci")
}

giveBackUserRights(cpDir)

files, err := ioutil.ReadDir(cpDir)
if err != nil {
return errs.WithEF(err, data.WithField("dir", cpDir), "Cannot read directory")
}

for _, f := range files {
from := cpDir + "/" + f.Name()
to := args[1] + "/" + f.Name()
if err := common.ExecCmd("cp", "-R", "--preserve=all", from, to); err != nil {
return errs.WithEF(err, data.WithField("from", from).
WithField("to", to),
"Failed to move file")
}
}

return nil
}
18 changes: 2 additions & 16 deletions dgr/aci-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"io/ioutil"

"github.com/blablacar/dgr/dgr/common"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
Expand Down Expand Up @@ -76,7 +75,7 @@ func (aci *Aci) deleteInTar() error {
inAciPrestartLatePath,
}
args = append(args, files...)
updateExec(args...) // err is ignored because it may already not exists
tarExec(args...) // err is ignored because it may already not exists
return nil
}

Expand All @@ -103,7 +102,7 @@ func getFileList(dir string, relativePath string, res *[]string) error {
func (aci *Aci) addDirToTar(localPath string, inAciPath string) error {
aciPath := aci.path + localPath
if _, err := os.Stat(aciPath); err == nil {
if err := updateExec("--owner=0", "--group=0", "-rf",
if err := tarExec("--owner=0", "--group=0", "-rf",
aci.target+pathImageAci,
"--transform",
"s,"+strings.TrimPrefix(aciPath, "/")+","+inAciPath+",",
Expand All @@ -113,16 +112,3 @@ func (aci *Aci) addDirToTar(localPath string, inAciPath string) error {
}
return nil
}

func updateExec(args ...string) error {
out, stderr, err := common.ExecCmdGetStdoutAndStderr("tar", args...)
if err != nil {
return errs.WithEF(err, data.
WithField("stdout", out).
WithField("stderr", stderr), "Tar update failed")
}
if logs.IsDebugEnabled() {
println(out)
}
return nil
}
13 changes: 13 additions & 0 deletions dgr/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type DgrCommand interface {
Graph() error
Sign() error
Init() error
Cp([]string) error
}

var cleanCmd = &cobra.Command{
Expand Down Expand Up @@ -105,6 +106,18 @@ var configCmd = &cobra.Command{
},
}

var cpCmd = &cobra.Command{
Use: "cp",
Short: "cp file",
Long: `cp file or directory from aci to host`,
Run: func(cmd *cobra.Command, args []string) {
command := NewAciOrPod(workPath, Args)
if err := command.Cp(args); err != nil {
logs.WithE(err).Fatal("Cp command failed")
}
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Version of dgr",
Expand Down
17 changes: 16 additions & 1 deletion dgr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,22 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&Args.PullPolicy, "pull-policy", "", "force rkt fetch Policy")
rootCmd.PersistentFlags().BoolVarP(&Args.ParallelBuild, "parallel", "P", false, "Run build in parallel for pod")

rootCmd.AddCommand(buildCmd, cleanCmd, pushCmd, installCmd, testCmd, versionCmd, initCmd, graphCmd, tryCmd, signCmd, aciVersion, configCmd, updateCmd)
rootCmd.AddCommand(
buildCmd,
cleanCmd,
pushCmd,
installCmd,
testCmd,
versionCmd,
initCmd,
graphCmd,
tryCmd,
signCmd,
aciVersion,
configCmd,
updateCmd,
cpCmd,
)

readEnvironment()
rootCmd.Execute()
Expand Down
7 changes: 7 additions & 0 deletions dgr/pod-cp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/n0rad/go-erlog/errs"

func (p *Pod) Cp(args []string) error {
return errs.With("Pod cp is not implemented")
}
21 changes: 21 additions & 0 deletions dgr/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/blablacar/dgr/dgr/common"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
)

func tarExec(args ...string) error {
out, stderr, err := common.ExecCmdGetStdoutAndStderr("tar", args...)
if err != nil {
return errs.WithEF(err, data.
WithField("stdout", out).
WithField("stderr", stderr), "Tar update failed")
}
if logs.IsDebugEnabled() {
println(out)
}
return nil
}

0 comments on commit 7af44d6

Please sign in to comment.