forked from OnCloud125252/SpiderTrigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (118 loc) · 3.52 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"github.com/acarl005/stripansi"
"github.com/spf13/viper"
)
type Payload struct {
Repository struct {
ID int `json:"id"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
} `json:"repository"`
}
var defaultPort = "8000"
var defaultPath = "/auto-deploy"
var defaultToken = ""
func main() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No config file found. Using default values.")
} else {
fmt.Println("Config file found. Using user defined values.")
}
port := viper.GetString("port")
if port == "" {
port = defaultPort
}
path := viper.GetString("path")
if path == "" {
path = defaultPath
}
token := viper.GetString("token")
if token == "" {
token = defaultToken
}
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
payloadHandler(w, r, token)
})
fmt.Println("Server listening on port " + port + " ...")
fmt.Println("Auto deploy path: " + path)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func payloadHandler(w http.ResponseWriter, r *http.Request, token string) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
defer r.Body.Close()
var payload Payload
err = json.Unmarshal(body, &payload)
if err != nil {
http.Error(w, "Failed to parse JSON body", http.StatusBadRequest)
return
}
if payload.Repository.CloneURL == "" {
http.Error(w, "Missing CloneURL in payload", http.StatusBadRequest)
return
}
if payload.Repository.DefaultBranch == "" {
http.Error(w, "Missing DefaultBranch in payload", http.StatusBadRequest)
return
}
repoURL := payload.Repository.CloneURL
defaultBranch := payload.Repository.DefaultBranch
repoID := strconv.Itoa(payload.Repository.ID)
parts := strings.Split(repoURL, "github.com")
newRepoURL := fmt.Sprintf("%s%[email protected]%s", parts[0], token, parts[1])
fmt.Println("Repo URL " + repoURL)
fmt.Println("Default Branch " + defaultBranch)
fmt.Println("Repo ID " + repoID)
fmt.Println("Token " + token)
fmt.Println("Repo URL With Token " + newRepoURL)
go func() {
buildContainer := exec.Command("./docker_builder.sh", "REPO_URL="+repoURL, "DEFAULT_BRANCH="+defaultBranch, "REPO_ID="+repoID)
logFile, buildContainerError := os.Create("./logs/log-" + repoID + ".log")
if buildContainerError != nil {
panic(buildContainerError)
}
defer logFile.Close()
buildContainer.Stdout = &cleanupWriter{writer: logFile}
buildContainer.Stderr = &cleanupWriter{writer: logFile}
buildContainerError = buildContainer.Start()
buildContainer.Wait()
if buildContainerError != nil {
fmt.Println("Deployment failed for "+repoID+": ", buildContainerError)
} else {
fmt.Println("Deployment for " + repoID + " successful")
}
}()
fmt.Fprint(w, "Deployment for "+repoID+" started")
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}
// cleanupWriter is a custom writer that strips ANSI escape codes before writing to a file
type cleanupWriter struct {
writer *os.File
}
// Write writes the cleaned output to the file
func (w *cleanupWriter) Write(p []byte) (n int, err error) {
cleanOutput := stripansi.Strip(string(p))
_, err = w.writer.WriteString(cleanOutput)
return len(p), err
}