/
push.go
103 lines (81 loc) · 1.96 KB
/
push.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"
)
func runPushes(inventory Inventory, opts TestOpts) (errs int) {
input := make(chan Job)
output := make(chan Job)
// Get job count
jobs := 0
for _, image := range inventory["images"] {
jobs++
aliases := getAliasArray(image)
for range aliases {
jobs++
}
}
done := make(chan bool, jobs)
for i := 0; i < opts.Threads; i++ {
go pushWorker(input, output)
}
go reporter(output, done)
for i, image := range inventory["images"] {
input <- Job{
Image: image,
Retries: opts.Retries,
Id: i,
}
aliases := getAliasArray(image)
for _, alias := range aliases {
aliasImage := make(ImageDefinition)
aliasImage["name"] = alias
input <- Job{
Image: aliasImage,
Retries: opts.Retries,
Id: i,
}
}
}
errs = 0
for i := 0; i < jobs; i++ {
if <-done == false {
errs++
}
}
return
return
}
func pushWorker(input chan Job, output chan Job) {
for {
job := <-input
var resultString string
// Initialize Output For Image
stdout := fmt.Sprintf("# Pushed image `%v`\n\n## Push Log\n\n", job.Image["name"].(string))
resultString, job = HandleSinglePushJob(job)
stdout = stdout + resultString
job.Output = stdout
output <- job
}
}
func HandleSinglePushJob(job Job) (string, Job) {
var stdout string
// Attempt to build the image until we run out of retries
for retries := job.Retries; retries >= 0; retries-- {
// Try to build the image
result, err := pushImage(job.Image["name"].(string))
stdout = stdout + fmt.Sprintf("```\n%v\n```\n\n", string(result))
// If we fail, determine proper notice to log, else break out of retry loop
if err != nil {
stdout = stdout + fmt.Sprintf("**Failed** with error: `%v`\nRetries Remaining: %v", err, retries)
if retries == 0 {
stdout = stdout + fmt.Sprintf("... Moving on")
job.Success = false
}
stdout = stdout + fmt.Sprintf("\n\n")
} else {
job.Success = true
break
}
}
return stdout, job
}