forked from GoogleCloudPlatform/cluster-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
217 lines (194 loc) · 7.33 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* 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.
*/
locals {
# This label allows for billing report tracking based on module.
labels = merge(var.labels, { ghpc_module = "startup-script", ghpc_role = "scripts" })
}
locals {
monitoring_agent_installer = (
var.install_cloud_ops_agent || var.install_stackdriver_agent ?
[{
type = "shell"
source = "${path.module}/files/install_monitoring_agent.sh"
destination = "install_monitoring_agent_automatic.sh"
args = var.install_cloud_ops_agent ? "ops" : "legacy" # install legacy (stackdriver)
}] :
[]
)
warnings = [
{
type = "data"
content = file("${path.module}/files/running-script-warning.sh")
destination = "/etc/profile.d/99-running-script-warning.sh"
}
]
configure_ssh = length(var.configure_ssh_host_patterns) > 0
host_args = {
host_name_prefix = var.configure_ssh_host_patterns
}
prefix_file = "/tmp/prefix_file.json"
configure_ssh_runners = local.configure_ssh ? [
{
type = "data"
source = "${path.module}/files/setup-ssh-keys.sh"
destination = "/usr/local/ghpc/setup-ssh-keys.sh"
},
{
type = "data"
source = "${path.module}/files/setup-ssh-keys.yml"
destination = "/usr/local/ghpc/setup-ssh-keys.yml"
},
{
type = "data"
content = jsonencode(local.host_args)
destination = local.prefix_file
},
{
type = "ansible-local"
content = file("${path.module}/files/configure-ssh.yml")
destination = "configure-ssh.yml"
args = "-e @${local.prefix_file}"
}
] : []
proxy_runner = var.http_proxy == "" ? [] : [
{
type = "data"
destination = "/etc/profile.d/http_proxy.sh"
content = <<-EOT
#!/bin/bash
export http_proxy=${var.http_proxy}
export https_proxy=${var.http_proxy}
export NO_PROXY=${var.http_no_proxy}
EOT
},
{
type = "shell"
source = "${path.module}/files/configure_proxy.sh"
destination = "configure_proxy.sh"
args = var.http_proxy
}
]
docker_runner = !var.install_docker ? [] : [
{
type = "ansible-local"
destination = "install_docker.yml"
content = file("${path.module}/files/install_docker.yml")
args = "-e enable_docker_world_writable=${var.enable_docker_world_writable}"
},
]
supplied_ansible_runners = anytrue([for r in var.runners : r.type == "ansible-local"])
has_ansible_runners = anytrue([local.supplied_ansible_runners, local.configure_ssh, var.install_docker])
install_ansible = coalesce(var.install_ansible, local.has_ansible_runners)
ansible_installer = local.install_ansible ? [{
type = "shell"
source = "${path.module}/files/install_ansible.sh"
destination = "install_ansible_automatic.sh"
args = var.ansible_virtualenv_path
}] : []
runners = concat(
local.warnings,
local.proxy_runner,
local.monitoring_agent_installer,
local.ansible_installer,
local.configure_ssh_runners,
local.docker_runner,
var.runners
)
bucket_regex = "^gs://([^/]*)/*(.*)"
gcs_bucket_path_trimmed = var.gcs_bucket_path == null ? null : trimsuffix(var.gcs_bucket_path, "/")
storage_folder_path = local.gcs_bucket_path_trimmed == null ? null : regex(local.bucket_regex, local.gcs_bucket_path_trimmed)[1]
storage_folder_path_prefix = local.storage_folder_path == null || local.storage_folder_path == "" ? "" : "${local.storage_folder_path}/"
user_provided_bucket_name = try(regex(local.bucket_regex, local.gcs_bucket_path_trimmed)[0], null)
storage_bucket_name = coalesce(one(google_storage_bucket.configs_bucket[*].name), local.user_provided_bucket_name)
load_runners = templatefile(
"${path.module}/templates/startup-script-custom.tftpl",
{
bucket = local.storage_bucket_name,
http_proxy = var.http_proxy,
no_proxy = var.http_no_proxy,
runners = [
for runner in local.runners : {
object = google_storage_bucket_object.scripts[basename(runner["destination"])].output_name
type = runner["type"]
destination = runner["destination"]
args = contains(keys(runner), "args") ? runner["args"] : ""
}
]
}
)
stdlib_head = file("${path.module}/files/startup-script-stdlib-head.sh")
get_from_bucket = file("${path.module}/files/get_from_bucket.sh")
stdlib_body = file("${path.module}/files/startup-script-stdlib-body.sh")
# List representing complete content, to be concatenated together.
stdlib_list = [
local.stdlib_head,
local.get_from_bucket,
local.load_runners,
local.stdlib_body,
]
# Final content output to the user
stdlib = join("", local.stdlib_list)
runners_map = { for runner in local.runners :
basename(runner["destination"]) => {
content = lookup(runner, "content", null)
source = lookup(runner, "source", null)
}
}
}
resource "random_id" "resource_name_suffix" {
byte_length = 4
}
resource "google_storage_bucket" "configs_bucket" {
count = var.gcs_bucket_path == null ? 1 : 0
project = var.project_id
name = "${var.deployment_name}-startup-scripts-${random_id.resource_name_suffix.hex}"
uniform_bucket_level_access = true
location = var.region
storage_class = "REGIONAL"
labels = local.labels
}
resource "google_storage_bucket_iam_binding" "viewers" {
bucket = local.storage_bucket_name
role = "roles/storage.objectViewer"
members = var.bucket_viewers
}
resource "google_storage_bucket_object" "scripts" {
# this writes all scripts exactly once into GCS
for_each = local.runners_map
name = "${local.storage_folder_path_prefix}${each.key}-${substr(try(md5(each.value.content), filemd5(each.value.source)), 0, 4)}"
content = each.value.content
source = each.value.source
bucket = local.storage_bucket_name
timeouts {
create = "10m"
update = "10m"
}
lifecycle {
precondition {
condition = !(var.install_cloud_ops_agent && var.install_stackdriver_agent)
error_message = "Only one of var.install_stackdriver_agent or var.install_cloud_ops_agent can be set. Stackdriver is recommended for best performance."
}
precondition {
condition = !var.enable_docker_world_writable || var.install_docker
error_message = "If var.enable_docker_world_writable is set to true, var.install_docker must also be set to true."
}
}
}
resource "local_file" "debug_file" {
for_each = toset(var.debug_file != null ? [var.debug_file] : [])
filename = var.debug_file
content = local.stdlib
}