Skip to content

Commit b2bd0d6

Browse files
committed
devapp: further preparation for running on GKE
+ Remove superfluous check for PORT env var since only the flag will be used. + Remove HTML being written to the page before escaping step, causing jank. + Grab the GitHub token from GCE metadata if it’s available. Updates golang/go#20691 Change-Id: I10fbc163ee91907ef0b843c823f40fd87a62f476 Reviewed-on: https://go-review.googlesource.com/46210 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 07279cc commit b2bd0d6

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

devapp/devapp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ func update(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
141141
if cls {
142142
data.PrintCLs(&output)
143143
} else {
144-
fmt.Fprintf(&output, fmt.Sprintf(`<a href="/stats/release?cycle=%d">Go 1.%d Issue Stats Dashboard</a>`, data.GoReleaseCycle, data.GoReleaseCycle))
145144
data.PrintIssues(&output)
146145
}
147146
var html bytes.Buffer

devapp/devappserver/Dockerfile.0

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ RUN go get -d golang.org/x/oauth2 && \
2828
RUN go get -d golang.org/x/sync/errgroup && \
2929
cd /go/src/golang.org/x/sync && git reset --hard f52d1811a62927559de87708c8913c1650ce4f26
3030

31+
RUN go get -d cloud.google.com/go/compute/metadata && \
32+
cd /go/src/cloud.google.com/go && git reset --hard 23179f286bc31e07fba2eddaa540fb999b1b1fd9
33+
3134
# Optimization to speed COPY+go install steps later. This go install
3235
# isn't required for correctness.
3336
RUN go install github.com/aclements/go-gg/generic/slice \
@@ -39,7 +42,8 @@ RUN go install github.com/aclements/go-gg/generic/slice \
3942
github.com/kylelemons/godebug/pretty \
4043
golang.org/x/net/context \
4144
golang.org/x/oauth2 \
42-
golang.org/x/sync/errgroup
45+
golang.org/x/sync/errgroup \
46+
cloud.google.com/go/compute/metadata
4347

4448
COPY . /go/src/golang.org/x/build/
4549

devapp/devappserver/main.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
//
88
// Usage:
99
//
10-
// devappserver --port=8081
10+
// devappserver --http=:8081
1111
//
12-
// By default devapp listens on port 8081. You can also configure the port by
13-
// setting the PORT environment variable (but not both).
12+
// By default devappserver listens on port 80.
1413
//
1514
// For the moment, Github issues and Gerrit CL's are stored in memory
1615
// in the running process. To trigger an initial download, visit
@@ -32,35 +31,21 @@ import (
3231

3332
func init() {
3433
flag.Usage = func() {
35-
os.Stderr.WriteString(`usage: devapp [-port=port]
34+
os.Stderr.WriteString(`usage: devappserver [-http=addr]
3635
37-
Devapp generates the dashboard that powers dev.golang.org.
36+
devappserver generates the dashboard that powers dev.golang.org.
3837
`)
3938
}
4039
}
4140

42-
const defaultPort = "8081"
43-
4441
func main() {
45-
portFlag := flag.String("port", "", "Port to listen on")
42+
httpAddr := flag.String("http", ":80", "HTTP service address (e.g., ':8080')")
4643
flag.Parse()
47-
if *portFlag != "" && os.Getenv("PORT") != "" {
48-
os.Stderr.WriteString("cannot set both $PORT and --port flags\n")
49-
os.Exit(2)
50-
}
51-
var port string
52-
if p := os.Getenv("PORT"); p != "" {
53-
port = p
54-
} else if *portFlag != "" {
55-
port = *portFlag
56-
} else {
57-
port = defaultPort
58-
}
59-
ln, err := net.Listen("tcp", ":"+port)
44+
ln, err := net.Listen("tcp", *httpAddr)
6045
if err != nil {
61-
fmt.Fprintf(os.Stderr, "Error listening on %s: %v\n", port, err)
46+
fmt.Fprintf(os.Stderr, "Error listening on %s: %v\n", *httpAddr, err)
6247
os.Exit(2)
6348
}
64-
fmt.Fprintf(os.Stderr, "Listening on port %s\n", port)
49+
fmt.Fprintf(os.Stderr, "Serving at %s\n", ln.Addr().String())
6550
log.Fatal(http.Serve(ln, nil))
6651
}

devapp/noappengine.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package devapp
99

1010
import (
11-
"errors"
1211
"flag"
1312
"fmt"
1413
"io/ioutil"
@@ -20,6 +19,7 @@ import (
2019
"sync"
2120
"time"
2221

22+
"cloud.google.com/go/compute/metadata"
2323
"golang.org/x/net/context"
2424
)
2525

@@ -130,11 +130,13 @@ func putCache(_ context.Context, name string, c *Cache) error {
130130
return nil
131131
}
132132

133-
var githubToken string
134-
var githubOnceErr error
135-
var githubOnce sync.Once
133+
var (
134+
githubToken string
135+
githubOnceErr error
136+
githubOnce sync.Once
137+
)
136138

137-
func getToken(ctx context.Context) (string, error) {
139+
func getTokenFromFile(ctx context.Context) (string, error) {
138140
githubOnce.Do(func() {
139141
const short = ".github-issue-token"
140142
filename := filepath.Clean(os.Getenv("HOME") + "/" + short)
@@ -145,14 +147,16 @@ func getToken(ctx context.Context) (string, error) {
145147
}
146148
data, err := ioutil.ReadFile(filename)
147149
if err != nil {
148-
msg := fmt.Sprintln("reading token: ", err, "\n\n"+
149-
"Please create a personal access token at https://github.com/settings/tokens/new\n"+
150-
"and write it to ", shortFilename, " to use this program.\n"+
151-
"The token only needs the repo scope, or private_repo if you want to\n"+
152-
"view or edit issues for private repositories.\n"+
153-
"The benefit of using a personal access token over using your GitHub\n"+
154-
"password directly is that you can limit its use and revoke it at any time.\n\n")
155-
githubOnceErr = errors.New(msg)
150+
const fmtStr = `reading token: %v
151+
152+
Please create a personal access token at https://github.com/settings/tokens/new
153+
and write it to %s or store it in GCE metadata using the
154+
key 'maintner-github-token' to use this program.
155+
The token only needs the repo scope, or private_repo if you want to view or edit
156+
issues for private repositories. The benefit of using a personal access token
157+
over using your GitHub password directly is that you can limit its use and revoke
158+
it at any time.`
159+
githubOnceErr = fmt.Errorf(fmtStr, err, shortFilename)
156160
return
157161
}
158162
fi, err := os.Stat(filename)
@@ -169,6 +173,17 @@ func getToken(ctx context.Context) (string, error) {
169173
return githubToken, githubOnceErr
170174
}
171175

176+
func getToken(ctx context.Context) (string, error) {
177+
if metadata.OnGCE() {
178+
// Re-use maintner-github-token until this is migrated to using the maintner API.
179+
token, err := metadata.ProjectAttributeValue("maintner-github-token")
180+
if len(token) > 0 && err == nil {
181+
return token, nil
182+
}
183+
}
184+
return getTokenFromFile(ctx)
185+
}
186+
172187
func getContext(r *http.Request) context.Context {
173188
return r.Context()
174189
}

0 commit comments

Comments
 (0)