2014-08-28 10:18:08 -04:00
|
|
|
package volumes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-09-24 09:07:11 -04:00
|
|
|
"io"
|
2014-08-28 10:18:08 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-09-24 09:07:11 -04:00
|
|
|
"path"
|
2014-08-28 10:18:08 -04:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2014-09-24 09:07:11 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-08-28 10:18:08 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Volume struct {
|
|
|
|
ID string
|
|
|
|
Path string
|
|
|
|
IsBindMount bool
|
|
|
|
Writable bool
|
2014-10-02 20:46:17 -04:00
|
|
|
containers map[string]struct{}
|
2014-08-28 10:18:08 -04:00
|
|
|
configPath string
|
|
|
|
repository *Repository
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:07:11 -04:00
|
|
|
func (v *Volume) Export(resource, name string) (io.ReadCloser, error) {
|
|
|
|
if v.IsBindMount && filepath.Base(resource) == name {
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
basePath, err := v.getResourcePath(resource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stat, err := os.Stat(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var filter []string
|
|
|
|
if !stat.IsDir() {
|
|
|
|
d, f := path.Split(basePath)
|
|
|
|
basePath = d
|
|
|
|
filter = []string{f}
|
|
|
|
} else {
|
|
|
|
filter = []string{path.Base(basePath)}
|
|
|
|
basePath = path.Dir(basePath)
|
|
|
|
}
|
|
|
|
return archive.TarWithOptions(basePath, &archive.TarOptions{
|
2014-10-23 17:30:11 -04:00
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
Name: name,
|
|
|
|
IncludeFiles: filter,
|
2014-09-24 09:07:11 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (v *Volume) IsDir() (bool, error) {
|
|
|
|
stat, err := os.Stat(v.Path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat.IsDir(), nil
|
|
|
|
}
|
|
|
|
|
2014-10-02 20:46:17 -04:00
|
|
|
func (v *Volume) Containers() []string {
|
|
|
|
v.lock.Lock()
|
|
|
|
|
|
|
|
var containers []string
|
|
|
|
for c := range v.containers {
|
|
|
|
containers = append(containers, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
v.lock.Unlock()
|
|
|
|
return containers
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (v *Volume) RemoveContainer(containerId string) {
|
|
|
|
v.lock.Lock()
|
2014-10-02 20:46:17 -04:00
|
|
|
delete(v.containers, containerId)
|
2014-08-28 10:18:08 -04:00
|
|
|
v.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) AddContainer(containerId string) {
|
|
|
|
v.lock.Lock()
|
2014-10-02 20:46:17 -04:00
|
|
|
v.containers[containerId] = struct{}{}
|
2014-08-28 10:18:08 -04:00
|
|
|
v.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) initialize() error {
|
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
|
2015-03-24 17:11:45 -04:00
|
|
|
if _, err := os.Stat(v.Path); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-21 12:04:43 -05:00
|
|
|
if err := os.MkdirAll(v.Path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(v.configPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
jsonPath, err := v.jsonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(jsonPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return v.toDisk()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) ToDisk() error {
|
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
return v.toDisk()
|
|
|
|
}
|
2015-01-21 12:04:43 -05:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (v *Volume) toDisk() error {
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pth, err := v.jsonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(pth, data, 0666)
|
|
|
|
}
|
2015-01-21 12:04:43 -05:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (v *Volume) FromDisk() error {
|
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
pth, err := v.jsonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 08:46:45 -05:00
|
|
|
jsonSource, err := os.Open(pth)
|
2014-08-28 10:18:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 08:46:45 -05:00
|
|
|
defer jsonSource.Close()
|
2014-08-28 10:18:08 -04:00
|
|
|
|
2014-11-04 08:46:45 -05:00
|
|
|
dec := json.NewDecoder(jsonSource)
|
|
|
|
|
|
|
|
return dec.Decode(v)
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) jsonPath() (string, error) {
|
|
|
|
return v.getRootResourcePath("config.json")
|
|
|
|
}
|
|
|
|
func (v *Volume) getRootResourcePath(path string) (string, error) {
|
|
|
|
cleanPath := filepath.Join("/", path)
|
|
|
|
return symlink.FollowSymlinkInScope(filepath.Join(v.configPath, cleanPath), v.configPath)
|
|
|
|
}
|
2014-09-24 09:07:11 -04:00
|
|
|
|
|
|
|
func (v *Volume) getResourcePath(path string) (string, error) {
|
|
|
|
cleanPath := filepath.Join("/", path)
|
|
|
|
return symlink.FollowSymlinkInScope(filepath.Join(v.Path, cleanPath), v.Path)
|
|
|
|
}
|