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-30 22:02:49 +00:00
|
|
|
"github.com/ouqiang/gocron/routers/base"
|
2017-05-05 08:31:24 +00:00
|
|
|
"github.com/go-macaron/binding"
|
2017-04-07 09:26:46 +00:00
|
|
|
)
|
2017-04-07 01:13:36 +00:00
|
|
|
|
2017-04-08 09:15:30 +00:00
|
|
|
func Index(ctx *macaron.Context) {
|
|
|
|
hostModel := new(models.Host)
|
2017-04-22 15:39:33 +00:00
|
|
|
queryParams := parseQueryParams(ctx)
|
2017-04-23 06:11:53 +00:00
|
|
|
total, err := hostModel.Total(queryParams)
|
2017-04-22 15:39:33 +00:00
|
|
|
hosts, err := hostModel.List(queryParams)
|
2017-04-08 09:15:30 +00:00
|
|
|
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
|
2017-04-08 09:15:30 +00:00
|
|
|
ctx.Data["Title"] = "主机列表"
|
2017-04-10 09:37:16 +00:00
|
|
|
ctx.Data["Hosts"] = hosts
|
2017-04-22 15:39:33 +00:00
|
|
|
ctx.Data["Params"] = queryParams
|
2017-04-08 09:15:30 +00:00
|
|
|
ctx.HTML(200, "host/index")
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:13:36 +00:00
|
|
|
func Create(ctx *macaron.Context) {
|
2017-04-08 09:15:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:59:52 +00:00
|
|
|
sshConfig := ssh.SSHConfig{}
|
|
|
|
sshConfig.User = hostModel.Username
|
|
|
|
sshConfig.Host = hostModel.Name
|
|
|
|
sshConfig.Port = hostModel.Port
|
|
|
|
sshConfig.ExecTimeout = 5
|
|
|
|
sshConfig.AuthType = hostModel.AuthType
|
|
|
|
var password string
|
|
|
|
var privateKey string
|
|
|
|
if hostModel.AuthType == ssh.HostPassword {
|
|
|
|
password, err = hostModel.GetPasswordByHost(hostModel.Name)
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure(err.Error(), err)
|
|
|
|
}
|
|
|
|
sshConfig.Password = password
|
|
|
|
} else {
|
|
|
|
privateKey, err = hostModel.GetPrivateKeyByHost(hostModel.Name)
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure(err.Error(), err)
|
|
|
|
}
|
|
|
|
sshConfig.PrivateKey = privateKey
|
2017-04-20 01:36:42 +00:00
|
|
|
}
|
2017-05-01 05:59:52 +00:00
|
|
|
|
2017-04-20 01:36:42 +00:00
|
|
|
_, 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-05-05 08:31:24 +00:00
|
|
|
Name string `binding:"Required;MaxSize(64)"`
|
2017-04-13 09:35:59 +00:00
|
|
|
Alias string `binding:"Required;MaxSize(32)"`
|
|
|
|
Username string `binding:"Required;MaxSize(32)"`
|
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)"`
|
2017-04-13 09:35:59 +00:00
|
|
|
Remark string
|
2017-04-07 09:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 08:31:24 +00:00
|
|
|
func (f HostForm) Error(ctx *macaron.Context, errs binding.Errors) {
|
|
|
|
if len(errs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json := utils.JsonResponse{}
|
|
|
|
content := json.CommonFailure("表单验证失败, 请检测输入")
|
|
|
|
|
|
|
|
ctx.Resp.Write([]byte(content))
|
|
|
|
}
|
|
|
|
|
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-21 02:17:03 +00:00
|
|
|
|
2017-04-07 09:26:46 +00:00
|
|
|
hostModel.Name = form.Name
|
|
|
|
hostModel.Alias = form.Alias
|
|
|
|
hostModel.Username = form.Username
|
|
|
|
hostModel.Port = form.Port
|
|
|
|
hostModel.Remark = form.Remark
|
2017-04-20 01:36:42 +00:00
|
|
|
hostModel.AuthType = form.AuthType
|
|
|
|
isCreate := false
|
|
|
|
if id > 0 {
|
2017-04-21 02:17:03 +00:00
|
|
|
_, 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)
|
2017-04-22 15:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 解析查询参数
|
|
|
|
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-30 22:02:49 +00:00
|
|
|
base.ParsePageAndPageSize(ctx, params)
|
2017-04-22 15:39:33 +00:00
|
|
|
|
|
|
|
return params
|
2017-04-07 01:13:36 +00:00
|
|
|
}
|