Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create and destroy scripts for the module to run #9

Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to

## [Unreleased]

### Added

- `create_script` and `destroy_script` variables. The variables take in a path to a script that will be run by the module with the correct bin directory prepended to `PATH`. [#9]

## [0.2.0] - 2019-12-18

### Changed
Expand All @@ -24,3 +28,5 @@ and this project adheres to
[Unreleased]: https://github.com/terraform-google-modules/terraform-google-gcloud/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/terraform-google-modules/terraform-google-gcloud/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/terraform-google-modules/terraform-google-gcloud/releases/tag/v0.1.0

[#9]: https://github.com/terraform-google-modules/terraform-google-gcloud/pull/9
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ The [jq](https://stedolan.github.io/jq/) binary is also included in this module
| additional\_components | Additional gcloud CLI components to install. Defaults to none. Valid value are components listed in `gcloud components list` | list | `<list>` | no |
| create\_cmd\_body | On create, the command body you'd like to run with your entrypoint. | string | `"info"` | no |
| create\_cmd\_entrypoint | On create, the command entrypoint you'd like to use. | string | `"gcloud"` | no |
| create\_script | On create, the file location of the script to run. Module's bin directory will be prepended to path. | string | `"null"` | no |
| create\_script\_arguments | String of arguments passed to the `create_script` at exec time | string | `""` | no |
| create\_script\_triggers | List of any additional triggers for the create script execution. | map | `<map>` | no |
| destroy\_cmd\_body | On destroy, the command body you'd like to run with your entrypoint. | string | `"info"` | no |
| destroy\_cmd\_entrypoint | On destroy, the command entrypoint you'd like to use. | string | `"gcloud"` | no |
| destroy\_script | On destroy, the file location of the script to run. Module's bin directory will be prepended to path. | string | `"null"` | no |
| destroy\_script\_arguments | String of arguments passed to the `destroy_script` at exec time | string | `""` | no |
| enabled | Flag to optionally disable usage of this module. | bool | `"true"` | no |
| platform | Platform CLI will run on. Defaults to linux. Valid values: linux, darwin | string | `"linux"` | no |
| service\_account\_key\_file | Path to service account key file to run `gcloud auth activate-service-account` with. Optional. | string | `""` | no |
Expand Down
3 changes: 3 additions & 0 deletions examples/simple_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ module "cli" {

create_cmd_body = "services enable youtube.googleapis.com --project ${var.project_id}"
destroy_cmd_body = "services disable youtube.googleapis.com --project ${var.project_id}"

create_script = "${path.module}/scripts/script.sh"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make a script a separate example from a simple command (though I'm actually curious as to whether scripts could simply be passed as a command?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a script_example and test. Similar to the simple_example but using a script to enable and disable an api.

I could probably remove the new script vars in favor of utilizing the existing create|destroy_cmd_entrypoint variables.

Right now they default to gcloud - but passing in the script instead should work (assuming I also prepend the bin directory to the path). Arguments to the script could use the existing create|destroy_cmd_body vars.

Something like this uses all existing vars and will achieve the same effect without breaking original functionality.

module "cli" {
  source = "../.."

  platform              = "linux"
  additional_components = ["kubectl", "beta"]

  create_cmd_entrypoint = "${path.module}/scripts/script.sh"
  create_cmd_body       = "enable ${var.project_id}"

  destroy_cmd_entrypoint = "${path.module}/scripts/script.sh"
  destroy_cmd_body       = "disable ${var.project_id}"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and committed that. It does make the change quite a bit simpler. I'll update the project-factory PR for this approach too

destroy_script = "${path.module}/scripts/script.sh"
}
19 changes: 19 additions & 0 deletions examples/simple_example/scripts/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# Copyright 2018 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.

#!/bin/sh

command -v gcloud
gcloud --version
48 changes: 48 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ locals {
cache_path = "${path.module}/cache/${var.platform}"
gcloud_tar_path = "${local.cache_path}/google-cloud-sdk.tar.gz"
gcloud_bin_path = "${local.cache_path}/google-cloud-sdk/bin"
gcloud_bin_abs_path = abspath(local.gcloud_bin_path)
components = join(" ", var.additional_components)

gcloud = "${local.gcloud_bin_path}/gcloud"
Expand Down Expand Up @@ -128,3 +129,50 @@ resource "null_resource" "run_command" {
command = "${local.destroy_cmd_bin} ${var.destroy_cmd_body}"
}
}

resource "null_resource" "run_script_create" {
count = var.enabled && var.create_script != null ? 1 : 0

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

triggers = merge({
md5 = filemd5(var.create_script)
arguments = md5(var.create_script_arguments)
}, var.create_script_triggers)

provisioner "local-exec" {
when = create

command = <<-EOT
PATH=${local.gcloud_bin_abs_path}:$PATH
${var.create_script} ${var.create_script_arguments}
EOT
}
}

resource "null_resource" "run_script_destroy" {
count = var.enabled && var.destroy_script != null ? 1 : 0

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

provisioner "local-exec" {
when = destroy

command = <<-EOT
PATH=${local.gcloud_bin_abs_path}:$PATH
${var.destroy_script} ${var.destroy_script_arguments}
EOT
}
}
26 changes: 26 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ variable "use_tf_google_credentials_env_var" {
description = "Use GOOGLE_CREDENTIALS environment variable to run `gcloud auth activate-service-account` with. Optional."
default = false
}

variable "create_script" {
description = "On create, the file location of the script to run. Module's bin directory will be prepended to path."
default = null
}

variable "create_script_arguments" {
description = "String of arguments passed to the `create_script` at exec time"
default = ""
}

variable "create_script_triggers" {
description = "List of any additional triggers for the create script execution."
type = map
default = {}
}

variable "destroy_script" {
description = "On destroy, the file location of the script to run. Module's bin directory will be prepended to path."
default = null
}

variable "destroy_script_arguments" {
description = "String of arguments passed to the `destroy_script` at exec time"
default = ""
}