parse.go 5.9 KB

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