From 7f8a98773b04c1581e47335ea22c1c252eb0ef74 Mon Sep 17 00:00:00 2001 From: Li Kexian Date: Thu, 28 Feb 2019 09:13:10 +0800 Subject: [PATCH] chown data dir to daemon user --- doc/CHANGS-ZH.md | 5 +++++ doc/CHANGS.md | 5 +++++ setup.sh | 2 +- src/.gitignore | 1 - src/main.go | 22 +++++++++++++++++++++- src/send.go | 2 +- src/status.go | 2 +- src/template.go | 2 +- src/utils.go | 41 +++++++++++++++++++++++++++++++++++++++++ src/version.go | 2 +- 10 files changed, 77 insertions(+), 7 deletions(-) diff --git a/doc/CHANGS-ZH.md b/doc/CHANGS-ZH.md index 948492e..d8ef01e 100644 --- a/doc/CHANGS-ZH.md +++ b/doc/CHANGS-ZH.md @@ -1,3 +1,8 @@ +0.102.1 Beta 版本的变更 2019-02-28 + +- 修正:数据目录所有者自动修改为 daemon 用户 + + 0.101.9 Beta 版本的变更 2019-02-27 - 修正:修复磁盘和网络数据的展示 diff --git a/doc/CHANGS.md b/doc/CHANGS.md index 8cfb4a8..2e072ca 100644 --- a/doc/CHANGS.md +++ b/doc/CHANGS.md @@ -1,3 +1,8 @@ +Changes with Stat Hub 0.102.1 Beta 2019-02-28 + +- Bugfix: chown data dir to daemon user + + Changes with Stat Hub 0.101.9 Beta 2019-02-27 - Bugfix: fixed disk and net stat display diff --git a/setup.sh b/setup.sh index be2b417..3ddddd5 100755 --- a/setup.sh +++ b/setup.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION="0.101.9" +VERSION="0.102.1" STATHUB_URL="https://github.com/likexian/stathub-go/releases/download/v${VERSION}" BASEDIR="/usr/local/stathub" diff --git a/src/.gitignore b/src/.gitignore index 6db3394..08cb523 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,2 +1 @@ -go.mod go.sum diff --git a/src/main.go b/src/main.go index 4b65327..2840111 100644 --- a/src/main.go +++ b/src/main.go @@ -97,6 +97,14 @@ func main() { SERVER_LOGGER.Fatal(fmt.Sprintf("configuration load failed, %s", err.Error())) } + var uid, gid int + if !DEBUG && SERVER_CONFIG.DaemonUser != "" { + uid, gid, err = daemon.LookupUser(SERVER_CONFIG.DaemonUser) + if err != nil { + SERVER_LOGGER.Fatal(err.Error()) + } + } + if SERVER_CONFIG.Role == "server" { if !FileExists(SERVER_CONFIG.BaseDir + SERVER_CONFIG.DataDir) { err := os.MkdirAll(SERVER_CONFIG.BaseDir+SERVER_CONFIG.DataDir, 0755) @@ -116,9 +124,15 @@ func main() { SERVER_LOGGER.Fatal(err.Error()) } } + if uid > 0 { + err := Chown(SERVER_CONFIG.BaseDir+SERVER_CONFIG.DataDir, uid, gid) + if err != nil { + SERVER_LOGGER.Fatal(err.Error()) + } + } } - for _, v := range []string{SERVER_CONFIG.PidFile, SERVER_CONFIG.LogFile} { + for _, v := range []string{*configFile, SERVER_CONFIG.PidFile, SERVER_CONFIG.LogFile} { ds, _ := filepath.Split(SERVER_CONFIG.BaseDir + v) if ds != "" && !FileExists(ds) { err := os.MkdirAll(ds, 0755) @@ -126,6 +140,12 @@ func main() { SERVER_LOGGER.Fatal(err.Error()) } } + if uid > 0 { + err := Chown(ds, uid, gid) + if err != nil { + SERVER_LOGGER.Fatal(err.Error()) + } + } } if !DEBUG { diff --git a/src/send.go b/src/send.go index d5481bc..4806de7 100644 --- a/src/send.go +++ b/src/send.go @@ -62,7 +62,7 @@ func httpSend(server, key, stat string) (err error) { status := jsonData.Get("status.code").MustInt(0) if status != 1 { message := jsonData.Get("status.message").MustString("unknown error") - return errors.New(message) + return errors.New("server return: " + message) } return diff --git a/src/status.go b/src/status.go index 48d7d56..b653bb3 100644 --- a/src/status.go +++ b/src/status.go @@ -15,8 +15,8 @@ import ( "io/ioutil" "os" "sort" - "time" "strings" + "time" ) // Status storing stat data diff --git a/src/template.go b/src/template.go index b7545f2..73528f5 100644 --- a/src/template.go +++ b/src/template.go @@ -11,7 +11,7 @@ package main // variable for tpl file var ( - TPL_REVHEAD = "5f7c8cf" + TPL_REVHEAD = "faa6343" TPL_CERT = map[string]string{} TPL_STATIC = map[string]string{} TPL_TEMPLATE = map[string]string{} diff --git a/src/utils.go b/src/utils.go index 517d1b1..6de0d14 100644 --- a/src/utils.go +++ b/src/utils.go @@ -102,6 +102,47 @@ func WriteFile(fname, text string) (err error) { return ioutil.WriteFile(fname, []byte(text), 0644) } +// Chown do recurse chown go file or folder +func Chown(fname string, uid, gid int) (err error) { + isDir, err := IsDir(fname) + if err != nil { + return + } + + err = os.Chown(fname, uid, gid) + if err != nil || !isDir { + return + } + + if !strings.HasSuffix(fname, "/") { + fname += "/" + } + + fs, err := ioutil.ReadDir(fname) + if err != nil { + return + } + + for _, f := range fs { + err = Chown(fname+f.Name(), uid, gid) + if err != nil { + return + } + } + + return +} + +// IsDir returns if path is a dir +func IsDir(fname string) (bool, error) { + f, err := os.Stat(fname) + if err != nil { + return false, err + } + + return f.Mode().IsDir(), nil +} + // SecondToHumanTime returns readable string for seconds func SecondToHumanTime(second int) string { if second < 60 { diff --git a/src/version.go b/src/version.go index b613257..f0eda78 100644 --- a/src/version.go +++ b/src/version.go @@ -11,7 +11,7 @@ package main // Version returns package version func Version() string { - return "0.101.9" + return "0.102.1" } // Author returns package author