feat: 支持调用docker容器
This commit is contained in:
48
handler/exec/exec.go
Normal file
48
handler/exec/exec.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"git.lxtend.com/qqbot/constants"
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
"git.lxtend.com/qqbot/service/exec"
|
||||
"git.lxtend.com/qqbot/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHandler("/exec", runInDocker, constants.LEVEL_TRUSTED)
|
||||
handler.RegisterHandler("/restart", restartDocker, constants.LEVEL_TRUSTED)
|
||||
}
|
||||
|
||||
func runInDocker(msg model.Message) (reply model.Reply) {
|
||||
token := util.SplitN(msg.RawMsg, 2)
|
||||
if len(token) < 2 {
|
||||
reply.ReplyMsg = "Usage: exec <command>"
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
}
|
||||
if res, err := exec.DockerContainer.CreateAndExecuteCommand(token[1]); err != nil {
|
||||
reply.ReplyMsg = "Error: " + err.Error()
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
} else {
|
||||
reply.ReplyMsg = res
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func restartDocker(msg model.Message) (reply model.Reply) {
|
||||
if err := exec.DockerContainer.RestartAndCleanContainer(); err != nil {
|
||||
reply.ReplyMsg = "Error: " + err.Error()
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
} else {
|
||||
reply.ReplyMsg = "成功重启容器"
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user