cmd/golangorg, internal/env: make deployment work for x/website

This change is part of an effort to deploy x/website to its own
subdomain on GCP. Everything needs to be working properly on the
subdomain before x/website can become the canonical website
(golang.org). There is currently a functional deployment of
x/website to the 'new-website' service under golang-org on GCP.

With these specific changes, x/website gets deployed to the
service 'new-website'. By changing both the service field in
app.prod.yaml and the GCP_SERVICE variable in Makefile to
'default', these files could be used to deploy x/website to
the 'default' service as well.

Updates golang/go#29206

Change-Id: I47340333b9e09672e8588c330d629857d1d0bf0d
Reviewed-on: https://go-review.googlesource.com/c/161198
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/golangorg/Dockerfile.prod b/cmd/golangorg/Dockerfile.prod
index 9c4fdb7..a154051 100644
--- a/cmd/golangorg/Dockerfile.prod
+++ b/cmd/golangorg/Dockerfile.prod
@@ -6,7 +6,7 @@
 RUN apt-get update && apt-get install -y \
       zip # required for generate-index.bash
 
-# Check out the desired version of Go, both to build the godoc binary and serve
+# Check out the desired version of Go, both to build the golangorg binary and serve
 # as the goroot for content serving.
 ARG GO_REF
 RUN test -n "$GO_REF" # GO_REF is required.
@@ -15,22 +15,16 @@
 
 ENV GOROOT /goroot
 ENV PATH=/goroot/bin:$PATH
+ENV GO111MODULE=on
 
 RUN go version
 
-RUN go get -v -d \
-      golang.org/x/net/context \
-      google.golang.org/appengine \
-      cloud.google.com/go/datastore \
-      golang.org/x/build \
-      github.com/gomodule/redigo/redis
+COPY . /website
 
-COPY . /go/src/golang.org/x/tools
+WORKDIR /website/cmd/golangorg
+RUN GOLANGORG_DOCSET=/goroot ./generate-index.bash
 
-WORKDIR /go/src/golang.org/x/website/cmd/golangorg
-RUN GODOC_DOCSET=/goroot ./generate-index.bash
-
-RUN go build -o /godoc -tags=golangorg golang.org/x/website/cmd/golangorg
+RUN go build -o /golangorg -tags=golangorg golang.org/x/website/cmd/golangorg
 
 # Clean up goroot for the final image.
 RUN cd /goroot && git clean -xdf
@@ -38,10 +32,10 @@
 # Add build metadata.
 RUN cd /goroot && echo "go repo HEAD: $(git rev-parse HEAD)" >> /goroot/buildinfo
 RUN echo "requested go ref: ${GO_REF}" >> /goroot/buildinfo
-ARG TOOLS_HEAD
-RUN echo "x/tools HEAD: ${TOOLS_HEAD}" >> /goroot/buildinfo
-ARG TOOLS_CLEAN
-RUN echo "x/tools clean: ${TOOLS_CLEAN}" >> /goroot/buildinfo
+ARG WEBSITE_HEAD
+RUN echo "x/website HEAD: ${WEBSITE_HEAD}" >> /goroot/buildinfo
+ARG WEBSITE_CLEAN
+RUN echo "x/website clean: ${WEBSITE_CLEAN}" >> /goroot/buildinfo
 ARG DOCKER_TAG
 RUN echo "image: ${DOCKER_TAG}" >> /goroot/buildinfo
 ARG BUILD_ENV
@@ -55,13 +49,13 @@
 FROM gcr.io/distroless/base
 
 WORKDIR /app
-COPY --from=build /godoc /app/
-COPY --from=build /go/src/golang.org/x/website/cmd/golangorg/hg-git-mapping.bin /app/
+COPY --from=build /golangorg /app/
+COPY --from=build /website/cmd/golangorg/hg-git-mapping.bin /app/
 
 COPY --from=build /goroot /goroot
 ENV GOROOT /goroot
 
-COPY --from=build /go/src/golang.org/x/website/cmd/golangorg/index.split.* /app/
-ENV GODOC_INDEX_GLOB index.split.*
+COPY --from=build /website/cmd/golangorg/index.split.* /app/
+ENV GOLANGORG_INDEX_GLOB index.split.*
 
-CMD ["/app/godoc"]
+CMD ["/app/golangorg"]
diff --git a/cmd/golangorg/Makefile b/cmd/golangorg/Makefile
index 889ef2d..a8cf873 100644
--- a/cmd/golangorg/Makefile
+++ b/cmd/golangorg/Makefile
@@ -5,15 +5,16 @@
 .PHONY: usage
 
 GO_REF ?= release-branch.go1.11
-TOOLS_HEAD := $(shell git rev-parse HEAD)
-TOOLS_CLEAN := $(shell (git status --porcelain | grep -q .) && echo dirty || echo clean)
-ifeq ($(TOOLS_CLEAN),clean)
-	DOCKER_VERSION ?= $(TOOLS_HEAD)
+WEBSITE_HEAD := $(shell git rev-parse HEAD)
+WEBSITE_CLEAN := $(shell (git status --porcelain | grep -q .) && echo dirty || echo clean)
+ifeq ($(WEBSITE_CLEAN),clean)
+	DOCKER_VERSION ?= $(WEBSITE_HEAD)
 else
-	DOCKER_VERSION ?= $(TOOLS_HEAD)-dirty
+	DOCKER_VERSION ?= $(WEBSITE_HEAD)-dirty
 endif
 GCP_PROJECT := golang-org
-DOCKER_TAG := gcr.io/$(GCP_PROJECT)/godoc:$(DOCKER_VERSION)
+GCP_SERVICE := new-website
+DOCKER_TAG := gcr.io/$(GCP_PROJECT)/golangorg:$(DOCKER_VERSION)
 
 usage:
 	@echo "See Makefile and README.golangorg-app"
@@ -23,17 +24,17 @@
 	gcloud builds submit \
 		--project=$(GCP_PROJECT) \
 		--config=cloudbuild.yaml \
-		--substitutions=_GO_REF=$(GO_REF),_TOOLS_HEAD=$(TOOLS_HEAD),_TOOLS_CLEAN=$(TOOLS_CLEAN),_DOCKER_TAG=$(DOCKER_TAG) \
+		--substitutions=_GO_REF=$(GO_REF),_WEBSITE_HEAD=$(WEBSITE_HEAD),_WEBSITE_CLEAN=$(WEBSITE_CLEAN),_DOCKER_TAG=$(DOCKER_TAG) \
 		../.. # source code
 
 docker-build: Dockerfile.prod
 	# NOTE(cbro): move up in directory to include entire tools repo.
 	# NOTE(cbro): any changes made to this command must also be made in cloudbuild.yaml.
 	cd ../..; docker build \
-		-f=cmd/godoc/Dockerfile.prod \
+		-f=cmd/golangorg/Dockerfile.prod \
 		--build-arg=GO_REF=$(GO_REF) \
-		--build-arg=TOOLS_HEAD=$(TOOLS_HEAD) \
-		--build-arg=TOOLS_CLEAN=$(TOOLS_CLEAN) \
+		--build-arg=WEBSITE_HEAD=$(WEBSITE_HEAD) \
+		--build-arg=WEBSITE_CLEAN=$(WEBSITE_CLEAN) \
 		--build-arg=DOCKER_TAG=$(DOCKER_TAG) \
 		--build-arg=BUILD_ENV=local \
 		--tag=$(DOCKER_TAG) \
@@ -50,7 +51,7 @@
 
 get-latest-url:
 	@gcloud app versions list \
-		--service=default \
+		--service=$(GCP_SERVICE) \
 		--project=$(GCP_PROJECT) \
 		--sort-by=~version.createTime \
 		--format='value(version.versionUrl)' \
@@ -58,7 +59,7 @@
 
 get-latest-id:
 	@gcloud app versions list \
-		--service=default \
+		--service=$(GCP_SERVICE) \
 		--project=$(GCP_PROJECT) \
 		--sort-by=~version.createTime \
 		--format='value(version.id)' \
@@ -70,11 +71,11 @@
 		-run=Live
 
 publish: regtest
-	gcloud -q app services set-traffic default \
+	gcloud -q app services set-traffic $(GCP_SERVICE) \
 		--splits=$(shell make get-latest-id)=1 \
 		--project=$(GCP_PROJECT)
 
 	@echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 	@echo Stop and/or delete old versions:
-	@echo "https://console.cloud.google.com/appengine/versions?project=$(GCP_PROJECT)&serviceId=default&versionssize=50"
+	@echo "https://console.cloud.google.com/appengine/versions?project=$(GCP_PROJECT)&serviceId=$(GCP_SERVICE)&versionssize=50"
 	@echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
diff --git a/cmd/golangorg/app.prod.yaml b/cmd/golangorg/app.prod.yaml
index 315c3db..2db7887 100644
--- a/cmd/golangorg/app.prod.yaml
+++ b/cmd/golangorg/app.prod.yaml
@@ -1,11 +1,12 @@
+service: new-website
 runtime: custom
 env: flex
 
 env_variables:
-  GODOC_PROD: true
-  GODOC_ENFORCE_HOSTS: true
-  GODOC_REDIS_ADDR: 10.0.0.4:6379 # instance "gophercache"
-  GODOC_ANALYTICS: UA-11222381-2
+  GOLANGORG_PROD: true
+  GOLANGORG_ENFORCE_HOSTS: true
+  GOLANGORG_REDIS_ADDR: 10.0.0.4:6379 # instance "gophercache"
+  GOLANGORG_ANALYTICS: UA-11222381-2
   DATASTORE_PROJECT_ID: golang-org
 
 network:
diff --git a/cmd/golangorg/appinit.go b/cmd/golangorg/appinit.go
index 4429c3d..0b3c4a0 100644
--- a/cmd/golangorg/appinit.go
+++ b/cmd/golangorg/appinit.go
@@ -42,14 +42,14 @@
 
 	var (
 		// .zip filename
-		zipFilename = os.Getenv("GODOC_ZIP")
+		zipFilename = os.Getenv("GOLANGORG_ZIP")
 
 		// goroot directory in .zip file
-		zipGoroot = os.Getenv("GODOC_ZIP_PREFIX")
+		zipGoroot = os.Getenv("GOLANGORG_ZIP_PREFIX")
 
 		// glob pattern describing search index files
 		// (if empty, the index is built at run-time)
-		indexFilenames = os.Getenv("GODOC_INDEX_GLOB")
+		indexFilenames = os.Getenv("GOLANGORG_INDEX_GLOB")
 	)
 
 	playEnabled = true
@@ -101,7 +101,7 @@
 	pres.ShowPlayground = true
 	pres.DeclLinks = true
 	pres.NotesRx = regexp.MustCompile("BUG")
-	pres.GoogleAnalytics = os.Getenv("GODOC_ANALYTICS")
+	pres.GoogleAnalytics = os.Getenv("GOLANGORG_ANALYTICS")
 
 	readTemplates(pres)
 
@@ -160,9 +160,9 @@
 		log.Fatalf("datastore.NewClient: %v.", err)
 	}
 
-	redisAddr := os.Getenv("GODOC_REDIS_ADDR")
+	redisAddr := os.Getenv("GOLANGORG_REDIS_ADDR")
 	if redisAddr == "" {
-		log.Fatalf("Missing redis server for godoc in production mode. set GODOC_REDIS_ADDR environment variable.")
+		log.Fatalf("Missing redis server for golangorg in production mode. set GOLANGORG_REDIS_ADDR environment variable.")
 	}
 	memcacheClient := memcache.New(redisAddr)
 	return datastoreClient, memcacheClient
diff --git a/cmd/golangorg/cloudbuild.yaml b/cmd/golangorg/cloudbuild.yaml
index fd297f0..af6e8a2 100644
--- a/cmd/golangorg/cloudbuild.yaml
+++ b/cmd/golangorg/cloudbuild.yaml
@@ -11,10 +11,10 @@
 - name: 'gcr.io/cloud-builders/docker'
   args: [
     'build',
-    '-f=cmd/godoc/Dockerfile.prod',
+    '-f=cmd/golangorg/Dockerfile.prod',
     '--build-arg=GO_REF=${_GO_REF}',
-    '--build-arg=TOOLS_HEAD=${_TOOLS_HEAD}',
-    '--build-arg=TOOLS_CLEAN=${_TOOLS_CLEAN}',
+    '--build-arg=WEBSITE_HEAD=${_WEBSITE_HEAD}',
+    '--build-arg=WEBSITE_CLEAN=${_WEBSITE_CLEAN}',
     '--build-arg=DOCKER_TAG=${_DOCKER_TAG}',
     '--build-arg=BUILD_ENV=cloudbuild',
     '--tag=${_DOCKER_TAG}',
diff --git a/cmd/golangorg/generate-index.bash b/cmd/golangorg/generate-index.bash
index 21b567a..0aee006 100755
--- a/cmd/golangorg/generate-index.bash
+++ b/cmd/golangorg/generate-index.bash
@@ -11,8 +11,8 @@
 
 set -e -u -x
 
-ZIPFILE=godoc.zip
-INDEXFILE=godoc.index
+ZIPFILE=golangorg.zip
+INDEXFILE=golangorg.index
 SPLITFILES=index.split.
 
 error() {
@@ -25,33 +25,33 @@
 }
 
 getArgs() {
-	if [ ! -v GODOC_DOCSET ]; then
-		GODOC_DOCSET="$(go env GOROOT)"
-		echo "GODOC_DOCSET not set explicitly, using GOROOT instead"
+	if [ ! -v GOLANGORG_DOCSET ]; then
+		GOLANGORG_DOCSET="$(go env GOROOT)"
+		echo "GOLANGORG_DOCSET not set explicitly, using GOROOT instead"
 	fi
 
 	# safety checks
-	if [ ! -d "$GODOC_DOCSET" ]; then
-		error "$GODOC_DOCSET is not a directory"
+	if [ ! -d "$GOLANGORG_DOCSET" ]; then
+		error "$GOLANGORG_DOCSET is not a directory"
 	fi
 
 	# reporting
-	echo "GODOC_DOCSET = $GODOC_DOCSET"
+	echo "GOLANGORG_DOCSET = $GOLANGORG_DOCSET"
 }
 
 makeZipfile() {
 	echo "*** make $ZIPFILE"
 	rm -f $ZIPFILE goroot
-	ln -s "$GODOC_DOCSET" goroot
+	ln -s "$GOLANGORG_DOCSET" goroot
 	zip -q -r $ZIPFILE goroot/* # glob to ignore dotfiles (like .git)
 	rm goroot
 }
 
 makeIndexfile() {
 	echo "*** make $INDEXFILE"
-	godoc=$(go env GOPATH)/bin/godoc
-	# NOTE: run godoc without GOPATH set. Otherwise third-party packages will end up in the index.
-	GOPATH= $godoc -write_index -goroot goroot -index_files=$INDEXFILE -zip=$ZIPFILE
+	golangorg=$(go env GOPATH)/bin/golangorg
+	# NOTE: run golangorg without GOPATH set. Otherwise third-party packages will end up in the index.
+	GOPATH= $golangorg -write_index -goroot goroot -index_files=$INDEXFILE -zip=$ZIPFILE
 }
 
 splitIndexfile() {
diff --git a/internal/env/env.go b/internal/env/env.go
index e1f55cd..1803f6c 100644
--- a/internal/env/env.go
+++ b/internal/env/env.go
@@ -13,8 +13,8 @@
 )
 
 var (
-	isProd       = boolEnv("GODOC_PROD")
-	enforceHosts = boolEnv("GODOC_ENFORCE_HOSTS")
+	isProd       = boolEnv("GOLANGORG_PROD")
+	enforceHosts = boolEnv("GOLANGORG_ENFORCE_HOSTS")
 )
 
 // IsProd reports whether the server is running in its production configuration