2021-01-03 01:05:52 +01:00
|
|
|
// Package docker serves a remote suitable for use with docker volume api
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-05 16:32:50 +02:00
|
|
|
_ "embed"
|
2021-01-03 01:05:52 +01:00
|
|
|
"path/filepath"
|
2024-04-05 13:27:33 +02:00
|
|
|
"strings"
|
2021-01-03 01:05:52 +01:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pluginName = "rclone"
|
|
|
|
pluginScope = "local"
|
|
|
|
baseDir = "/var/lib/docker-volumes/rclone"
|
2022-06-24 18:34:37 +02:00
|
|
|
sockDir = "/run/docker/plugins" //lint:ignore U1000 unused when not building linux
|
2021-01-03 01:05:52 +01:00
|
|
|
defSpecDir = "/etc/docker/plugins"
|
|
|
|
stateFile = "docker-plugin.state"
|
|
|
|
socketAddr = "" // TCP listening address or empty string for Unix socket
|
|
|
|
socketGid = syscall.Getgid()
|
|
|
|
canPersist = false // allows writing to config file
|
|
|
|
forgetState = false
|
|
|
|
noSpec = false
|
|
|
|
)
|
|
|
|
|
2023-10-05 16:32:50 +02:00
|
|
|
//go:embed docker.md
|
|
|
|
var longHelp string
|
|
|
|
|
2024-04-05 13:27:33 +02:00
|
|
|
// help returns the help string cleaned up to simplify appending
|
|
|
|
func help() string {
|
|
|
|
return strings.TrimSpace(longHelp) + "\n\n"
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:05:52 +01:00
|
|
|
func init() {
|
|
|
|
cmdFlags := Command.Flags()
|
|
|
|
// Add command specific flags
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.StringVarP(cmdFlags, &baseDir, "base-dir", "", baseDir, "Base directory for volumes", "")
|
|
|
|
flags.StringVarP(cmdFlags, &socketAddr, "socket-addr", "", socketAddr, "Address <host:port> or absolute path (default: /run/docker/plugins/rclone.sock)", "")
|
|
|
|
flags.IntVarP(cmdFlags, &socketGid, "socket-gid", "", socketGid, "GID for unix socket (default: current process GID)", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &forgetState, "forget-state", "", forgetState, "Skip restoring previous state", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &noSpec, "no-spec", "", noSpec, "Do not write spec file", "")
|
2021-01-03 01:05:52 +01:00
|
|
|
// Add common mount/vfs flags
|
|
|
|
mountlib.AddFlags(cmdFlags)
|
|
|
|
vfsflags.AddFlags(cmdFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "docker",
|
|
|
|
Short: `Serve any remote on docker's volume plugin API.`,
|
2024-04-05 13:27:33 +02:00
|
|
|
Long: help() + vfs.Help(),
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.56",
|
2023-07-10 19:34:10 +02:00
|
|
|
"groups": "Filter",
|
2022-11-26 23:40:49 +01:00
|
|
|
},
|
2021-01-03 01:05:52 +01:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
ctx := context.Background()
|
|
|
|
drv, err := NewDriver(ctx, baseDir, nil, nil, false, forgetState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srv := NewServer(drv)
|
|
|
|
if socketAddr == "" {
|
|
|
|
// Listen on unix socket at /run/docker/plugins/<pluginName>.sock
|
|
|
|
return srv.ServeUnix(pluginName, socketGid)
|
|
|
|
}
|
|
|
|
if filepath.IsAbs(socketAddr) {
|
|
|
|
// Listen on unix socket at given path
|
|
|
|
return srv.ServeUnix(socketAddr, socketGid)
|
|
|
|
}
|
|
|
|
return srv.ServeTCP(socketAddr, "", nil, noSpec)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|