1 package opts
2
3 import (
4 "encoding/csv"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
9 "strconv"
10 "strings"
11
12 mounttypes "github.com/docker/docker/api/types/mount"
13 "github.com/docker/go-units"
14 "github.com/sirupsen/logrus"
15 )
16
17
18 type MountOpt struct {
19 values []mounttypes.Mount
20 }
21
22
23
24
25 func (m *MountOpt) Set(value string) error {
26 csvReader := csv.NewReader(strings.NewReader(value))
27 fields, err := csvReader.Read()
28 if err != nil {
29 return err
30 }
31
32 mount := mounttypes.Mount{}
33
34 volumeOptions := func() *mounttypes.VolumeOptions {
35 if mount.VolumeOptions == nil {
36 mount.VolumeOptions = &mounttypes.VolumeOptions{
37 Labels: make(map[string]string),
38 }
39 }
40 if mount.VolumeOptions.DriverConfig == nil {
41 mount.VolumeOptions.DriverConfig = &mounttypes.Driver{}
42 }
43 return mount.VolumeOptions
44 }
45
46 bindOptions := func() *mounttypes.BindOptions {
47 if mount.BindOptions == nil {
48 mount.BindOptions = new(mounttypes.BindOptions)
49 }
50 return mount.BindOptions
51 }
52
53 tmpfsOptions := func() *mounttypes.TmpfsOptions {
54 if mount.TmpfsOptions == nil {
55 mount.TmpfsOptions = new(mounttypes.TmpfsOptions)
56 }
57 return mount.TmpfsOptions
58 }
59
60 setValueOnMap := func(target map[string]string, value string) {
61 k, v, _ := strings.Cut(value, "=")
62 if k != "" {
63 target[k] = v
64 }
65 }
66
67 mount.Type = mounttypes.TypeVolume
68
69 for _, field := range fields {
70 key, val, ok := strings.Cut(field, "=")
71
72
73 key = strings.ToLower(key)
74
75 if !ok {
76 switch key {
77 case "readonly", "ro":
78 mount.ReadOnly = true
79 continue
80 case "volume-nocopy":
81 volumeOptions().NoCopy = true
82 continue
83 case "bind-nonrecursive":
84 bindOptions().NonRecursive = true
85 continue
86 default:
87 return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
88 }
89 }
90
91 switch key {
92 case "type":
93 mount.Type = mounttypes.Type(strings.ToLower(val))
94 case "source", "src":
95 mount.Source = val
96 if strings.HasPrefix(val, "."+string(filepath.Separator)) || val == "." {
97 if abs, err := filepath.Abs(val); err == nil {
98 mount.Source = abs
99 }
100 }
101 case "target", "dst", "destination":
102 mount.Target = val
103 case "readonly", "ro":
104 mount.ReadOnly, err = strconv.ParseBool(val)
105 if err != nil {
106 return fmt.Errorf("invalid value for %s: %s", key, val)
107 }
108 case "consistency":
109 mount.Consistency = mounttypes.Consistency(strings.ToLower(val))
110 case "bind-propagation":
111 bindOptions().Propagation = mounttypes.Propagation(strings.ToLower(val))
112 case "bind-nonrecursive":
113 bindOptions().NonRecursive, err = strconv.ParseBool(val)
114 if err != nil {
115 return fmt.Errorf("invalid value for %s: %s", key, val)
116 }
117 logrus.Warn("bind-nonrecursive is deprecated, use bind-recursive=disabled instead")
118 case "bind-recursive":
119 switch val {
120 case "enabled":
121
122 case "disabled":
123 bindOptions().NonRecursive = true
124 case "writable":
125 bindOptions().ReadOnlyNonRecursive = true
126 case "readonly":
127 bindOptions().ReadOnlyForceRecursive = true
128
129
130 default:
131 return fmt.Errorf("invalid value for %s: %s (must be \"enabled\", \"disabled\", \"writable\", or \"readonly\")",
132 key, val)
133 }
134 case "volume-nocopy":
135 volumeOptions().NoCopy, err = strconv.ParseBool(val)
136 if err != nil {
137 return fmt.Errorf("invalid value for volume-nocopy: %s", val)
138 }
139 case "volume-label":
140 setValueOnMap(volumeOptions().Labels, val)
141 case "volume-driver":
142 volumeOptions().DriverConfig.Name = val
143 case "volume-opt":
144 if volumeOptions().DriverConfig.Options == nil {
145 volumeOptions().DriverConfig.Options = make(map[string]string)
146 }
147 setValueOnMap(volumeOptions().DriverConfig.Options, val)
148 case "tmpfs-size":
149 sizeBytes, err := units.RAMInBytes(val)
150 if err != nil {
151 return fmt.Errorf("invalid value for %s: %s", key, val)
152 }
153 tmpfsOptions().SizeBytes = sizeBytes
154 case "tmpfs-mode":
155 ui64, err := strconv.ParseUint(val, 8, 32)
156 if err != nil {
157 return fmt.Errorf("invalid value for %s: %s", key, val)
158 }
159 tmpfsOptions().Mode = os.FileMode(ui64)
160 default:
161 return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
162 }
163 }
164
165 if mount.Type == "" {
166 return fmt.Errorf("type is required")
167 }
168
169 if mount.Target == "" {
170 return fmt.Errorf("target is required")
171 }
172
173 if mount.VolumeOptions != nil && mount.Type != mounttypes.TypeVolume {
174 return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mount.Type)
175 }
176 if mount.BindOptions != nil && mount.Type != mounttypes.TypeBind {
177 return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mount.Type)
178 }
179 if mount.TmpfsOptions != nil && mount.Type != mounttypes.TypeTmpfs {
180 return fmt.Errorf("cannot mix 'tmpfs-*' options with mount type '%s'", mount.Type)
181 }
182
183 if mount.BindOptions != nil {
184 if mount.BindOptions.ReadOnlyNonRecursive {
185 if !mount.ReadOnly {
186 return errors.New("option 'bind-recursive=writable' requires 'readonly' to be specified in conjunction")
187 }
188 }
189 if mount.BindOptions.ReadOnlyForceRecursive {
190 if !mount.ReadOnly {
191 return errors.New("option 'bind-recursive=readonly' requires 'readonly' to be specified in conjunction")
192 }
193 if mount.BindOptions.Propagation != mounttypes.PropagationRPrivate {
194 return errors.New("option 'bind-recursive=readonly' requires 'bind-propagation=rprivate' to be specified in conjunction")
195 }
196 }
197 }
198
199 m.values = append(m.values, mount)
200 return nil
201 }
202
203
204 func (m *MountOpt) Type() string {
205 return "mount"
206 }
207
208
209 func (m *MountOpt) String() string {
210 mounts := []string{}
211 for _, mount := range m.values {
212 repr := fmt.Sprintf("%s %s %s", mount.Type, mount.Source, mount.Target)
213 mounts = append(mounts, repr)
214 }
215 return strings.Join(mounts, ", ")
216 }
217
218
219 func (m *MountOpt) Value() []mounttypes.Mount {
220 return m.values
221 }
222
View as plain text