summaryrefslogtreecommitdiff
path: root/test/testvol/main.go
blob: 883f55f82155c6a8a8698acb0335fd413cf21fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/docker/go-plugins-helpers/volume"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "testvol",
	Short: "testvol - volume plugin for Podman",
	Long:  `Creates simple directory volumes using the Volume Plugin API for testing volume plugin functionality`,
	RunE: func(cmd *cobra.Command, args []string) error {
		return startServer(config.sockName)
	},
	PersistentPreRunE: before,
}

// Configuration for the volume plugin
type cliConfig struct {
	logLevel string
	sockName string
	path     string
}

// Default configuration is stored here. Will be overwritten by flags.
var config = cliConfig{
	logLevel: "error",
	sockName: "test-volume-plugin",
}

func init() {
	rootCmd.Flags().StringVar(&config.sockName, "sock-name", config.sockName, "Name of unix socket for plugin")
	rootCmd.Flags().StringVar(&config.path, "path", "", "Path to initialize state and mount points")
	rootCmd.PersistentFlags().StringVar(&config.logLevel, "log-level", config.logLevel, "Log messages including and over the specified level: debug, info, warn, error, fatal, panic")
}

func before(cmd *cobra.Command, args []string) error {
	if config.logLevel == "" {
		config.logLevel = "error"
	}

	level, err := logrus.ParseLevel(config.logLevel)
	if err != nil {
		return err
	}

	logrus.SetLevel(level)

	return nil
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		logrus.Errorf("Running volume plugin: %v", err)
		os.Exit(1)
	}

	os.Exit(0)
}

// startServer runs the HTTP server and responds to requests
func startServer(socketPath string) error {
	logrus.Debugf("Starting server...")

	if config.path == "" {
		path, err := ioutil.TempDir("", "test_volume_plugin")
		if err != nil {
			return errors.Wrapf(err, "error getting directory for plugin")
		}
		config.path = path
	} else {
		pathStat, err := os.Stat(config.path)
		if err != nil {
			return errors.Wrapf(err, "unable to access requested plugin state directory")
		}
		if !pathStat.IsDir() {
			return errors.Errorf("cannot use %v as plugin state dir as it is not a directory", config.path)
		}
	}

	handle, err := makeDirDriver(config.path)
	if err != nil {
		return errors.Wrapf(err, "error making volume driver")
	}
	logrus.Infof("Using %s for volume path", config.path)

	server := volume.NewHandler(handle)
	if err := server.ServeUnix(socketPath, 0); err != nil {
		return errors.Wrapf(err, "error starting server")
	}
	return nil
}

// DirDriver is a trivial volume driver implementation.
// the volumes field maps name to volume
type DirDriver struct {
	lock        sync.Mutex
	volumesPath string
	volumes     map[string]*dirVol
}

type dirVol struct {
	name       string
	path       string
	options    map[string]string
	mounts     map[string]bool
	createTime time.Time
}

// Make a new DirDriver.
func makeDirDriver(path string) (volume.Driver, error) {
	drv := new(DirDriver)
	drv.volumesPath = path
	drv.volumes = make(map[string]*dirVol)

	return drv, nil
}

// Capabilities returns the capabilities of the driver.
func (d *DirDriver) Capabilities() *volume.CapabilitiesResponse {
	logrus.Infof("Hit Capabilities() endpoint")

	return &volume.CapabilitiesResponse{
		Capabilities: volume.Capability{
			Scope: "local",
		},
	}
}

// Create creates a volume.
func (d *DirDriver) Create(opts *volume.CreateRequest) error {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Create() endpoint")

	if _, exists := d.volumes[opts.Name]; exists {
		return errors.Errorf("volume with name %s already exists", opts.Name)
	}

	newVol := new(dirVol)
	newVol.name = opts.Name
	newVol.mounts = make(map[string]bool)
	newVol.options = make(map[string]string)
	newVol.createTime = time.Now()
	for k, v := range opts.Options {
		newVol.options[k] = v
	}

	volPath := filepath.Join(d.volumesPath, opts.Name)
	if err := os.Mkdir(volPath, 0755); err != nil {
		return errors.Wrapf(err, "error making volume directory")
	}
	newVol.path = volPath

	d.volumes[opts.Name] = newVol

	logrus.Debugf("Made volume with name %s and path %s", newVol.name, newVol.path)

	return nil
}

// List lists all volumes available.
func (d *DirDriver) List() (*volume.ListResponse, error) {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit List() endpoint")

	vols := new(volume.ListResponse)
	vols.Volumes = []*volume.Volume{}

	for _, vol := range d.volumes {
		newVol := new(volume.Volume)
		newVol.Name = vol.name
		newVol.Mountpoint = vol.path
		newVol.CreatedAt = vol.createTime.String()
		vols.Volumes = append(vols.Volumes, newVol)
		logrus.Debugf("Adding volume %s to list response", newVol.Name)
	}

	return vols, nil
}

// Get retrieves a single volume.
func (d *DirDriver) Get(req *volume.GetRequest) (*volume.GetResponse, error) {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Get() endpoint")

	vol, exists := d.volumes[req.Name]
	if !exists {
		logrus.Debugf("Did not find volume %s", req.Name)
		return nil, errors.Errorf("no volume with name %s found", req.Name)
	}

	logrus.Debugf("Found volume %s", req.Name)

	resp := new(volume.GetResponse)
	resp.Volume = new(volume.Volume)
	resp.Volume.Name = vol.name
	resp.Volume.Mountpoint = vol.path
	resp.Volume.CreatedAt = vol.createTime.String()

	return resp, nil
}

// Remove removes a single volume.
func (d *DirDriver) Remove(req *volume.RemoveRequest) error {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Remove() endpoint")

	vol, exists := d.volumes[req.Name]
	if !exists {
		logrus.Debugf("Did not find volume %s", req.Name)
		return errors.Errorf("no volume with name %s found", req.Name)
	}
	logrus.Debugf("Found volume %s", req.Name)

	if len(vol.mounts) > 0 {
		logrus.Debugf("Cannot remove %s, is mounted", req.Name)
		return errors.Errorf("volume %s is mounted and cannot be removed", req.Name)
	}

	delete(d.volumes, req.Name)

	if err := os.RemoveAll(vol.path); err != nil {
		return errors.Wrapf(err, "error removing mountpoint of volume %s", req.Name)
	}

	logrus.Debugf("Removed volume %s", req.Name)

	return nil
}

// Path returns the path a single volume is mounted at.
func (d *DirDriver) Path(req *volume.PathRequest) (*volume.PathResponse, error) {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Path() endpoint")

	// TODO: Should we return error if not mounted?

	vol, exists := d.volumes[req.Name]
	if !exists {
		logrus.Debugf("Cannot locate volume %s", req.Name)
		return nil, errors.Errorf("no volume with name %s found", req.Name)
	}

	return &volume.PathResponse{
		Mountpoint: vol.path,
	}, nil
}

// Mount mounts the volume.
func (d *DirDriver) Mount(req *volume.MountRequest) (*volume.MountResponse, error) {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Mount() endpoint")

	vol, exists := d.volumes[req.Name]
	if !exists {
		logrus.Debugf("Cannot locate volume %s", req.Name)
		return nil, errors.Errorf("no volume with name %s found", req.Name)
	}

	vol.mounts[req.ID] = true

	return &volume.MountResponse{
		Mountpoint: vol.path,
	}, nil
}

// Unmount unmounts the volume.
func (d *DirDriver) Unmount(req *volume.UnmountRequest) error {
	d.lock.Lock()
	defer d.lock.Unlock()

	logrus.Infof("Hit Unmount() endpoint")

	vol, exists := d.volumes[req.Name]
	if !exists {
		logrus.Debugf("Cannot locate volume %s", req.Name)
		return errors.Errorf("no volume with name %s found", req.Name)
	}

	mount := vol.mounts[req.ID]
	if !mount {
		logrus.Debugf("Volume %s is not mounted by %s", req.Name, req.ID)
		return errors.Errorf("volume %s is not mounted by %s", req.Name, req.ID)
	}

	delete(vol.mounts, req.ID)

	return nil
}