dashboard/coordinator: run background goroutine cleaning old docker containers

I'm tired of figuring out what isn't cleaning up after itself, so keep
a background goroutine that looks at old containers and deletes them
as a backup measure. Verified it works by creating some dummy containers on
the machine.

Also adds df output to the HTML status page.

Change-Id: I23adc22872def882b3b9b3a4ec730017899bb966
Reviewed-on: https://go-review.googlesource.com/1537
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Brad Fitzpatrick 2014-12-15 11:51:42 +11:00 committed by Brad Fitzpatrick
parent 14ecce811f
commit 36f7c537c7
1 changed files with 24 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"html"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -129,6 +130,8 @@ func main() {
http.HandleFunc("/logs", handleLogs) http.HandleFunc("/logs", handleLogs)
go http.ListenAndServe(":80", nil) go http.ListenAndServe(":80", nil)
go cleanUpOldContainers()
for _, watcher := range watchers { for _, watcher := range watchers {
if err := startWatching(watchers[watcher.repo]); err != nil { if err := startWatching(watchers[watcher.repo]); err != nil {
log.Printf("Error starting watcher for %s: %v", watcher.repo, err) log.Printf("Error starting watcher for %s: %v", watcher.repo, err)
@ -218,7 +221,12 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%-22s hg %s in container <a href='/logs?name=%s&rev=%s'>%s</a>, %v ago\n", st.name, st.rev, st.name, st.rev, fmt.Fprintf(w, "%-22s hg %s in container <a href='/logs?name=%s&rev=%s'>%s</a>, %v ago\n", st.name, st.rev, st.name, st.rev,
st.container, time.Now().Sub(st.start)) st.container, time.Now().Sub(st.start))
} }
fmt.Fprintf(w, "</pre></body></html>") fmt.Fprintf(w, "</pre><h2>disk space</h2><pre>%s</pre></body></html>", html.EscapeString(diskFree()))
}
func diskFree() string {
out, _ := exec.Command("df", "-h").Output()
return string(out)
} }
func handleLogs(w http.ResponseWriter, r *http.Request) { func handleLogs(w http.ResponseWriter, r *http.Request) {
@ -558,3 +566,18 @@ func loadKey() {
} }
masterKeyCache = bytes.TrimSpace(slurp) masterKeyCache = bytes.TrimSpace(slurp)
} }
func cleanUpOldContainers() {
for {
for _, cid := range oldContainers() {
log.Printf("Cleaning old container %v", cid)
exec.Command("docker", "rm", "-v", cid).Run()
}
time.Sleep(30 * time.Second)
}
}
func oldContainers() []string {
out, _ := exec.Command("docker", "ps", "-a", "--filter=status=exited", "--no-trunc", "-q").Output()
return strings.Fields(string(out))
}