parse.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package configuration
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/libcontainer"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. )
  9. type Action func(*libcontainer.Container, interface{}, string) error
  10. var actions = map[string]Action{
  11. "cap.add": addCap,
  12. "cap.drop": dropCap,
  13. "fs.readonly": readonlyFs,
  14. "ns.add": addNamespace,
  15. "ns.drop": dropNamespace,
  16. "net.join": joinNetNamespace,
  17. }
  18. func addCap(container *libcontainer.Container, context interface{}, value string) error {
  19. c := container.CapabilitiesMask.Get(value)
  20. if c == nil {
  21. return fmt.Errorf("%s is not a valid capability", value)
  22. }
  23. c.Enabled = true
  24. return nil
  25. }
  26. func dropCap(container *libcontainer.Container, context interface{}, value string) error {
  27. c := container.CapabilitiesMask.Get(value)
  28. if c == nil {
  29. return fmt.Errorf("%s is not a valid capability", value)
  30. }
  31. c.Enabled = false
  32. return nil
  33. }
  34. func addNamespace(container *libcontainer.Container, context interface{}, value string) error {
  35. ns := container.Namespaces.Get(value)
  36. if ns == nil {
  37. return fmt.Errorf("%s is not a valid namespace", value[1:])
  38. }
  39. ns.Enabled = true
  40. return nil
  41. }
  42. func dropNamespace(container *libcontainer.Container, context interface{}, value string) error {
  43. ns := container.Namespaces.Get(value)
  44. if ns == nil {
  45. return fmt.Errorf("%s is not a valid namespace", value[1:])
  46. }
  47. ns.Enabled = false
  48. return nil
  49. }
  50. func readonlyFs(container *libcontainer.Container, context interface{}, value string) error {
  51. switch value {
  52. case "1", "true":
  53. container.ReadonlyFs = true
  54. default:
  55. container.ReadonlyFs = false
  56. }
  57. return nil
  58. }
  59. func joinNetNamespace(container *libcontainer.Container, context interface{}, value string) error {
  60. var (
  61. running = context.(map[string]*exec.Cmd)
  62. cmd = running[value]
  63. )
  64. if cmd == nil || cmd.Process == nil {
  65. return fmt.Errorf("%s is not a valid running container to join", value)
  66. }
  67. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  68. container.Networks = append(container.Networks, &libcontainer.Network{
  69. Type: "netns",
  70. Context: libcontainer.Context{
  71. "nspath": nspath,
  72. },
  73. })
  74. return nil
  75. }
  76. // configureCustomOptions takes string commands from the user and allows modification of the
  77. // container's default configuration.
  78. //
  79. // format: <key> <...value>
  80. // i.e: cgroup devices.allow *:*
  81. func ParseConfiguration(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
  82. for _, opt := range opts {
  83. kv := strings.SplitN(opt, "=", 2)
  84. if len(kv) < 2 {
  85. return fmt.Errorf("invalid format for %s", opt)
  86. }
  87. action, exists := actions[kv[0]]
  88. if !exists {
  89. return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
  90. }
  91. if err := action(container, running, kv[1]); err != nil {
  92. return err
  93. }
  94. }
  95. return nil
  96. }