From 2bd2068c999cda5bda8c0787ed0dcaac6cb7afdb Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期四, 31 十月 2019 13:27:46 +0800
Subject: [PATCH] feat: add system reboot api

---
 controllers/syssetcont.go |   56 +++++++++++++++++++++++----
 extend/sys/system.go      |   55 +++++++++++++++++++++------
 router/router.go          |    2 +
 3 files changed, 92 insertions(+), 21 deletions(-)

diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 4045ebf..96705e3 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"regexp"
 	"webserver/extend/code"
 	"webserver/extend/config"
 	"webserver/extend/sys"
@@ -196,7 +197,6 @@
 // @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/sysset/saveDevInfo [POST]
 func (sset SysSetController) SaveDevInfo(c *gin.Context) {
-
 	dev_name := c.PostForm("server_name")
 	var api dbapi.SysSetApi
 	paramBody := make(map[string]interface{}, 0)
@@ -298,17 +298,14 @@
 		return
 	}
 
-	isIP, err := sys.RunNTPDate(ntpServer)
-	if !isIP {
-		util.ResponseFormat(c, code.RequestParamError, "鎸囧畾鐨勬湇鍔″櫒鍦板潃閿欒")
+	isConn := sys.RunNTPDate(ntpServer)
+	if !isConn {
+		util.ResponseFormat(c, code.RequestParamError, "NTP鏈嶅姟鍣ㄤ笉鍙敤")
 		return
 	}
 
-	if err == nil {
-		util.ResponseFormat(c, code.Success, "")
-	} else {
-		util.ResponseFormat(c, code.TaskStoped, "")
-	}
+	util.ResponseFormat(c, code.Success, "")
+
 }
 
 // @Security ApiKeyAuth
@@ -392,3 +389,44 @@
 
 	util.ResponseFormat(c, code.Success, "姝e湪閲嶅惎")
 }
+
+// @Summary 鑾峰彇瀹氭椂閲嶅惎
+// @Description 鑾峰彇瀹氭椂閲嶅惎浠诲姟鐨勯厤缃鍒�
+// @Produce json
+// @Tags sysset
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
+// @Router /data/api-v/sysset/rebootTask [GET]
+func (sset SysSetController) GetRebootTask(c *gin.Context) {
+	_, msg := sys.ReadRebootTaskInCrontab()
+	util.ResponseFormat(c, code.Success, msg)
+}
+
+// @Summary 璁剧疆瀹氭椂閲嶅惎
+// @Description 璁剧疆瀹氭椂閲嶅惎浠诲姟鐨勯厤缃鍒�
+// @Produce json
+// @Tags sysset
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
+// @Router /data/api-v/sysset/rebootTask [POST]
+func (sset SysSetController) SetRebootTask(c *gin.Context) {
+	task := c.PostForm("task")
+	if len(task) < 1 {
+		sys.CleanRebootTask()
+		util.ResponseFormat(c, code.Success, "閰嶇疆鎴愬姛")
+		return
+	}
+
+	regEx := `^([0-9*]+\s){4}[0-9*]+$`
+	if r, _ := regexp.MatchString(regEx, task); !r {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟閿欒")
+		return
+	}
+
+	if r := sys.UpdateRebootTask(task); !r {
+		util.ResponseFormat(c, code.UpdateFail, "閰嶇疆澶辫触")
+		return
+	}
+
+	util.ResponseFormat(c, code.Success, "閰嶇疆鎴愬姛")
+}
diff --git a/extend/sys/system.go b/extend/sys/system.go
index cccba7e..b1bb46d 100644
--- a/extend/sys/system.go
+++ b/extend/sys/system.go
@@ -119,8 +119,7 @@
 func NTPConfig() (bool, string, string) {
 	status, server, interval := false, "", ""
 
-	cmd := exec.Command("/bin/sh", "-c", "crontab -l | grep ntpdate | tr -d '\n'")
-	cron, _ := cmd.Output()
+	cron, _ := execRootCommand("crontab -l | grep ntpdate | sed -z -r 's/([^0-9* ]+)(.+)//g'")
 	if task := string(cron); task != "" {
 		status = true
 		slice := strings.Split(task, " ")
@@ -160,8 +159,6 @@
 		return false
 	}
 
-	// args := []string{"-s", newTime}
-	// exec.Command("date", args...).Run()
 	dateCMD := fmt.Sprintf("date -s \"%s\"", newTime)
 	execRootCommand(dateCMD)
 	stopNTPCron()
@@ -169,7 +166,7 @@
 	return true
 }
 
-const NTPCRONTABFILE = "~/.webServer.crontab"
+const NTPCRONTABFILE = "/tmp/.webServer.crontab"
 
 func EnableNTPCron(server string, interval int) bool {
 	stopNTPCron()
@@ -178,24 +175,32 @@
 		return false
 	}
 
-	addTask := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s; crontab %s", interval, server, NTPCRONTABFILE, NTPCRONTABFILE)
-	exec.Command("/bin/sh", "-c", addTask).Run()
+	update := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s", interval, server, NTPCRONTABFILE)
+	execRootCommand(update)
+
+	addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
+	execRootCommand(addNTPTask)
 
 	return true
 }
 
 func stopNTPCron() {
-	cleanTask := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s; crontab %s", NTPCRONTABFILE, NTPCRONTABFILE)
-	exec.Command("/bin/sh", "-c", cleanTask).Run()
+	update := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s", NTPCRONTABFILE)
+	execRootCommand(update)
+
+	cleanNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
+	execRootCommand(cleanNTPTask)
 }
 
-func RunNTPDate(server string) (bool, error) {
+func RunNTPDate(server string) bool {
 	if ip := net.ParseIP(server); ip == nil {
-		return false, errors.New("鍙傛暟閿欒")
+		return false
 	}
 
 	ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server)
-	return true, exec.Command("/bin/sh", "-c", ntpdate).Run()
+	_, err := execRootCommand(ntpdate)
+
+	return err == nil
 }
 
 func Reboot() (bool, string) {
@@ -203,3 +208,29 @@
 
 	return err == nil, string(stdout)
 }
+
+// * * * * * /bin/echo "$(date) Perform basic-reboot-task" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log
+func ReadRebootTaskInCrontab() (bool, string) {
+	stdout, err := execRootCommand("crontab -l | grep basic-reboot-task | sed -z -r 's/([^0-9* ]+)(.+)//g'")
+	return err == nil, string(stdout)
+}
+
+func CleanRebootTask() {
+	update := fmt.Sprintf("crontab -l | grep -v basic-reboot-task > %s", NTPCRONTABFILE)
+	execRootCommand(update)
+
+	crontab := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
+	execRootCommand(crontab)
+}
+
+func UpdateRebootTask(task string) bool {
+	CleanRebootTask()
+
+	tasks := fmt.Sprintf("%s /bin/echo \"$(date) Perform basic-reboot-task\" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log", task)
+	update := fmt.Sprintf("echo '%s' >> %s", tasks, NTPCRONTABFILE)
+	execRootCommand(update)
+	addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
+	_, err := execRootCommand(addNTPTask)
+
+	return err == nil
+}
diff --git a/router/router.go b/router/router.go
index 2d1530d..4f3ee1e 100644
--- a/router/router.go
+++ b/router/router.go
@@ -210,6 +210,8 @@
 		vsset.GET("/sysThresholds", ssController.GetSysThresholds)
 
 		vsset.GET("/reboot", ssController.RebootOS)
+		vsset.GET("/rebootTask", ssController.GetRebootTask)
+		vsset.POST("/rebootTask", ssController.SetRebootTask)
 	}
 
 	//绠楁硶搴撴搷浣�

--
Gitblit v1.8.0