Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add daemon-socket-path #1521

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type ClientOption struct {
// CallSystem system name that executes dfget.
CallSystem string `yaml:"callSystem,omitempty" mapstructure:"callSystem,omitempty"`

// DaemonSock is daemon download socket path.
DaemonSock string `yaml:"daemonSock,omitempty" mapstructure:"daemon-sock,omitempty"`

// Pattern download pattern, must be 'p2p' or 'seed-peer' or 'source',
// default:`p2p`.
Pattern string `yaml:"pattern,omitempty" mapstructure:"pattern,omitempty"`
Expand Down
5 changes: 1 addition & 4 deletions client/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,10 @@ func (cd *clientDaemon) Serve() error {
if cd.Option.Download.DownloadGRPC.UnixListen == nil {
return errors.New("download grpc unix listen option is empty")
}
if cd.Option.Download.DownloadGRPC.UnixListen.Socket == "" {
cd.Option.Download.DownloadGRPC.UnixListen.Socket = cd.dfpath.DaemonSockPath()
}
_ = os.Remove(cd.Option.Download.DownloadGRPC.UnixListen.Socket)
downloadListener, err := rpc.Listen(dfnet.NetAddr{
Type: dfnet.UNIX,
Addr: cd.Option.Download.DownloadGRPC.UnixListen.Socket,
Addr: cd.dfpath.DaemonSockPath(),
})
if err != nil {
logger.Errorf("failed to listen for download grpc service: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/dfget/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func initDaemonDfpath(cfg *config.DaemonOption) (dfpath.Dfpath, error) {
options = append(options, dfpath.WithLogDir(cfg.LogDir))
}

if cfg.Download.DownloadGRPC.UnixListen.Socket != "" {
options = append(options, dfpath.WithDownloadUnixSocketPath(cfg.Download.DownloadGRPC.UnixListen.Socket))
}

cacheDir := dfpath.DefaultCacheDir
if cfg.CacheDir != "" {
cacheDir = cfg.CacheDir
Expand Down
6 changes: 6 additions & 0 deletions cmd/dfget/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func init() {

flagSet.String("callsystem", dfgetConfig.CallSystem, "The caller name which is mainly used for statistics and access control")

flagSet.String("daemon-sock", dfgetConfig.DaemonSock, "Download socket path of daemon. In linux, default value is /var/run/dfdaemon.sock, in macos(just for testing), default value is /tmp/dfdaemon.sock")

flagSet.String("workhome", dfgetConfig.WorkHome, "Dfget working directory")

flagSet.String("logdir", dfgetConfig.LogDir, "Dfget log directory")
Expand Down Expand Up @@ -205,6 +207,10 @@ func initDfgetDfpath(cfg *config.ClientOption) (dfpath.Dfpath, error) {
options = append(options, dfpath.WithLogDir(cfg.LogDir))
}

if cfg.DaemonSock != "" {
options = append(options, dfpath.WithDownloadUnixSocketPath(cfg.DaemonSock))
}

return dfpath.New(options...)
}

Expand Down
21 changes: 17 additions & 4 deletions pkg/dfpath/dfpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package dfpath
import (
"io/fs"
"os"
"path"
"path/filepath"
"sync"

Expand Down Expand Up @@ -96,21 +97,28 @@ func WithPluginDir(dir string) Option {
}
}

// WithDownloadUnixSocketPath set unix socket path.
func WithDownloadUnixSocketPath(path string) Option {
return func(d *dfpath) {
d.daemonSockPath = path
}
}

// New returns a new dfpath interface.
func New(options ...Option) (Dfpath, error) {
cache.Do(func() {
d := &dfpath{
workHome: DefaultWorkHome,
logDir: DefaultLogDir,
pluginDir: DefaultPluginDir,
workHome: DefaultWorkHome,
logDir: DefaultLogDir,
pluginDir: DefaultPluginDir,
daemonSockPath: DefaultDownloadUnixSocketPath,
}

for _, opt := range options {
opt(d)
}

// Initialize dfdaemon path.
d.daemonSockPath = filepath.Join(d.workHome, "daemon.sock")
d.daemonLockPath = filepath.Join(d.workHome, "daemon.lock")
d.dfgetLockPath = filepath.Join(d.workHome, "dfget.lock")

Expand All @@ -129,6 +137,11 @@ func New(options ...Option) (Dfpath, error) {
cache.err = multierror.Append(cache.err, err)
}

// Create unix socket directory.
if err := os.MkdirAll(path.Dir(d.daemonSockPath), fs.FileMode(0755)); err != nil {
cache.err = multierror.Append(cache.err, err)
}

// Create cache directory.
if d.cacheDir != "" {
if err := os.MkdirAll(d.cacheDir, fs.FileMode(0755)); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/dfpath/dfpath_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
var DefaultDataDir = filepath.Join(DefaultWorkHome, "data")
var DefaultPluginDir = filepath.Join(DefaultWorkHome, "plugins")
var DefaultDownloadUnixSocketPath = filepath.Join(DefaultWorkHome, "dfdaemon.sock")
1 change: 1 addition & 0 deletions pkg/dfpath/dfpath_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ var DefaultConfigDir = "/etc/dragonfly"
var DefaultLogDir = "/var/log/dragonfly"
var DefaultDataDir = "/var/lib/dragonfly"
var DefaultPluginDir = "/usr/local/dragonfly/plugins"
var DefaultDownloadUnixSocketPath = "/var/run/dfdaemon.sock"
21 changes: 21 additions & 0 deletions pkg/dfpath/dfpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestNew(t *testing.T) {
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), "")
assert.Equal(d.PluginDir(), DefaultPluginDir)
assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
},
},
{
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestNew(t *testing.T) {
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), "")
assert.Equal(d.PluginDir(), DefaultPluginDir)
assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
},
},
{
Expand All @@ -100,6 +102,7 @@ func TestNew(t *testing.T) {
assert.Equal(d.LogDir(), "foo")
assert.Equal(d.DataDir(), "")
assert.Equal(d.PluginDir(), DefaultPluginDir)
assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
},
},
{
Expand All @@ -116,6 +119,24 @@ func TestNew(t *testing.T) {
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), "")
assert.Equal(d.PluginDir(), "foo")
assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
},
},
{
name: "new dfpath by daemonSockPath",
options: []Option{WithDownloadUnixSocketPath("foo")},
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
cache.Once = sync.Once{}
cache.err = &multierror.Error{}
d, err := New(options...)
assert.NoError(err)
assert.Equal(d.WorkHome(), DefaultWorkHome)
assert.Equal(d.CacheDir(), "")
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), "")
assert.Equal(d.PluginDir(), DefaultPluginDir)
assert.Equal(d.DaemonSockPath(), "foo")
},
},
}
Expand Down