gocron/routers/host/host.go

186 lines
5.2 KiB
Go
Raw Normal View History

2017-04-07 01:13:36 +00:00
package host
2017-04-07 09:26:46 +00:00
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/utils"
"github.com/ouqiang/gocron/modules/logger"
2017-04-13 09:35:59 +00:00
"strconv"
2017-04-20 01:36:42 +00:00
"github.com/ouqiang/gocron/modules/ssh"
"github.com/ouqiang/gocron/service"
2017-04-23 06:11:53 +00:00
"github.com/Unknwon/paginater"
"fmt"
"html/template"
2017-04-07 09:26:46 +00:00
)
2017-04-07 01:13:36 +00:00
func Index(ctx *macaron.Context) {
hostModel := new(models.Host)
queryParams := parseQueryParams(ctx)
2017-04-23 06:11:53 +00:00
total, err := hostModel.Total(queryParams)
hosts, err := hostModel.List(queryParams)
if err != nil {
logger.Error(err)
}
2017-04-23 06:11:53 +00:00
name, ok := queryParams["name"].(string)
var safeNameHTML = ""
if ok {
safeNameHTML = template.HTMLEscapeString(name)
}
PageParams := fmt.Sprintf("id=%d&name=%s&page_size=%d",
queryParams["Id"], safeNameHTML, queryParams["PageSize"]);
queryParams["PageParams"] = template.URL(PageParams)
p := paginater.New(int(total), queryParams["PageSize"].(int), queryParams["Page"].(int), 5)
ctx.Data["Pagination"] = p
ctx.Data["Title"] = "主机列表"
ctx.Data["Hosts"] = hosts
ctx.Data["Params"] = queryParams
ctx.HTML(200, "host/index")
}
2017-04-07 01:13:36 +00:00
func Create(ctx *macaron.Context) {
ctx.Data["Title"] = "添加主机"
2017-04-16 08:24:09 +00:00
ctx.HTML(200, "host/host_form")
}
func Edit(ctx *macaron.Context) {
ctx.Data["Title"] = "编辑主机"
hostModel := new(models.Host)
id := ctx.ParamsInt(":id")
err := hostModel.Find(id)
if err != nil {
logger.Errorf("获取主机详情失败#主机id-%d", id)
}
ctx.Data["Host"] = hostModel
ctx.HTML(200, "host/host_form")
2017-04-07 01:13:36 +00:00
}
2017-04-20 01:36:42 +00:00
func Ping(ctx *macaron.Context) string {
id := ctx.ParamsInt(":id")
hostModel := new(models.Host)
err := hostModel.Find(id)
json := utils.JsonResponse{}
if err != nil || hostModel.Id <= 0{
return json.CommonFailure("主机不存在", err)
}
sshConfig := ssh.SSHConfig{
User: hostModel.Username,
Password: hostModel.Password,
Host: hostModel.Name,
Port: hostModel.Port,
ExecTimeout: 5,
AuthType: hostModel.AuthType,
PrivateKey: hostModel.PrivateKey,
}
_, err = ssh.Exec(sshConfig, "pwd")
if err != nil {
return json.CommonFailure("连接失败-" + err.Error(), err)
}
return json.Success("连接成功", nil)
}
2017-04-07 09:26:46 +00:00
type HostForm struct {
2017-04-20 01:36:42 +00:00
Id int16
2017-04-13 09:35:59 +00:00
Name string `binding:"Required;MaxSize(100)"`
Alias string `binding:"Required;MaxSize(32)"`
Username string `binding:"Required;MaxSize(32)"`
2017-04-20 01:36:42 +00:00
Password string
2017-04-07 09:26:46 +00:00
Port int `binding:"Required;Range(1-65535)"`
2017-04-20 01:36:42 +00:00
AuthType ssh.HostAuthType `binding:"Required:Range(1,2)"`
PrivateKey string
2017-04-13 09:35:59 +00:00
Remark string
2017-04-07 09:26:46 +00:00
}
func Store(ctx *macaron.Context, form HostForm) string {
2017-04-13 09:35:59 +00:00
json := utils.JsonResponse{}
2017-04-07 09:26:46 +00:00
hostModel := new(models.Host)
2017-04-20 01:36:42 +00:00
id := form.Id
nameExist, err := hostModel.NameExists(form.Name, form.Id)
2017-04-13 09:35:59 +00:00
if err != nil {
return json.CommonFailure("操作失败", err)
}
if nameExist {
return json.CommonFailure("主机名已存在")
}
2017-04-07 09:26:46 +00:00
hostModel.Name = form.Name
hostModel.Alias = form.Alias
hostModel.Username = form.Username
hostModel.Password = form.Password
hostModel.Port = form.Port
hostModel.Remark = form.Remark
2017-04-20 01:36:42 +00:00
hostModel.PrivateKey = form.PrivateKey
hostModel.AuthType = form.AuthType
if hostModel.AuthType == ssh.HostPublicKey {
hostModel.Password = ""
} else {
hostModel.PrivateKey = ""
}
2017-04-20 01:36:42 +00:00
isCreate := false
if id > 0 {
_, err = hostModel.UpdateBean(id)
2017-04-20 01:36:42 +00:00
} else {
isCreate = true
id, err = hostModel.Create()
}
2017-04-07 09:26:46 +00:00
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("保存失败", err)
2017-04-07 09:26:46 +00:00
}
2017-04-07 01:13:36 +00:00
2017-04-20 01:36:42 +00:00
taskModel := new(models.TaskHost)
tasks, err := taskModel.ActiveListByHostId(id)
if err != nil {
return json.CommonFailure("刷新任务主机信息失败", err)
}
if !isCreate && len(tasks) > 0 {
serviceTask := new(service.Task)
serviceTask.BatchAdd(tasks)
}
2017-04-07 09:26:46 +00:00
return json.Success("保存成功", nil)
2017-04-13 09:35:59 +00:00
}
func Remove(ctx *macaron.Context) string {
id, err := strconv.Atoi(ctx.Params(":id"))
json := utils.JsonResponse{}
if err != nil {
return json.CommonFailure("参数错误", err)
}
taskModel := new(models.Task)
exist,err := taskModel.HostIdExist(int16(id))
if err != nil {
return json.CommonFailure("操作失败", err)
}
if exist {
return json.CommonFailure("有任务引用此主机,不能删除")
}
hostModel := new(models.Host)
_, err =hostModel.Delete(id)
if err != nil {
return json.CommonFailure("操作失败", err)
}
return json.Success("操作成功", nil)
}
// 解析查询参数
func parseQueryParams(ctx *macaron.Context) (models.CommonMap) {
var params models.CommonMap = models.CommonMap{}
params["Id"] = ctx.QueryInt("id")
params["Name"] = ctx.QueryTrim("name")
2017-04-23 06:11:53 +00:00
page := ctx.QueryInt("page")
pageSize := ctx.QueryInt("page_size")
if page <= 0 {
page = 1
}
if pageSize <= 0 {
pageSize = models.PageSize
}
params["Page"] = page
params["PageSize"] = pageSize
return params
2017-04-07 01:13:36 +00:00
}