-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitw-http.go
365 lines (336 loc) · 10.7 KB
/
gitw-http.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* gitw-http.go - simple utility that shows gitw projects and the checkout,
* build and test statuses
*
* version 0.1
*
* Copyright (c) 2011, Luka Napotnik <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"io"
"net/http"
"log"
"encoding/gob"
"strings"
"strconv"
"encoding/json"
"fmt"
"os"
"io/ioutil"
"text/template"
"html"
)
type LongTestConfig struct {
Executable string
FollowChildren string
}
type Repository struct {
Repository string
Source string
Name string
Location string
Build string
Test string
LongTest LongTestConfig
OutputDirectory string
NotifyEmail string
CommitMessage string
Description string
CommitNumber int
CheckoutSucc bool
BuildSucc bool
TestSucc bool
Touched bool
LongTestRunning bool
}
type SystemSnapshot struct {
Name string
Pid int
CpuUsage int
MemoryUsage int
OpenFiles int
}
func LoadRepository(configFile string) Repository {
r := strings.NewReader(configFile)
decoder := json.NewDecoder(r)
repo := Repository{}
e := decoder.Decode(&repo)
if e != nil {
fmt.Printf("Error decoding repository configuration from file: %s\n", e)
}
return repo
}
func LoadSystemSnapshot(fileName string) *SystemSnapshot {
gob.Register(&SystemSnapshot{})
snapshotText, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Printf("Unable to open system snapshot output file: %s\n", err.Error())
return nil
}
r := strings.NewReader(string(snapshotText))
decoder := json.NewDecoder(r)
snapshot := SystemSnapshot{}
e := decoder.Decode(&snapshot)
if e != nil {
fmt.Printf("Error decoding repository configuration from file: %s\n", e)
}
return &snapshot
}
func LoadRepositories(configDir string) []Repository {
gob.Register(&Repository{})
repositories := make([]Repository, 0)
if configDir[0] != '/' { // have relative path
cwd, _ := os.Getwd()
configDir = cwd + "/" + configDir
}
fmt.Printf(":: Reading repositories from '%s' ...\n", configDir)
ctx, err := ioutil.ReadDir(configDir)
if err != nil {
fmt.Printf("Error reading configuration directory: %s\n", err)
return nil
}
for _, file := range ctx {
if file.Mode().IsDir() {
continue
}
if strings.HasSuffix(file.Name(), ".json") == false {
continue
}
fileCtx, err := ioutil.ReadFile(configDir + "/" + file.Name())
if err != nil {
fmt.Printf("Error reading repository configuration from '%s': %s\n", file.Name(), err)
continue
}
repository := LoadRepository(string(fileCtx))
repositories = append(repositories, repository)
}
return repositories
}
func (repository *Repository) AnalyzeCheckut() bool {
out, err := ioutil.ReadFile(repository.OutputDirectory + "/" + repository.Name + "-checkout-output.txt")
if err != nil {
fmt.Printf(":: Error opening checkout output for '%s': %s\n", repository.Name, err.Error())
repository.CheckoutSucc = false
return false
}
out_str := string(out)
line_n := 0
line := []byte{}
for _, ch := range(out_str) {
ch_int := int(ch)
if ch_int == 0 {
continue
}
if ch_int == 10 || ch_int == 13 {
line_n++
if line_n == 3 {
break
}
line = []byte{}
}
line = append(line, byte(ch))
}
tokens := strings.Split(string(line), "|")
if len(tokens) >= 2 {
repository.CommitNumber, err = strconv.Atoi(tokens[0][1:len(tokens[0])])
if err != nil {
fmt.Printf(":: NOTE - error reading comit number: %s\n", err.Error())
}
repository.CommitMessage = tokens[1]
}
if strings.Index(out_str, "succ") == 0 {
repository.CheckoutSucc = true
return true
}
repository.CheckoutSucc = false
return false
}
func (repository *Repository) AnalyzeBuild() bool {
out, err := ioutil.ReadFile(repository.OutputDirectory + "/" + repository.Name + "-build-output.txt")
if err != nil {
fmt.Printf(":: Error opening build output for '%s': %s\n", repository.Name, err.Error())
repository.BuildSucc = false
return false
}
if strings.Index(string(out), "succ") == 0 {
repository.BuildSucc = true
return true
}
repository.BuildSucc = false
return false
}
func (repository *Repository) AnalyzeTest() bool {
out, err := ioutil.ReadFile(repository.OutputDirectory + "/" + repository.Name + "-test-output.txt")
if err != nil {
fmt.Printf(":: Error opening build output for '%s': %s\n", repository.Name, err.Error())
repository.TestSucc = false
return false
}
if strings.Index(string(out), "succ") == 0 {
repository.TestSucc = true
return true
}
repository.TestSucc = false
return false
}
func (repository *Repository) WasTouched() bool {
logFilename := fmt.Sprintf("%s/%s-checkout-output.txt", repository.OutputDirectory, repository.Name)
_, err := os.Stat(logFilename)
if err != nil {
return false
}
repository.Touched = true
return true
}
func ViewLog(w http.ResponseWriter, req *http.Request) {
// how to get url arguments:
//fmt.Fprintf(w, "Hello, %q", html.EscapeString(req.URL.RawQuery))
path := strings.SplitN(html.EscapeString(req.URL.Path[1:]), "/", 3)
if len(path) < 3 {
fmt.Fprintf(w, "Invalid url")
return
}
name, log := path[1], path[2]
if log != "build" && log != "checkout" && log != "test" {
fmt.Fprintf(w, "Log not valid, must be either build, checkout or test")
return
}
found := false
log_filename := ""
fmt.Printf("Showing %s for repository %s\n", log, name)
repositories := LoadRepositories("repositories")
for _, repository := range repositories {
if repository.Name == name {
log_filename = fmt.Sprintf("%s/%s-%s-output.txt", repository.OutputDirectory, repository.Name, log)
found = true
break
}
}
if !found {
fmt.Fprintf(w, "Invalid repository '%s'", name)
return
}
log_file, err := ioutil.ReadFile(log_filename)
if err != nil {
fmt.Fprintf(w, "Error opening log file: %s", err.Error())
}
out := fmt.Sprintf("%s", string(log_file))
io.WriteString(w, out)
}
func ViewLongRun(w http.ResponseWriter, req *http.Request) {
path := strings.SplitN(html.EscapeString(req.URL.Path[1:]), "/", 3)
if len(path) != 2 {
fmt.Fprintf(w, "Invalid url")
return
}
name := path[1]
found := false
log_filename := ""
repositories := LoadRepositories("repositories")
for _, repository := range repositories {
if repository.Name == name {
log_filename = fmt.Sprintf("%s/%s-system-output.txt", repository.OutputDirectory, repository.Name)
found = true
break
}
}
if !found {
fmt.Fprintf(w, "Invalid repository '%s'", name)
return
}
/*
log_file, err := ioutil.ReadFile(log_filename)
if err != nil {
fmt.Fprintf(w, "Error opening log file: %s", err.Error())
}
*/
snapshot := LoadSystemSnapshot(log_filename)
fmt.Println(snapshot)
templ, err := template.ParseFiles("template-longrun.html")
if err != nil {
io.WriteString(w, err.Error())
} else {
templ.Execute(w, snapshot)
}
}
func GetLongTest(w http.ResponseWriter, req *http.Request) {
path := strings.SplitN(html.EscapeString(req.URL.Path[1:]), "/", 3)
if len(path) != 2 {
fmt.Fprintf(w, "Invalid url")
return
}
name := path[1]
found := false
log_filename := ""
repositories := LoadRepositories("repositories")
for _, repository := range repositories {
if repository.Name == name {
log_filename = fmt.Sprintf("%s/%s-system-output.txt", repository.OutputDirectory, repository.Name)
found = true
break
}
}
if !found {
fmt.Fprintf(w, "Invalid repository '%s'", name)
return
}
log_file, err := ioutil.ReadFile(log_filename)
if err != nil {
fmt.Fprintf(w, "Error opening log file: %s", err.Error())
}
io.WriteString(w, string(log_file))
}
func Index(w http.ResponseWriter, req *http.Request) {
templ, err := template.ParseFiles("template.html")
if err != nil {
io.WriteString(w, err.Error())
} else {
repositories := LoadRepositories("repositories")
for i := 0; i < len(repositories); i++ {
if repositories[i].WasTouched() {
repositories[i].AnalyzeCheckut()
repositories[i].AnalyzeBuild()
repositories[i].AnalyzeTest()
repositories[i].LongTestRunning = true
}
}
templ.Execute(w, &repositories)
}
}
func main() {
http.HandleFunc("/", Index)
http.HandleFunc("/viewlog/", ViewLog)
http.HandleFunc("/longrun/", ViewLongRun)
http.HandleFunc("/getlongtest/", GetLongTest)
// serve static files on port 12343
go http.ListenAndServe(":12343", http.FileServer(http.Dir("static")))
// and the web interface on port 12344
err := http.ListenAndServe(":12344", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}