Skip to content

Commit

Permalink
chown data dir to daemon user
Browse files Browse the repository at this point in the history
  • Loading branch information
likexian committed Feb 28, 2019
1 parent faa6343 commit 7f8a987
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 7 deletions.
5 changes: 5 additions & 0 deletions doc/CHANGS-ZH.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.102.1 Beta 版本的变更 2019-02-28

- 修正:数据目录所有者自动修改为 daemon 用户


0.101.9 Beta 版本的变更 2019-02-27

- 修正:修复磁盘和网络数据的展示
Expand Down
5 changes: 5 additions & 0 deletions doc/CHANGS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go.mod
go.sum
22 changes: 21 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -116,16 +124,28 @@ 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)
if err != nil {
SERVER_LOGGER.Fatal(err.Error())
}
}
if uid > 0 {
err := Chown(ds, uid, gid)
if err != nil {
SERVER_LOGGER.Fatal(err.Error())
}
}
}

if !DEBUG {
Expand Down
2 changes: 1 addition & 1 deletion src/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"io/ioutil"
"os"
"sort"
"time"
"strings"
"time"
)

// Status storing stat data
Expand Down
2 changes: 1 addition & 1 deletion src/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
41 changes: 41 additions & 0 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f8a987

Please sign in to comment.