-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
76 lines (67 loc) · 1.98 KB
/
pipe.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
package main
import (
"fmt"
"net/http"
"strings"
sh "github.com/codeskyblue/go-sh"
)
var tokenStr string
func main() {
if !CheckFileIsExist(tokenFile) {
tokenStr = SaveToken()
fmt.Println("Pipeline .token file not found, aoto generator a token")
}
tokenStr = GetToken()
fmt.Println("Pipeline token: " + tokenStr)
fmt.Println("Sirasagi Pipeline listening :59000")
http.HandleFunc("/pipe/", PipeHandler)
err := http.ListenAndServe("127.0.0.1:59000", nil)
if err != nil {
fmt.Println(err)
}
}
func PipeHandler(w http.ResponseWriter, r *http.Request) {
t := r.PostFormValue("token")
fmt.Println("deploy token: ", t)
project := r.PostFormValue("project")
fmt.Println("Deploy project: ", project)
version := r.PostFormValue("ver")
fmt.Println("Deploy version: ", version)
if t == "" || project == "" || t != tokenStr {
w.WriteHeader(403)
fmt.Fprintln(w, "")
return
}
go RunDockerPipeline(project, version)
fmt.Fprintln(w, "{ message: \"async deploy message\"}")
}
/**
* Run async deploy
*/
func RunDockerPipeline(project string, version string) {
projectDir := "/data/docker/" + project
projectCmd := project
dockerServiceName := strings.Replace(project, "/", "-", -1)
if version != "" {
projectCmd += ":" + version
}
// registry host
commandStr := "registry.docker.io/" + projectCmd
fmt.Println("Deploy docker service name: ", dockerServiceName)
fmt.Println("Deploy project: ", projectCmd)
fmt.Println("Deploy command: ", commandStr)
fmt.Println("Deploy dir: ", projectDir)
session := sh.NewSession()
session.ShowCMD = true
session.SetDir(projectDir)
// stop old image
session.Command("docker", "stop", dockerServiceName).Run()
// remove old runtime image
session.Command("docker", "rm", dockerServiceName).Run()
// pull new image
session.Command("docker", "pull", commandStr).Run()
// docker-compose
session.Command("docker-compose", "up", "-d").Run()
// docker rm image
session.Command("docker rmi $(docker images | grep none | awk '{print $3}')").Run()
}