Skip to content

Commit

Permalink
support send files to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed May 14, 2019
1 parent fa2f33c commit e2ec551
Show file tree
Hide file tree
Showing 12 changed files with 801 additions and 337 deletions.
9 changes: 6 additions & 3 deletions cluster/calcium/calcium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ func NewTestCluster() *Calcium {
func TestNewCluster(t *testing.T) {
_, err := New(types.Config{}, false)
assert.Error(t, err)
_, err = New(types.Config{}, true)
c, err := New(types.Config{}, true)
assert.NoError(t, err)
_, err = New(types.Config{Git: types.GitConfig{SCMType: "gitlab"}}, true)
c.Finalizer()
c, err = New(types.Config{Git: types.GitConfig{SCMType: "gitlab"}}, true)
assert.NoError(t, err)
_, err = New(types.Config{Git: types.GitConfig{SCMType: "github"}}, true)
c.Finalizer()
c, err = New(types.Config{Git: types.GitConfig{SCMType: "github"}}, true)
c.Finalizer()
assert.NoError(t, err)
}

Expand Down
14 changes: 1 addition & 13 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package calcium
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"

"github.com/projecteru2/core/cluster"
Expand Down Expand Up @@ -198,20 +196,10 @@ func (c *Calcium) doCreateAndStartContainer(
// Copy data to container
if len(opts.Data) > 0 {
for dst, src := range opts.Data {
path := filepath.Dir(dst)
filename := filepath.Base(dst)
log.Infof("[doCreateAndStartContainer] Copy file %s to dir %s", filename, path)
log.Debugf("[doCreateAndStartContainer] Local file %s, remote path %s", src, dst)
f, err := os.Open(src)
if err != nil {
if err := c.doSendFileToContainer(ctx, node.Engine, containerCreated.ID, dst, src, true, true); err != nil {
createContainerMessage.Error = err
return createContainerMessage
}
if err = node.Engine.VirtualizationCopyTo(ctx, containerCreated.ID, path, f, true, true); err != nil {
createContainerMessage.Error = err
return createContainerMessage
}
f.Close()
}
}

Expand Down
49 changes: 49 additions & 0 deletions cluster/calcium/send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package calcium

import (
"context"
"os"
"path/filepath"

"github.com/projecteru2/core/engine"
"github.com/projecteru2/core/types"
log "github.com/sirupsen/logrus"
)

// Send send files to container
func (c *Calcium) Send(ctx context.Context, opts *types.SendOptions) (chan *types.SendMessage, error) {
ch := make(chan *types.SendMessage)
go func() {
defer close(ch)
// TODO use wait group, do it concurrently
for dst, src := range opts.Data {
log.Infof("[Send] Send files to containers' %s", dst)
for _, ID := range opts.IDs {
container, err := c.GetContainer(ctx, ID)
if err != nil {
ch <- &types.SendMessage{ID: ID, Path: dst, Error: err}
continue
}
if err := c.doSendFileToContainer(ctx, container.Engine, container.ID, dst, src, true, true); err != nil {
ch <- &types.SendMessage{ID: ID, Path: dst, Error: err}
continue
}
ch <- &types.SendMessage{ID: ID, Path: dst}
}
}
}()
return ch, nil
}

func (c *Calcium) doSendFileToContainer(ctx context.Context, engine engine.API, ID, dst, src string, AllowOverwriteDirWithFile bool, CopyUIDGID bool) error {
path := filepath.Dir(dst)
filename := filepath.Base(dst)
log.Infof("[doSendFileToContainer] Send file %s to dir %s", filename, path)
log.Debugf("[doSendFileToContainer] Local file %s, remote path %s", src, dst)
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
return engine.VirtualizationCopyTo(ctx, ID, path, f, AllowOverwriteDirWithFile, CopyUIDGID)
}
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Cluster interface {
PodResource(ctx context.Context, podname string) (*types.PodResource, error)
ControlContainer(ctx context.Context, IDs []string, t string) (chan *types.ControlContainerMessage, error)
Copy(ctx context.Context, opts *types.CopyOptions) (chan *types.CopyMessage, error)
Send(ctx context.Context, opts *types.SendOptions) (chan *types.SendMessage, error)
CacheImage(ctx context.Context, podname, nodenmae string, images []string, step int) (chan *types.CacheImageMessage, error)
RemoveImage(ctx context.Context, podname, nodename string, images []string, step int, prune bool) (chan *types.RemoveImageMessage, error)
RunAndWait(ctx context.Context, opts *types.DeployOptions, stdin io.ReadCloser) (chan *types.RunAndWaitMessage, error)
Expand Down
Loading

0 comments on commit e2ec551

Please sign in to comment.