Skip to content

Commit

Permalink
feat: Decoupled the upsert command and destroy command (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
tishen25 authored Aug 12, 2020
1 parent 7113d90 commit 497886c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Make will use bash instead of sh
SHELL := /usr/bin/env bash -O extglob

DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.12.0
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
REGISTRY_URL := gcr.io/cloud-foundation-cicd

Expand Down
2 changes: 1 addition & 1 deletion build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ tags:
- 'integration'
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.12.0'
2 changes: 1 addition & 1 deletion build/lint.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ tags:
- 'lint'
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.12.0'
41 changes: 29 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ locals {
wait = length(null_resource.additional_components.*.triggers) + length(
null_resource.gcloud_auth_service_account_key_file.*.triggers,
) + length(null_resource.gcloud_auth_google_credentials.*.triggers,
) + length(null_resource.run_command.*.triggers)
) + length(null_resource.run_command.*.triggers) + length(null_resource.run_destroy_command.*.triggers)

prepare_cache_command = "mkdir -p ${local.cache_path}"
download_gcloud_command = "curl -sL -o ${local.cache_path}/google-cloud-sdk.tar.gz ${local.gcloud_download_url}"
Expand Down Expand Up @@ -220,13 +220,11 @@ resource "null_resource" "run_command" {
]

triggers = merge({
md5 = md5(var.create_cmd_entrypoint)
arguments = md5(var.create_cmd_body)
create_cmd_entrypoint = var.create_cmd_entrypoint
create_cmd_body = var.create_cmd_body
destroy_cmd_entrypoint = var.destroy_cmd_entrypoint
destroy_cmd_body = var.destroy_cmd_body
gcloud_bin_abs_path = local.gcloud_bin_abs_path
md5 = md5(var.create_cmd_entrypoint)
arguments = md5(var.create_cmd_body)
create_cmd_entrypoint = var.create_cmd_entrypoint
create_cmd_body = var.create_cmd_body
gcloud_bin_abs_path = local.gcloud_bin_abs_path
}, var.create_cmd_triggers)

provisioner "local-exec" {
Expand All @@ -237,6 +235,25 @@ resource "null_resource" "run_command" {
EOT
}

}

resource "null_resource" "run_destroy_command" {
count = var.enabled ? 1 : 0

depends_on = [
null_resource.module_depends_on,
null_resource.decompress,
null_resource.additional_components,
null_resource.gcloud_auth_google_credentials,
null_resource.gcloud_auth_service_account_key_file
]

triggers = merge({
destroy_cmd_entrypoint = var.destroy_cmd_entrypoint
destroy_cmd_body = var.destroy_cmd_body
gcloud_bin_abs_path = local.gcloud_bin_abs_path
}, var.create_cmd_triggers)

provisioner "local-exec" {
when = destroy
command = <<-EOT
Expand All @@ -247,10 +264,10 @@ resource "null_resource" "run_command" {
}

// Destroy provision steps in opposite depdenency order
// so they run before `run_command` destroy
// so they run before `run_destroy_command` destroy
resource "null_resource" "gcloud_auth_google_credentials_destroy" {
count = var.enabled && var.use_tf_google_credentials_env_var ? 1 : 0
depends_on = [null_resource.run_command]
depends_on = [null_resource.run_destroy_command]

triggers = {
gcloud_auth_google_credentials_command = local.gcloud_auth_google_credentials_command
Expand All @@ -263,7 +280,7 @@ resource "null_resource" "gcloud_auth_google_credentials_destroy" {

resource "null_resource" "gcloud_auth_service_account_key_file_destroy" {
count = var.enabled && length(var.service_account_key_file) > 0 ? 1 : 0
depends_on = [null_resource.run_command]
depends_on = [null_resource.run_destroy_command]

triggers = {
gcloud_auth_service_account_key_file_command = local.gcloud_auth_service_account_key_file_command
Expand All @@ -277,7 +294,7 @@ resource "null_resource" "gcloud_auth_service_account_key_file_destroy" {

resource "null_resource" "additional_components_destroy" {
count = var.enabled && length(var.additional_components) > 0 ? 1 : 0
depends_on = [null_resource.run_command]
depends_on = [null_resource.run_destroy_command]

triggers = {
additional_components_command = local.additional_components_command
Expand Down

0 comments on commit 497886c

Please sign in to comment.