Browse Source

nps some config hot reload

刘河 6 years ago
parent
commit
65e0822b63
4 changed files with 36 additions and 6 deletions
  1. 11 1
      cmd/nps/nps.go
  2. 1 0
      conf/nps.conf
  3. 23 4
      lib/daemon/daemon.go
  4. 1 1
      web/controllers/base.go

+ 11 - 1
cmd/nps/nps.go

@@ -17,7 +17,9 @@ import (
 	_ "github.com/cnlh/nps/web/routers"
 	"log"
 	"os"
+	"os/signal"
 	"path/filepath"
+	"syscall"
 )
 
 var (
@@ -34,7 +36,7 @@ func main() {
 			test.TestServerConfig()
 			log.Println("test ok, no error")
 			return
-		case "start", "restart", "stop", "status":
+		case "start", "restart", "stop", "status","reload":
 			daemon.InitDaemon("nps", common.GetRunPath(), common.GetTmpPath())
 		case "install":
 			install.InstallNps()
@@ -64,5 +66,13 @@ func main() {
 	connection.InitConnectionService()
 	crypt.InitTls(filepath.Join(beego.AppPath, "conf", "server.pem"), filepath.Join(beego.AppPath, "conf", "server.key"))
 	tool.InitAllowPort()
+	s := make(chan os.Signal, 1)
+	signal.Notify(s, syscall.SIGUSR1)
+	go func() {
+		for {
+			<-s
+			beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf"))
+		}
+	}()
 	server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type"))
 }

+ 1 - 0
conf/nps.conf

@@ -47,4 +47,5 @@ auth_crypt_key =1234567812345678
 
 #allow_ports=9001-9009,10001,11000-12000
 
+#Web management multi-user login
 #allow_user_login=true

+ 23 - 4
lib/daemon/daemon.go

@@ -24,21 +24,40 @@ func InitDaemon(f string, runPath string, pidPath string) {
 	switch os.Args[1] {
 	case "start":
 		start(args, f, pidPath, runPath)
-		os.Exit(0)
 	case "stop":
 		stop(f, args[0], pidPath)
-		os.Exit(0)
 	case "restart":
 		stop(f, args[0], pidPath)
 		start(args, f, pidPath, runPath)
-		os.Exit(0)
 	case "status":
 		if status(f, pidPath) {
 			log.Printf("%s is running", f)
 		} else {
 			log.Printf("%s is not running", f)
 		}
-		os.Exit(0)
+	case "reload":
+		reload(f, pidPath)
+	}
+	os.Exit(0)
+}
+
+func reload(f string, pidPath string) {
+	if f == "nps" && !common.IsWindows() && !status(f, pidPath) {
+		log.Println("reload fail")
+		return
+	}
+	var c *exec.Cmd
+	var err error
+	b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
+	if err == nil {
+		c = exec.Command("/bin/bash", "-c", `kill -30 `+string(b))
+	} else {
+		log.Fatalln("reload error,pid file does not exist")
+	}
+	if c.Run() == nil {
+		log.Println("reload success")
+	} else {
+		log.Println("reload fail")
 	}
 }
 

+ 1 - 1
web/controllers/base.go

@@ -145,7 +145,7 @@ func (s *BaseController) CheckUserAuth() {
 	if s.controllerName == "index" {
 		if id := s.GetIntNoErr("id"); id != 0 {
 			belong := false
-			if strings.Contains(s.actionName, "H") {
+			if strings.Contains(s.actionName, "h") {
 				if v, ok := file.GetCsvDb().Hosts.Load(id); ok {
 					if v.(*file.Host).Client.Id == s.GetSession("clientId").(int) {
 						belong = true