81fa9feb |
package volume
|
a7e686a7 |
import ( |
a793564b |
"fmt" |
a7e686a7 |
"os"
"runtime"
"strings"
)
|
9af963ab |
// DefaultDriverName is the driver name used for the driver
// implemented in the local package.
const DefaultDriverName string = "local" |
81fa9feb |
|
9af963ab |
// Driver is for creating and removing volumes. |
81fa9feb |
type Driver interface {
// Name returns the name of the volume driver.
Name() string
// Create makes a new volume with the given id. |
b3b7eb27 |
Create(name string, opts map[string]string) (Volume, error) |
81fa9feb |
// Remove deletes the volume. |
d3eca445 |
Remove(vol Volume) (err error)
// List lists all the volumes the driver has
List() ([]Volume, error) |
99a39690 |
// Get retrieves the volume with the requested name |
d3eca445 |
Get(name string) (Volume, error) |
81fa9feb |
}
|
9af963ab |
// Volume is a place to store data. It is backed by a specific driver, and can be mounted. |
81fa9feb |
type Volume interface {
// Name returns the name of the volume
Name() string
// DriverName returns the name of the driver which owns this volume.
DriverName() string
// Path returns the absolute path to the volume.
Path() string
// Mount mounts the volume and returns the absolute path to
// where it can be consumed.
Mount() (string, error)
// Unmount unmounts the volume when it is no longer in use.
Unmount() error
} |
dfc6c04f |
|
a7e686a7 |
// MountPoint is the intersection point between a volume and a container. It
// specifies which volume is to be used and where inside a container it should
// be mounted.
type MountPoint struct {
Source string // Container host directory
Destination string // Inside the container
RW bool // True if writable
Name string // Name set by user
Driver string // Volume driver to use
Volume Volume `json:"-"`
// Note Mode is not used on Windows
Mode string `json:"Relabel"` // Originally field was `Relabel`" |
a2dc4f79 |
// Note Propagation is not used on Windows
Propagation string // Mount propagation string |
dd7d1c8a |
Named bool // specifies if the mountpoint was specified by name |
b0ac69b6 |
// Specifies if data should be copied from the container before the first mount
// Use a pointer here so we can tell if the user set this value explicitly
// This allows us to error out when the user explicitly enabled copy but we can't copy due to the volume being populated
CopyData bool `json:"-"` |
a7e686a7 |
}
// Setup sets up a mount point by either mounting the volume if it is
// configured, or creating the source directory if supplied.
func (m *MountPoint) Setup() (string, error) {
if m.Volume != nil {
return m.Volume.Mount()
}
if len(m.Source) > 0 {
if _, err := os.Stat(m.Source); err != nil {
if !os.IsNotExist(err) {
return "", err
}
if runtime.GOOS != "windows" { // Windows does not have deprecation issues here |
ccfb894e |
if err := os.MkdirAll(m.Source, 0755); err != nil { |
a7e686a7 |
return "", err
}
}
}
return m.Source, nil
} |
a793564b |
return "", fmt.Errorf("Unable to setup mount point, neither source nor volume defined") |
dfc6c04f |
}
|
a7e686a7 |
// Path returns the path of a volume in a mount point.
func (m *MountPoint) Path() string {
if m.Volume != nil {
return m.Volume.Path()
}
return m.Source |
dfc6c04f |
}
|
34b82a69 |
// ParseVolumesFrom ensures that the supplied volumes-from is valid. |
a7e686a7 |
func ParseVolumesFrom(spec string) (string, string, error) {
if len(spec) == 0 { |
a793564b |
return "", "", fmt.Errorf("malformed volumes-from specification: %s", spec) |
a7e686a7 |
}
specParts := strings.SplitN(spec, ":", 2)
id := specParts[0]
mode := "rw"
if len(specParts) == 2 {
mode = specParts[1]
if !ValidMountMode(mode) { |
a793564b |
return "", "", errInvalidMode(mode) |
a7e686a7 |
} |
a2dc4f79 |
// For now don't allow propagation properties while importing
// volumes from data container. These volumes will inherit
// the same propagation property as of the original volume
// in data container. This probably can be relaxed in future.
if HasPropagation(mode) { |
a793564b |
return "", "", errInvalidMode(mode) |
a2dc4f79 |
} |
b0ac69b6 |
// Do not allow copy modes on volumes-from
if _, isSet := getCopyMode(mode); isSet {
return "", "", errInvalidMode(mode)
} |
a7e686a7 |
}
return id, mode, nil
} |
a793564b |
func errInvalidMode(mode string) error {
return fmt.Errorf("invalid mode: %v", mode)
}
func errInvalidSpec(spec string) error {
return fmt.Errorf("Invalid volume specification: '%s'", spec)
} |