Skip to content

Commit

Permalink
change volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed Nov 22, 2016
1 parent dccd499 commit d955f69
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
23 changes: 2 additions & 21 deletions cluster/calcium/create_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,37 +526,18 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
env = append(env, fmt.Sprintf("ERU_NODE_IP=%s", nodeIP))

// mount paths
// 先把mount_paths给挂载了, 没有的话生成俩空的返回去也好啊.
permDirHost := filepath.Join(c.config.PermDir, specs.Appname)
binds, volumes := makeMountPaths(specs.MountPaths, permDirHost)

// volumes and binds
volumes["/writable-proc/sys"] = struct{}{}
binds = append(binds, "/proc/sys:/writable-proc/sys:ro")
binds, volumes := makeMountPaths(specs, c.config)

// add permdir to container
if entry.PermDir {
permDir := filepath.Join("/", specs.Appname, "permdir")
volumes[permDir] = struct{}{}

permDirHost := filepath.Join(c.config.PermDir, specs.Appname)
binds = append(binds, strings.Join([]string{permDirHost, permDir, "rw"}, ":"))
env = append(env, fmt.Sprintf("ERU_PERMDIR=%s", permDir))
}

for _, volume := range specs.Volumes {
volumes[volume] = struct{}{}
}

var mode string
for hostPath, bind := range specs.Binds {
if bind.ReadOnly {
mode = "ro"
} else {
mode = "rw"
}
binds = append(binds, strings.Join([]string{hostPath, bind.InContainerPath, mode}, ":"))
}

// log config
// 默认是配置里的driver, 如果entrypoint有指定json-file就用json-file.
// 如果是debug模式就用syslog, 拿配置里的syslog配置来发送.
Expand Down
51 changes: 49 additions & 2 deletions cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
enginetypes "github.com/docker/engine-api/types"
enginecontainer "github.com/docker/engine-api/types/container"
enginenetwork "github.com/docker/engine-api/types/network"
"gitlab.ricebook.net/platform/core/types"
"gitlab.ricebook.net/platform/core/utils"
)

Expand Down Expand Up @@ -65,6 +66,7 @@ func trimLeftSlash(name string) string {
}

// make mount paths
// app.yaml支持三种写法.
// app.yaml里可以支持mount_paths的写法, 例如
// mount_paths:
// - "/var/www/html"
Expand All @@ -73,13 +75,58 @@ func trimLeftSlash(name string) string {
// /mnt/mfs/permdirs/eggsy/data/eggsy
// /mnt/mfs/permdirs/eggsy/var/www/html
// 而且这些路径是可以读写的.
func makeMountPaths(paths []string, permDirHost string) ([]string, map[string]struct{}) {
//
// 或者使用volumes, 参数格式跟docker一样, 例如
// volumes:
// - "/data/test:/test:ro"
// - "/data/testx:/testx"
// 说明把宿主机的/data/test映射到容器里的/test, 只读, 同时
// 把宿主机的/data/tests映射到容器里的/testx, 读写.
//
// 或者使用binds, 例如
// binds:
// "/host/path":
// bind: "/container/path"
// ro: true
// 说明把宿主机的/host/path映射到容器里的/container/path, 并且只读
func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[string]struct{}) {
binds := []string{}
volumes := make(map[string]struct{})
for _, path := range paths {
permDirHost := filepath.Join(config.PermDir, specs.Appname)

// mount_paths
for _, path := range specs.MountPaths {
hostPath := filepath.Join(permDirHost, path)
binds = append(binds, fmt.Sprintf("%s:%s:rw", hostPath, path))
volumes[path] = struct{}{}
}

// volumes
for _, path := range specs.Volumes {
parts := strings.Split(path, ":")
if len(parts) == 2 {
binds = append(binds, fmt.Sprintf("%s:%s:ro", parts[0], parts[1]))
volumes[parts[1]] = struct{}{}
} else if len(parts) == 3 {
binds = append(binds, fmt.Sprintf("%s:%s:%s", parts[0], parts[1], parts[2]))
volumes[parts[1]] = struct{}{}
}
}

// binds
var mode string
for hostPath, bind := range specs.Binds {
if bind.ReadOnly {
mode = "ro"
} else {
mode = "rw"
}
binds = append(binds, fmt.Sprintf("%s:%s:%s", hostPath, bind.InContainerPath, mode))
volumes[bind.InContainerPath] = struct{}{}
}

// /proc/sys
volumes["/writable-proc/sys"] = struct{}{}
binds = append(binds, "/proc/sys:/writable-proc/sys:ro")
return binds, volumes
}

0 comments on commit d955f69

Please sign in to comment.