-
Notifications
You must be signed in to change notification settings - Fork 14
/
mkdirs.go
67 lines (57 loc) · 1.61 KB
/
mkdirs.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package common
import (
"context"
"path/filepath"
"strings"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
)
type makeDirsCommand struct {
nodes []*cke.Node
dirs []string
mode string
}
// MakeDirsCommand returns a Commander to make directories on nodes.
func MakeDirsCommand(nodes []*cke.Node, dirs []string) cke.Commander {
return makeDirsCommand{nodes, dirs, "755"}
}
// MakeDirsCommandWithMode returns a Commander to make directories on nodes with given permission mode.
func MakeDirsCommandWithMode(nodes []*cke.Node, dirs []string, mode string) cke.Commander {
return makeDirsCommand{nodes, dirs, mode}
}
func (c makeDirsCommand) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
bindMap := make(map[string]cke.Mount)
dests := make([]string, len(c.dirs))
for i, d := range c.dirs {
dests[i] = filepath.Join("/mnt", d)
parentDir := filepath.Dir(d)
if _, ok := bindMap[parentDir]; ok {
continue
}
bindMap[parentDir] = cke.Mount{
Source: parentDir,
Destination: filepath.Join("/mnt", parentDir),
Label: cke.LabelPrivate,
}
}
binds := make([]cke.Mount, 0, len(bindMap))
for _, m := range bindMap {
binds = append(binds, m)
}
args := append([]string{"--mode=" + c.mode}, dests...)
env := well.NewEnvironment(ctx)
for _, n := range c.nodes {
ce := inf.Engine(n.Address)
env.Go(func(ctx context.Context) error {
return ce.Run(cke.ToolsImage, binds, "make_directories", args...)
})
}
env.Stop()
return env.Wait()
}
func (c makeDirsCommand) Command() cke.Command {
return cke.Command{
Name: "make-dirs",
Target: strings.Join(c.dirs, " "),
}
}