-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
229 lines (190 loc) · 5.3 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
218
219
220
221
222
223
224
225
226
227
228
229
# See https://cloud.google.com/compute/docs/load-balancing/network/example
provider "google" {
region = var.region
project = var.project_name
credentials = file("${var.credentials_file_path}")
}
# Set up db groups
resource "google_compute_instance_group" "db" {
name = "terraform-db"
description = "Terraform DB instance group"
zone = var.region_zone
instances = ["${google_compute_instance.db.0.self_link}"]
#instances = "${join(" ", ${google_compute_instance.db.*.self_link})}"
#instances = join(" ", "${google_compute_instance.db.*.self_link}")
}
#resource "google_compute_address" "static" {
#name = "ipv4-address"
#}
## Disk image depends on region and zone
resource "google_compute_disk" "data1" {
count = 1
name = "data-disk1"
type = "pd-ssd"
zone = var.region_zone
#auto_delete = true deprecated ?
#scratch = true
size = 100
}
resource "google_compute_disk" "data2" {
count = 1
name = "data-disk2"
type = "pd-ssd"
zone = var.region_zone
#auto_delete = true deprecated ?
#scratch = true
size = 100
}
# Disk image depends on region and zone
resource "google_compute_instance" "db" {
count = 1
name = "grb-db-${count.index}"
machine_type = "n1-highmem-8"
# machine_type = "custom-6-15360"
zone = var.region_zone
scheduling {
preemptible = false
automatic_restart = true
on_host_maintenance = "MIGRATE"
}
# "db-node", "www-node","http-server", "https-server"
tags = ["db-node", "www-node"]
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-1804-bionic-v20211214"
size = 100
}
}
connection {
# host = google_compute_instance.db.0.network_interface.0.network_ip
host = google_compute_instance.db[count.index].network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "root"
private_key = file("${var.private_key_path}")
agent = false
}
attached_disk {
source = google_compute_disk.data1[count.index].self_link
}
attached_disk {
source = google_compute_disk.data2[count.index].self_link
}
network_interface {
network = "default"
access_config {
# Ephemeral
}
}
metadata = {
ssh-keys = "root:${file("${var.public_key_path}")}"
}
provisioner "file" {
source = var.install_script_src_path
destination = var.install_script_dest_path
}
# Copies the deployment keys over
provisioner "file" {
source = "keys/"
destination = "/root/.ssh/"
}
# installs helper script to set kernel shared segment size
provisioner "file" {
source = "scripts/shmsetup.sh"
destination = "/usr/local/bin/shmsetup.sh"
}
# installs google deprecated script to mounts disk without changing too much code (they removed it)
provisioner "file" {
source = "scripts/safe_format_and_mount.sh"
destination = "/usr/local/bin/safe_format_and_mount.sh"
}
# prepare TF corner
provisioner "remote-exec" {
inline = [
"mkdir -p /tmp/terraform",
"mkdir -p /tmp/rcfiles"
]
}
provisioner "file" {
source = "terraform.tfvars"
destination = "/tmp/terraform/terraform.tfvars"
}
# Install all terraform runtime stuff
provisioner "file" {
source = var.credentials_file_path
destination = "/tmp/terraform/terraform-${var.project_name}.json"
}
# all the shell files in /tmp
provisioner "file" {
source = "helpers/"
destination = "/tmp/"
}
# all the resource files in /tmp/rcfiles
provisioner "file" {
source = "rcfiles/"
destination = "/tmp/rcfiles/"
}
# Create an empty file under etc to indicate the project this server belongs to
# Thought about doing this differently in a few ways since this is the only missing
# Piece of information in order to configure the python script that can call it all.
provisioner "remote-exec" {
inline = [
"mkdir -p /tmp/skeys",
"sudo echo ${var.project_name} > /etc/myproject",
"sudo chmod +r /etc/myproject"
]
}
provisioner "remote-exec" {
inline = [
"mkdir -p /tmp/configs"
]
}
provisioner "file" {
source = "configs/"
destination = "/tmp/configs/"
}
# Copy cron snippets to nodes
provisioner "remote-exec" {
inline = [
"mkdir -p /tmp/crons"
]
}
provisioner "file" {
source = "crons/"
destination = "/tmp/crons/"
}
provisioner "remote-exec" {
inline = [
"chmod +x /usr/local/bin/safe_format_and_mount.sh",
"chmod +x /tmp/mountformat.sh",
"sudo /tmp/mountformat.sh",
"chmod +x ${var.install_script_dest_path}",
"chmod +x /usr/local/bin/shmsetup.sh",
"sudo ${var.install_script_dest_path} ${count.index}"
]
}
service_account {
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
}
}
resource "google_compute_firewall" "default" {
name = "grb-www-firewall"
network = "default"
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["80", "443"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["www-node"]
}
# this is the S3 remote TF state location, it's meant to be used when you
# work in teams
#terraform {
# backend "s3" {
# bucket = "my-tf-states"
# key = "api-project-37604919139/terraform.state"
# region = "eu-central-1"
# }
#}