1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"bytes"
9	"errors"
10	"fmt"
11	"os"
12	"os/exec"
13	"path/filepath"
14	"runtime"
15)
16
17type talksBuilder struct {
18}
19
20func (b talksBuilder) Signature(heads map[string]string) string {
21	return heads["talks"]
22}
23
24const talksToolsRev = "e04df2157ae7263e17159baabadc99fb03fc7514"
25
26func (b talksBuilder) Init(dir, hostport string, heads map[string]string) (*exec.Cmd, error) {
27	toolsDir := filepath.Join(dir, "gopath/src/golang.org/x/tools")
28	if err := checkout(repoURL+"tools", talksToolsRev, toolsDir); err != nil {
29		return nil, err
30	}
31	talksDir := filepath.Join(dir, "gopath/src/golang.org/x/talks")
32	if err := checkout(repoURL+"talks", heads["talks"], talksDir); err != nil {
33		return nil, err
34	}
35
36	goDir := os.Getenv("GOROOT_BOOTSTRAP")
37	if goDir == "" {
38		goDir = runtime.GOROOT()
39	}
40	goBin := filepath.Join(goDir, "bin/go")
41	goPath := filepath.Join(dir, "gopath")
42	presentPath := "golang.org/x/tools/cmd/present"
43	install := exec.Command(goBin, "install", "-tags=appenginevm", presentPath)
44	install.Env = []string{"GOROOT=" + goDir, "GOPATH=" + goPath}
45	if err := runErr(install); err != nil {
46		return nil, err
47	}
48
49	talksBin := filepath.Join(goPath, "bin/present")
50	presentSrc := filepath.Join(goPath, "src", presentPath)
51	present := exec.Command(talksBin, "-http="+hostport, "-base="+presentSrc)
52	present.Dir = talksDir
53	// TODO(adg): log this somewhere useful
54	present.Stdout = os.Stdout
55	present.Stderr = os.Stderr
56	if err := present.Start(); err != nil {
57		return nil, err
58	}
59	return present, nil
60}
61
62var talksMsg = []byte("Talks - The Go Programming Language")
63
64func (b talksBuilder) HealthCheck(hostport string) error {
65	body, err := getOK(fmt.Sprintf("http://%v/", hostport))
66	if err != nil {
67		return err
68	}
69	if !bytes.Contains(body, talksMsg) {
70		return errors.New("couldn't match string")
71	}
72	return nil
73}
74