parse.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package configuration
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/docker/libcontainer"
  9. "github.com/dotcloud/docker/pkg/units"
  10. )
  11. type Action func(*libcontainer.Config, 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.Config, 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.Config, 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.Config, context interface{}, value string) error {
  42. container.AppArmorProfile = value
  43. return nil
  44. }
  45. func cpuShares(container *libcontainer.Config, 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.Config, 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 := units.RAMInBytes(value)
  61. if err != nil {
  62. return err
  63. }
  64. container.Cgroups.Memory = v
  65. return nil
  66. }
  67. func memoryReservation(container *libcontainer.Config, 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 := units.RAMInBytes(value)
  72. if err != nil {
  73. return err
  74. }
  75. container.Cgroups.MemoryReservation = v
  76. return nil
  77. }
  78. func memorySwap(container *libcontainer.Config, 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.Config, context interface{}, value string) error {
  90. container.Capabilities = append(container.Capabilities, value)
  91. return nil
  92. }
  93. func dropCap(container *libcontainer.Config, context interface{}, value string) error {
  94. // If the capability is specified multiple times, remove all instances.
  95. for i, capability := range container.Capabilities {
  96. if capability == value {
  97. container.Capabilities = append(container.Capabilities[:i], container.Capabilities[i+1:]...)
  98. }
  99. }
  100. // The capability wasn't found so we will drop it anyways.
  101. return nil
  102. }
  103. func addNamespace(container *libcontainer.Config, context interface{}, value string) error {
  104. container.Namespaces[value] = true
  105. return nil
  106. }
  107. func dropNamespace(container *libcontainer.Config, context interface{}, value string) error {
  108. container.Namespaces[value] = false
  109. return nil
  110. }
  111. func readonlyFs(container *libcontainer.Config, context interface{}, value string) error {
  112. switch value {
  113. case "1", "true":
  114. container.MountConfig.ReadonlyFs = true
  115. default:
  116. container.MountConfig.ReadonlyFs = false
  117. }
  118. return nil
  119. }
  120. func joinNetNamespace(container *libcontainer.Config, context interface{}, value string) error {
  121. var (
  122. running = context.(map[string]*exec.Cmd)
  123. cmd = running[value]
  124. )
  125. if cmd == nil || cmd.Process == nil {
  126. return fmt.Errorf("%s is not a valid running container to join", value)
  127. }
  128. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  129. container.Networks = append(container.Networks, &libcontainer.Network{
  130. Type: "netns",
  131. NsPath: nspath,
  132. })
  133. return nil
  134. }
  135. // configureCustomOptions takes string commands from the user and allows modification of the
  136. // container's default configuration.
  137. //
  138. // TODO: this can be moved to a general utils or parser in pkg
  139. func ParseConfiguration(container *libcontainer.Config, running map[string]*exec.Cmd, opts []string) error {
  140. for _, opt := range opts {
  141. kv := strings.SplitN(opt, "=", 2)
  142. if len(kv) < 2 {
  143. return fmt.Errorf("invalid format for %s", opt)
  144. }
  145. action, exists := actions[kv[0]]
  146. if !exists {
  147. return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
  148. }
  149. if err := action(container, running, kv[1]); err != nil {
  150. return err
  151. }
  152. }
  153. return nil
  154. }