mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Implement the CmdAdd instruction
This commit is contained in:
parent
54db18625a
commit
6ae3800151
4 changed files with 54 additions and 41 deletions
48
buildfile.go
48
buildfile.go
|
@ -27,6 +27,7 @@ type buildFile struct {
|
||||||
image string
|
image string
|
||||||
maintainer string
|
maintainer string
|
||||||
config *Config
|
config *Config
|
||||||
|
context string
|
||||||
|
|
||||||
tmpContainers map[string]struct{}
|
tmpContainers map[string]struct{}
|
||||||
tmpImages map[string]struct{}
|
tmpImages map[string]struct{}
|
||||||
|
@ -168,6 +169,7 @@ func (b *buildFile) CmdInsert(args string) error {
|
||||||
}
|
}
|
||||||
defer file.Body.Close()
|
defer file.Body.Close()
|
||||||
|
|
||||||
|
b.config.Cmd = []string{"echo", "INSERT", sourceUrl, "in", destPath}
|
||||||
cid, err := b.run()
|
cid, err := b.run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -185,6 +187,36 @@ func (b *buildFile) CmdInsert(args string) error {
|
||||||
return b.commit(cid)
|
return b.commit(cid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *buildFile) CmdAdd(args string) error {
|
||||||
|
tmp := strings.SplitN(args, " ", 2)
|
||||||
|
if len(tmp) != 2 {
|
||||||
|
return fmt.Errorf("Invalid INSERT format")
|
||||||
|
}
|
||||||
|
orig := strings.Trim(tmp[0], " ")
|
||||||
|
dest := strings.Trim(tmp[1], " ")
|
||||||
|
|
||||||
|
b.config.Cmd = []string{"echo", "PUSH", orig, "in", dest}
|
||||||
|
cid, err := b.run()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
container := b.runtime.Get(cid)
|
||||||
|
if container == nil {
|
||||||
|
return fmt.Errorf("Error while creating the container (CmdAdd)")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(path.Join(container.rwPath(), dest), 0700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := utils.CopyDirectory(path.Join(b.context, orig), path.Join(container.rwPath(), dest)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.commit(cid)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *buildFile) run() (string, error) {
|
func (b *buildFile) run() (string, error) {
|
||||||
if b.image == "" {
|
if b.image == "" {
|
||||||
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
|
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
|
||||||
|
@ -216,7 +248,6 @@ func (b *buildFile) commit(id string) error {
|
||||||
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
||||||
}
|
}
|
||||||
b.config.Image = b.image
|
b.config.Image = b.image
|
||||||
|
|
||||||
if id == "" {
|
if id == "" {
|
||||||
cmd := b.config.Cmd
|
cmd := b.config.Cmd
|
||||||
b.config.Cmd = []string{"true"}
|
b.config.Cmd = []string{"true"}
|
||||||
|
@ -245,9 +276,19 @@ func (b *buildFile) commit(id string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) Build(dockerfile, context io.Reader) (string, error) {
|
func (b *buildFile) Build(dockerfile, context io.Reader) (string, error) {
|
||||||
b.out = os.Stdout
|
|
||||||
|
|
||||||
defer b.clearTmp(b.tmpContainers, b.tmpImages)
|
defer b.clearTmp(b.tmpContainers, b.tmpImages)
|
||||||
|
|
||||||
|
if context != nil {
|
||||||
|
name, err := ioutil.TempDir("/tmp", "docker-build")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := Untar(context, name); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(name)
|
||||||
|
b.context = name
|
||||||
|
}
|
||||||
file := bufio.NewReader(dockerfile)
|
file := bufio.NewReader(dockerfile)
|
||||||
for {
|
for {
|
||||||
line, err := file.ReadString('\n')
|
line, err := file.ReadString('\n')
|
||||||
|
@ -307,6 +348,7 @@ func NewBuildFile(srv *Server, out io.Writer) BuildFile {
|
||||||
runtime: srv.runtime,
|
runtime: srv.runtime,
|
||||||
srv: srv,
|
srv: srv,
|
||||||
config: &Config{},
|
config: &Config{},
|
||||||
|
out: out,
|
||||||
tmpContainers: make(map[string]struct{}),
|
tmpContainers: make(map[string]struct{}),
|
||||||
tmpImages: make(map[string]struct{}),
|
tmpImages: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
|
|
29
commands.go
29
commands.go
|
@ -189,35 +189,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *DockerCli) CmdBuildClient(args ...string) error {
|
|
||||||
cmd := Subcmd("build", "-|Dockerfile", "Build an image from Dockerfile or via stdin")
|
|
||||||
if err := cmd.Parse(args); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var (
|
|
||||||
file io.ReadCloser
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if cmd.NArg() == 0 {
|
|
||||||
file, err = os.Open("Dockerfile")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else if cmd.Arg(0) == "-" {
|
|
||||||
file = os.Stdin
|
|
||||||
} else {
|
|
||||||
file, err = os.Open(cmd.Arg(0))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := NewBuilderClient("0.0.0.0", 4243).Build(file, nil); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 'docker login': login / register a user to registry service.
|
// 'docker login': login / register a user to registry service.
|
||||||
func (cli *DockerCli) CmdLogin(args ...string) error {
|
func (cli *DockerCli) CmdLogin(args ...string) error {
|
||||||
var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
|
var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
|
||||||
|
|
|
@ -32,13 +32,6 @@ func nuke(runtime *Runtime) error {
|
||||||
return os.RemoveAll(runtime.root)
|
return os.RemoveAll(runtime.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CopyDirectory(source, dest string) error {
|
|
||||||
if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func layerArchive(tarfile string) (io.Reader, error) {
|
func layerArchive(tarfile string) (io.Reader, error) {
|
||||||
// FIXME: need to close f somewhere
|
// FIXME: need to close f somewhere
|
||||||
f, err := os.Open(tarfile)
|
f, err := os.Open(tarfile)
|
||||||
|
@ -88,7 +81,7 @@ func newTestRuntime() (*Runtime, error) {
|
||||||
if err := os.Remove(root); err != nil {
|
if err := os.Remove(root); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := CopyDirectory(unitTestStoreBase, root); err != nil {
|
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -345,7 +338,7 @@ func TestRestore(t *testing.T) {
|
||||||
if err := os.Remove(root); err != nil {
|
if err := os.Remove(root); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := CopyDirectory(unitTestStoreBase, root); err != nil {
|
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -531,6 +531,13 @@ func GetKernelVersion() (*KernelVersionInfo, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CopyDirectory(source, dest string) error {
|
||||||
|
if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type NopFlusher struct{}
|
type NopFlusher struct{}
|
||||||
|
|
||||||
func (f *NopFlusher) Flush() {}
|
func (f *NopFlusher) Flush() {}
|
||||||
|
|
Loading…
Reference in a new issue