This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpromguard.tf
More file actions
194 lines (168 loc) · 5.61 KB
/
Copy pathpromguard.tf
File metadata and controls
194 lines (168 loc) · 5.61 KB
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
/*
* PromGuard Terraform
* @cpu - 2018
*
* An example of creating 1 monitoring node, and n target nodes. Target nodes
* are spread across geographically diverse regions. Template datasources are
* used to populate an Ansible inventory output. Later Ansible playbooks will
* create an encrypted site-to-site tunnel between the monitoring node and the
* target nodes.
*
*/
/*
* Placeholder variables - populate these in your `terraform.tfvars` file.
*/
variable "do_token" {}
variable "do_ssh_key_file" {}
variable "do_ssh_key_name" {
default = "promguard-ssh"
}
/*
* PromGuard Configuration Options
*/
# What size of Droplet should be used?
variable "do_size" {
default = "1gb"
}
# What droplet image should be used?
variable "do_image" {
default = "ubuntu-16-04-x64"
}
# What region should the monitoring droplet be in?
# Default: Toronto, CA
variable "do_monitor_region" {
default = "tor1"
}
# How many nodes should be created - make sure `do_node_regions` has an entry
# for each `node_count` if you change this!
variable "node_count" {
default = 3
}
# What regions should each of the remote nodes be created in?
# Default:
# Node 1 - London, UK
# Node 2 - San Franciso, US
# Node 3 - Singapore, SG
variable "do_node_regions" {
type = "map"
default = {
"promguard-node-1" = "lon1"
"promguard-node-2" = "sfo2"
"promguard-node-3" = "sgp1"
}
}
/*
* Terraform outputs
*/
# ansible_inventory is an output for the rendered ansible_inventory template
output "ansible_inventory" {
value = "${data.template_file.ansible_inventory.rendered}"
}
# monitor_ip is an output for the public IPv4 address of the monitor droplet
# used for SSH port forwarding
output "monitor_ip" {
value = "${digitalocean_droplet.monitor.ipv4_address}"
}
/*
* Template Data Sources
*/
# nodes_ansible is a template datasource that constructs a hostname template for
# each of the "node" droplets
data "template_file" "nodes_ansible" {
count = "${var.node_count}"
template = "${file("${path.module}/templates/hostname.tpl")}"
vars {
name = "promguard-node-${count.index + 1}"
ansible_host = "ansible_host=${digitalocean_droplet.node.*.ipv4_address[count.index]}"
# Each monitor host is given a sequential address in the RFC 1918 10.0.0.0
# network using the `cidrhost` function. The node offset is the index
# + 2 - this accounts for both zero offset indexing as well as for the
# monitor host
wireguard_ip = "wireguard_ip=${cidrhost("10.0.0.0/24", count.index + 2)}"
}
}
# monitor_ansible is a template datasource that constructs a hostname template
# only for the "monitor" droplet
data "template_file" "monitor_ansible" {
template = "${file("${path.module}/templates/hostname.tpl")}"
vars {
name = "promguard-monitor-1"
ansible_host = "ansible_host=${digitalocean_droplet.monitor.ipv4_address}"
# The monitor wireguard_ip is always the first host in the 10.0.0.0 subnet
# for this example code.
wireguard_ip = "wireguard_ip=${cidrhost("10.0.0.0/24", 1)}"
}
}
# ansible_inventory is a template datasource that stitches together the
# nodes_ansible and monitor_ansible datasources to make an Ansible inventory
data "template_file" "ansible_inventory" {
template = "${file("${path.module}/templates/inventory.tpl")}"
vars {
monitor = "${data.template_file.monitor_ansible.rendered}"
node_hosts = "${join("",data.template_file.nodes_ansible.*.rendered)}"
}
}
/*
* DigitalOcean provider & resources
*/
# Use DigitalOcean as the provider
provider "digitalocean" {
token = "${var.do_token}"
}
# Create an SSH key for the PromGuard droplets to reference
resource "digitalocean_ssh_key" "promguard-ssh" {
name = "${var.do_ssh_key_name}"
public_key = "${file(var.do_ssh_key_file)}"
}
# Create an overall "promguard" tag that all droplets will reference
resource "digitalocean_tag" "promguard" {
name = "promguard"
}
# Create a "promguard_monitor" tag that only the monitor host will reference
resource "digitalocean_tag" "promguard_monitor" {
name = "promguard_monitor"
}
# Create a "promguard_node" tag that only the to-be-monitored nodes will
# reference
resource "digitalocean_tag" "promguard_node" {
name = "promguard_node"
}
# Create a single "monitor" droplet in the do_monitor_region
# A "remote-exec" provisioner is used to ensure SSH access is available and
# Python installed before continuing with further provisioning stages.
resource "digitalocean_droplet" "monitor" {
name = "promguard-monitor-1"
region = "${var.do_monitor_region}"
image = "${var.do_image}"
size = "${var.do_size}"
ssh_keys = [ "${digitalocean_ssh_key.promguard-ssh.id}" ]
tags = [ "${digitalocean_tag.promguard.id}", "${digitalocean_tag.promguard_monitor.id}" ]
provisioner "remote-exec" {
inline = [
"# Connected!",
"apt update",
"apt install -y python"
]
}
}
# Create node_count separate to-be-monitored nodes, each in a unique region
# based on the do_node_regions map.
# A "remote-exec" provisioner is used to ensure SSH access is available and
# Python installed before continuing with further provisioning stages.
resource "digitalocean_droplet" "node" {
count = "${var.node_count}"
name = "promguard-node-${count.index + 1}"
region = "${var.do_node_regions["promguard-node-${count.index +1 }"]}"
image = "${var.do_image}"
size = "${var.do_size}"
ssh_keys = [ "${digitalocean_ssh_key.promguard-ssh.id}" ]
tags = [ "${digitalocean_tag.promguard.id}", "${digitalocean_tag.promguard_node.id}" ]
depends_on = [ "digitalocean_droplet.monitor" ]
provisioner "remote-exec" {
inline = [
"# Connected!",
"apt update",
"apt install -y python"
]
}
}