mount.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package opts
  2. import (
  3. "encoding/csv"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. mounttypes "github.com/docker/docker/api/types/mount"
  9. "github.com/docker/go-units"
  10. )
  11. // MountOpt is a Value type for parsing mounts
  12. type MountOpt struct {
  13. values []mounttypes.Mount
  14. }
  15. // Set a new mount value
  16. func (m *MountOpt) Set(value string) error {
  17. csvReader := csv.NewReader(strings.NewReader(value))
  18. fields, err := csvReader.Read()
  19. if err != nil {
  20. return err
  21. }
  22. mount := mounttypes.Mount{}
  23. volumeOptions := func() *mounttypes.VolumeOptions {
  24. if mount.VolumeOptions == nil {
  25. mount.VolumeOptions = &mounttypes.VolumeOptions{
  26. Labels: make(map[string]string),
  27. }
  28. }
  29. if mount.VolumeOptions.DriverConfig == nil {
  30. mount.VolumeOptions.DriverConfig = &mounttypes.Driver{}
  31. }
  32. return mount.VolumeOptions
  33. }
  34. bindOptions := func() *mounttypes.BindOptions {
  35. if mount.BindOptions == nil {
  36. mount.BindOptions = new(mounttypes.BindOptions)
  37. }
  38. return mount.BindOptions
  39. }
  40. tmpfsOptions := func() *mounttypes.TmpfsOptions {
  41. if mount.TmpfsOptions == nil {
  42. mount.TmpfsOptions = new(mounttypes.TmpfsOptions)
  43. }
  44. return mount.TmpfsOptions
  45. }
  46. setValueOnMap := func(target map[string]string, value string) {
  47. parts := strings.SplitN(value, "=", 2)
  48. if len(parts) == 1 {
  49. target[value] = ""
  50. } else {
  51. target[parts[0]] = parts[1]
  52. }
  53. }
  54. mount.Type = mounttypes.TypeVolume // default to volume mounts
  55. // Set writable as the default
  56. for _, field := range fields {
  57. parts := strings.SplitN(field, "=", 2)
  58. key := strings.ToLower(parts[0])
  59. if len(parts) == 1 {
  60. switch key {
  61. case "readonly", "ro":
  62. mount.ReadOnly = true
  63. continue
  64. case "volume-nocopy":
  65. volumeOptions().NoCopy = true
  66. continue
  67. }
  68. }
  69. if len(parts) != 2 {
  70. return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
  71. }
  72. value := parts[1]
  73. switch key {
  74. case "type":
  75. mount.Type = mounttypes.Type(strings.ToLower(value))
  76. case "source", "src":
  77. mount.Source = value
  78. case "target", "dst", "destination":
  79. mount.Target = value
  80. case "readonly", "ro":
  81. mount.ReadOnly, err = strconv.ParseBool(value)
  82. if err != nil {
  83. return fmt.Errorf("invalid value for %s: %s", key, value)
  84. }
  85. case "bind-propagation":
  86. bindOptions().Propagation = mounttypes.Propagation(strings.ToLower(value))
  87. case "volume-nocopy":
  88. volumeOptions().NoCopy, err = strconv.ParseBool(value)
  89. if err != nil {
  90. return fmt.Errorf("invalid value for populate: %s", value)
  91. }
  92. case "volume-label":
  93. setValueOnMap(volumeOptions().Labels, value)
  94. case "volume-driver":
  95. volumeOptions().DriverConfig.Name = value
  96. case "volume-opt":
  97. if volumeOptions().DriverConfig.Options == nil {
  98. volumeOptions().DriverConfig.Options = make(map[string]string)
  99. }
  100. setValueOnMap(volumeOptions().DriverConfig.Options, value)
  101. case "tmpfs-size":
  102. sizeBytes, err := units.RAMInBytes(value)
  103. if err != nil {
  104. return fmt.Errorf("invalid value for %s: %s", key, value)
  105. }
  106. tmpfsOptions().SizeBytes = sizeBytes
  107. case "tmpfs-mode":
  108. ui64, err := strconv.ParseUint(value, 8, 32)
  109. if err != nil {
  110. return fmt.Errorf("invalid value for %s: %s", key, value)
  111. }
  112. tmpfsOptions().Mode = os.FileMode(ui64)
  113. default:
  114. return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
  115. }
  116. }
  117. if mount.Type == "" {
  118. return fmt.Errorf("type is required")
  119. }
  120. if mount.Target == "" {
  121. return fmt.Errorf("target is required")
  122. }
  123. if mount.VolumeOptions != nil && mount.Type != mounttypes.TypeVolume {
  124. return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mount.Type)
  125. }
  126. if mount.BindOptions != nil && mount.Type != mounttypes.TypeBind {
  127. return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mount.Type)
  128. }
  129. if mount.TmpfsOptions != nil && mount.Type != mounttypes.TypeTmpfs {
  130. return fmt.Errorf("cannot mix 'tmpfs-*' options with mount type '%s'", mount.Type)
  131. }
  132. m.values = append(m.values, mount)
  133. return nil
  134. }
  135. // Type returns the type of this option
  136. func (m *MountOpt) Type() string {
  137. return "mount"
  138. }
  139. // String returns a string repr of this option
  140. func (m *MountOpt) String() string {
  141. mounts := []string{}
  142. for _, mount := range m.values {
  143. repr := fmt.Sprintf("%s %s %s", mount.Type, mount.Source, mount.Target)
  144. mounts = append(mounts, repr)
  145. }
  146. return strings.Join(mounts, ", ")
  147. }
  148. // Value returns the mounts
  149. func (m *MountOpt) Value() []mounttypes.Mount {
  150. return m.values
  151. }