-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
285 lines (226 loc) · 5.77 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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
func exists(dir string) bool {
_, err := os.Stat(dir)
if err != nil && os.IsNotExist(err) {
return false
}
if err != nil {
panic(err)
}
return true
}
func clone(url, dir string) error {
fmt.Printf("clone repo %v\n", url)
cmd := exec.Command("git", "clone", "--quiet", url, dir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func update(dir string) error {
cmd := exec.Command("git", "pull", "--quiet")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
return cmd.Run()
}
func commitID(dir string) string {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Stderr = os.Stderr
cmd.Dir = dir
buf, err := cmd.Output()
if err != nil {
panic(err)
}
return strings.TrimSpace(string(buf))
}
// getVersionFromGit returns a version string that identifies the currently
// checked out git commit.
func getVersionFromGit(repodir string) string {
cmd := exec.Command("git", "describe",
"--long", "--tags", "--dirty", "--always")
cmd.Dir = repodir
out, err := cmd.Output()
if err != nil {
panic(fmt.Sprintf("git describe returned error: %v\n", err))
}
return strings.TrimSpace(string(out))
}
func readCurrentCommit(commitfile string) (string, error) {
buf, err := ioutil.ReadFile(commitfile)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("reading commit failed: %w", err)
}
return string(buf), nil
}
func writeCurrentCommit(commitfile, commit string) error {
return ioutil.WriteFile(commitfile, []byte(commit), 0600)
}
// BuildTarget specifies an OS/architecture pair for compilation.
type BuildTarget struct {
OS string
Arch string
}
// BuildTargets is a list of OS/architecture pairs to build for.
var BuildTargets = []BuildTarget{
{"darwin", "amd64"},
{"darwin", "arm64"},
{"freebsd", "386"},
{"freebsd", "amd64"},
{"freebsd", "arm"},
{"linux", "386"},
{"linux", "amd64"},
{"linux", "arm"},
{"linux", "arm64"},
{"linux", "ppc64le"},
{"openbsd", "386"},
{"openbsd", "amd64"},
{"windows", "386"},
{"windows", "amd64"},
}
// symlinkAndRename atomically creates a symlink by using symlink+rename.
func symlinkAndRename(oldname, newname string) error {
tempname := filepath.Join(filepath.Dir(newname), "symlink-"+filepath.Base(oldname))
err := os.Symlink(oldname, tempname)
if err != nil {
return fmt.Errorf("symlink: %w", err)
}
err = os.Rename(tempname, newname)
if err != nil {
return fmt.Errorf("rename: %w", err)
}
return nil
}
func build(repodir, outputdir string) error {
version := getVersionFromGit(repodir)
start := time.Now()
outputdir = filepath.Join(outputdir, fmt.Sprintf("restic-%v", version))
fmt.Printf("compiling %v\n", version)
err := os.MkdirAll(outputdir, 0755)
if err != nil {
return fmt.Errorf("mkdir output dir failed: %w", err)
}
ch := make(chan BuildTarget)
var wg sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go func() {
defer wg.Done()
for build := range ch {
filename := fmt.Sprintf("restic_%v_%v_%v", version, build.OS, build.Arch)
if build.OS == "windows" {
filename += ".exe"
}
cmd := exec.Command("go", "build", "-o", filepath.Join(outputdir, filename), "./cmd/restic")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = repodir
cmd.Env = append(os.Environ(),
"GOOS="+build.OS,
"GOARCH="+build.Arch,
"CGO_ENABLED=0",
)
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "compiling %v for %v/%v failed: %v\n",
version, build.OS, build.Arch, err)
panic(err)
}
}
}()
}
for _, target := range BuildTargets {
ch <- target
}
close(ch)
wg.Wait()
fmt.Printf("built version %v in %v\n", version, time.Since(start))
// create new symlink "latest" pointing to the current dir
err = symlinkAndRename(filepath.Base(outputdir), filepath.Join(filepath.Dir(outputdir), "latest"))
if err != nil {
return err
}
for _, build := range BuildTargets {
filename := fmt.Sprintf("restic_%v_%v_%v", version, build.OS, build.Arch)
if build.OS == "windows" {
filename += ".exe"
}
symlink := fmt.Sprintf("latest_restic_%v_%v", build.OS, build.Arch)
err = symlinkAndRename(
filepath.Join(filepath.Base(outputdir), filename),
filepath.Join(filepath.Dir(outputdir), symlink))
if err != nil {
return err
}
}
return nil
}
const (
repodir = "restic.git"
outputdir = "/var/www/beta.restic.net"
commitfile = "commit.current"
pollInterval = 5 * time.Minute
)
func goVersion() (string, error) {
cmd := exec.Command("go", "version")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("detect go version failed: %w", err)
}
return string(buf), nil
}
func main() {
v, err := goVersion()
if err != nil {
fmt.Fprintf(os.Stderr, "unable to get Go version: %v\n", err)
os.Exit(1)
}
fmt.Printf("Go version %v\n", v)
if !exists(repodir) {
err := clone("https://github.com/restic/restic", repodir)
if err != nil {
fmt.Fprintf(os.Stderr, "clone error: %v\n", err)
os.Exit(1)
}
}
commit, err := readCurrentCommit(commitfile)
if err != nil {
fmt.Fprintf(os.Stderr, "read state file %v: %v\n", commitfile, err)
os.Exit(1)
}
for {
err := update(repodir)
if err != nil {
fmt.Fprintf(os.Stderr, "error update: %v\n", err)
time.Sleep(pollInterval)
continue
}
newCommit := commitID(repodir)
if commit != newCommit {
err = build(repodir, outputdir)
if err != nil {
fmt.Fprintf(os.Stderr, "MkdirAll(%v) failed: %v\n", outputdir, err)
}
}
commit = newCommit
err = writeCurrentCommit(commitfile, commit)
if err != nil {
fmt.Fprintf(os.Stderr, "write state file %v: %v\n", commitfile, err)
}
time.Sleep(pollInterval)
}
}