From 0205714b0681c7d26ff53edea32fbdc98d9eb908 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Fri, 12 May 2023 09:17:45 +1000 Subject: [PATCH 01/18] WIP: placeholder --- infra/jobs.tf | 26 +++++++++++++++++++++++ infra/postdeployment.tf | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/infra/jobs.tf b/infra/jobs.tf index ae72982e..598513ef 100644 --- a/infra/jobs.tf +++ b/infra/jobs.tf @@ -136,3 +136,29 @@ resource "google_cloud_run_v2_job" "client" { google_project_service.enabled ] } + + +resource "google_cloud_run_v2_job" "placeholder" { + name = var.random_suffix ? "placeholder-${random_id.suffix.hex}" : "placeholder" + location = var.region + + labels = var.labels + + template { + template { + service_account = google_service_account.client.email + containers { + image = local.placeholder_image + env { + name = "PROJECT_ID" + value = var.project_id + } + + } + } + } + + depends_on = [ + google_project_service.enabled + ] +} diff --git a/infra/postdeployment.tf b/infra/postdeployment.tf index 491f19d8..f16923c2 100644 --- a/infra/postdeployment.tf +++ b/infra/postdeployment.tf @@ -91,3 +91,49 @@ curl ${local.server_url}/api/products/?warmup shutdown -h now EOT } + + +resource "google_compute_instance" "placeholder_init" { + count = var.init ? 1 : 0 + + depends_on = [ + google_project_service.enabled, + google_cloud_run_v2_job.placeholder, + ] + + name = var.random_suffix ? "placeholder-initialize-${random_id.suffix.hex}" : "placeholder-initialize" + machine_type = "n1-standard-1" + zone = var.zone + desired_status = "RUNNING" + + allow_stopping_for_update = true + + boot_disk { + initialize_params { + image = "debian-cloud/debian-11" + } + } + + network_interface { + network = google_compute_network.gce_init[0].self_link + subnetwork = google_compute_subnetwork.gce_init[0].self_link + + access_config { + // Ephemeral public IP + } + } + + service_account { + email = google_service_account.compute[0].email + scopes = ["cloud-platform"] # TODO: Restrict?? + } + + metadata_startup_script = < Date: Fri, 12 May 2023 12:26:14 +1000 Subject: [PATCH 02/18] wip: add placeholder image local --- infra/containers.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/infra/containers.tf b/infra/containers.tf index 9e89732e..a81cb8fd 100644 --- a/infra/containers.tf +++ b/infra/containers.tf @@ -19,4 +19,5 @@ locals { server_image = "gcr.io/${var.server_image_host}/server:${var.image_version}" client_image = "gcr.io/${var.client_image_host}/client:${var.image_version}" + placeholder_image = "gcr.io/hsa-public/avocano-placeholder:latest" } From c2d1ba0b7ad5827a5be01be9330a8dcd792e168e Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Fri, 12 May 2023 13:38:49 +1000 Subject: [PATCH 03/18] fix: add PURGE to ensure website refresh --- infra/postdeployment.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/infra/postdeployment.tf b/infra/postdeployment.tf index f16923c2..a6051591 100644 --- a/infra/postdeployment.tf +++ b/infra/postdeployment.tf @@ -133,6 +133,7 @@ resource "google_compute_instance" "placeholder_init" { echo "Running placeholder deployment" gcloud beta run jobs execute ${google_cloud_run_v2_job.placeholder.name} --wait --project ${var.project_id} --region ${var.region} +curl -X PURGE "${local.firebase_url}/" shutdown -h now EOT From 96006b127228bda9def266f6b6f1cbcfadd48a07 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Fri, 12 May 2023 14:35:06 +1000 Subject: [PATCH 04/18] adapt site check method and docs --- .../simple_example/simple_example_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/infra/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 94665067..dac48896 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -35,20 +35,20 @@ func TestSimpleExample(t *testing.T) { example.DefineApply(func(assert *assert.Assertions) { example.DefaultApply(assert) - // Use of this module as part of a Jump Start Solution triggers a URL - // request when terraform apply completes. This primes the Firebase Hosting - // CDN with a platform-supplied 404 page. + // This module deploys a 'placeholder' Firebase Hosting release early + // in the process, to prevent a "Site Not Found" displaying when Terraform + // has finished applying, but the deployment is not yet complete. // // This extension of apply is meant to emulate that behavior. We confirm - // the 404 behavior here to boost confidence that the frontend test in - // example.DefineVerify proves the 404 page is fixed. + // the placeholder behavior here to boost confidence that the frontend test in + // example.DefineVerify proves the placeholder page is replaced. // - // If the check for "Site Not Found" is flaky, remove it in favor of + // If the check is flaky, remove it in favor of // a simpler HTTP request. // // https://github.com/GoogleCloudPlatform/terraform-dynamic-python-webapp/issues/64 firebase_url := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") - assertErrorResponseContains(assert, firebase_url, http.StatusNotFound, "Site Not Found") + assertResponseContains(assert, firebase_url, "Your application is still deploying") }) example.DefineVerify(func(assert *assert.Assertions) { From 3a6cdc242a7da7a54b41a06194f592762fae2edd Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Fri, 12 May 2023 14:36:32 +1000 Subject: [PATCH 05/18] terraform fmt --- infra/containers.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/infra/containers.tf b/infra/containers.tf index a81cb8fd..1fefdade 100644 --- a/infra/containers.tf +++ b/infra/containers.tf @@ -17,7 +17,7 @@ # Base images supplied pre-built. locals { - server_image = "gcr.io/${var.server_image_host}/server:${var.image_version}" - client_image = "gcr.io/${var.client_image_host}/client:${var.image_version}" + server_image = "gcr.io/${var.server_image_host}/server:${var.image_version}" + client_image = "gcr.io/${var.client_image_host}/client:${var.image_version}" placeholder_image = "gcr.io/hsa-public/avocano-placeholder:latest" } From 285cbf3068d29f6d45d14076d43d69c771046448 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Mon, 15 May 2023 10:29:22 +1000 Subject: [PATCH 06/18] Merge https://github.com/GoogleCloudPlatform/avocano/pull/231 --- app/README.md | 6 ++- app/placeholder/Dockerfile | 21 ++++++++ app/placeholder/README.md | 11 ++++ app/placeholder/dist/index.html | 51 +++++++++++++++++++ app/placeholder/firebase.json | 5 ++ app/placeholder/placeholder-deploy.sh | 8 +++ .../placeholder-image.cloudbuild.yaml | 38 ++++++++++++++ 7 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 app/placeholder/Dockerfile create mode 100644 app/placeholder/README.md create mode 100644 app/placeholder/dist/index.html create mode 100644 app/placeholder/firebase.json create mode 100755 app/placeholder/placeholder-deploy.sh create mode 100644 app/placeholder/placeholder-image.cloudbuild.yaml diff --git a/app/README.md b/app/README.md index 2fb12db7..f1438ac2 100644 --- a/app/README.md +++ b/app/README.md @@ -1 +1,5 @@ -See https://github.com/GoogleCloudPlatform/avocano +This application uses three containers images: + + * `client`: https://github.com/GoogleCloudPlatform/avocano/tree/main/client + * `server`: https://github.com/GoogleCloudPlatform/avocano/tree/main/server + * `placeholder`: [placeholder/](placeholder/) \ No newline at end of file diff --git a/app/placeholder/Dockerfile b/app/placeholder/Dockerfile new file mode 100644 index 00000000..cfce24d4 --- /dev/null +++ b/app/placeholder/Dockerfile @@ -0,0 +1,21 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# Execute with "docker run --build-arg PROJECT_ID=$PROJECT_ID ..." +ARG PROJECT_ID=YOURPROJECTID +FROM gcr.io/$PROJECT_ID/firebase + +COPY . ./ +ENTRYPOINT ./placeholder-deploy.sh diff --git a/app/placeholder/README.md b/app/placeholder/README.md new file mode 100644 index 00000000..eba1ea41 --- /dev/null +++ b/app/placeholder/README.md @@ -0,0 +1,11 @@ +# Placeholder + +This folder is designed to hold a static page that can be deployed to Firebase as a placeholder until the real website is deployed. + +Features: + + * replaces the Firebase Hosting "Site Not Found" 404 page with a placeholder + * page set to automatically refresh every 5 seconds + * uses embedded image (like the "Site Not Found" does) to prevent image loading flickering. + +This placeholder is built in `placeholder-image.cloudbuild.json`, automatically built and deployed using Cloud Build triggers for use in the Jump Start Solution terraform. diff --git a/app/placeholder/dist/index.html b/app/placeholder/dist/index.html new file mode 100644 index 00000000..c6549efc --- /dev/null +++ b/app/placeholder/dist/index.html @@ -0,0 +1,51 @@ + + + + +
+
+
Your application is still deploying...
+

You can check the status of your deployment by checking your + Cloud Run jobs. +

This page will refresh automatically.

+
+
+ + + + \ No newline at end of file diff --git a/app/placeholder/firebase.json b/app/placeholder/firebase.json new file mode 100644 index 00000000..76683fd0 --- /dev/null +++ b/app/placeholder/firebase.json @@ -0,0 +1,5 @@ +{ + "hosting": { + "public": "dist" + } +} diff --git a/app/placeholder/placeholder-deploy.sh b/app/placeholder/placeholder-deploy.sh new file mode 100755 index 00000000..8509674e --- /dev/null +++ b/app/placeholder/placeholder-deploy.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Script to assist in Dockerfile-based deployments. +# any errors? exit immediately. +set -e + +echo "Deploying placeholder to Firebase..." + +firebase deploy --project $PROJECT_ID --only hosting diff --git a/app/placeholder/placeholder-image.cloudbuild.yaml b/app/placeholder/placeholder-image.cloudbuild.yaml new file mode 100644 index 00000000..23fe9ddf --- /dev/null +++ b/app/placeholder/placeholder-image.cloudbuild.yaml @@ -0,0 +1,38 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Build placeholder site code into container + +steps: + - id: build + name: "gcr.io/cloud-builders/docker" + dir: client/placeholder + args: + [ + "build", + "--build-arg", + "PROJECT_ID=$PROJECT_ID", + "-t", + "gcr.io/$PROJECT_ID/$_IMAGE_NAME", + ".", + ] + +images: + - gcr.io/$PROJECT_ID/$_IMAGE_NAME + +substitutions: + _IMAGE_NAME: placeholder + +options: + dynamic_substitutions: true From c0773c008b597bbf38b9f5c129c285ca3ddcb019 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Mon, 15 May 2023 11:14:45 +1000 Subject: [PATCH 07/18] Improve accessibility on placeholder site. --- app/placeholder/dist/index.html | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/placeholder/dist/index.html b/app/placeholder/dist/index.html index c6549efc..250dde4e 100644 --- a/app/placeholder/dist/index.html +++ b/app/placeholder/dist/index.html @@ -1,4 +1,11 @@ - + + +Application still deploying + + + +