-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
207 lines (185 loc) · 5.12 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
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
package main
import (
"bufio"
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"net/http"
"net/smtp"
"os"
"os/exec"
"strings"
)
type Settings map[string]string
const BASE_DIR string = "."
const REPO_NAME string = "zfs-binaries"
// permit offline operation, for testing
/*
const FAKE_NETWORK_SERVICES bool = true
const DESTINATION_GIT_URL string = "/tmp/repo"
*/
const FAKE_NETWORK_SERVICES bool = true
const DESTINATION_GIT_URL string = "[email protected]:clusterhq/zfs-binaries"
func main() {
settings := getSettings()
kernel, channel := getBuildEnv()
operatingSystem := settings["operating_system"]
var (
exists bool // defaults to false
err error
)
// if faking network services, always build.
if !FAKE_NETWORK_SERVICES {
exists, err = checkReleaseExists(operatingSystem, channel, kernel)
}
if err != nil {
sendReport(settings, err, []byte("Error from checkReleaseExists"), kernel, channel)
return
}
if exists {
sendReport(settings, nil, []byte("Build already exists, skipping..."), kernel, channel)
return
}
// We got here so this is a new kernel version never seen before. Build it!
lines, err := runBuild()
if err == nil {
// This means the build command outputted a valid artifact.
// Upload it to github.
pushToGit(operatingSystem, channel, kernel)
}
sendReport(settings, err, lines, kernel, channel)
}
func runCommand(cmds ...string) []byte {
log.Printf("Running command %s", strings.Join(cmds, " "))
out, cmdErr := exec.Command(cmds[0], cmds[1:]...).CombinedOutput()
if cmdErr != nil {
log.Fatal(cmdErr, "\n\n", string(out))
}
return out
}
func pushToGit(operatingSystem string, channel string, kernel string) {
gitDir := fmt.Sprintf("%s/%s", BASE_DIR, REPO_NAME)
releaseFile := fmt.Sprintf("zfs-%s.tar.gz", kernel)
cmdErr := os.Chdir(BASE_DIR)
if cmdErr != nil {
log.Fatal(cmdErr)
}
runCommand("rm", "-rf", REPO_NAME)
runCommand("git", "clone", DESTINATION_GIT_URL, REPO_NAME)
runCommand("mkdir", "-p", fmt.Sprintf("%s/%s", gitDir, operatingSystem))
runCommand("cp", fmt.Sprintf("%s/%s", BASE_DIR, releaseFile),
fmt.Sprintf("zfs-binaries/%s/", operatingSystem))
cmdErr = os.Chdir(gitDir)
if cmdErr != nil {
log.Fatal(cmdErr)
}
runCommand("git", "add", fmt.Sprintf("%s/%s", operatingSystem, releaseFile))
runCommand("git", "commit", "-m",
fmt.Sprintf("Automated build for kernel %s on %s %s.",
kernel, operatingSystem, channel))
runCommand("git", "push")
}
func checkReleaseExists(operatingSystem string, channel string, kernel string) (bool, error) {
// Check whether a release exists on GitHub... returns true/false, or an
// error.
url := "https://raw.githubusercontent.com/ClusterHQ/zfs-binaries/master"
resp, err := http.Head(fmt.Sprintf("%s/%s/zfs-%s.tar.gz", url, operatingSystem, kernel))
if err != nil {
return false, err
}
if resp.StatusCode == 200 {
return true, nil
} else if resp.StatusCode == 404 {
return false, nil
} else {
return false, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
}
func getBuildEnv() (string, string) {
kernelVersionOverride := os.Getenv("UNAME_R")
var kernelVersion string
if len(kernelVersionOverride) > 0 {
kernelVersion = kernelVersionOverride
} else {
kernelVersionOutput, err := exec.Command("uname", "-r").Output()
if err != nil {
log.Fatal(err)
}
kernelVersion = strings.TrimSpace(string(kernelVersionOutput))
}
updateFile, err := ioutil.ReadFile("/etc/coreos/update.conf")
if err != nil {
return kernelVersion, ""
}
coreOsChannel := strings.Split(strings.Split(string(updateFile), "\n")[0], "=")[1]
return kernelVersion, coreOsChannel
}
func runBuild() ([]byte, error) {
var buffer bytes.Buffer
var result []byte
cmd := exec.Command(os.Args[1], os.Args[2:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
in := bufio.NewScanner(io.MultiReader(stdout, stderr))
for in.Scan() {
line := in.Text()
buffer.WriteString(line + "\n")
log.Printf(line)
}
err = cmd.Wait()
result = []byte(buffer.String())
return result, err
}
func getSettings() Settings {
var settings Settings
settingsFile, err := ioutil.ReadFile("settings.yml")
if err != nil {
log.Fatal(err)
}
yaml.Unmarshal(settingsFile, &settings)
return settings
}
func sendReport(settings Settings, reportErr error, buffer []byte, kernel string, channel string) {
var stringResult string
if reportErr != nil {
stringResult = fmt.Sprintf("failure: %v", reportErr)
} else {
stringResult = "success"
}
emailBody := fmt.Sprintf(`From: %s
To: %s
Subject: zfs builder result: %s on %s (%s)
Build results:
%s`,
settings["email_from"],
settings["email_to"],
stringResult, kernel, channel,
buffer)
if FAKE_NETWORK_SERVICES {
log.Printf("Would send email: %s", string(emailBody))
} else {
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("",
settings["gmail_smtp_username"],
settings["gmail_smtp_password"],
"smtp.gmail.com"),
settings["email_from"],
[]string{settings["email_to"]},
[]byte(emailBody))
if err != nil {
log.Fatal(err)
}
}
}