diff --git a/config.json b/config.json index 15e0b8d..687ff30 100644 --- a/config.json +++ b/config.json @@ -6,8 +6,16 @@ "docker.volumedriver/1.0" ] }, - "entrypoint": ["/objectivefs-docker-volume"], + "entrypoint": ["/objectivefs-docker-volume", "-logfile", "/var/log/objectivefs-docker-volume.log"], "env": [ + { + "description": "Log level", + "name": "LOG_LEVEL", + "value": "info", + "settable": [ + "value" + ] + }, { "name": "ACCESS_KEY", "value": "", @@ -28,8 +36,42 @@ "settable": [ "value" ] + }, + { + "name": "ENDPOINT", + "value": "", + "settable": [ + "value" + ] + }, + { + "name": "CACHESIZE", + "value": "", + "settable": [ + "value" + ] + }, + { + "name": "DISKCACHE_SIZE", + "value": "", + "settable": [ + "value" + ] + }, + { + "name": "DISKCACHE_PATH", + "value": "", + "settable": [ + "value" + ] + }, + { + "name": "OBJECTIVEFS_PASSPHRASE", + "value": "", + "settable": [ + "value" + ] } - ], "network": { "type": "host" diff --git a/go.mod b/go.mod index 9ed9a5c..94c0bc7 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.ivasoft.cz/sw/objectivefs-docker-volume go 1.20 -require github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 +require ( + github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 + github.com/sirupsen/logrus v1.9.0 +) require ( github.com/Microsoft/go-winio v0.6.1 // indirect diff --git a/go.sum b/go.sum index 663d2ad..b2ae7c8 100644 --- a/go.sum +++ b/go.sum @@ -2,16 +2,30 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU= github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 h1:YcvzLmdrP/b8kLAGJ8GT7bdncgCAiWxJZIlt84D+RJg= github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 12ca660..1b45a97 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,8 @@ package main import ( + "flag" "fmt" - "github.com/docker/go-plugins-helpers/volume" - "log" "os" "os/exec" "os/user" @@ -24,6 +23,9 @@ import ( "strconv" "sync" "time" + + "github.com/docker/go-plugins-helpers/volume" + log "github.com/sirupsen/logrus" ) type ofsVolume struct { @@ -44,7 +46,7 @@ type ofsDriver struct { var version = "1.0" func (d ofsDriver) Create(r *volume.CreateRequest) error { - log.Printf("Create ObjectiveFS Volume '%s'", r.Name) + log.Info("Create ObjectiveFS Volume '%s'", r.Name) d.Lock() defer d.Unlock() @@ -94,7 +96,7 @@ func (d ofsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) { } func umount(v *ofsVolume) error { - log.Printf("Unmount ObjectiveFS Volume '%s'", v.volume.Name) + log.Info("Unmount ObjectiveFS Volume '%s'", v.volume.Name) if !v.mounted { return nil } @@ -145,14 +147,14 @@ func (d ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) if !ok { return &volume.MountResponse{}, fmt.Errorf("volume '%s' not found", r.Name) } - log.Printf("Attach ObjectiveFS Volume '%s' to '%s'", r.Name, r.ID) + log.Info("Attach ObjectiveFS Volume '%s' to '%s'", r.Name, r.ID) if !v.mounted { 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) cmd.Env = v.env - log.Printf("Mount ObjectiveFS Volume '%s': '%s'", r.Name, cmd) + log.Info("Mount ObjectiveFS Volume '%s': '%s'", r.Name, cmd) 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()) } @@ -170,7 +172,7 @@ func (d ofsDriver) Unmount(r *volume.UnmountRequest) error { if !ok { return fmt.Errorf("volume '%s' not found", r.Name) } - log.Printf("Detach ObjectiveFS Volume '%s' from '%s'", r.Name, r.ID) + log.Info("Detach ObjectiveFS Volume '%s' from '%s'", r.Name, r.ID) delete(v.use, r.ID) if len(v.use) == 0 && v.asap { if err := umount(v); err != nil { @@ -187,11 +189,43 @@ func (d ofsDriver) Capabilities() *volume.CapabilitiesResponse { return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}} } +var ( + logLevel = flag.String("log", "", "log level") + logFile = flag.String("logfile", "", "log file") +) + func main() { - log.Printf("Starting ObjectiveFS Volume Driver, version " + version) + log.Info("Starting ObjectiveFS Volume Driver, version " + version) + + flag.Parse() + + if *logLevel == "" { + if *logLevel = os.Getenv("LOG_LEVEL"); *logLevel == "" { + *logLevel = "info" + } + } + + level, err := log.ParseLevel(*logLevel) + if err != nil { + log.WithError(err).Fatal("Failed to parse log level") + } + log.SetLevel(level) + + if *logFile != "" { + f, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + log.WithError(err).Fatal("Failed to open log file for writing") + } + defer f.Close() + + log.StandardLogger().Out = f + } + d := ofsDriver{volumes: make(map[string]*ofsVolume)} h := volume.NewHandler(d) u, _ := user.Lookup("root") gid, _ := strconv.Atoi(u.Gid) - h.ServeUnix("objectivefs", gid) + if err := h.ServeUnix("/run/docker/plugins/objectivefs", gid); err != nil { + log.Fatal(err) + } }