-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
97 lines (83 loc) · 2.94 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
provider "google" {
region = "${var.gce_region}"
}
resource "google_compute_instance" "cockroach" {
count = "${var.num_instances}"
name = "cockroach-${count.index}"
machine_type = "${var.gce_machine_type}"
zone = "${var.gce_zone}"
tags = ["cockroach"]
disk {
image = "${var.gce_image}"
}
network_interface {
network = "default"
access_config {
# Ephemeral
}
}
metadata {
sshKeys = "ubuntu:${file("~/.ssh/${var.key_name}.pub")}"
}
connection {
user = "ubuntu"
key_file = "~/.ssh/${var.key_name}"
}
service_account {
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
}
}
resource "template_file" "supervisor" {
count = "${var.num_instances}"
template = "${file("supervisor.conf.tpl")}"
vars {
stores = "${var.stores}"
cockroach_port = "${var.sql_port}"
# The value of the --join flag must be empty for the first node,
# and a running node for all others. We built a list of addresses
# shifted by one (first element is empty), then take the value at index "instance.index".
join_address = "${element(concat(split(",", ""), google_compute_instance.cockroach.*.network_interface.0.access_config.0.assigned_nat_ip), count.index)}"
# We need to provide one node address for the block writer.
node_address = "${google_compute_instance.cockroach.0.network_interface.0.access_config.0.assigned_nat_ip}"
}
}
resource "null_resource" "cockroach-runner" {
count = "${var.num_instances}"
connection {
user = "ubuntu"
key_file = "~/.ssh/${var.key_name}"
host = "${element(google_compute_instance.cockroach.*.network_interface.0.access_config.0.assigned_nat_ip, count.index)}"
}
provisioner "file" {
source = "download_binary.sh"
destination = "/home/ubuntu/download_binary.sh"
}
# This writes the filled-in supervisor template. It would be nice if we could
# use rendered templates in the file provisioner.
provisioner "remote-exec" {
inline = <<FILE
echo '${element(template_file.supervisor.*.rendered, count.index)}' > supervisor.conf
FILE
}
provisioner "file" {
# If no binary is specified, we'll copy /dev/null (always 0 bytes) to the
# instance. The "remote-exec" block will then overwrite that. There's no
# such thing as conditional file copying in Terraform, so we fake it.
source = "${coalesce(var.cockroach_binary, "/dev/null")}"
destination = "/home/ubuntu/cockroach"
}
# Launch CockroachDB.
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update",
"sudo apt-get -y install supervisor",
"sudo apt-get -y install nethogs",
"sudo service supervisor stop",
"mkdir -p logs",
"chmod 755 cockroach",
"[ $(stat --format=%s cockroach) -ne 0 ] || bash download_binary.sh cockroach/cockroach ${var.cockroach_sha}",
"if [ ! -e supervisor.pid ]; then supervisord -c supervisor.conf; fi",
"supervisorctl -c supervisor.conf start cockroach",
]
}
}