-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
124 lines (98 loc) · 3.21 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
data "template_file" "install" {
template = "${file("${path.module}/templates/install.tpl")}"
vars {
cassandra_version = "${var.cassandra_version}"
}
}
data "template_file" "configure" {
template = "${var.bootstrap == "true" ? file("${path.module}/templates/configure-add.tpl") : file("${path.module}/templates/configure-new.tpl") }"
vars {
cluster_name = "${var.cassandra_cluster_name}"
seeds = "${var.bootstrap == "true" ? local.seeds_add : local.seeds_new }"
}
}
data "template_file" "attach_disk" {
template = "${file("${path.module}/templates/attach_disk.tpl")}"
}
data "google_compute_zones" "available" {}
locals {
count = "${length(data.google_compute_zones.available.names)}"
seeds_new = "${join("," , concat( slice(google_compute_instance.default.*.network_interface.0.address,0,2),var.add_seeds))}"
seeds_add = "${join("," , var.add_seeds)}"
}
resource "google_compute_disk" "default" {
count = "${local.count}"
name = "${var.project_name}-data-${count.index}"
type = "pd-ssd"
zone = "${data.google_compute_zones.available.names[count.index]}"
size = "${var.data_disk_size}"
}
resource "google_compute_instance" "default" {
count = "${local.count}"
name = "${var.project_name}-${count.index}"
machine_type = "${var.machine_type}"
zone = "${data.google_compute_zones.available.names[count.index]}"
allow_stopping_for_update = true
tags = "${var.tags}"
boot_disk {
initialize_params {
image = "ubuntu-1604-xenial-v20180509"
size = "${var.boot_disk_size}"
type = "pd-ssd"
}
}
attached_disk {
source = "${element(google_compute_disk.default.*.name, count.index)}"
}
network_interface {
subnetwork = "${var.subnetwork}"
}
}
resource "null_resource" "attach_disk" {
count = "${local.count}"
depends_on = ["google_compute_disk.default"]
triggers {
script = "${data.template_file.attach_disk.rendered}"
}
connection {
type = "ssh"
user = "${var.ssh_user}"
private_key = "${file("${var.ssh_key_path}")}"
host = "${element(google_compute_instance.default.*.network_interface.0.address, count.index)}"
}
provisioner "remote-exec" {
inline = ["${data.template_file.attach_disk.rendered}"]
}
}
resource "null_resource" "install" {
count = "${local.count}"
depends_on = ["null_resource.attach_disk"]
triggers {
script = "${data.template_file.install.rendered}"
}
connection {
type = "ssh"
user = "${var.ssh_user}"
private_key = "${file("${var.ssh_key_path}")}"
host = "${element(google_compute_instance.default.*.network_interface.0.address, count.index)}"
}
provisioner "remote-exec" {
inline = ["${data.template_file.install.rendered}"]
}
}
resource "null_resource" "configure" {
count = "${local.count}"
depends_on = ["null_resource.install"]
triggers {
script = "${data.template_file.configure.rendered}"
}
connection {
type = "ssh"
user = "${var.ssh_user}"
private_key = "${file("${var.ssh_key_path}")}"
host = "${element(google_compute_instance.default.*.network_interface.0.address, count.index)}"
}
provisioner "remote-exec" {
inline = ["${data.template_file.configure.rendered}"]
}
}