This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompute.tf
148 lines (126 loc) · 4.47 KB
/
compute.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
# Copyright 2020 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
#
# https://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.
####
## Data Sources
####
data "google_compute_image" "image" {
family = "cos-stable"
project = "cos-cloud"
}
data "template_file" "cloud-config" {
vars = {
fah_worker_image = var.fah_worker_image
fah_user_name = var.fah_user_name
fah_passkey = var.fah_passkey
fah_team_id = var.fah_team_id
}
template = var.fah_worker_gpu != "" ? file("${path.module}/assets/cloud-config.yaml") : file("${path.module}/assets/cloud-config-no-gpu.yaml")
}
####
## Locals
####
locals {
boot_disk = [
{
source_image = data.google_compute_image.image.self_link
disk_size_gb = "50"
disk_type = "pd-standard"
auto_delete = true
boot = true
},
]
additional_disks = []
all_disks = concat(local.boot_disk, local.additional_disks)
}
####
## Instance Template
####
resource "google_compute_instance_template" "mig_template" {
name_prefix = "${var.network}-"
machine_type = var.fah_worker_type
can_ip_forward = false
metadata_startup_script = ""
labels = {
"container-vm" = data.google_compute_image.image.name
}
metadata = merge(
var.additional_metadata,
map("cos-gpu-installer-env", file("${path.module}/cos-gpu-installer/scripts/gpu-installer-env")),
map("user-data", data.template_file.cloud-config.rendered),
map("run-installer-script", file("${path.module}/cos-gpu-installer/scripts/run_installer.sh")),
map("run-cuda-test-script", file("${path.module}/cos-gpu-installer/scripts/run_cuda_test.sh")),
)
tags = ["fah-worker"]
dynamic "disk" {
for_each = local.all_disks
content {
auto_delete = lookup(disk.value, "auto_delete", null)
boot = lookup(disk.value, "boot", null)
device_name = lookup(disk.value, "device_name", null)
disk_name = lookup(disk.value, "disk_name", null)
disk_size_gb = lookup(disk.value, "disk_size_gb", null)
disk_type = lookup(disk.value, "disk_type", null)
interface = lookup(disk.value, "interface", null)
mode = lookup(disk.value, "mode", null)
source = lookup(disk.value, "source", null)
source_image = lookup(disk.value, "source_image", null)
type = lookup(disk.value, "type", null)
dynamic "disk_encryption_key" {
for_each = lookup(disk.value, "disk_encryption_key", [])
content {
kms_key_self_link = lookup(disk_encryption_key.value, "kms_key_self_link", null)
}
}
}
}
dynamic "service_account" {
for_each = [var.service_account]
content {
email = lookup(service_account.value, "email", null)
scopes = lookup(service_account.value, "scopes", null)
}
}
network_interface {
network = var.create_network ? google_compute_network.default[0].self_link : var.network
subnetwork = var.create_network ? google_compute_subnetwork.default[0].self_link : var.subnetwork
subnetwork_project = var.project
}
lifecycle {
create_before_destroy = true
}
scheduling {
preemptible = true
on_host_maintenance = "TERMINATE"
automatic_restart = false
}
guest_accelerator {
type = var.fah_worker_gpu
count = trimspace(var.fah_worker_gpu) != "" ? 1 : 0
}
}
####
## Managed Instance Group
####
module "mig" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 2.0.0"
instance_template = google_compute_instance_template.mig_template.self_link
subnetwork_project = var.project
region = var.region
distribution_policy_zones = var.zones
hostname = var.network
target_size = var.fah_worker_count
network = var.create_network ? google_compute_network.default[0].self_link : var.network
subnetwork = var.create_network ? google_compute_subnetwork.default[0].self_link : var.subnetwork
}