Repository navigation
Expand file tree
/
Copy pathbootstrap.go
More file actions
495 lines (405 loc) · 12.2 KB
/
Copy pathbootstrap.go
File metadata and controls
495 lines (405 loc) · 12.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package main
import (
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
)
//go:embed .env
var envFile string
//go:embed distbuild.service
var agentServiceFile string
var (
BuildTime string
CommitID string
)
var (
aospPath string
distbuildPath string
deployAgent bool
enableToolchains bool
)
var rootCmd = &cobra.Command{
Use: "bootstrap",
Short: "boong bootstrap",
Version: BuildTime + "-" + CommitID,
Run: func(cmd *cobra.Command, args []string) {
if err := checkFlags(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err.Error())
os.Exit(1)
}
ctx := context.Background()
if err := run(ctx); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err.Error())
os.Exit(1)
}
},
}
// nolint:gochecknoinits
func init() {
rootCmd.Flags().StringVar(&aospPath, "aosp-path", "", "aosp base path")
rootCmd.Flags().StringVar(&distbuildPath, "distbuild-path", "", "distbuild binaries path")
rootCmd.Flags().BoolVar(&deployAgent, "deploy-agent", false, "deploy agent service")
rootCmd.Flags().BoolVar(&enableToolchains, "enable-toolchains", false, "download prebuilt toolchains")
_ = rootCmd.MarkFlagRequired("distbuild-path")
rootCmd.MarkFlagsMutuallyExclusive("aosp-path", "deploy-agent")
rootCmd.Root().CompletionOptions.DisableDefaultCmd = true
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func run(_ context.Context) error {
if err := loadEnvFile(envFile); err != nil {
return fmt.Errorf("load .env failed: %w", err)
}
if err := cloneDistbuildRepo(); err != nil {
return fmt.Errorf("git clone failed: %w", err)
}
if err := downloadResources(); err != nil {
return fmt.Errorf("download resources failed: %w", err)
}
if deployAgent {
if err := downloadAgent(); err != nil {
return fmt.Errorf("download agent failed: %w", err)
}
if err := installAgentService(); err != nil {
return fmt.Errorf("install agent service failed: %w", err)
}
fmt.Println()
fmt.Println("agent service installed and started successfully!")
fmt.Println("check status: sudo systemctl status distbuild.service")
fmt.Println()
}
if enableToolchains {
if err := downloadToolchains(); err != nil {
return fmt.Errorf("download toolchains failed: %w", err)
}
}
return nil
}
func installAgentService() error {
servicePath := "/etc/systemd/system/distbuild.service"
agentSource := filepath.Join(distbuildPath, "boong", "bin", "agent")
agentTarget := "/usr/local/bin/distbuild-agent"
bar, done, _ := runProgress("installing agent service...")
defer func() { _ = stopProgress(bar, done) }()
if err := exec.Command("sudo", "mkdir", "-p", "/usr/local/bin").Run(); err != nil {
return fmt.Errorf("create bin directory failed: %w", err)
}
if err := exec.Command("sudo", "mv", agentSource, agentTarget).Run(); err != nil {
return fmt.Errorf("move agent failed: %w", err)
}
tempFile, err := os.CreateTemp("", "distbuild-service-*.service")
if err != nil {
return fmt.Errorf("create temp file failed: %w", err)
}
defer func(name string) {
_ = os.Remove(name)
}(tempFile.Name())
if _, err := tempFile.WriteString(agentServiceFile); err != nil {
return fmt.Errorf("write service file failed: %w", err)
}
defer func(tempFile *os.File) {
_ = tempFile.Close()
}(tempFile)
moveCmd := exec.Command("sudo", "mv", tempFile.Name(), servicePath)
if err := moveCmd.Run(); err != nil {
return fmt.Errorf("install service file failed: %w", err)
}
commands := []*exec.Cmd{
exec.Command("sudo", "systemctl", "daemon-reload"),
exec.Command("sudo", "systemctl", "enable", "distbuild.service"),
exec.Command("sudo", "systemctl", "start", "distbuild.service"),
}
for _, cmd := range commands {
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("command failed [%s]: %w\n%s",
strings.Join(cmd.Args, " "), err, string(output))
}
}
return nil
}
func checkFlags() error {
var err error
if aospPath == "" && !deployAgent {
return fmt.Errorf("--aosp-path or --deploy-agent flag is required")
}
aospPath, err = expandTildeIfPresent(aospPath)
if err != nil {
return fmt.Errorf("failed to expand tilde: %w", err)
}
distbuildPath, err = expandTildeIfPresent(distbuildPath)
if err != nil {
return fmt.Errorf("failed to expand tilde: %w", err)
}
return nil
}
func expandTildeIfPresent(path string) (string, error) {
if !strings.Contains(path, "~") {
return path, nil
}
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
return "", err
}
if path == "~" {
return usr.HomeDir, nil
}
if strings.HasPrefix(path, "~/") {
return filepath.Join(usr.HomeDir, path[2:]), nil
}
}
return path, nil
}
func loadEnvFile(content string) error {
scanner := bufio.NewScanner(strings.NewReader(content))
for scanner.Scan() {
line := scanner.Text()
// Skip comments or empty lines
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if _, ok := os.LookupEnv(key); !ok {
if err := os.Setenv(key, value); err != nil {
return err
}
}
}
return nil
}
func cloneDistbuildRepo() error {
targetPath := filepath.Join(aospPath, "build", "distbuild")
if err := os.RemoveAll(targetPath); err != nil {
return fmt.Errorf("failed to remove existing distbuild directory: %w", err)
}
if err := os.MkdirAll(targetPath, 0755); err != nil {
return fmt.Errorf("create directory failed: %w", err)
}
host, exists := os.LookupEnv("REPO_HOST")
if !exists || host == "" {
return fmt.Errorf("environment variable REPO_HOST not set")
}
repo, exists := os.LookupEnv("DISTBUILD_REPO")
if !exists || repo == "" {
repo, exists = os.LookupEnv("WRAPPER_REPO")
if !exists || repo == "" {
return fmt.Errorf("environment variable DISTBUILD_REPO or WRAPPER_REPO not set")
}
targetPath = filepath.Join(targetPath, "boong")
_ = os.MkdirAll(targetPath, 0755)
targetPath = filepath.Join(targetPath, "wrapper")
}
bar, done, _ := runProgress("clone repo...")
defer func(bar *progressbar.ProgressBar, done chan bool) {
_ = stopProgress(bar, done)
}(bar, done)
cmd := exec.Command("git", "clone", fmt.Sprintf("%s/%s", host, repo), targetPath)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v\n%s", err, stderr.String())
}
return nil
}
func downloadAgent() error {
binDir := filepath.Join(distbuildPath, "boong", "bin")
if err := os.MkdirAll(binDir, 0755); err != nil {
return fmt.Errorf("create bin directory failed: %w", err)
}
agentBin, exists := os.LookupEnv("AGENT_BIN")
if !exists || agentBin == "" {
fmt.Println("warning: environment variable AGENT_BIN not set")
return nil
}
bar, done, _ := runProgress("download agent...")
defer func(bar *progressbar.ProgressBar, done chan bool) {
_ = stopProgress(bar, done)
}(bar, done)
return downloadFile(
agentBin,
filepath.Join(binDir, "agent"),
)
}
func downloadResources() error {
binDir := filepath.Join(distbuildPath, "boong", "bin")
if err := os.MkdirAll(binDir, 0755); err != nil {
return fmt.Errorf("create bin directory failed: %w", err)
}
proxyBin, exists := os.LookupEnv("PROXY_BIN")
if exists && proxyBin != "" {
bar, done, _ := runProgress("download proxy...")
defer func(bar *progressbar.ProgressBar, done chan bool) {
_ = stopProgress(bar, done)
}(bar, done)
if err := downloadFile(proxyBin, filepath.Join(binDir, "proxy")); err != nil {
return fmt.Errorf("download proxy binary failed: %w", err)
}
if err := createSymlinks("proxy"); err != nil {
return fmt.Errorf("create symlinks failed: %w", err)
}
} else {
fmt.Println("warning: environment variable PROXY_BIN not set")
}
distninjaBin, exists := os.LookupEnv("DISTNINJA_BIN")
if exists && distninjaBin != "" {
bar, done, _ := runProgress("download distninja...")
defer func(bar *progressbar.ProgressBar, done chan bool) {
_ = stopProgress(bar, done)
}(bar, done)
if err := downloadFile(distninjaBin, filepath.Join(binDir, "distninja")); err != nil {
return fmt.Errorf("download distninja binary failed: %w", err)
}
if err := createSymlinks("distninja"); err != nil {
return fmt.Errorf("create symlinks failed: %w", err)
}
} else {
fmt.Println("warning: environment variable DISTNINJA_BIN not set")
}
return nil
}
func downloadFile(url, filePath string) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("create request failed: %v [%s]", err, filepath.Base(filePath))
}
username := os.Getenv("AUTH_USER")
password := os.Getenv("AUTH_PASS")
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("download failed: %v [%s]", err, filepath.Base(filePath))
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download failed with status code %d [%s]", resp.StatusCode, filepath.Base(filePath))
}
out, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("create file failed: %v [%s]", err, filepath.Base(filePath))
}
defer func(out *os.File) {
_ = out.Close()
}(out)
if _, err = io.Copy(out, resp.Body); err != nil {
return fmt.Errorf("write file failed: %v [%s]", err, filepath.Base(filePath))
}
if err := os.Chmod(filePath, 0755); err != nil {
return fmt.Errorf("chmod failed: %v [%s]", err, filepath.Base(filePath))
}
return nil
}
func createSymlinks(name string) error {
source := filepath.Join(distbuildPath, "boong", "bin", name)
target := filepath.Join("/usr/local/bin", name)
cmd := exec.Command("sudo", "ln", "-sf", source, target)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("create symlink failed: %v [%s]", err, filepath.Base(name))
}
return nil
}
func downloadToolchains() error {
host, exists := os.LookupEnv("REPO_HOST")
if !exists || host == "" {
return fmt.Errorf("environment variable REPO_HOST not set")
}
toolchains := []struct {
name string
repo string
path string
}{
{
name: "clang",
repo: fmt.Sprintf("%s/platform/prebuilts/clang/host/linux-x86", host),
path: filepath.Join(distbuildPath, "prebuilts/clang/host/linux-x86"),
},
{
name: "gcc",
repo: fmt.Sprintf("%s/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8", host),
path: filepath.Join(distbuildPath, "prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8"),
},
}
for _, tc := range toolchains {
if err := cloneToolchain(tc.repo, tc.path, tc.name); err != nil {
return err
}
}
return nil
}
func cloneToolchain(repo, path, name string) error {
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove existing %s directory: %w", name, err)
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return fmt.Errorf("create directory for %s failed: %w", name, err)
}
bar, done, _ := runProgress(fmt.Sprintf("clone %s...", name))
defer func(bar *progressbar.ProgressBar, done chan bool) {
_ = stopProgress(bar, done)
}(bar, done)
cmd := exec.Command("git", "clone", repo, "-b", "master", "--depth", "1", path)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s clone failed: %v\n%s", name, err, stderr.String())
}
return nil
}
func runProgress(description string) (*progressbar.ProgressBar, chan bool, error) {
bar := progressbar.NewOptions(-1,
progressbar.OptionSetDescription(description),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: "",
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionOnCompletion(func() {
fmt.Println("")
}),
)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
default:
_ = bar.Add(1)
time.Sleep(100 * time.Millisecond)
}
}
}()
return bar, done, nil
}
func stopProgress(bar *progressbar.ProgressBar, done chan bool) error {
done <- true
return bar.Finish()
}