forked from Ivasoft/objectivefs-docker-volume
Run mount in foreground mode
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:
42
main.go
42
main.go
@@ -14,6 +14,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -57,7 +58,7 @@ 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 = "auto"
|
||||
v.opts = ""
|
||||
v.fs = r.Name
|
||||
env := make(map[string]string)
|
||||
for id, val := range d.defEnv {
|
||||
@@ -162,18 +163,32 @@ func (d ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
|
||||
if err := os.MkdirAll(v.volume.Mountpoint, 0755); err != nil {
|
||||
return &volume.MountResponse{}, err
|
||||
}
|
||||
cmd := exec.Command("/sbin/mount.objectivefs", "-o"+v.opts, v.fs, v.volume.Mountpoint)
|
||||
// Note: The first argument ("mount") causes running in the foreground, its absence in the background
|
||||
cmd := exec.Command("/sbin/mount.objectivefs", "mount", "-o"+v.opts, v.fs, v.volume.Mountpoint)
|
||||
cmd.Env = v.env
|
||||
cmd.Stdout = NewLogWriter()
|
||||
cmd.Stderr = NewLogWriter()
|
||||
cmdReader, _ := cmd.StderrPipe()
|
||||
log.WithFields(log.Fields{
|
||||
"name": r.Name,
|
||||
"cmd": cmd,
|
||||
"env": v.env,
|
||||
//"env": v.env, // for security reasons disabled
|
||||
}).Info("Mount ObjectiveFS Volume")
|
||||
if err := cmd.Run(); err != nil {
|
||||
return &volume.MountResponse{}, fmt.Errorf("unexpected error mounting '%s' check log (/var/log/syslog or /var/log/messages): %s", r.Name, err.Error())
|
||||
scanner := bufio.NewScanner(cmdReader)
|
||||
go func() {
|
||||
for scanner.Scan() {
|
||||
log.WithFields(log.Fields{"name": r.Name}).Info(scanner.Text())
|
||||
}
|
||||
}()
|
||||
if err := cmd.Start(); err != nil {
|
||||
return &volume.MountResponse{}, fmt.Errorf("unexpected error mounting '%s' error: %s", r.Name, err.Error())
|
||||
}
|
||||
|
||||
// The drawback of running the mount in the foreground is there is no way to tell if it failed
|
||||
// to initially connect. So we just wait a fixed amount of time and check for process exit.
|
||||
time.Sleep(1 * time.Second)
|
||||
if cmd.ProcessState.Exited() {
|
||||
return &volume.MountResponse{}, fmt.Errorf("unexpected error mounting '%s' exist status: %d", r.Name, cmd.ProcessState.ExitCode())
|
||||
}
|
||||
|
||||
v.mounted = true
|
||||
}
|
||||
v.use[r.ID] = true
|
||||
@@ -251,16 +266,3 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type LogWriter struct {
|
||||
}
|
||||
|
||||
func NewLogWriter() *LogWriter {
|
||||
lw := &LogWriter{}
|
||||
return lw
|
||||
}
|
||||
|
||||
func (lw LogWriter) Write(p []byte) (n int, err error) {
|
||||
log.Println(p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user