parse.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package configuration
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/dotcloud/docker/pkg/libcontainer"
  9. "github.com/dotcloud/docker/utils"
  10. )
  11. type Action func(*libcontainer.Container, interface{}, string) error
  12. var actions = map[string]Action{
  13. "cap.add": addCap, // add a cap
  14. "cap.drop": dropCap, // drop a cap
  15. "ns.add": addNamespace, // add a namespace
  16. "ns.drop": dropNamespace, // drop a namespace when cloning
  17. "net.join": joinNetNamespace, // join another containers net namespace
  18. "cgroups.cpu_shares": cpuShares, // set the cpu shares
  19. "cgroups.memory": memory, // set the memory limit
  20. "cgroups.memory_reservation": memoryReservation, // set the memory reservation
  21. "cgroups.memory_swap": memorySwap, // set the memory swap limit
  22. "cgroups.cpuset.cpus": cpusetCpus, // set the cpus used
  23. "apparmor_profile": apparmorProfile, // set the apparmor profile to apply
  24. "fs.readonly": readonlyFs, // make the rootfs of the container read only
  25. }
  26. func cpusetCpus(container *libcontainer.Container, context interface{}, value string) error {
  27. if container.Cgroups == nil {
  28. return fmt.Errorf("cannot set cgroups when they are disabled")
  29. }
  30. container.Cgroups.CpusetCpus = value
  31. return nil
  32. }
  33. func apparmorProfile(container *libcontainer.Container, context interface{}, value string) error {
  34. container.Context["apparmor_profile"] = value
  35. return nil
  36. }
  37. func cpuShares(container *libcontainer.Container, context interface{}, value string) error {
  38. if container.Cgroups == nil {
  39. return fmt.Errorf("cannot set cgroups when they are disabled")
  40. }
  41. v, err := strconv.ParseInt(value, 10, 0)
  42. if err != nil {
  43. return err
  44. }
  45. container.Cgroups.CpuShares = v
  46. return nil
  47. }
  48. func memory(container *libcontainer.Container, context interface{}, value string) error {
  49. if container.Cgroups == nil {
  50. return fmt.Errorf("cannot set cgroups when they are disabled")
  51. }
  52. v, err := utils.RAMInBytes(value)
  53. if err != nil {
  54. return err
  55. }
  56. container.Cgroups.Memory = v
  57. return nil
  58. }
  59. func memoryReservation(container *libcontainer.Container, context interface{}, value string) error {
  60. if container.Cgroups == nil {
  61. return fmt.Errorf("cannot set cgroups when they are disabled")
  62. }
  63. v, err := utils.RAMInBytes(value)
  64. if err != nil {
  65. return err
  66. }
  67. container.Cgroups.MemoryReservation = v
  68. return nil
  69. }
  70. func memorySwap(container *libcontainer.Container, context interface{}, value string) error {
  71. if container.Cgroups == nil {
  72. return fmt.Errorf("cannot set cgroups when they are disabled")
  73. }
  74. v, err := strconv.ParseInt(value, 0, 64)
  75. if err != nil {
  76. return err
  77. }
  78. container.Cgroups.MemorySwap = v
  79. return nil
  80. }
  81. func addCap(container *libcontainer.Container, context interface{}, value string) error {
  82. container.CapabilitiesMask[value] = true
  83. return nil
  84. }
  85. func dropCap(container *libcontainer.Container, context interface{}, value string) error {
  86. container.CapabilitiesMask[value] = false
  87. return nil
  88. }
  89. func addNamespace(container *libcontainer.Container, context interface{}, value string) error {
  90. container.Namespaces[value] = true
  91. return nil
  92. }
  93. func dropNamespace(container *libcontainer.Container, context interface{}, value string) error {
  94. container.Namespaces[value] = false
  95. return nil
  96. }
  97. func readonlyFs(container *libcontainer.Container, context interface{}, value string) error {
  98. switch value {
  99. case "1", "true":
  100. container.ReadonlyFs = true
  101. default:
  102. container.ReadonlyFs = false
  103. }
  104. return nil
  105. }
  106. func joinNetNamespace(container *libcontainer.Container, context interface{}, value string) error {
  107. var (
  108. running = context.(map[string]*exec.Cmd)
  109. cmd = running[value]
  110. )
  111. if cmd == nil || cmd.Process == nil {
  112. return fmt.Errorf("%s is not a valid running container to join", value)
  113. }
  114. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  115. container.Networks = append(container.Networks, &libcontainer.Network{
  116. Type: "netns",
  117. Context: libcontainer.Context{
  118. "nspath": nspath,
  119. },
  120. })
  121. return nil
  122. }
  123. func vethMacAddress(container *libcontainer.Container, context interface{}, value string) error {
  124. var veth *libcontainer.Network
  125. for _, network := range container.Networks {
  126. if network.Type == "veth" {
  127. veth = network
  128. break
  129. }
  130. }
  131. if veth == nil {
  132. return fmt.Errorf("not veth configured for container")
  133. }
  134. veth.Context["mac"] = value
  135. return nil
  136. }
  137. // configureCustomOptions takes string commands from the user and allows modification of the
  138. // container's default configuration.
  139. //
  140. // TODO: this can be moved to a general utils or parser in pkg
  141. func ParseConfiguration(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
  142. for _, opt := range opts {
  143. kv := strings.SplitN(opt, "=", 2)
  144. if len(kv) < 2 {
  145. return fmt.Errorf("invalid format for %s", opt)
  146. }
  147. action, exists := actions[kv[0]]
  148. if !exists {
  149. return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
  150. }
  151. if err := action(container, running, kv[1]); err != nil {
  152. return err
  153. }
  154. }
  155. return nil
  156. }