forked from Ivasoft/objectivefs-docker-volume
GOB encoding requires public members
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
76
main.go
76
main.go
@@ -33,13 +33,13 @@ import (
|
||||
)
|
||||
|
||||
type ofsVolume struct {
|
||||
volume *volume.Volume
|
||||
fs string
|
||||
opts string
|
||||
env []string
|
||||
use map[string]bool
|
||||
mounted bool
|
||||
asap bool
|
||||
Volume *volume.Volume
|
||||
Fs string
|
||||
Opts string
|
||||
Env []string
|
||||
Use map[string]bool
|
||||
Mounted bool
|
||||
Asap bool
|
||||
}
|
||||
|
||||
type ofsDriver struct {
|
||||
@@ -71,10 +71,10 @@ func (d *ofsDriver) Create(r *volume.CreateRequest) error {
|
||||
}
|
||||
|
||||
v := &ofsVolume{}
|
||||
v.volume = &volume.Volume{Name: r.Name, Mountpoint: filepath.Join(volume.DefaultDockerRootDirectory, "objectivefs", r.Name), CreatedAt: time.Now().Format(time.RFC3339Nano)}
|
||||
v.use = make(map[string]bool)
|
||||
v.opts = ""
|
||||
v.fs = r.Name
|
||||
v.Volume = &volume.Volume{Name: r.Name, Mountpoint: filepath.Join(volume.DefaultDockerRootDirectory, "objectivefs", r.Name), CreatedAt: time.Now().Format(time.RFC3339Nano)}
|
||||
v.Use = make(map[string]bool)
|
||||
v.Opts = ""
|
||||
v.Fs = r.Name
|
||||
env := make(map[string]string)
|
||||
for id, val := range d.defEnv {
|
||||
env[id] = val
|
||||
@@ -82,21 +82,21 @@ func (d *ofsDriver) Create(r *volume.CreateRequest) error {
|
||||
for key, val := range r.Options {
|
||||
switch key {
|
||||
case "fs":
|
||||
v.fs = val
|
||||
v.Fs = val
|
||||
case "options", "ptions":
|
||||
if len(v.opts) == 0 {
|
||||
v.opts = val
|
||||
if len(v.Opts) == 0 {
|
||||
v.Opts = val
|
||||
} else {
|
||||
v.opts = v.opts + "," + val
|
||||
v.Opts = v.Opts + "," + val
|
||||
}
|
||||
case "asap":
|
||||
v.asap = true
|
||||
v.Asap = true
|
||||
default:
|
||||
env[key] = val
|
||||
}
|
||||
}
|
||||
for id, val := range env {
|
||||
v.env = append(v.env, id+"="+val)
|
||||
v.Env = append(v.Env, id+"="+val)
|
||||
}
|
||||
|
||||
if err := d.storeVolumeInfo(tx, r.Name, v); err != nil {
|
||||
@@ -123,7 +123,7 @@ func (d *ofsDriver) List() (*volume.ListResponse, error) {
|
||||
var vols []*volume.Volume
|
||||
volumeMap, err := d.getVolumeMap(tx)
|
||||
for _, v := range volumeMap {
|
||||
vols = append(vols, v.volume)
|
||||
vols = append(vols, v.Volume)
|
||||
}
|
||||
return &volume.ListResponse{Volumes: vols}, nil
|
||||
}
|
||||
@@ -145,21 +145,21 @@ func (d *ofsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
|
||||
if getVolErr != nil {
|
||||
return &volume.GetResponse{}, getVolErr
|
||||
}
|
||||
return &volume.GetResponse{Volume: volumeInfo.volume}, nil
|
||||
return &volume.GetResponse{Volume: volumeInfo.Volume}, nil
|
||||
}
|
||||
|
||||
func umount(v *ofsVolume) error {
|
||||
log.WithFields(log.Fields{"name": v.volume.Name}).Info("Unmount ObjectiveFS Volume")
|
||||
if !v.mounted {
|
||||
log.WithFields(log.Fields{"name": v.Volume.Name}).Info("Unmount ObjectiveFS Volume")
|
||||
if !v.Mounted {
|
||||
return nil
|
||||
}
|
||||
if err := exec.Command("umount", v.volume.Mountpoint).Run(); err != nil {
|
||||
if err := exec.Command("umount", v.Volume.Mountpoint).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Remove(v.volume.Mountpoint); err != nil {
|
||||
if err := os.Remove(v.Volume.Mountpoint); err != nil {
|
||||
return err
|
||||
}
|
||||
v.mounted = false
|
||||
v.Mounted = false
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -180,8 +180,8 @@ func (d *ofsDriver) Remove(r *volume.RemoveRequest) error {
|
||||
if getVolErr != nil {
|
||||
return getVolErr
|
||||
}
|
||||
if len(v.use) != 0 {
|
||||
return fmt.Errorf("volume '%s' currently in use (%d unique)", r.Name, len(v.use))
|
||||
if len(v.Use) != 0 {
|
||||
return fmt.Errorf("volume '%s' currently in use (%d unique)", r.Name, len(v.Use))
|
||||
}
|
||||
if err := umount(v); err != nil {
|
||||
return err
|
||||
@@ -211,7 +211,7 @@ func (d *ofsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
|
||||
return &volume.PathResponse{}, getVolErr
|
||||
}
|
||||
|
||||
return &volume.PathResponse{Mountpoint: volumeInfo.volume.Mountpoint}, nil
|
||||
return &volume.PathResponse{Mountpoint: volumeInfo.Volume.Mountpoint}, nil
|
||||
}
|
||||
|
||||
func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
|
||||
@@ -233,20 +233,20 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{"name": r.Name, "id": r.ID}).Info("Attach ObjectiveFS Volume")
|
||||
if !v.mounted {
|
||||
if err := os.MkdirAll(v.volume.Mountpoint, 0755); err != nil {
|
||||
if !v.Mounted {
|
||||
if err := os.MkdirAll(v.Volume.Mountpoint, 0755); err != nil {
|
||||
return &volume.MountResponse{}, err
|
||||
}
|
||||
|
||||
// Note: The first argument ("mount") causes running in the foreground, its absence in the background
|
||||
var cmd *exec.Cmd
|
||||
if len(v.opts) == 0 {
|
||||
cmd = exec.Command("/sbin/mount.objectivefs", "mount", v.fs, v.volume.Mountpoint)
|
||||
if len(v.Opts) == 0 {
|
||||
cmd = exec.Command("/sbin/mount.objectivefs", "mount", v.Fs, v.Volume.Mountpoint)
|
||||
} else {
|
||||
cmd = exec.Command("/sbin/mount.objectivefs", "mount", "-o"+v.opts, v.fs, v.volume.Mountpoint)
|
||||
cmd = exec.Command("/sbin/mount.objectivefs", "mount", "-o"+v.Opts, v.Fs, v.Volume.Mountpoint)
|
||||
}
|
||||
|
||||
cmd.Env = v.env
|
||||
cmd.Env = v.Env
|
||||
cmdReader, _ := cmd.StderrPipe()
|
||||
log.WithFields(log.Fields{
|
||||
"name": r.Name,
|
||||
@@ -278,13 +278,13 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{"name": r.Name}).Info("Volume mounted")
|
||||
v.mounted = true
|
||||
v.Mounted = true
|
||||
}
|
||||
v.use[r.ID] = true
|
||||
v.Use[r.ID] = true
|
||||
|
||||
d.storeVolumeInfo(tx, r.Name, v)
|
||||
|
||||
return &volume.MountResponse{Mountpoint: v.volume.Mountpoint}, tx.Commit()
|
||||
return &volume.MountResponse{Mountpoint: v.Volume.Mountpoint}, tx.Commit()
|
||||
}
|
||||
|
||||
func (d *ofsDriver) Unmount(r *volume.UnmountRequest) error {
|
||||
@@ -307,8 +307,8 @@ func (d *ofsDriver) Unmount(r *volume.UnmountRequest) error {
|
||||
return getVolErr
|
||||
}
|
||||
|
||||
delete(v.use, r.ID)
|
||||
if len(v.use) == 0 && v.asap {
|
||||
delete(v.Use, r.ID)
|
||||
if len(v.Use) == 0 && v.Asap {
|
||||
if err := umount(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user