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"
|
2015-03-07 15:25:49 -07:00
|
|
|
"bytes"
|
2014-12-16 21:49:59 -07:00
|
|
|
"encoding/json"
|
|
|
|
"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"
|
|
|
|
startTimeout = 5 * time.Minute
|
2015-01-06 22:00:26 -07:00
|
|
|
)
|
2014-12-16 21:49:59 -07:00
|
|
|
|
2015-03-07 15:25:49 -07:00
|
|
|
var indexingMsg = []byte("Indexing in progress: result may be inaccurate")
|
|
|
|
|
2015-01-08 16:53:37 -07:00
|
|
|
func main() {
|
2014-12-16 21:49:59 -07:00
|
|
|
p := new(Proxy)
|
|
|
|
go p.run()
|
|
|
|
http.Handle("/", p)
|
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 {
|
2014-12-17 21:40:08 -07:00
|
|
|
mu sync.Mutex // protects the followin'
|
2015-01-06 22:00:26 -07:00
|
|
|
proxy http.Handler
|
2015-01-09 14:28:29 -07:00
|
|
|
cur string // signature of gorepo+toolsrepo
|
|
|
|
cmd *exec.Cmd // live godoc instance, or nil for none
|
2014-12-16 21:49:59 -07:00
|
|
|
side string
|
2015-01-06 22:00:26 -07:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
s := "tip.golang.org is starting up"
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-12-17 21:40:08 -07:00
|
|
|
sig := heads["go"] + "-" + heads["tools"]
|
|
|
|
|
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-01-09 14:28:29 -07:00
|
|
|
cmd, hostport, err := initSide(newSide, heads["go"], heads["tools"])
|
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 {
|
|
|
|
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
|
|
|
|
if p.cmd != nil {
|
|
|
|
p.cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
p.cmd = cmd
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 14:28:29 -07:00
|
|
|
func initSide(side, goHash, toolsHash string) (godoc *exec.Cmd, hostport string, err error) {
|
2014-12-16 21:49:59 -07:00
|
|
|
dir := filepath.Join(os.TempDir(), "tipgodoc", side)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
goDir := filepath.Join(dir, "go")
|
|
|
|
toolsDir := filepath.Join(dir, "gopath/src/golang.org/x/tools")
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := checkout(repoURL+"go", goHash, goDir); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := checkout(repoURL+"tools", toolsHash, toolsDir); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
2014-12-17 21:40:08 -07:00
|
|
|
make := exec.Command(filepath.Join(goDir, "src/make.bash"))
|
2014-12-16 21:49:59 -07:00
|
|
|
make.Dir = filepath.Join(goDir, "src")
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(make); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
goBin := filepath.Join(goDir, "bin/go")
|
|
|
|
install := exec.Command(goBin, "install", "golang.org/x/tools/cmd/godoc")
|
2015-01-24 08:40:49 -07:00
|
|
|
install.Env = []string{
|
|
|
|
"GOROOT=" + goDir,
|
|
|
|
"GOPATH=" + filepath.Join(dir, "gopath"),
|
|
|
|
"GOROOT_BOOTSTRAP=" + os.Getenv("GOROOT_BOOTSTRAP"),
|
|
|
|
}
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(install); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
godocBin := filepath.Join(goDir, "bin/godoc")
|
|
|
|
hostport = "localhost:8081"
|
|
|
|
if side == "b" {
|
|
|
|
hostport = "localhost:8082"
|
|
|
|
}
|
2015-03-07 15:25:49 -07:00
|
|
|
godoc = exec.Command(godocBin, "-http="+hostport, "-index", "-index_interval=-1s")
|
2015-01-06 22:00:26 -07:00
|
|
|
godoc.Env = []string{"GOROOT=" + goDir}
|
|
|
|
// TODO(adg): log this somewhere useful
|
2014-12-16 21:49:59 -07:00
|
|
|
godoc.Stdout = os.Stdout
|
|
|
|
godoc.Stderr = os.Stderr
|
|
|
|
if err := godoc.Start(); err != nil {
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", err
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// TODO(bradfitz): tell the proxy that this side is dead
|
|
|
|
if err := godoc.Wait(); err != nil {
|
|
|
|
log.Printf("side %v exited: %v", side, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-03-09 19:13:37 -06:00
|
|
|
deadline := time.Now().Add(startTimeout)
|
|
|
|
for time.Now().Before(deadline) {
|
2014-12-16 21:49:59 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
var res *http.Response
|
2015-03-07 15:25:49 -07:00
|
|
|
res, err = http.Get(fmt.Sprintf("http://%v/search?q=FALLTHROUGH", hostport))
|
2014-12-16 21:49:59 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-07 15:25:49 -07:00
|
|
|
rbody, err := ioutil.ReadAll(res.Body)
|
2014-12-16 21:49:59 -07:00
|
|
|
res.Body.Close()
|
2015-03-07 15:25:49 -07:00
|
|
|
if err == nil && res.StatusCode == http.StatusOK &&
|
|
|
|
!bytes.Contains(rbody, indexingMsg) {
|
2015-01-09 14:28:29 -07:00
|
|
|
return godoc, hostport, nil
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 15:25:49 -07:00
|
|
|
godoc.Process.Kill()
|
2015-01-09 14:28:29 -07:00
|
|
|
return nil, "", fmt.Errorf("timed out waiting for side %v at %v (%v)", side, hostport, err)
|
2014-12-16 21:49:59 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-16 21:49:59 -07:00
|
|
|
return err
|
|
|
|
}
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(exec.Command("git", "clone", repo, path)); err != nil {
|
2014-12-16 21:49:59 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-16 21:49:59 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = exec.Command("git", "reset", "--hard", hash)
|
|
|
|
cmd.Dir = path
|
2015-01-06 22:00:26 -07:00
|
|
|
if err := runErr(cmd); err != nil {
|
2014-12-16 21:49:59 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = exec.Command("git", "clean", "-d", "-f", "-x")
|
|
|
|
cmd.Dir = path
|
2015-01-06 22:00:26 -07:00
|
|
|
return runErr(cmd)
|
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
|
|
|
|
}
|