gocron/internal/routers/host/host.go

202 lines
5.1 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 (
2017-09-16 09:58:33 +00:00
"fmt"
2018-01-30 11:26:04 +00:00
"strconv"
"strings"
2017-09-16 09:58:33 +00:00
"github.com/go-macaron/binding"
2018-03-25 05:12:12 +00:00
"github.com/ouqiang/gocron/internal/models"
"github.com/ouqiang/gocron/internal/modules/logger"
"github.com/ouqiang/gocron/internal/modules/rpc/client"
"github.com/ouqiang/gocron/internal/modules/rpc/grpcpool"
"github.com/ouqiang/gocron/internal/modules/rpc/proto"
"github.com/ouqiang/gocron/internal/modules/utils"
"github.com/ouqiang/gocron/internal/routers/base"
"github.com/ouqiang/gocron/internal/service"
2017-09-16 09:58:33 +00:00
"gopkg.in/macaron.v1"
2017-04-07 09:26:46 +00:00
)
2017-04-07 01:13:36 +00:00
2018-05-02 12:41:41 +00:00
const testConnectionCommand = "echo hello"
2018-05-26 03:16:36 +00:00
const testConnectionTimeout = 5
2018-05-02 12:41:41 +00:00
2017-10-01 13:49:46 +00:00
// Index 主机列表
2018-05-02 12:41:41 +00:00
func Index(ctx *macaron.Context) string {
2017-09-16 09:58:33 +00:00
hostModel := new(models.Host)
queryParams := parseQueryParams(ctx)
total, err := hostModel.Total(queryParams)
hosts, err := hostModel.List(queryParams)
if err != nil {
logger.Error(err)
}
2018-05-02 12:41:41 +00:00
jsonResp := utils.JsonResponse{}
return jsonResp.Success(utils.SuccessContent, map[string]interface{}{
"total": total,
"data": hosts,
})
}
2018-05-02 12:41:41 +00:00
// All 获取所有主机
func All(ctx *macaron.Context) string {
hostModel := new(models.Host)
hostModel.PageSize = -1
hosts, err := hostModel.List(models.CommonMap{})
if err != nil {
logger.Error(err)
}
jsonResp := utils.JsonResponse{}
return jsonResp.Success(utils.SuccessContent, hosts)
2017-04-16 08:24:09 +00:00
}
2018-05-02 12:41:41 +00:00
// Detail 主机详情
func Detail(ctx *macaron.Context) string {
2017-09-16 09:58:33 +00:00
hostModel := new(models.Host)
id := ctx.ParamsInt(":id")
err := hostModel.Find(id)
2018-05-02 12:41:41 +00:00
jsonResp := utils.JsonResponse{}
if err != nil || hostModel.Id == 0 {
2017-09-16 09:58:33 +00:00
logger.Errorf("获取主机详情失败#主机id-%d", id)
2018-05-02 12:41:41 +00:00
return jsonResp.Success(utils.SuccessContent, nil)
2017-09-16 09:58:33 +00:00
}
2018-05-02 12:41:41 +00:00
return jsonResp.Success(utils.SuccessContent, hostModel)
2017-04-07 01:13:36 +00:00
}
2017-04-07 09:26:46 +00:00
type HostForm struct {
2017-09-16 09:58:33 +00:00
Id int16
Name string `binding:"Required;MaxSize(64)"`
Alias string `binding:"Required;MaxSize(32)"`
Port int `binding:"Required;Range(1-65535)"`
Remark string
2017-04-07 09:26:46 +00:00
}
2017-10-01 13:49:46 +00:00
// Error 表单验证错误处理
2017-05-05 08:31:24 +00:00
func (f HostForm) Error(ctx *macaron.Context, errs binding.Errors) {
2017-09-16 09:58:33 +00:00
if len(errs) == 0 {
return
}
json := utils.JsonResponse{}
content := json.CommonFailure("表单验证失败, 请检测输入")
2018-05-02 12:41:41 +00:00
ctx.Write([]byte(content))
2017-05-05 08:31:24 +00:00
}
2017-10-01 13:49:46 +00:00
// Store 保存、修改主机信息
2017-09-16 09:58:33 +00:00
func Store(ctx *macaron.Context, form HostForm) string {
json := utils.JsonResponse{}
hostModel := new(models.Host)
id := form.Id
nameExist, err := hostModel.NameExists(form.Name, form.Id)
if err != nil {
return json.CommonFailure("操作失败", err)
}
if nameExist {
return json.CommonFailure("主机名已存在")
}
hostModel.Name = strings.TrimSpace(form.Name)
hostModel.Alias = strings.TrimSpace(form.Alias)
hostModel.Port = form.Port
hostModel.Remark = strings.TrimSpace(form.Remark)
isCreate := false
oldHostModel := new(models.Host)
err = oldHostModel.Find(int(id))
if err != nil {
return json.CommonFailure("主机不存在")
}
if id > 0 {
_, err = hostModel.UpdateBean(id)
} else {
isCreate = true
id, err = hostModel.Create()
}
if err != nil {
return json.CommonFailure("保存失败", err)
}
if !isCreate {
oldAddr := fmt.Sprintf("%s:%d", oldHostModel.Name, oldHostModel.Port)
newAddr := fmt.Sprintf("%s:%d", hostModel.Name, hostModel.Port)
if oldAddr != newAddr {
grpcpool.Pool.Release(oldAddr)
}
taskModel := new(models.Task)
tasks, err := taskModel.ActiveListByHostId(id)
if err != nil {
return json.CommonFailure("刷新任务主机信息失败", err)
}
2018-01-27 10:08:46 +00:00
service.ServiceTask.BatchAdd(tasks)
2017-09-16 09:58:33 +00:00
}
return json.Success("保存成功", nil)
2017-04-13 09:35:59 +00:00
}
2017-10-01 13:49:46 +00:00
// Remove 删除主机
2017-09-16 09:58:33 +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)
}
taskHostModel := new(models.TaskHost)
exist, err := taskHostModel.HostIdExist(int16(id))
if err != nil {
return json.CommonFailure("操作失败", err)
}
if exist {
return json.CommonFailure("有任务引用此主机,不能删除")
}
hostModel := new(models.Host)
err = hostModel.Find(int(id))
if err != nil {
return json.CommonFailure("主机不存在")
}
_, err = hostModel.Delete(id)
if err != nil {
return json.CommonFailure("操作失败", err)
}
addr := fmt.Sprintf("%s:%d", hostModel.Name, hostModel.Port)
grpcpool.Pool.Release(addr)
return json.Success("操作成功", nil)
}
2017-10-01 13:49:46 +00:00
// Ping 测试主机是否可连接
2017-09-16 09:58:33 +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)
}
taskReq := &rpc.TaskRequest{}
2018-05-02 12:41:41 +00:00
taskReq.Command = testConnectionCommand
taskReq.Timeout = testConnectionTimeout
2017-09-16 09:58:33 +00:00
output, err := client.Exec(hostModel.Name, hostModel.Port, taskReq)
if err != nil {
return json.CommonFailure("连接失败-"+err.Error()+" "+output, err)
}
return json.Success("连接成功", nil)
2017-06-08 13:25:42 +00:00
}
// 解析查询参数
2017-09-16 09:58:33 +00:00
func parseQueryParams(ctx *macaron.Context) models.CommonMap {
2018-01-27 10:08:46 +00:00
var params = models.CommonMap{}
2017-09-16 09:58:33 +00:00
params["Id"] = ctx.QueryInt("id")
params["Name"] = ctx.QueryTrim("name")
base.ParsePageAndPageSize(ctx, params)
return params
}