Skip to content

Commit 9041027

Browse files
authored
feat: Allow overriding download behavior using GCLOUD_TF_DOWNLOAD environment variable. (#59)
1 parent e0595b0 commit 9041027

File tree

16 files changed

+249
-16
lines changed

16 files changed

+249
-16
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ Functional examples are included in the
2828

2929
The [jq](https://stedolan.github.io/jq/) binary is also included in this module so you can use it as well for either of your `create_cmd_entrypoint` or `destroy_cmd_entrypoint` values.
3030

31+
## Downloading
32+
This module will automatically download gcloud on your behalf,
33+
allowing you to run it in environments without a gcloud binary available.
34+
35+
You can override this behavior by setting the `skip_download` variable to `true` or `false`.
36+
37+
You can also override the behavior by setting the `GCLOUD_TF_DOWNLOAD` environment variable.
38+
This environment variable will override all other settings.
39+
Setting it to `never` will *never* gcloud download and setting it to `always` will always download gcloud.
40+
3141
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
3242
## Inputs
3343

@@ -58,6 +68,7 @@ The [jq](https://stedolan.github.io/jq/) binary is also included in this module
5868
| bin\_dir | The full bin path of the modules executables |
5969
| create\_cmd\_bin | The full bin path & command used on create |
6070
| destroy\_cmd\_bin | The full bin path & command used on destroy |
71+
| downloaded | Whether gcloud was downloaded or not |
6172
| wait | An output to use when you want to depend on cmd finishing |
6273

6374
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

build/int.cloudbuild.yaml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,36 @@ steps:
2323
- 'TF_VAR_billing_account=$_BILLING_ACCOUNT'
2424
- id: create
2525
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
26-
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create']
26+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create default']
2727
- id: converge
2828
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
29-
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge']
29+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge default']
3030
- id: verify
3131
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
32-
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify']
32+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify default']
3333
- id: destroy
3434
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
35-
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy']
35+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy default']
36+
- id: create-skipped
37+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
38+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create skipped-env']
39+
env:
40+
- 'GCLOUD_TF_DOWNLOAD=never'
41+
- id: converge-skipped
42+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
43+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge skipped-env']
44+
env:
45+
- 'GCLOUD_TF_DOWNLOAD=never'
46+
- id: verify-skipped
47+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
48+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify skipped-env']
49+
env:
50+
- 'GCLOUD_TF_DOWNLOAD=never'
51+
- id: destroy-skipped
52+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
53+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy skipped-env']
54+
env:
55+
- 'GCLOUD_TF_DOWNLOAD=never'
3656
tags:
3757
- 'ci'
3858
- 'integration'

examples/simple_example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This example illustrates how to use the `gcloud` module.
99
|------|-------------|:----:|:-----:|:-----:|
1010
| project\_id | The ID of the project in which to provision resources. | string | n/a | yes |
1111

12+
## Outputs
13+
14+
| Name | Description |
15+
|------|-------------|
16+
| downloaded | Whether gcloud was downloaded or not |
17+
1218
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1319

1420
To provision this example, run the following from within this directory:

examples/simple_example/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
output "downloaded" {
18+
description = "Whether gcloud was downloaded or not"
19+
value = module.cli.downloaded
20+
}

kitchen.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ verifier:
2424

2525
platforms:
2626
- name: default
27+
- name: skipped_env
2728

2829
suites:
2930
- name: simple_example
@@ -37,6 +38,21 @@ suites:
3738
backend: local
3839
controls:
3940
- gcloud
41+
excludes:
42+
- skipped_env
43+
- name: override_example
44+
driver:
45+
command_timeout: 1800
46+
root_module_directory: test/fixtures/override_example/
47+
verifier:
48+
color: false
49+
systems:
50+
- name: override_example local
51+
backend: local
52+
controls:
53+
- gcloud
54+
excludes:
55+
- default
4056
- name: script_example
4157
driver:
4258
command_timeout: 1800
@@ -48,6 +64,8 @@ suites:
4864
backend: local
4965
controls:
5066
- gcloud
67+
excludes:
68+
- skipped_env
5169
- name: dependency_example
5270
driver:
5371
command_timeout: 1800
@@ -59,6 +77,8 @@ suites:
5977
backend: local
6078
controls:
6179
- file
80+
excludes:
81+
- skipped_env
6282
- name: kubectl_wrapper_example
6383
driver:
6484
command_timeout: 3600
@@ -70,3 +90,5 @@ suites:
7090
backend: local
7191
controls:
7292
- kubectl
93+
excludes:
94+
- skipped_env

main.tf

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616

1717
locals {
1818
tmp_credentials_path = "${path.module}/terraform-google-credentials.json"
19-
cache_path = "${path.module}/cache/${random_id.cache.hex}"
19+
cache_path = local.skip_download ? "" : "${path.module}/cache/${random_id.cache[0].hex}"
2020
gcloud_tar_path = "${local.cache_path}/google-cloud-sdk.tar.gz"
2121
gcloud_bin_path = "${local.cache_path}/google-cloud-sdk/bin"
2222
gcloud_bin_abs_path = abspath(local.gcloud_bin_path)
2323
components = join(",", var.additional_components)
2424

25-
gcloud = var.skip_download ? "gcloud" : "${local.gcloud_bin_path}/gcloud"
25+
download_override = data.external.env_override.result.download
26+
skip_download = local.download_override == "always" ? false : (local.download_override == "never" ? true : var.skip_download)
27+
28+
gcloud = local.skip_download ? "gcloud" : "${local.gcloud_bin_path}/gcloud"
2629
gcloud_download_url = var.gcloud_download_url != "" ? var.gcloud_download_url : "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${var.gcloud_sdk_version}-${var.platform}-x86_64.tar.gz"
2730
jq_platform = var.platform == "darwin" ? "osx-amd" : var.platform
2831
jq_download_url = var.jq_download_url != "" ? var.jq_download_url : "https://github.com/stedolan/jq/releases/download/jq-${var.jq_version}/jq-${local.jq_platform}64"
2932

30-
create_cmd_bin = var.skip_download ? var.create_cmd_entrypoint : "${local.gcloud_bin_path}/${var.create_cmd_entrypoint}"
31-
destroy_cmd_bin = var.skip_download ? var.destroy_cmd_entrypoint : "${local.gcloud_bin_path}/${var.destroy_cmd_entrypoint}"
33+
create_cmd_bin = local.skip_download ? var.create_cmd_entrypoint : "${local.gcloud_bin_path}/${var.create_cmd_entrypoint}"
34+
destroy_cmd_bin = local.skip_download ? var.destroy_cmd_entrypoint : "${local.gcloud_bin_path}/${var.destroy_cmd_entrypoint}"
3235

3336
wait = length(null_resource.additional_components.*.triggers) + length(
3437
null_resource.gcloud_auth_service_account_key_file.*.triggers,
@@ -41,7 +44,7 @@ locals {
4144
decompress_command = "tar -xzf ${local.gcloud_tar_path} -C ${local.cache_path} && cp ${local.cache_path}/jq ${local.cache_path}/google-cloud-sdk/bin/"
4245
decompress_wrapper = fileexists(local.gcloud_tar_path) ? local.decompress_command : "${local.prepare_cache_command} && ${local.download_gcloud_command} && ${local.download_jq_command} && ${local.decompress_command}"
4346
upgrade_command = "${local.gcloud} components update --quiet"
44-
additional_components_command = "${path.module}/check_components.sh ${local.gcloud} ${local.components}"
47+
additional_components_command = "${path.module}/scripts/check_components.sh ${local.gcloud} ${local.components}"
4548
gcloud_auth_service_account_key_file_command = "${local.gcloud} auth activate-service-account --key-file ${var.service_account_key_file}"
4649
gcloud_auth_google_credentials_command = <<-EOT
4750
printf "%s" "$GOOGLE_CREDENTIALS" > ${local.tmp_credentials_path} &&
@@ -51,6 +54,8 @@ locals {
5154
}
5255

5356
resource "random_id" "cache" {
57+
count = (! local.skip_download) ? 1 : 0
58+
5459
byte_length = 4
5560
}
5661

@@ -62,8 +67,14 @@ resource "null_resource" "module_depends_on" {
6267
}
6368
}
6469

70+
data "external" "env_override" {
71+
program = ["${path.module}/scripts/check_env.sh"]
72+
73+
query = {}
74+
}
75+
6576
resource "null_resource" "prepare_cache" {
66-
count = (var.enabled && ! var.skip_download) ? 1 : 0
77+
count = (var.enabled && ! local.skip_download) ? 1 : 0
6778

6879
triggers = merge({
6980
md5 = md5(var.create_cmd_entrypoint)
@@ -80,7 +91,7 @@ resource "null_resource" "prepare_cache" {
8091
}
8192

8293
resource "null_resource" "download_gcloud" {
83-
count = (var.enabled && ! var.skip_download) ? 1 : 0
94+
count = (var.enabled && ! local.skip_download) ? 1 : 0
8495

8596
triggers = merge({
8697
md5 = md5(var.create_cmd_entrypoint)
@@ -97,7 +108,7 @@ resource "null_resource" "download_gcloud" {
97108
}
98109

99110
resource "null_resource" "download_jq" {
100-
count = (var.enabled && ! var.skip_download) ? 1 : 0
111+
count = (var.enabled && ! local.skip_download) ? 1 : 0
101112

102113
triggers = merge({
103114
md5 = md5(var.create_cmd_entrypoint)
@@ -114,7 +125,7 @@ resource "null_resource" "download_jq" {
114125
}
115126

116127
resource "null_resource" "decompress" {
117-
count = (var.enabled && ! var.skip_download) ? 1 : 0
128+
count = (var.enabled && ! local.skip_download) ? 1 : 0
118129

119130
triggers = merge({
120131
md5 = md5(var.create_cmd_entrypoint)
@@ -133,7 +144,7 @@ resource "null_resource" "decompress" {
133144
}
134145

135146
resource "null_resource" "upgrade" {
136-
count = (var.enabled && var.upgrade && ! var.skip_download) ? 1 : 0
147+
count = (var.enabled && var.upgrade && ! local.skip_download) ? 1 : 0
137148

138149
depends_on = [null_resource.decompress]
139150

@@ -279,7 +290,7 @@ resource "null_resource" "additional_components_destroy" {
279290
}
280291

281292
resource "null_resource" "upgrade_destroy" {
282-
count = (var.enabled && var.upgrade && ! var.skip_download) ? 1 : 0
293+
count = (var.enabled && var.upgrade && ! local.skip_download) ? 1 : 0
283294

284295
depends_on = [
285296
null_resource.additional_components_destroy,
@@ -298,7 +309,7 @@ resource "null_resource" "upgrade_destroy" {
298309
}
299310

300311
resource "null_resource" "decompress_destroy" {
301-
count = (var.enabled && ! var.skip_download) ? 1 : 0
312+
count = (var.enabled && ! local.skip_download) ? 1 : 0
302313
depends_on = [null_resource.upgrade_destroy]
303314

304315
triggers = {

outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ output "wait" {
3333
description = "An output to use when you want to depend on cmd finishing"
3434
value = local.wait
3535
}
36+
37+
output "downloaded" {
38+
description = "Whether gcloud was downloaded or not"
39+
value = ! local.skip_download
40+
depends_on = [local.wait]
41+
}

scripts/check_env.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -e
17+
18+
SKIP=${GCLOUD_TF_DOWNLOAD}
19+
20+
echo "{\"download\": \"${SKIP}\"}"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "example" {
18+
source = "../../../examples/simple_example"
19+
20+
project_id = var.project_id
21+
}

0 commit comments

Comments
 (0)