Merge branch 'master' of http://192.168.5.5:10010/r/webserver
| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "regexp" |
| | | "webserver/extend/code" |
| | | "webserver/extend/config" |
| | | "webserver/extend/sys" |
| | |
| | | // @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) |
| | |
| | | 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 |
| | |
| | | |
| | | util.ResponseFormat(c, code.Success, "正在重启") |
| | | } |
| | | |
| | | // @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, "配置成功") |
| | | } |
| | |
| | | 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, " ") |
| | |
| | | return false |
| | | } |
| | | |
| | | // args := []string{"-s", newTime} |
| | | // exec.Command("date", args...).Run() |
| | | dateCMD := fmt.Sprintf("date -s \"%s\"", newTime) |
| | | execRootCommand(dateCMD) |
| | | stopNTPCron() |
| | |
| | | return true |
| | | } |
| | | |
| | | const NTPCRONTABFILE = "~/.webServer.crontab" |
| | | const NTPCRONTABFILE = "/tmp/.webServer.crontab" |
| | | |
| | | func EnableNTPCron(server string, interval int) bool { |
| | | stopNTPCron() |
| | |
| | | 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) { |
| | |
| | | |
| | | 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 |
| | | } |
| | |
| | | vsset.GET("/sysThresholds", ssController.GetSysThresholds) |
| | | |
| | | vsset.GET("/reboot", ssController.RebootOS) |
| | | vsset.GET("/rebootTask", ssController.GetRebootTask) |
| | | vsset.POST("/rebootTask", ssController.SetRebootTask) |
| | | } |
| | | |
| | | //算法库操作 |