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

cmd/gateway&webdav: gateways and webdav support daemon running #3993

Merged
merged 3 commits into from
Nov 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,41 +153,15 @@ func getCmdMount(mp string) (uid, pid, cmd string, err error) {
return uid, pid, cmd, nil
}

func getDefaultLogDir(requireRootPrivileges bool) (string, error) {
var defaultLogDir = "/var/log"
switch runtime.GOOS {
case "linux":
if os.Getuid() == 0 {
break
}
fallthrough
case "darwin":
if requireRootPrivileges {
defaultLogDir = "/var/root/.juicefs"
break
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory")
}
defaultLogDir = filepath.Join(homeDir, ".juicefs")
}
return defaultLogDir, nil
}

var logArg = regexp.MustCompile(`--log(\s*=?\s*)(\S+)`)

func getLogPath(cmd string, requireRootPrivileges bool) (string, error) {
func getLogPath(cmd string) (string, error) {
var logPath string
tmp := logArg.FindStringSubmatch(cmd)
if len(tmp) == 3 {
logPath = tmp[2]
} else {
defaultLogDir, err := getDefaultLogDir(requireRootPrivileges)
if err != nil {
return "", err
}
logPath = filepath.Join(defaultLogDir, "juicefs.log")
logPath = filepath.Join(getDefaultLogDir(), "juicefs.log")
}

return logPath, nil
Expand Down Expand Up @@ -463,7 +437,7 @@ func collectLog(ctx *cli.Context, cmd string, requireRootPrivileges bool, currDi
logger.Warnf("The juicefs mount by foreground, the log will not be collected")
return nil
}
logPath, err := getLogPath(cmd, requireRootPrivileges)
logPath, err := getLogPath(cmd)
if err != nil {
return fmt.Errorf("failed to get log path: %v", err)
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"path"
"strconv"
"syscall"
"time"
Expand All @@ -42,10 +43,20 @@ import (

func cmdGateway() *cli.Command {
selfFlags := []cli.Flag{
&cli.StringFlag{
Name: "log",
Usage: "path for gateway log",
Value: path.Join(getDefaultLogDir(), "juicefs-gateway.log"),
},
&cli.StringFlag{
Name: "access-log",
Usage: "path for JuiceFS access log",
},
&cli.BoolFlag{
Name: "background",
Aliases: []string{"d"},
Usage: "run in background",
},
&cli.BoolFlag{
Name: "no-banner",
Usage: "disable MinIO startup information",
Expand Down Expand Up @@ -179,6 +190,13 @@ func initForSvc(c *cli.Context, mp string, metaUrl string) (*vfs.Config, *fs.Fil
removePassword(metaUrl)
metaConf := getMetaConf(c, mp, c.Bool("read-only"))
metaCli := meta.NewClient(metaUrl, metaConf)

if c.Bool("background") {
if err := makeDaemonForSvc(c, metaCli); err != nil {
logger.Fatalf("make daemon: %s", err)
}
}

format, err := metaCli.Load(true)
if err != nil {
logger.Fatalf("load setting: %s", err)
Expand Down
19 changes: 19 additions & 0 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -532,6 +533,24 @@ func insideContainer() bool {
return false
}

func getDefaultLogDir() string {
var defaultLogDir = "/var/log"
switch runtime.GOOS {
case "linux":
if os.Getuid() == 0 {
break
}
fallthrough
case "darwin":
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Fatalf("%v", err)
}
defaultLogDir = path.Join(homeDir, ".juicefs")
}
return defaultLogDir
}

func updateFstab(c *cli.Context) error {
addr := expandPathForEmbedded(c.Args().Get(0))
mp := c.Args().Get(1)
Expand Down
44 changes: 28 additions & 16 deletions cmd/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ func makeDaemon(c *cli.Context, name, mp string, m meta.Meta) error {
return err
}

func makeDaemonForSvc(c *cli.Context, m meta.Meta) error {
var attrs godaemon.DaemonAttr
logfile := c.String("log")
attrs.OnExit = func(stage int) error {
return nil
}

// the current dir will be changed to root in daemon,
// so the mount point has to be an absolute path.
if godaemon.Stage() == 0 {
var err error
attrs.Stdout, err = os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
logger.Infof("open log file %s: %s", logfile, err)
if err != nil {
logger.Errorf("open log file %s: %s", logfile, err)
}
}
if godaemon.Stage() <= 1 {
err := m.Shutdown()
if err != nil {
logger.Errorf("shutdown: %s", err)
}
}
_, _, err := godaemon.MakeDaemon(&attrs)
return err
}

func fuseFlags() []cli.Flag {
return addCategories("FUSE", []cli.Flag{
&cli.BoolFlag{
Expand Down Expand Up @@ -131,21 +158,6 @@ func fuseFlags() []cli.Flag {
}

func mount_flags() []cli.Flag {
var defaultLogDir = "/var/log"
switch runtime.GOOS {
case "linux":
if os.Getuid() == 0 {
break
}
fallthrough
case "darwin":
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Fatalf("%v", err)
return nil
}
defaultLogDir = path.Join(homeDir, ".juicefs")
}
selfFlags := []cli.Flag{
&cli.BoolFlag{
Name: "d",
Expand All @@ -158,7 +170,7 @@ func mount_flags() []cli.Flag {
},
&cli.StringFlag{
Name: "log",
Value: path.Join(defaultLogDir, "juicefs.log"),
Value: path.Join(getDefaultLogDir(), "juicefs.log"),
Usage: "path of log file when running in background",
},
&cli.BoolFlag{
Expand Down
5 changes: 5 additions & 0 deletions cmd/mount_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func makeDaemon(c *cli.Context, name, mp string, m meta.Meta) error {
return nil
}

func makeDaemonForSvc(c *cli.Context, m meta.Meta) error {
logger.Warnf("Cannot run in background in Windows.")
return nil
}

func mount_main(v *vfs.VFS, c *cli.Context) {
winfsp.Serve(v, c.String("o"), c.Float64("file-cache-to"), c.Bool("as-root"), c.Int("delay-close"))
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd

import (
"os"
"path"

"github.com/juicedata/juicefs/pkg/fs"
"github.com/urfave/cli/v2"
Expand All @@ -45,10 +46,20 @@ func cmdWebDav() *cli.Command {
Name: "disallowList",
Usage: "disallow list a directory",
},
&cli.StringFlag{
Name: "log",
Usage: "path for WebDAV log",
Value: path.Join(getDefaultLogDir(), "juicefs-webdav.log"), //nolint:typecheck
},
&cli.StringFlag{
Name: "access-log",
Usage: "path for JuiceFS access log",
},
&cli.BoolFlag{
Name: "background",
Aliases: []string{"d"},
Usage: "run in background",
},
}

return &cli.Command{
Expand Down