2017-03-10 09:24:06 +00:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
"time"
|
2017-04-20 01:36:42 +00:00
|
|
|
|
"github.com/ouqiang/gocron/modules/ssh"
|
2017-04-21 09:41:59 +00:00
|
|
|
|
"github.com/go-xorm/xorm"
|
2017-03-10 09:24:06 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
type TaskProtocol int8
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
const (
|
2017-04-13 09:35:59 +00:00
|
|
|
|
TaskHTTP TaskProtocol = iota + 1 // HTTP协议
|
|
|
|
|
TaskSSH // SSH命令
|
2017-04-16 18:01:41 +00:00
|
|
|
|
TaskLocalCommand // 本地命令
|
2017-03-24 05:06:53 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-03-23 05:31:16 +00:00
|
|
|
|
// 任务
|
2017-03-10 09:24:06 +00:00
|
|
|
|
type Task struct {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Id int `xorm:"int pk autoincr"`
|
2017-04-25 11:05:24 +00:00
|
|
|
|
Name string `xorm:"varchar(32) notnull"` // 任务名称
|
2017-04-07 01:13:36 +00:00
|
|
|
|
Spec string `xorm:"varchar(64) notnull"` // crontab
|
2017-04-24 05:57:39 +00:00
|
|
|
|
Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command 3: 系统命令
|
2017-04-25 11:05:24 +00:00
|
|
|
|
Command string `xorm:"varchar(256) notnull"` // URL地址或shell命令
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Timeout int `xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
|
2017-04-25 17:47:38 +00:00
|
|
|
|
Multi int8 `xorm:"tinyint notnull default 1"` // 是否允许多实例运行
|
2017-04-21 06:50:40 +00:00
|
|
|
|
RetryTimes int8 `xorm:"tinyint notnull default 0"` // 重试次数
|
2017-04-13 09:35:59 +00:00
|
|
|
|
HostId int16 `xorm:"smallint notnull default 0"` // SSH host id,
|
2017-04-30 17:12:07 +00:00
|
|
|
|
NotifyStatus int8 `xorm:"smallint notnull default 1"` // 任务执行结束是否通知 0: 不通知 1: 失败通知 2: 执行结束通知
|
|
|
|
|
NotifyType int8 `xorm:"smallint notnull default 0"` // 通知类型 1: 邮件 2: slack
|
|
|
|
|
NotifyReceiverId string `xorm:"varchar(256) notnull default '' "` // 通知接受者ID, setting表主键ID,多个ID逗号分隔
|
2017-04-25 11:54:06 +00:00
|
|
|
|
Remark string `xorm:"varchar(100) notnull default ''"` // 备注
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Created time.Time `xorm:"datetime notnull created"` // 创建时间
|
|
|
|
|
Deleted time.Time `xorm:"datetime deleted"` // 删除时间
|
|
|
|
|
Status Status `xorm:"tinyint notnull default 1"` // 状态 1:正常 0:停止
|
2017-04-23 06:11:53 +00:00
|
|
|
|
BaseModel `xorm:"-"`
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-10 09:37:16 +00:00
|
|
|
|
type TaskHost struct {
|
|
|
|
|
Task `xorm:"extends"`
|
|
|
|
|
Name string
|
|
|
|
|
Port int
|
|
|
|
|
Username string
|
2017-04-13 09:35:59 +00:00
|
|
|
|
Alias string
|
2017-04-20 01:36:42 +00:00
|
|
|
|
AuthType ssh.HostAuthType
|
2017-04-10 09:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (TaskHost) TableName() string {
|
2017-04-26 08:17:18 +00:00
|
|
|
|
return TablePrefix + "task"
|
2017-04-10 09:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
|
// 新增
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Create() (insertId int, err error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
_, err = Db.Insert(task)
|
|
|
|
|
if err == nil {
|
|
|
|
|
insertId = task.Id
|
|
|
|
|
}
|
2017-03-24 09:55:44 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 02:17:03 +00:00
|
|
|
|
func (task *Task) UpdateBean(id int) (int64, error) {
|
2017-04-30 17:12:07 +00:00
|
|
|
|
return Db.ID(id).Cols("name,spec,protocol,command,timeout,multi,retry_times,host_id,remark,status,notify_status,notify_type,notify_receiver_id").Update(task)
|
2017-04-17 10:04:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
|
// 更新
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Update(id int, data CommonMap) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Table(task).ID(id).Update(data)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Delete(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Id(id).Delete(task)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 禁用
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Disable(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return task.Update(id, CommonMap{"status": Disabled})
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 激活
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Enable(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return task.Update(id, CommonMap{"status": Enabled})
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 01:36:42 +00:00
|
|
|
|
// 获取所有激活任务
|
2017-04-10 09:37:16 +00:00
|
|
|
|
func (task *Task) ActiveList() ([]TaskHost, error) {
|
|
|
|
|
list := make([]TaskHost, 0)
|
2017-05-01 05:59:52 +00:00
|
|
|
|
fields := "t.*, host.alias,host.name,host.username,host.port,host.auth_type"
|
2017-04-26 08:17:18 +00:00
|
|
|
|
err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.status = ?", Enabled).Cols(fields).Find(&list)
|
2017-03-24 09:55:44 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return list, err
|
2017-03-24 09:55:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 01:36:42 +00:00
|
|
|
|
// 获取某个主机下的所有激活任务
|
2017-04-28 03:54:46 +00:00
|
|
|
|
func (task *Task) ActiveListByHostId(hostId int16) ([]TaskHost, error) {
|
2017-04-20 01:36:42 +00:00
|
|
|
|
list := make([]TaskHost, 0)
|
2017-05-01 05:59:52 +00:00
|
|
|
|
fields := "t.*, host.alias,host.name,host.username,host.port,host.auth_type"
|
2017-04-26 08:17:18 +00:00
|
|
|
|
err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.status = ? AND t.host_id = ?", Enabled, hostId).Cols(fields).Find(&list)
|
2017-04-20 01:36:42 +00:00
|
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
// 判断主机id是否有引用
|
|
|
|
|
func (task *Task) HostIdExist(hostId int16) (bool, error) {
|
|
|
|
|
count, err := Db.Where("host_id = ?", hostId).Count(task);
|
|
|
|
|
|
|
|
|
|
return count > 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断任务名称是否存在
|
2017-04-17 10:04:30 +00:00
|
|
|
|
func (task *Task) NameExist(name string, id int) (bool, error) {
|
|
|
|
|
if id > 0 {
|
|
|
|
|
count, err := Db.Where("name = ? AND status = ? AND id != ?", name, Enabled, id).Count(task);
|
|
|
|
|
return count > 0, err
|
|
|
|
|
}
|
2017-04-13 09:35:59 +00:00
|
|
|
|
count, err := Db.Where("name = ? AND status = ?", name, Enabled).Count(task);
|
|
|
|
|
|
|
|
|
|
return count > 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func(task *Task) Detail(id int) (TaskHost, error) {
|
|
|
|
|
taskHost := TaskHost{}
|
2017-05-01 05:59:52 +00:00
|
|
|
|
fields := "t.*, host.alias,host.name,host.username,host.port,host.auth_type"
|
2017-04-26 08:17:18 +00:00
|
|
|
|
_, err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.id=?", id).Cols(fields).Get(&taskHost)
|
2017-04-10 09:37:16 +00:00
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
return taskHost, err
|
2017-04-10 09:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
|
func (task *Task) List(params CommonMap) ([]TaskHost, error) {
|
|
|
|
|
task.parsePageAndPageSize(params)
|
2017-04-10 09:37:16 +00:00
|
|
|
|
list := make([]TaskHost, 0)
|
2017-04-26 08:17:18 +00:00
|
|
|
|
fields := "t.*, host.alias,host.name"
|
|
|
|
|
session := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id")
|
2017-04-22 15:39:33 +00:00
|
|
|
|
task.parseWhere(session, params)
|
2017-04-21 09:41:59 +00:00
|
|
|
|
err := session.Cols(fields).Desc("t.id").Limit(task.PageSize, task.pageLimitOffset()).Find(&list)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return list, err
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 06:11:53 +00:00
|
|
|
|
func (task *Task) Total(params CommonMap) (int64, error) {
|
2017-04-26 08:17:18 +00:00
|
|
|
|
session := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id")
|
2017-04-23 06:11:53 +00:00
|
|
|
|
task.parseWhere(session, params)
|
|
|
|
|
return session.Count(task)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
|
// 解析where
|
2017-04-22 15:39:33 +00:00
|
|
|
|
func (task *Task) parseWhere(session *xorm.Session, params CommonMap) {
|
2017-04-21 09:41:59 +00:00
|
|
|
|
if len(params) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-04-22 15:39:33 +00:00
|
|
|
|
id, ok := params["Id"]
|
|
|
|
|
if ok && id.(int) > 0 {
|
|
|
|
|
session.And("t.id = ?", id)
|
|
|
|
|
}
|
2017-04-21 09:41:59 +00:00
|
|
|
|
hostId, ok := params["HostId"]
|
|
|
|
|
if ok && hostId.(int) > 0 {
|
2017-04-22 15:39:33 +00:00
|
|
|
|
session.And("host_id = ?", hostId)
|
2017-04-21 09:41:59 +00:00
|
|
|
|
}
|
|
|
|
|
name, ok := params["Name"]
|
|
|
|
|
if ok && name.(string) != "" {
|
2017-04-22 15:39:33 +00:00
|
|
|
|
session.And("t.name LIKE ?", "%" + name.(string) + "%")
|
2017-04-21 09:41:59 +00:00
|
|
|
|
}
|
|
|
|
|
protocol, ok := params["Protocol"]
|
|
|
|
|
if ok && protocol.(int) > 0 {
|
|
|
|
|
session.And("protocol = ?", protocol)
|
|
|
|
|
}
|
|
|
|
|
status, ok := params["Status"]
|
|
|
|
|
if ok && status.(int) > -1 {
|
|
|
|
|
session.And("status = ?", status)
|
|
|
|
|
}
|
2017-04-26 08:17:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hostTableName() []string {
|
|
|
|
|
return []string{TablePrefix + "host", "host"}
|
2017-04-13 09:35:59 +00:00
|
|
|
|
}
|