parse.go 5.3 KB

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