|
| 1 | +// Copyright 2017 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// The rundockerbuildlet command loops forever and creates and cleans |
| 6 | +// up Docker containers running reverse buildlets. It keeps a fixed |
| 7 | +// number of them running at a time. See x/build/env/linux-arm64/packet/README |
| 8 | +// for one example user. |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "bytes" |
| 13 | + "flag" |
| 14 | + "fmt" |
| 15 | + "io/ioutil" |
| 16 | + "log" |
| 17 | + "os" |
| 18 | + "os/exec" |
| 19 | + "path/filepath" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + image = flag.String("image", "", "docker image to run; required.") |
| 26 | + numInst = flag.Int("n", 1, "number of containers to keep running at once") |
| 27 | + basename = flag.String("basename", "builder", "prefix before the builder number to use for the container names and host names") |
| 28 | + memory = flag.String("memory", "3g", "memory limit flag for docker run") |
| 29 | + keyFile = flag.String("key", "/etc/gobuild.key", "go build key file") |
| 30 | +) |
| 31 | + |
| 32 | +var buildKey []byte |
| 33 | + |
| 34 | +func main() { |
| 35 | + flag.Parse() |
| 36 | + |
| 37 | + key, err := ioutil.ReadFile(*keyFile) |
| 38 | + if err != nil { |
| 39 | + log.Fatalf("error reading build key from --key=%s: %v", buildKey, err) |
| 40 | + } |
| 41 | + buildKey = bytes.TrimSpace(key) |
| 42 | + |
| 43 | + if *image == "" { |
| 44 | + log.Fatalf("docker --image is required") |
| 45 | + } |
| 46 | + |
| 47 | + log.Printf("Started. Will keep %d copies of %s running.", *numInst, *image) |
| 48 | + for { |
| 49 | + if err := checkFix(); err != nil { |
| 50 | + log.Print(err) |
| 51 | + } |
| 52 | + time.Sleep(time.Second) // TODO: docker wait on the running containers? |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func checkFix() error { |
| 57 | + running := map[string]bool{} |
| 58 | + |
| 59 | + out, err := exec.Command("docker", "ps", "-a", "--format", "{{.ID}} {{.Names}} {{.Status}}").Output() |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("error running docker ps: %v", err) |
| 62 | + } |
| 63 | + // Out is like: |
| 64 | + // b1dc9ec2e646 packet14 Up 23 minutes |
| 65 | + // eeb458938447 packet11 Exited (0) About a minute ago |
| 66 | + // ... |
| 67 | + lines := strings.Split(string(out), "\n") |
| 68 | + for _, line := range lines { |
| 69 | + f := strings.SplitN(line, " ", 3) |
| 70 | + if len(f) < 3 { |
| 71 | + continue |
| 72 | + } |
| 73 | + container, name, status := f[0], f[1], f[2] |
| 74 | + if !strings.HasPrefix(name, *basename) { |
| 75 | + continue |
| 76 | + } |
| 77 | + if strings.HasPrefix(status, "Exited") { |
| 78 | + if out, err := exec.Command("docker", "rm", container).CombinedOutput(); err != nil { |
| 79 | + log.Printf("error running docker rm %s: %v, %s", container, err, out) |
| 80 | + } |
| 81 | + log.Printf("Removed container %s (%s)", container, name) |
| 82 | + } |
| 83 | + running[name] = strings.HasPrefix(status, "Up") |
| 84 | + } |
| 85 | + |
| 86 | + for num := 1; num <= *numInst; num++ { |
| 87 | + name := fmt.Sprintf("%s%02d", *basename, num) |
| 88 | + if running[name] { |
| 89 | + continue |
| 90 | + } |
| 91 | + log.Printf("Creating %s ...", name) |
| 92 | + keyFile := fmt.Sprintf("/tmp/buildkey%s/gobuildkey", num) |
| 93 | + if err := os.MkdirAll(filepath.Dir(keyFile), 0700); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + if err := ioutil.WriteFile(keyFile, buildKey, 0600); err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + out, err := exec.Command("docker", "run", |
| 100 | + "-d", |
| 101 | + "--memory="+*memory, |
| 102 | + "--name="+name, |
| 103 | + "-v", filepath.Dir(keyFile)+":/buildkey/", |
| 104 | + "-e", "HOSTNAME="+name, |
| 105 | + "--tmpfs=/workdir:rw,exec", |
| 106 | + *image).CombinedOutput() |
| 107 | + if err != nil { |
| 108 | + log.Printf("Error creating %s: %v, %s", name, err, out) |
| 109 | + continue |
| 110 | + } |
| 111 | + log.Printf("Created %v", name) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments