2016-02-12 18:02:16 +00:00
|
|
|
// Copyright 2015 go-dockerclient authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-11-29 22:08:16 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2018-11-29 22:08:16 +00:00
|
|
|
"time"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNoSuchVolume is the error returned when the volume does not exist.
|
|
|
|
ErrNoSuchVolume = errors.New("no such volume")
|
|
|
|
|
|
|
|
// ErrVolumeInUse is the error returned when the volume requested to be removed is still in use.
|
|
|
|
ErrVolumeInUse = errors.New("volume in use and cannot be removed")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Volume represents a volume.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/3wgTsd for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
type Volume struct {
|
2017-02-22 00:21:13 +00:00
|
|
|
Name string `json:"Name" yaml:"Name" toml:"Name"`
|
|
|
|
Driver string `json:"Driver,omitempty" yaml:"Driver,omitempty" toml:"Driver,omitempty"`
|
|
|
|
Mountpoint string `json:"Mountpoint,omitempty" yaml:"Mountpoint,omitempty" toml:"Mountpoint,omitempty"`
|
|
|
|
Labels map[string]string `json:"Labels,omitempty" yaml:"Labels,omitempty" toml:"Labels,omitempty"`
|
2018-11-29 22:08:16 +00:00
|
|
|
Options map[string]string `json:"Options,omitempty" yaml:"Options,omitempty" toml:"Options,omitempty"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt,omitempty" yaml:"CreatedAt,omitempty" toml:"CreatedAt,omitempty"`
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolumesOptions specify parameters to the ListVolumes function.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/3wgTsd for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
type ListVolumesOptions struct {
|
|
|
|
Filters map[string][]string
|
2016-09-23 20:43:03 +00:00
|
|
|
Context context.Context
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolumes returns a list of available volumes in the server.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/3wgTsd for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (c *Client) ListVolumes(opts ListVolumesOptions) ([]Volume, error) {
|
2016-09-23 20:43:03 +00:00
|
|
|
resp, err := c.do("GET", "/volumes?"+queryString(opts), doOptions{
|
|
|
|
context: opts.Context,
|
|
|
|
})
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
m := make(map[string]interface{})
|
2016-09-23 20:43:03 +00:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&m); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var volumes []Volume
|
|
|
|
volumesJSON, ok := m["Volumes"]
|
|
|
|
if !ok {
|
|
|
|
return volumes, nil
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(volumesJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &volumes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return volumes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVolumeOptions specify parameters to the CreateVolume function.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/qEhmEC for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
type CreateVolumeOptions struct {
|
|
|
|
Name string
|
|
|
|
Driver string
|
|
|
|
DriverOpts map[string]string
|
2016-09-23 20:43:03 +00:00
|
|
|
Context context.Context `json:"-"`
|
2017-02-15 01:34:05 +00:00
|
|
|
Labels map[string]string
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVolume creates a volume on the server.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/qEhmEC for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (c *Client) CreateVolume(opts CreateVolumeOptions) (*Volume, error) {
|
2016-09-23 20:43:03 +00:00
|
|
|
resp, err := c.do("POST", "/volumes/create", doOptions{
|
|
|
|
data: opts,
|
|
|
|
context: opts.Context,
|
|
|
|
})
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var volume Volume
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &volume, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InspectVolume returns a volume by its name.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/GMjsMc for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (c *Client) InspectVolume(name string) (*Volume, error) {
|
|
|
|
resp, err := c.do("GET", "/volumes/"+name, doOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
|
|
|
|
return nil, ErrNoSuchVolume
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var volume Volume
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &volume, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveVolume removes a volume by its name.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// Deprecated: Use RemoveVolumeWithOptions instead.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (c *Client) RemoveVolume(name string) error {
|
2018-11-29 22:08:16 +00:00
|
|
|
return c.RemoveVolumeWithOptions(RemoveVolumeOptions{Name: name})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveVolumeOptions specify parameters to the RemoveVolumeWithOptions
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
// See https://goo.gl/nvd6qj for more details.
|
|
|
|
type RemoveVolumeOptions struct {
|
|
|
|
Context context.Context
|
|
|
|
Name string `qs:"-"`
|
|
|
|
Force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveVolumeWithOptions removes a volume by its name and takes extra
|
|
|
|
// parameters.
|
|
|
|
//
|
|
|
|
// See https://goo.gl/nvd6qj for more details.
|
|
|
|
func (c *Client) RemoveVolumeWithOptions(opts RemoveVolumeOptions) error {
|
|
|
|
path := "/volumes/" + opts.Name
|
|
|
|
resp, err := c.do("DELETE", path+"?"+queryString(opts), doOptions{context: opts.Context})
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*Error); ok {
|
|
|
|
if e.Status == http.StatusNotFound {
|
|
|
|
return ErrNoSuchVolume
|
|
|
|
}
|
|
|
|
if e.Status == http.StatusConflict {
|
|
|
|
return ErrVolumeInUse
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 23:27:57 +00:00
|
|
|
return err
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-10 16:19:19 +00:00
|
|
|
|
|
|
|
// PruneVolumesOptions specify parameters to the PruneVolumes function.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/f9XDem for more details.
|
2017-08-10 16:19:19 +00:00
|
|
|
type PruneVolumesOptions struct {
|
|
|
|
Filters map[string][]string
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneVolumesResults specify results from the PruneVolumes function.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/f9XDem for more details.
|
2017-08-10 16:19:19 +00:00
|
|
|
type PruneVolumesResults struct {
|
|
|
|
VolumesDeleted []string
|
|
|
|
SpaceReclaimed int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneVolumes deletes volumes which are unused.
|
|
|
|
//
|
2018-11-29 22:08:16 +00:00
|
|
|
// See https://goo.gl/f9XDem for more details.
|
2017-08-10 16:19:19 +00:00
|
|
|
func (c *Client) PruneVolumes(opts PruneVolumesOptions) (*PruneVolumesResults, error) {
|
|
|
|
path := "/volumes/prune?" + queryString(opts)
|
|
|
|
resp, err := c.do("POST", path, doOptions{context: opts.Context})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var results PruneVolumesResults
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &results, nil
|
|
|
|
}
|