diff --git a/platforms/gke/base/use-cases/federated-learning/common.sh b/platforms/gke/base/use-cases/federated-learning/common.sh index 382a7eb3..e61877b6 100755 --- a/platforms/gke/base/use-cases/federated-learning/common.sh +++ b/platforms/gke/base/use-cases/federated-learning/common.sh @@ -31,12 +31,8 @@ FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR="${FEDERATED_LEARNING_USE_CASE_DIR}/te # shellcheck disable=SC2034 # Variable is used in other scripts FEDERATED_LEARNING_SHARED_CONFIG_DIR="${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/_shared_config" -# shellcheck disable=SC2034 # Variable is used in other scripts -FEDERATED_LEARNING_USE_CASE_INITIALIZE_SERVICE_DIR="${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/initialize" - # Terraservices that are necessary for the core platform federated_learning_core_platform_terraservices=( - "initialize" "key_management_service" ) @@ -47,16 +43,6 @@ federated_learning_terraservices=( "private_google_access" ) -TERRAFORM_INIT_COMMAND=( - terraform init -) - -# shellcheck disable=SC2034 # Variable is used in other scripts -TERRAFORM_INIT_BACKEND_CONFIG_COMMAND=( - "${TERRAFORM_INIT_COMMAND[@]}" - -backend-config="backend.config" -) - # The values of some variables depends on Terraform outputs # shellcheck disable=SC2034 # Variable is used in other scripts TERRAFORM_CLUSTER_CONFIGURATION=( @@ -66,38 +52,50 @@ TERRAFORM_CLUSTER_CONFIGURATION=( "cluster_database_encryption_key_name = \"cluster_database_encryption_key_name_placeholder\"" ) -provision_terraservice() { +apply_or_destroy_terraservice() { local terraservice terraservice="${1}" - local -a TERRASERVICE_TERRAFORM_INIT_COMMAND + local operation_mode + operation_mode="${2:-"not set"}" + + cd "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}" && + terraform init - echo "Provisioning ${terraservice}" - TERRASERVICE_TERRAFORM_INIT_COMMAND=( - "${TERRAFORM_INIT_BACKEND_CONFIG_COMMAND[@]}" - ) + echo "Current working directory: $(pwd)" - if [ "${terraservice:-}" == "initialize" ]; then - TERRASERVICE_TERRAFORM_INIT_COMMAND=( - "${TERRAFORM_INIT_COMMAND[@]}" - ) + if [[ "${operation_mode}" == "apply" ]]; then + echo "Provisioning ${terraservice}" + terraform plan -input=false -out=tfplan && + terraform apply -input=false tfplan + _terraform_result=$? + elif [[ "${operation_mode}" == "destroy" ]]; then + echo "Destroying ${terraservice}" + terraform destroy -auto-approve + _terraform_result=$? + else + echo "Error: operation mode not supported: ${operation_mode}" + _terraform_result=1 fi - echo "Terraform init command for ${terraservice}: ${TERRASERVICE_TERRAFORM_INIT_COMMAND[*]}" + rm -rf \ + "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}/.terraform" \ + "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}/tfplan" - cd "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}" && - echo "Current directory: $(pwd)" && - "${TERRASERVICE_TERRAFORM_INIT_COMMAND[@]}" && - terraform plan -input=false -out=tfplan && - terraform apply -input=false tfplan - local _apply_result=$? - rm tfplan - if [[ ${_apply_result} -ne 0 ]]; then - echo "Terraform apply for ${terraservice} failed with code ${_apply_result}" - exit $_apply_result + if [[ ${_terraform_result} -ne 0 ]]; then + echo "Terraform ${operation_mode} command failed with code ${_terraform_result} for ${terraservice}" + exit ${_terraform_result} fi } +provision_terraservice() { + apply_or_destroy_terraservice "${1}" "apply" +} + +destroy_terraservice() { + apply_or_destroy_terraservice "${1}" "destroy" +} + get_terraform_output() { terraservice="${1}" output_name="${2}" diff --git a/platforms/gke/base/use-cases/federated-learning/teardown.sh b/platforms/gke/base/use-cases/federated-learning/teardown.sh index c3ecc5b8..41ab65c6 100755 --- a/platforms/gke/base/use-cases/federated-learning/teardown.sh +++ b/platforms/gke/base/use-cases/federated-learning/teardown.sh @@ -28,34 +28,7 @@ start_timestamp_federated_learning=$(date +%s) # shellcheck disable=SC2154 # variable defined in common.sh for ((i = ${#federated_learning_terraservices[@]} - 1; i >= 0; i--)); do terraservice=${federated_learning_terraservices[i]} - echo "Destroying ${terraservice}" - - TERRASERVICE_TERRAFORM_INIT_COMMAND=( - "${TERRAFORM_INIT_BACKEND_CONFIG_COMMAND[@]}" - ) - - # The initialize terraservice uses a local backend, so we don't add the remote backend configuration - if [ "${terraservice:-}" == "initialize" ]; then - TERRASERVICE_TERRAFORM_INIT_COMMAND=( - "${TERRAFORM_INIT_COMMAND[@]}" - ) - fi - - echo "Terraform init command: ${TERRASERVICE_TERRAFORM_INIT_COMMAND[*]}" - - cd "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}" && - echo "Current directory: $(pwd)" && - "${TERRASERVICE_TERRAFORM_INIT_COMMAND[@]}" && - terraform destroy -auto-approve - - _destroy_result=$? - rm -rf \ - "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}/.terraform" - - if [[ ${_destroy_result} -ne 0 ]]; then - echo "Terraform destroy failed with code ${_destroy_result}" - exit ${_destroy_result} - fi + destroy_terraservice "${terraservice}" done echo "Destroying the core platform" diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/container_image_repository/backend.tf b/platforms/gke/base/use-cases/federated-learning/terraform/container_image_repository/backend.tf deleted file mode 100644 index 13bf431a..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/container_image_repository/backend.tf +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2024 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. - -terraform { - backend "gcs" { - bucket = "" - prefix = "terraform/federated-learning/container-image-repository" - } -} diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/.terraform.lock.hcl b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/.terraform.lock.hcl deleted file mode 100644 index a5c88c2e..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/.terraform.lock.hcl +++ /dev/null @@ -1,42 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/hashicorp/google" { - version = "6.12.0" - constraints = "6.12.0" - hashes = [ - "h1:rvZHMkoxkHrBYQXb/waoZiD2oo3FS1AF8HoWHlb6SN8=", - "zh:14701aa307a832d99f567b8056a4c5e4ee5a403d984c98f024deee7507a3f29c", - "zh:344eca00ffb2643c2fa7f52f069b659d50bb4c9369df4cad96ea0fadb54282c8", - "zh:5fb57c0acfd4d30a39941900040d5518a909d8c975af0c4366a7bfd0d0bb09a8", - "zh:617a77048a5b9aa568e8bc706cc84307a237b2dd0e49709028b283f8bbe42475", - "zh:677837a05fefe0342cf4d4bdc494e8fd4d62331cac947820e73df37e8f512688", - "zh:7b79f6e02474eef4a1480fc6589afb63ed16b25bf019b6056f9838e2845e2ef8", - "zh:7d891fceb5b15e81240d829f42e1a36e4c812bfc1abe7856756e59101932205f", - "zh:97f1e0ac799faf382426e070e888fac36b0867597b460dc95b0e7f657de21ba9", - "zh:9855f2f2f5919ff6a6a2c982439c910d28c8978ad18cd8f549a5d1ba9b4dc4c3", - "zh:ac551367180eb396af2a50244e80243d333d600a76002e29935262d76a02290b", - "zh:c354f34e6579933d21a98ce7f31f4ef8aeaceb04cfaedaff6d3f3c0be56b2c79", - "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", - ] -} - -provider "registry.terraform.io/hashicorp/local" { - version = "2.5.2" - constraints = "2.5.2" - hashes = [ - "h1:JlMZD6nYqJ8sSrFfEAH0Vk/SL8WLZRmFaMUF9PJK5wM=", - "zh:136299545178ce281c56f36965bf91c35407c11897f7082b3b983d86cb79b511", - "zh:3b4486858aa9cb8163378722b642c57c529b6c64bfbfc9461d940a84cd66ebea", - "zh:4855ee628ead847741aa4f4fc9bed50cfdbf197f2912775dd9fe7bc43fa077c0", - "zh:4b8cd2583d1edcac4011caafe8afb7a95e8110a607a1d5fb87d921178074a69b", - "zh:52084ddaff8c8cd3f9e7bcb7ce4dc1eab00602912c96da43c29b4762dc376038", - "zh:71562d330d3f92d79b2952ffdda0dad167e952e46200c767dd30c6af8d7c0ed3", - "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", - "zh:805f81ade06ff68fa8b908d31892eaed5c180ae031c77ad35f82cb7a74b97cf4", - "zh:8b6b3ebeaaa8e38dd04e56996abe80db9be6f4c1df75ac3cccc77642899bd464", - "zh:ad07750576b99248037b897de71113cc19b1a8d0bc235eb99173cc83d0de3b1b", - "zh:b9f1c3bfadb74068f5c205292badb0661e17ac05eb23bfe8bd809691e4583d0e", - "zh:cc4cbcd67414fefb111c1bf7ab0bc4beb8c0b553d01719ad17de9a047adff4d1", - ] -} diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster.auto.tfvars b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster.auto.tfvars deleted file mode 120000 index 98a694db..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster.auto.tfvars +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/cluster.auto.tfvars \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster_variables.tf b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster_variables.tf deleted file mode 120000 index 00625515..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_cluster_variables.tf +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/cluster_variables.tf \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform.auto.tfvars b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform.auto.tfvars deleted file mode 120000 index 125a652c..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform.auto.tfvars +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/platform.auto.tfvars \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform_variables.tf b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform_variables.tf deleted file mode 120000 index 486b3eae..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_platform_variables.tf +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/platform_variables.tf \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform.auto.tfvars b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform.auto.tfvars deleted file mode 120000 index 4af8b832..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform.auto.tfvars +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/terraform.auto.tfvars \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform_variables.tf b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform_variables.tf deleted file mode 120000 index d51ad696..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/_terraform_variables.tf +++ /dev/null @@ -1 +0,0 @@ -../../../../_shared_config/terraform_variables.tf \ No newline at end of file diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/main.tf b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/main.tf deleted file mode 100644 index 7eebfe03..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/main.tf +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright 2024 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. - -locals { - _template_backend = "${path.module}/templates/backend.config.tpl" - - backend = templatefile(local._template_backend, { - bucket = data.google_storage_bucket.terraform.name - }) - - terraservices_path = "${path.module}/.." - - # Search for backend.tf files, assuming that a directory that contains a - # backend.tf file is a terraservice to provision. - # This search excludes the initialize terraservice - # because we use a local backend for that in order to solve a - # chicken-and-egg issue where the initialize terraservice depends on - # the configuration files that the initialize terraservice creates. - backend_files = flatten([for _, v in flatten(fileset("${local.terraservices_path}/", "**/backend.tf")) : v]) - use_case_terraservices = toset([for _, v in local.backend_files : abspath("${local.terraservices_path}/${trimprefix(trimsuffix(dirname(v), "/backend.tf"), "../")}")]) -} - -data "google_project" "default" { - project_id = var.cluster_project_id -} - -data "google_storage_bucket" "terraform" { - name = local.terraform_bucket_name - project = data.google_project.default.project_id -} - -resource "local_file" "backend" { - for_each = toset(local.use_case_terraservices) - - file_permission = "0644" - filename = "${each.value}/backend.config" - content = try(local.backend, null) -} diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/templates/backend.config.tpl b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/templates/backend.config.tpl deleted file mode 100644 index 4322bb84..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/templates/backend.config.tpl +++ /dev/null @@ -1 +0,0 @@ -bucket = "${bucket}" diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/versions.tf b/platforms/gke/base/use-cases/federated-learning/terraform/initialize/versions.tf deleted file mode 100644 index 39074817..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/initialize/versions.tf +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2024 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. - -terraform { - required_version = ">= 1.5.7" - - required_providers { - google = { - source = "hashicorp/google" - version = "6.12.0" - } - local = { - source = "hashicorp/local" - version = "2.5.2" - } - } - - provider_meta "google" { - module_name = "cloud-solutions/acp_fl_initialize_deploy-v1" - } -} diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/key_management_service/backend.tf b/platforms/gke/base/use-cases/federated-learning/terraform/key_management_service/backend.tf deleted file mode 100644 index 1c11445e..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/key_management_service/backend.tf +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2024 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. - -terraform { - backend "gcs" { - bucket = "" - prefix = "terraform/federated-learning/key-management-service" - } -} diff --git a/platforms/gke/base/use-cases/federated-learning/terraform/private_google_access/backend.tf b/platforms/gke/base/use-cases/federated-learning/terraform/private_google_access/backend.tf deleted file mode 100644 index 5cb33201..00000000 --- a/platforms/gke/base/use-cases/federated-learning/terraform/private_google_access/backend.tf +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2024 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. - -terraform { - backend "gcs" { - bucket = "" - prefix = "terraform/federated-learning/private-google-access" - } -}