parse.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package configuration
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/libcontainer"
  5. "github.com/dotcloud/docker/utils"
  6. "os/exec"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  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. c := container.CapabilitiesMask.Get(value)
  83. if c == nil {
  84. return fmt.Errorf("%s is not a valid capability", value)
  85. }
  86. c.Enabled = true
  87. return nil
  88. }
  89. func dropCap(container *libcontainer.Container, context interface{}, value string) error {
  90. c := container.CapabilitiesMask.Get(value)
  91. if c == nil {
  92. return fmt.Errorf("%s is not a valid capability", value)
  93. }
  94. c.Enabled = false
  95. return nil
  96. }
  97. func addNamespace(container *libcontainer.Container, context interface{}, value string) error {
  98. ns := container.Namespaces.Get(value)
  99. if ns == nil {
  100. return fmt.Errorf("%s is not a valid namespace", value[1:])
  101. }
  102. ns.Enabled = true
  103. return nil
  104. }
  105. func dropNamespace(container *libcontainer.Container, context interface{}, value string) error {
  106. ns := container.Namespaces.Get(value)
  107. if ns == nil {
  108. return fmt.Errorf("%s is not a valid namespace", value[1:])
  109. }
  110. ns.Enabled = false
  111. return nil
  112. }
  113. func readonlyFs(container *libcontainer.Container, context interface{}, value string) error {
  114. switch value {
  115. case "1", "true":
  116. container.ReadonlyFs = true
  117. default:
  118. container.ReadonlyFs = false
  119. }
  120. return nil
  121. }
  122. func joinNetNamespace(container *libcontainer.Container, context interface{}, value string) error {
  123. var (
  124. running = context.(map[string]*exec.Cmd)
  125. cmd = running[value]
  126. )
  127. if cmd == nil || cmd.Process == nil {
  128. return fmt.Errorf("%s is not a valid running container to join", value)
  129. }
  130. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  131. container.Networks = append(container.Networks, &libcontainer.Network{
  132. Type: "netns",
  133. Context: libcontainer.Context{
  134. "nspath": nspath,
  135. },
  136. })
  137. return nil
  138. }
  139. func vethMacAddress(container *libcontainer.Container, context interface{}, value string) error {
  140. var veth *libcontainer.Network
  141. for _, network := range container.Networks {
  142. if network.Type == "veth" {
  143. veth = network
  144. break
  145. }
  146. }
  147. if veth == nil {
  148. return fmt.Errorf("not veth configured for container")
  149. }
  150. veth.Context["mac"] = value
  151. return nil
  152. }
  153. // configureCustomOptions takes string commands from the user and allows modification of the
  154. // container's default configuration.
  155. //
  156. // TODO: this can be moved to a general utils or parser in pkg
  157. func ParseConfiguration(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
  158. for _, opt := range opts {
  159. kv := strings.SplitN(opt, "=", 2)
  160. if len(kv) < 2 {
  161. return fmt.Errorf("invalid format for %s", opt)
  162. }
  163. action, exists := actions[kv[0]]
  164. if !exists {
  165. return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
  166. }
  167. if err := action(container, running, kv[1]); err != nil {
  168. return err
  169. }
  170. }
  171. return nil
  172. }