2015-08-13 11:32:40 -06:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-12-16 21:49:59 -07:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Command tipgodoc is the beginning of the new tip.golang.org server,
|
|
|
|
// serving the latest HEAD straight from the Git oven.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
2015-09-16 21:15:13 -06:00
|
|
|
"errors"
|
2014-12-16 21:49:59 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-01-06 22:00:26 -07:00
|
|
|
const (
|
2015-03-09 19:13:37 -06:00
|
|
|
repoURL = "https://go.googlesource.com/"
|
|
|
|
metaURL = "https://go.googlesource.com/?b=master&format=JSON"
|
2016-03-08 15:17:37 -07:00
|
|
|
startTimeout = 10 * time.Minute
|
2015-01-06 22:00:26 -07:00
|
|
|
)
|
2014-12-16 21:49:59 -07:00
|
|
|
|
2015-01-08 16:53:37 -07:00
|
|
|
func main() {
|
2015-09-16 19:19:51 -06:00
|
|
|
const k = "TIP_BUILDER"
|
|
|
|
var b Builder
|
|
|
|
switch os.Getenv(k) {
|
|
|
|
case "godoc":
|
|
|
|
b = godocBuilder{}
|
2015-09-16 21:15:13 -06:00
|
|
|
case "talks":
|
|
|
|
b = talksBuilder{}
|
2015-09-16 19:19:51 -06:00
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown %v value: %q", k, os.Getenv(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Proxy{builder: b}
|
2014-12-16 21:49:59 -07:00
|
|
|
go p.run()
|
|
|
|
http.Handle("/", p)
|
2015-12-17 19:23:29 -07:00
|
|
|
http.HandleFunc("/_ah/health", p.serveHealthCheck)
|
2015-04-07 14:03:20 -06:00
|
|
|
|
2015-12-17 19:23:29 -07:00
|
|
|
log.Print("Starting up")
|
|
|
|
|
2015-04-07 14:03:20 -06:00
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
p.stop()
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 14:28:29 -07:00
|
|
|
// Proxy implements the tip.golang.org server: a reverse-proxy
|
|
|
|
// that builds and runs godoc instances showing the latest docs.
|
2014-12-16 21:49:59 -07:00
|
|
|
type Proxy struct {
|
2015-09-16 19:19:51 -06:00
|
|
|
builder Builder
|
|
|
|
|
2015-09-16 21:15:13 -06:00
|
|
|
mu sync.Mutex // protects the followin'
|
|
|
|
proxy http.Handler
|
|
|
|
cur string // signature of gorepo+toolsrepo
|
|
|
|
cmd *exec.Cmd // live godoc instance, or nil for none
|
|
|
|
side string
|
|
|
|
hostport string // host and port of the live instance
|
|
|
|
err error
|
2015-01-06 22:00:26 -07:00
|
|
|
}
|
|
|
|
|
2015-09-16 19:19:51 -06:00
|
|
|
type Builder interface {
|
|
|
|
Signature(heads map[string]string) string
|
|
|
|
Init(dir, hostport string, heads map[string]string) (*exec.Cmd, error)
|
2015-09-16 21:15:13 -06:00
|
|
|
HealthCheck(hostport string) error
|
2015-09-16 19:19:51 -06:00
|
|
|
}
|
|
|
|
|
2015-01-06 22:00:26 -07:00
|
|
|
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/_tipstatus" {
|
|
|
|
p.serveStatus(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.mu.Lock()
|
|
|
|
proxy := p.proxy
|
|
|
|
err := p.err
|
|
|
|
p.mu.Unlock()
|
|
|
|
if proxy == nil {
|
2015-09-16 19:19:51 -06:00
|
|
|
s := "starting up"
|
2015-01-06 22:00:26 -07:00
|
|
|
if err != nil {
|
|
|
|
s = err.Error()
|
|
|
|
}
|
|
|
|
http.Error(w, s, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proxy.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Proxy) serveStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
fmt.Fprintf(w, "side=%v\ncurrent=%v\nerror=%v\n", p.side, p.cur, p.err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2015-12-17 19:23:29 -07:00
|
|
|
func (p *Proxy) serveHealthCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2015-12-17 19:23:29 -07:00
|
|
|
// NOTE: Status 502, 503, 504 are the only status codes that signify an unhealthy app.
|
|
|
|
// So long as this handler returns one of those codes, this instance will not be sent any requests.
|
2015-12-17 19:23:29 -07:00
|
|
|
if p.proxy == nil {
|
2015-12-17 19:23:29 -07:00
|
|
|
log.Printf("Health check: not ready")
|
|
|
|
http.Error(w, "Not ready", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.builder.HealthCheck(p.hostport); err != nil {
|
|
|
|
log.Printf("Health check failed: %v", err)
|
|
|
|
http.Error(w, "Health check failed", http.StatusServiceUnavailable)
|
2015-12-17 19:23:29 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
io.WriteString(w, "ok")
|
|
|
|
}
|
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
// run runs in its own goroutine.
|
|
|
|
func (p *Proxy) run() {
|
|
|
|
p.side = "a"
|
|
|
|
for {
|
|
|
|
p.poll()
|
2014-12-17 21:40:08 -07:00
|
|
|
time.Sleep(30 * time.Second)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 14:03:20 -06:00
|
|
|
func (p *Proxy) stop() {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
if p.cmd != nil {
|
|
|
|
p.cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
// poll runs from the run loop goroutine.
|
|
|
|
func (p *Proxy) poll() {
|
|
|
|
heads := gerritMetaMap()
|
|
|
|
if heads == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-16 19:19:51 -06:00
|
|
|
sig := p.builder.Signature(heads)
|
2014-12-17 21:40:08 -07:00
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
p.mu.Lock()
|
2014-12-17 21:40:08 -07:00
|
|
|
changes := sig != p.cur
|
2014-12-16 21:49:59 -07:00
|
|
|
curSide := p.side
|
2014-12-17 21:40:08 -07:00
|
|
|
p.cur = sig
|
2014-12-16 21:49:59 -07:00
|
|
|
p.mu.Unlock()
|
|
|
|
|
2014-12-17 21:40:08 -07:00
|
|
|
if !changes {
|
2014-12-16 21:49:59 -07:00
|
|
|
return
|
|
|
|
}
|
2014-12-17 21:40:08 -07:00
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
newSide := "b"
|
|
|
|
if curSide == "b" {
|
|
|
|
newSide = "a"
|
|
|
|
}
|
|
|
|
|
2015-09-16 19:19:51 -06:00
|
|
|
dir := filepath.Join(os.TempDir(), "tip", newSide)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
p.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hostport := "localhost:8081"
|
|
|
|
if newSide == "b" {
|
|
|
|
hostport = "localhost:8082"
|
|
|
|
}
|
|
|
|
cmd, err := p.builder.Init(dir, hostport, heads)
|
2016-03-15 21:51:36 -06:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("builder.Init: %v", err)
|
|
|
|
} else {
|
2015-09-16 21:15:13 -06:00
|
|
|
go func() {
|
|
|
|
// TODO(adg,bradfitz): be smarter about dead processes
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
log.Printf("process in %v exited: %v", dir, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = waitReady(p.builder, hostport)
|
2016-03-08 15:17:37 -07:00
|
|
|
if err != nil {
|
|
|
|
cmd.Process.Kill()
|
2016-03-15 21:51:36 -06:00
|
|
|
err = fmt.Errorf("waitReady: %v", err)
|
2016-03-08 15:17:37 -07:00
|
|
|
}
|
2015-09-16 21:15:13 -06:00
|
|
|
}
|
2015-01-06 22:00:26 -07:00
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2014-12-16 21:49:59 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2015-01-06 22:00:26 -07:00
|
|
|
p.err = err
|
2014-12-16 21:49:59 -07:00
|
|
|
return
|
|
|
|
}
|
2015-01-09 14:28:29 -07:00
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
u, err := url.Parse(fmt.Sprintf("http://%v/", hostport))
|
|
|
|
if err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
err = fmt.Errorf("parsing hostport: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
log.Println(err)
|
2015-01-06 22:00:26 -07:00
|
|
|
p.err = err
|
2014-12-16 21:49:59 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.proxy = httputil.NewSingleHostReverseProxy(u)
|
2015-01-09 14:28:29 -07:00
|
|
|
p.side = newSide
|
2015-09-16 21:15:13 -06:00
|
|
|
p.hostport = hostport
|
2015-01-09 14:28:29 -07:00
|
|
|
if p.cmd != nil {
|
|
|
|
p.cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
p.cmd = cmd
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2015-09-16 21:15:13 -06:00
|
|
|
func waitReady(b Builder, hostport string) error {
|
|
|
|
var err error
|
|
|
|
deadline := time.Now().Add(startTimeout)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
if err = b.HealthCheck(hostport); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("timed out waiting for process at %v: %v", hostport, err)
|
|
|
|
}
|
|
|
|
|
2015-01-06 22:00:26 -07:00
|
|
|
func runErr(cmd *exec.Cmd) error {
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
if len(out) == 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s\n%v", out, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-16 21:49:59 -07:00
|
|
|
func checkout(repo, hash, path string) error {
|
|
|
|
// Clone git repo if it doesn't exist.
|
|
|
|
if _, err := os.Stat(filepath.Join(path, ".git")); os.IsNotExist(err) {
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
return fmt.Errorf("mkdir: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(exec.Command("git", "clone", repo, path)); err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
return fmt.Errorf("clone: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
return fmt.Errorf("stat .git: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2015-01-06 22:00:26 -07:00
|
|
|
// Pull down changes and update to hash.
|
2014-12-16 21:49:59 -07:00
|
|
|
cmd := exec.Command("git", "fetch")
|
|
|
|
cmd.Dir = path
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(cmd); err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
return fmt.Errorf("fetch: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
cmd = exec.Command("git", "reset", "--hard", hash)
|
|
|
|
cmd.Dir = path
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(cmd); err != nil {
|
2016-03-15 21:51:36 -06:00
|
|
|
return fmt.Errorf("reset: %v", err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
cmd = exec.Command("git", "clean", "-d", "-f", "-x")
|
|
|
|
cmd.Dir = path
|
2016-03-15 21:51:36 -06:00
|
|
|
if err := runErr(cmd); err != nil {
|
|
|
|
return fmt.Errorf("clean: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// gerritMetaMap returns the map from repo name (e.g. "go") to its
|
|
|
|
// latest master hash.
|
|
|
|
// The returned map is nil on any transient error.
|
|
|
|
func gerritMetaMap() map[string]string {
|
|
|
|
res, err := http.Get(metaURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
defer io.Copy(ioutil.Discard, res.Body) // ensure EOF for keep-alive
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var meta map[string]struct {
|
|
|
|
Branches map[string]string
|
|
|
|
}
|
|
|
|
br := bufio.NewReader(res.Body)
|
|
|
|
// For security reasons or something, this URL starts with ")]}'\n" before
|
|
|
|
// the JSON object. So ignore that.
|
|
|
|
// Shawn Pearce says it's guaranteed to always be just one line, ending in '\n'.
|
|
|
|
for {
|
|
|
|
b, err := br.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b == '\n' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(br).Decode(&meta); err != nil {
|
|
|
|
log.Printf("JSON decoding error from %v: %s", metaURL, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m := map[string]string{}
|
|
|
|
for repo, v := range meta {
|
|
|
|
if master, ok := v.Branches["master"]; ok {
|
|
|
|
m[repo] = master
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2015-09-16 21:15:13 -06:00
|
|
|
|
|
|
|
func getOK(url string) (body []byte, err error) {
|
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body, err = ioutil.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.New(res.Status)
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|