forked from yugabyte/terraform-gcp-yugabyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
130 lines (118 loc) · 4.49 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
data "google_compute_image" "YugaByte_DB_Image" {
family = "centos-7"
project = "centos-cloud"
}
data "google_compute_zones" "available" {
region = "${var.region_name}"
}
resource "google_compute_firewall" "YugaByte-Firewall" {
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-firewall"
network = var.vpc_network
allow {
protocol = "tcp"
ports = ["9000","7000","6379","9042","5433","22"]
}
target_tags = ["${var.prefix}${var.cluster_name}"]
}
resource "google_compute_firewall" "YugaByte-Intra-Firewall" {
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-intra-firewall"
network = var.vpc_network
allow {
protocol = "tcp"
ports = ["7100", "9100"]
}
target_tags = ["${var.prefix}${var.cluster_name}"]
}
resource "google_compute_instance" "yugabyte_node" {
count = var.node_count
name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}"
machine_type = var.node_type
zone = element(data.google_compute_zones.available.names, count.index)
tags=["${var.prefix}${var.cluster_name}"]
boot_disk{
initialize_params {
image = data.google_compute_image.YugaByte_DB_Image.self_link
size = var.disk_size
}
}
metadata = {
sshKeys = "${var.ssh_user}:${file(var.ssh_public_key)}"
}
network_interface{
network = var.vpc_network
access_config {
// external ip to instance
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/install_software.sh"
destination = "/home/${var.ssh_user}/install_software.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/create_universe.sh"
destination ="/home/${var.ssh_user}/create_universe.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_master.sh"
destination ="/home/${var.ssh_user}/start_master.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_tserver.sh"
destination ="/home/${var.ssh_user}/start_tserver.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/${var.ssh_user}/install_software.sh",
"chmod +x /home/${var.ssh_user}/create_universe.sh",
"chmod +x /home/${var.ssh_user}/start_tserver.sh",
"chmod +x /home/${var.ssh_user}/start_master.sh",
"/home/${var.ssh_user}/install_software.sh '${var.yb_version}'"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
}
locals {
depends_on = ["google_compute_instance.yugabyte_node"]
ssh_ip_list = "${var.use_public_ip_for_ssh == "true" ? join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.access_config.0.nat_ip) : join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.network_ip)}"
config_ip_list = "${join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.network_ip)}"
zone = "${join(" ", google_compute_instance.yugabyte_node.*.zone)}"
}
resource "null_resource" "create_yugabyte_universe" {
# Define the trigger condition to run the provisioner block
triggers = {
cluster_instance_ids = "${join(",", google_compute_instance.yugabyte_node.*.id)}"
}
depends_on = [google_compute_instance.yugabyte_node]
provisioner "local-exec" {
command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${var.ssh_private_key}"
}
}