Skip to content

Commit

Permalink
Merge pull request #45 from whoisix/master
Browse files Browse the repository at this point in the history
修复任务列表下次执行时间问题,修改错别字
  • Loading branch information
george518 authored May 17, 2021
2 parents 78efeef + b3678fa commit fdb6ab5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion controllers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ func (self *TaskController) Table() {

TaskServerIdsArr := strings.Split(v.ServerIds, ",")
serverId := 0
if len(TaskServerIdsArr) > 1 {
if len(TaskServerIdsArr) > 0 {
serverId, _ = strconv.Atoi(TaskServerIdsArr[0])
}
jobskey := libs.JobKey(v.Id, serverId)
Expand Down
25 changes: 18 additions & 7 deletions jobs/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,19 @@ func RemoteCommandJob(id int, serverId int, name string, command string, servers
jobresult.OutMsg = ""
jobresult.ErrMsg = ""
jobresult.IsTimeout = false
jobresult.IsOk = true

key, err := ioutil.ReadFile(servers.PrivateKeySrc)
if err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("读取私钥失败,%v", err.Error())
return
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("创建签名失败,%v", err.Error())
return
}
addr := fmt.Sprintf("%s:%d", servers.ServerIp, servers.Port)
Expand All @@ -250,6 +253,7 @@ func RemoteCommandJob(id int, serverId int, name string, command string, servers
client, err := ssh.Dial("tcp", addr, config)
if err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败,%v", err.Error())
return
}

Expand All @@ -258,6 +262,7 @@ func RemoteCommandJob(id int, serverId int, name string, command string, servers
session, err := client.NewSession()
if err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败,%v", err.Error())
return
}
defer session.Close()
Expand All @@ -272,13 +277,11 @@ func RemoteCommandJob(id int, serverId int, name string, command string, servers

//session.Output(command)
if err := session.Run(command); err != nil {
jobresult.ErrMsg = err.Error()
jobresult.IsOk = false
return
}
jobresult.OutMsg = b.String()
jobresult.ErrMsg = c.String()
jobresult.IsOk = true
jobresult.IsTimeout = false
return
}
return job
Expand Down Expand Up @@ -306,6 +309,7 @@ func RemoteCommandJobByPassword(id int, serverId int, name string, command strin
jobresult.OutMsg = ""
jobresult.ErrMsg = ""
jobresult.IsTimeout = false
jobresult.IsOk = true

// get auth method
auth = make([]ssh.AuthMethod, 0)
Expand All @@ -325,6 +329,7 @@ func RemoteCommandJobByPassword(id int, serverId int, name string, command strin

if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("连接服务器失败,%v", err.Error())
return
}

Expand All @@ -333,6 +338,7 @@ func RemoteCommandJobByPassword(id int, serverId int, name string, command strin
// create session
if session, err = client.NewSession(); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("连接服务器失败,%v", err.Error())
return
}

Expand All @@ -343,12 +349,9 @@ func RemoteCommandJobByPassword(id int, serverId int, name string, command strin
//session.Output(command)
if err := session.Run(command); err != nil {
jobresult.IsOk = false
return
}
jobresult.OutMsg = b.String()
jobresult.ErrMsg = c.String()
jobresult.IsOk = true
jobresult.IsTimeout = false
return
}

Expand All @@ -369,11 +372,13 @@ func RemoteCommandJobByTelnetPassword(id int, serverId int, name string, command
jobresult.OutMsg = ""
jobresult.ErrMsg = ""
jobresult.IsTimeout = false
jobresult.IsOk = true

addr := fmt.Sprintf("%s:%d", servers.ServerIp, servers.Port)
conn, err := gote.DialTimeout("tcp", addr, timeout)
if err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败0,%v", err.Error())
return
}

Expand All @@ -383,26 +388,31 @@ func RemoteCommandJobByTelnetPassword(id int, serverId int, name string, command

if _, err = conn.Read(buf); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-1,%v", err.Error())
return
}

if _, err = conn.Write([]byte(servers.ServerAccount + "\r\n")); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-2,%v", err.Error())
return
}

if _, err = conn.Read(buf); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-3,%v", err.Error())
return
}

if _, err = conn.Write([]byte(servers.Password + "\r\n")); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-4,%v", err.Error())
return
}

if _, err = conn.Read(buf); err != nil {
jobresult.IsOk = false
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-5,%v", err.Error())
return
}

Expand All @@ -419,6 +429,7 @@ func RemoteCommandJobByTelnetPassword(id int, serverId int, name string, command
for _, c := range commandArr {
_, err = conn.Write([]byte(c + "\r\n"))
if err != nil {
jobresult.ErrMsg = fmt.Sprintf("服务器连接失败-6,%v", err.Error())
jobresult.IsOk = false
return
}
Expand Down Expand Up @@ -717,7 +728,7 @@ func (j *Job) Run() {
content = strings.Replace(content, "{{TaskId}}", strconv.Itoa(j.Task.Id), -1)
content = strings.Replace(content, "{{ServerId}}", strconv.Itoa(j.ServerId), -1)
content = strings.Replace(content, "{{TaskName}}", j.Task.TaskName, -1)
content = strings.Replace(content, "{{ExecuteCommand}}", j.Task.Command, -1)
content = strings.Replace(content, "{{ExecuteCommand}}", strings.Replace(j.Task.Command, "\"", "\\\"", -1), -1)
content = strings.Replace(content, "{{ExecuteTime}}", beego.Date(time.Unix(log.CreateTime, 0), "Y-m-d H:i:s"), -1)
content = strings.Replace(content, "{{ProcessTime}}", strconv.FormatFloat(float64(log.ProcessTime)/1000, 'f', 6, 64), -1)
content = strings.Replace(content, "{{ExecuteStatus}}", TextStatus[status], -1)
Expand Down
2 changes: 1 addition & 1 deletion ppgo_job2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ ALTER TABLE `pp_task_server` ADD `connection_type` TINYINT(1) NOT NULL DEFAULT '
COMMIT;

BEGIN;
ALTER TABLE `pp_task` CHANGE COLUMN `server_id` `server_ids` varchar(200) NOT NULL DEFAULT '0' COMMENT '服务器id字符串,英文都好隔开';
ALTER TABLE `pp_task` CHANGE COLUMN `server_id` `server_ids` varchar(200) NOT NULL DEFAULT '0' COMMENT '服务器id字符串,英文逗号隔开';
COMMIT;

BEGIN;
Expand Down

0 comments on commit fdb6ab5

Please sign in to comment.