forked from projecteru2/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.go
42 lines (39 loc) · 1.21 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package calcium
import (
"context"
"sync"
"github.com/pkg/errors"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/types"
)
// Copy uses VirtualizationCopyFrom cp to copy specified things and send to remote
func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *types.CopyMessage, error) {
logger := log.WithField("Calcium", "Copy").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return nil, logger.Err(errors.WithStack(err))
}
ch := make(chan *types.CopyMessage)
go func() {
defer close(ch)
wg := sync.WaitGroup{}
log.Infof("[Copy] Copy %d workloads files", len(opts.Targets))
// workload one by one
for id, paths := range opts.Targets {
wg.Add(1)
go func(id string, paths []string) {
defer wg.Done()
if err := c.withWorkloadLocked(ctx, id, func(ctx context.Context, workload *types.Workload) error {
for _, path := range paths {
resp, name, err := workload.Engine.VirtualizationCopyFrom(ctx, workload.ID, path)
ch <- makeCopyMessage(id, name, path, err, resp)
}
return nil
}); err != nil {
ch <- makeCopyMessage(id, "", "", logger.Err(err), nil)
}
}(id, paths)
}
wg.Wait()
}()
return ch, nil
}