forked from Jinnrry/autobuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (85 loc) · 2.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"gopkg.in/go-playground/webhooks.v5/github"
"io/ioutil"
"log"
"net/http"
"time"
)
const (
path = "/"
)
func main() {
hook, _ := github.New(github.Options.Secret("ImJw"))
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
payload, err := hook.Parse(r, github.PushEvent)
if err != nil {
if err == github.ErrEventNotFound {
// ok event wasn;t one of the ones asked to be parsed
}
}
switch payload.(type) {
case github.PushPayload:
rebuild()
}
})
http.ListenAndServe(":80", nil)
}
func rebuild(){
sshHost := "_"
sshUser := "_"
sshPassword := "_"
sshType := "_"//password 或者 key
sshKeyPath := ""//ssh id_rsa.id 路径"
sshPort := 27947
//创建sshp登陆配置
config := &ssh.ClientConfig{
Timeout: time.Second,//ssh 连接time out 时间一秒钟, 如果ssh验证错误 会在一秒内返回
User: sshUser,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
}
if sshType == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(sshPassword)}
} else {
config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(sshKeyPath)}
}
//dial 获取ssh client
addr := fmt.Sprintf("%s:%d", sshHost, sshPort)
sshClient, err := ssh.Dial("tcp", addr, config)
if err != nil {
log.Fatal("创建ssh client 失败",err)
}
defer sshClient.Close()
//创建ssh-session
session, err := sshClient.NewSession()
if err != nil {
log.Fatal("创建ssh session 失败",err)
}
defer session.Close()
//执行远程命令
combo,err := session.CombinedOutput("cd privateServer; bash ./rebuild.sh &")
if err != nil {
log.Fatal("远程执行cmd 失败",err)
}
log.Println("命令输出:",string(combo))
}
func publicKeyAuthFunc(kPath string) ssh.AuthMethod {
keyPath, err := homedir.Expand(kPath)
if err != nil {
log.Fatal("find key's home dir failed", err)
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
log.Fatal("ssh key file read failed", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatal("ssh key signer failed", err)
}
return ssh.PublicKeys(signer)
}