Skip to content

Commit 27e208b

Browse files
committed
playground: drop NaCL support
It is longer being used in production as of Go 1.14beta1. Updates golang/go#25224 Change-Id: I5a83c88387f722bb0be476d0500f74a061827770 Reviewed-on: https://go-review.googlesource.com/c/playground/+/227351 Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent cb3741f commit 27e208b

File tree

8 files changed

+76
-189
lines changed

8 files changed

+76
-189
lines changed

Dockerfile

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44

55
ARG GO_VERSION=go1.14.1
66

7-
############################################################################
8-
FROM debian:buster AS nacl
9-
10-
RUN apt-get update && apt-get install -y --no-install-recommends curl bzip2 ca-certificates
11-
12-
RUN curl -s https://storage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk/trunk.544461/naclsdk_linux.tar.bz2 | tar -xj -C /tmp --strip-components=2 pepper_67/tools/sel_ldr_x86_64
13-
14-
############################################################################
157
FROM debian:buster AS build
168
LABEL maintainer="[email protected]"
179

@@ -20,87 +12,54 @@ RUN apt-get update && apt-get install -y ${BUILD_DEPS} --no-install-recommends
2012

2113
ENV GOPATH /go
2214
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
23-
ENV GOROOT_BOOTSTRAP /usr/local/gobootstrap
24-
ENV GO_BOOTSTRAP_VERSION go1.13.9
15+
ENV GO_BOOTSTRAP_VERSION go1.14.1
2516
ARG GO_VERSION
2617
ENV GO_VERSION ${GO_VERSION}
2718

28-
# Fake time
29-
COPY enable-fake-time.patch /usr/local/playground/
30-
# Fake file system
31-
COPY fake_fs.lst /usr/local/playground/
32-
33-
# Get a bootstrap version of Go for building from source.
19+
# Get a version of Go for building the playground
3420
RUN curl -sSL https://dl.google.com/go/$GO_BOOTSTRAP_VERSION.linux-amd64.tar.gz -o /tmp/go.tar.gz
3521
RUN curl -sSL https://dl.google.com/go/$GO_BOOTSTRAP_VERSION.linux-amd64.tar.gz.sha256 -o /tmp/go.tar.gz.sha256
3622
RUN echo "$(cat /tmp/go.tar.gz.sha256) /tmp/go.tar.gz" | sha256sum -c -
37-
RUN mkdir -p $GOROOT_BOOTSTRAP
38-
RUN tar --strip=1 -C $GOROOT_BOOTSTRAP -vxzf /tmp/go.tar.gz
39-
40-
# Fetch Go source for tag $GO_VERSION.
41-
RUN git clone --depth=1 --branch=$GO_BOOTSTRAP_VERSION https://go.googlesource.com/go /usr/local/go
42-
# Apply the fake time and fake filesystem patches.
43-
RUN patch /usr/local/go/src/runtime/rt0_nacl_amd64p32.s /usr/local/playground/enable-fake-time.patch
44-
RUN cd /usr/local/go && $GOROOT_BOOTSTRAP/bin/go run misc/nacl/mkzip.go -p syscall /usr/local/playground/fake_fs.lst src/syscall/fstest_nacl.go
45-
# Build the Go toolchain.
46-
RUN cd /usr/local/go/src && GOOS=nacl GOARCH=amd64p32 ./make.bash --no-clean
23+
RUN mkdir -p /usr/local/go
24+
RUN tar --strip=1 -C /usr/local/go -vxzf /tmp/go.tar.gz
4725

4826
RUN mkdir /gocache
4927
ENV GOCACHE /gocache
5028
ENV GO111MODULE on
5129
ENV GOPROXY=https://proxy.golang.org
5230

31+
# Compile Go at target sandbox version and install standard library with --tags=faketime.
32+
WORKDIR /usr/local
33+
RUN git clone https://go.googlesource.com/go go-faketime && cd go-faketime && git reset --hard $GO_VERSION
34+
WORKDIR /usr/local/go-faketime/src
35+
RUN ./make.bash
36+
ENV GOROOT /usr/local/go-faketime
37+
RUN ../bin/go install --tags=faketime std
38+
5339
COPY go.mod /go/src/playground/go.mod
5440
COPY go.sum /go/src/playground/go.sum
5541
WORKDIR /go/src/playground
56-
57-
# Pre-build some packages to speed final install later.
58-
RUN go install cloud.google.com/go/compute/metadata
59-
RUN go install cloud.google.com/go/datastore
60-
RUN go install github.com/bradfitz/gomemcache/memcache
61-
RUN go install golang.org/x/tools/godoc/static
62-
RUN go install golang.org/x/tools/imports
63-
RUN go install github.com/rogpeppe/go-internal/modfile
64-
RUN go install github.com/rogpeppe/go-internal/txtar
42+
RUN go mod download
6543

6644
# Add and compile playground daemon
6745
COPY . /go/src/playground/
68-
WORKDIR /go/src/playground
6946
RUN go install
7047

71-
############################################################################
72-
# Temporary Docker stage to add a pre-Go1.14 $GOROOT into our
73-
# container for early linux/amd64 testing.
74-
FROM golang:1.13 AS temp_pre_go14
75-
76-
ENV BUILD_DEPS 'curl git gcc patch libc6-dev ca-certificates'
77-
RUN apt-get update && apt-get install -y --no-install-recommends ${BUILD_DEPS}
78-
79-
ARG GO_VERSION
80-
ENV GO_VERSION ${GO_VERSION}
81-
RUN cd /usr/local && git clone https://go.googlesource.com/go go1.14 && cd go1.14 && git reset --hard $GO_VERSION
82-
WORKDIR /usr/local/go1.14/src
83-
RUN ./make.bash
84-
ENV GOROOT /usr/local/go1.14
85-
RUN ../bin/go install --tags=faketime std
86-
8748
############################################################################
8849
# Final stage.
8950
FROM debian:buster
9051

9152
RUN apt-get update && apt-get install -y git ca-certificates --no-install-recommends
9253

93-
COPY --from=build /usr/local/go /usr/local/go
94-
COPY --from=nacl /tmp/sel_ldr_x86_64 /usr/local/bin
95-
COPY --from=temp_pre_go14 /usr/local/go1.14 /usr/local/go1.14
54+
COPY --from=build /usr/local/go-faketime /usr/local/go-faketime
9655

9756
ARG GO_VERSION
9857
ENV GO_VERSION ${GO_VERSION}
9958
ENV GOPATH /go
100-
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
59+
ENV PATH /usr/local/go-faketime/bin:$GOPATH/bin:$PATH
10160

10261
# Add and compile tour packages
103-
RUN GOOS=nacl GOARCH=amd64p32 go get \
62+
RUN go get \
10463
golang.org/x/tour/pic \
10564
golang.org/x/tour/reader \
10665
golang.org/x/tour/tree \

Makefile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@ docker:
1212
runlocal:
1313
docker network create sandnet || true
1414
docker kill play_dev || true
15-
docker run --name=play_dev --rm --network=sandnet --env SANDBOX_BACKEND_URL="http://sandbox_dev.sandnet/run" -ti -p 127.0.0.1:8081:8080/tcp golang/playground
15+
docker run --name=play_dev --rm --network=sandnet -ti -p 127.0.0.1:8081:8080/tcp golang/playground --backend-url="http://sandbox_dev.sandnet/run"
1616

1717
test_go:
1818
# Run fast tests first: (and tests whether, say, things compile)
1919
GO111MODULE=on go test -v
2020

21-
test_nacl: docker
22-
docker kill sandbox_front_test || true
23-
docker run --rm --name=sandbox_front_test --network=sandnet -t golang/playground testnacl
24-
2521
test_gvisor: docker
2622
docker kill sandbox_front_test || true
27-
docker run --rm --name=sandbox_front_test --network=sandnet -t golang/playground test
23+
docker run --rm --name=sandbox_front_test --network=sandnet -t golang/playground --runtests
2824

2925
# Note: test_gvisor is not included in "test" yet, because it requires
3026
# running a separate server first ("make runlocal" in the sandbox
3127
# directory)
32-
test: test_go test_nacl
28+
test: test_go
3329

3430
update-cloudbuild-trigger:
3531
# The gcloud CLI doesn't yet support updating a trigger.

main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"context"
9+
"flag"
910
"fmt"
1011
"net/http"
1112
"os"
@@ -16,7 +17,13 @@ import (
1617

1718
var log = newStdLogger()
1819

20+
var (
21+
runtests = flag.Bool("runtests", false, "Run integration tests instead of Playground server.")
22+
backendURL = flag.String("backend-url", "", "URL for sandbox backend that runs Go binaries.")
23+
)
24+
1925
func main() {
26+
flag.Parse()
2027
s, err := newServer(func(s *server) error {
2128
pid := projectID()
2229
if pid == "" {
@@ -41,13 +48,13 @@ func main() {
4148
log.Fatalf("Error creating server: %v", err)
4249
}
4350

44-
if len(os.Args) > 1 && os.Args[1] == "test" {
51+
if *runtests {
4552
s.test()
4653
return
4754
}
48-
if len(os.Args) > 1 && os.Args[1] == "testnacl" {
49-
s.testNacl()
50-
return
55+
if *backendURL != "" {
56+
// TODO(golang.org/issue/25224) - Remove environment variable and use a flag.
57+
os.Setenv("SANDBOX_BACKEND_URL", *backendURL)
5158
}
5259

5360
port := os.Getenv("PORT")

sandbox.go

Lines changed: 43 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -373,42 +373,22 @@ func compileAndRun(ctx context.Context, req *request) (*response, error) {
373373
}
374374
}
375375

376-
// TODO: simplify this once Go 1.14 is out. We should remove
377-
// the //play:gvisor substring hack and DEBUG_FORCE_GVISOR and
378-
// instead implement https://golang.org/issue/33629 to
379-
// officially support different Go versions (Go tip + past two
380-
// releases).
381-
useGvisor := os.Getenv("GO_VERSION") >= "go1.14" ||
382-
os.Getenv("DEBUG_FORCE_GVISOR") == "1" ||
383-
strings.Contains(req.Body, "//play:gvisor\n")
384-
385376
exe := filepath.Join(tmpDir, "a.out")
386377
goCache := filepath.Join(tmpDir, "gocache")
387378

388379
buildCtx, cancel := context.WithTimeout(ctx, maxCompileTime)
389380
defer cancel()
390-
goBin := "go"
391-
if useGvisor {
392-
goBin = "/usr/local/go1.14/bin/go"
393-
}
394-
cmd := exec.CommandContext(buildCtx, goBin,
395-
"build",
396-
"-o", exe,
397-
"-tags=faketime", // required for Go 1.14+, no-op before
398-
buildPkgArg)
381+
cmd := exec.CommandContext(ctx, "/usr/local/go-faketime/bin/go", "build", "-o", exe, "-tags=faketime", buildPkgArg)
399382
cmd.Dir = tmpDir
400383
var goPath string
401-
if useGvisor {
402-
cmd.Env = []string{"GOOS=linux", "GOARCH=amd64", "GOROOT=/usr/local/go1.14"}
403-
} else {
404-
cmd.Env = []string{"GOOS=nacl", "GOARCH=amd64p32"}
405-
}
384+
cmd.Env = []string{"GOOS=linux", "GOARCH=amd64", "GOROOT=/usr/local/go-faketime"}
406385
cmd.Env = append(cmd.Env, "GOCACHE="+goCache)
407386
if useModules {
408387
// Create a GOPATH just for modules to be downloaded
409388
// into GOPATH/pkg/mod.
410389
goPath, err = ioutil.TempDir("", "gopath")
411390
if err != nil {
391+
log.Printf("error creating temp directory: %v", err)
412392
return nil, fmt.Errorf("error creating temp directory: %v", err)
413393
}
414394
defer os.RemoveAll(goPath)
@@ -438,72 +418,53 @@ func compileAndRun(ctx context.Context, req *request) (*response, error) {
438418
}
439419
return nil, fmt.Errorf("error building go source: %v", err)
440420
}
441-
runCtx, cancel := context.WithTimeout(ctx, maxRunTime)
442-
defer cancel()
443421
rec := new(Recorder)
444422
var exitCode int
445-
if useGvisor {
446-
const maxBinarySize = 100 << 20 // copied from sandbox backend; TODO: unify?
447-
if fi, err := os.Stat(exe); err != nil || fi.Size() == 0 || fi.Size() > maxBinarySize {
448-
if err != nil {
449-
return nil, fmt.Errorf("failed to stat binary: %v", err)
450-
}
451-
return nil, fmt.Errorf("invalid binary size %d", fi.Size())
452-
}
453-
exeBytes, err := ioutil.ReadFile(exe)
454-
if err != nil {
455-
return nil, err
456-
}
457-
req, err := http.NewRequestWithContext(runCtx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
423+
const maxBinarySize = 100 << 20 // copied from sandbox backend; TODO: unify?
424+
if fi, err := os.Stat(exe); err != nil || fi.Size() == 0 || fi.Size() > maxBinarySize {
458425
if err != nil {
459-
return nil, err
460-
}
461-
req.Header.Add("Idempotency-Key", "1") // lets Transport do retries with a POST
462-
if testParam != "" {
463-
req.Header.Add("X-Argument", testParam)
464-
}
465-
req.GetBody = func() (io.ReadCloser, error) { return ioutil.NopCloser(bytes.NewReader(exeBytes)), nil }
466-
res, err := sandboxBackendClient().Do(req)
467-
if err != nil {
468-
return nil, err
469-
}
470-
defer res.Body.Close()
471-
if res.StatusCode != http.StatusOK {
472-
return nil, fmt.Errorf("unexpected response from backend: %v", res.Status)
473-
}
474-
var execRes sandboxtypes.Response
475-
if err := json.NewDecoder(res.Body).Decode(&execRes); err != nil {
476-
log.Printf("JSON decode error from backend: %v", err)
477-
return nil, errors.New("error parsing JSON from backend")
478-
}
479-
if execRes.Error != "" {
480-
return &response{Errors: execRes.Error}, nil
481-
}
482-
exitCode = execRes.ExitCode
483-
rec.Stdout().Write(execRes.Stdout)
484-
rec.Stderr().Write(execRes.Stderr)
485-
} else {
486-
cmd := exec.CommandContext(runCtx, "sel_ldr_x86_64", "-l", "/dev/null", "-S", "-e", exe, testParam)
487-
cmd.Stdout = rec.Stdout()
488-
cmd.Stderr = rec.Stderr()
489-
if err := cmd.Run(); err != nil {
490-
if runCtx.Err() == context.DeadlineExceeded {
491-
// Send what was captured before the timeout.
492-
events, err := rec.Events()
493-
if err != nil {
494-
return nil, fmt.Errorf("error decoding events: %v", err)
495-
}
496-
return &response{Errors: "process took too long", Events: events}, nil
497-
}
498-
exitErr, ok := err.(*exec.ExitError)
499-
if !ok {
500-
return nil, fmt.Errorf("error running sandbox: %v", err)
501-
}
502-
exitCode = exitErr.ExitCode()
426+
return nil, fmt.Errorf("failed to stat binary: %v", err)
503427
}
428+
return nil, fmt.Errorf("invalid binary size %d", fi.Size())
429+
}
430+
exeBytes, err := ioutil.ReadFile(exe)
431+
if err != nil {
432+
return nil, err
433+
}
434+
runCtx, cancel := context.WithTimeout(ctx, maxRunTime)
435+
defer cancel()
436+
sreq, err := http.NewRequestWithContext(runCtx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
437+
if err != nil {
438+
return nil, fmt.Errorf("NewRequestWithContext %q: %w", sandboxBackendURL(), err)
439+
}
440+
sreq.Header.Add("Idempotency-Key", "1") // lets Transport do retries with a POST
441+
if testParam != "" {
442+
sreq.Header.Add("X-Argument", testParam)
443+
}
444+
sreq.GetBody = func() (io.ReadCloser, error) { return ioutil.NopCloser(bytes.NewReader(exeBytes)), nil }
445+
res, err := sandboxBackendClient().Do(sreq)
446+
if err != nil {
447+
return nil, fmt.Errorf("POST %q: %w", sandboxBackendURL(), err)
448+
}
449+
defer res.Body.Close()
450+
if res.StatusCode != http.StatusOK {
451+
log.Printf("unexpected response from backend: %v", res.Status)
452+
return nil, fmt.Errorf("unexpected response from backend: %v", res.Status)
453+
}
454+
var execRes sandboxtypes.Response
455+
if err := json.NewDecoder(res.Body).Decode(&execRes); err != nil {
456+
log.Printf("JSON decode error from backend: %v", err)
457+
return nil, errors.New("error parsing JSON from backend")
458+
}
459+
if execRes.Error != "" {
460+
return &response{Errors: execRes.Error}, nil
504461
}
462+
exitCode = execRes.ExitCode
463+
rec.Stdout().Write(execRes.Stdout)
464+
rec.Stderr().Write(execRes.Stderr)
505465
events, err := rec.Events()
506466
if err != nil {
467+
log.Printf("error decoding events: %v", err)
507468
return nil, fmt.Errorf("error decoding events: %v", err)
508469
}
509470
var fails int

sandbox/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# environment so the play-sandbox server can connect to the host's
55
# docker daemon, which has the gvisor "runsc" runtime available.
66

7-
FROM golang:1.13 AS build
7+
FROM golang:1.14 AS build
88

99
COPY . /go/src/playground
1010
WORKDIR /go/src/playground/sandbox
@@ -15,7 +15,7 @@ FROM debian:buster
1515
RUN apt-get update
1616

1717
# Extra stuff for occasional debugging:
18-
RUN apt-get install --yes strace lsof emacs25-nox net-tools tcpdump procps
18+
RUN apt-get install --yes strace lsof emacs-nox net-tools tcpdump procps
1919

2020
# Install Docker CLI:
2121
RUN apt-get install --yes \

sandbox/Dockerfile.gvisor

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,11 @@
88
# gvisor-contained helper.
99
FROM golang/playground-sandbox AS server
1010

11-
############################################################################
12-
# Temporary nacl compatibility for development & incremental
13-
# deployment purposes, so we can run the new server architecture in
14-
# nacl mode for a bit, then opt-in linux/amd64 gvisor mode, and
15-
# then once Go 1.14 is out for real we remove the nacl option and
16-
# delete all the nacl code.
17-
FROM debian:buster AS nacl
18-
RUN apt-get update && apt-get install -y --no-install-recommends curl bzip2 ca-certificates
19-
RUN curl -s https://storage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk/trunk.544461/naclsdk_linux.tar.bz2 | tar -xj -C /tmp --strip-components=2 pepper_67/tools/sel_ldr_x86_64
20-
21-
2211
############################################################################
2312
# This is the actual environment things run in: a minimal busybox with glibc
2413
# binaries so we can use cgo.
2514
FROM busybox:glibc
2615

2716
COPY --from=server /usr/local/bin/play-sandbox /usr/local/bin/play-sandbox
2817

29-
# And this, temporarily, for being able to test the old nacl binaries
30-
# with the new sandbox:
31-
COPY --from=nacl /tmp/sel_ldr_x86_64 /usr/local/bin
32-
3318
ENTRYPOINT ["/usr/local/bin/play-sandbox"]

0 commit comments

Comments
 (0)