-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
143 lines (120 loc) · 4.37 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
locals {
private_subnet_cidr = "10.0.1.0/24"
db_server_private_ip = "10.0.1.101"
asciinema_server_private_ip = [
for p in openstack_compute_instance_v2.asciinema_server.network :
p if p.uuid == openstack_networking_network_v2.asciinema_private_network.id
][0].fixed_ip_v4
asciinema_server_public_ip = [
for p in openstack_compute_instance_v2.asciinema_server.network :
p if p.name == "public"][0].fixed_ip_v4
db_server_public_ip = [
for p in openstack_compute_instance_v2.db_server.network :
p if p.name == "public"][0].fixed_ip_v4
# Hackish way to check that user provides a PAT if wanting to use livedns
validate_pat = (var.manage_dns_record_using_livedns == true && var.gandi_personal_access_token == null) ? tobool("Please set the variable gandi_personal_access_token to use livedns.") : true
}
data "openstack_images_image_ids_v2" "images" {
name = var.image_name
sort = "updated_at"
}
###################
# Gandi Resources #
###################
resource "gandi_livedns_record" "asciinema_server_dns_record_v4" {
count = (var.manage_dns_record_using_livedns ? 1 : 0)
name = var.asciinema_server_subdomain
ttl = 300
type = "A"
values = ["${local.asciinema_server_public_ip}"]
zone = var.asciinema_server_domain_apex
}
#######################
# OpenStack Resources #
#######################
resource "openstack_compute_keypair_v2" "admin_keypair" {
name = "asciinema_admin_pubkey"
public_key = var.admin_ssh_pubkey
}
# Private network
resource "openstack_networking_network_v2" "asciinema_private_network" {
name = "asciinema_private_network"
}
resource "openstack_networking_subnet_v2" "asciinema_private_subnet" {
name = "asciinema_private_subnet"
network_id = openstack_networking_network_v2.asciinema_private_network.id
ip_version = 4
no_gateway = true
dns_nameservers = ["0.0.0.0"]
cidr = local.private_subnet_cidr
}
# Database server
resource "openstack_blockstorage_volume_v3" "db_boot" {
name = "db_boot"
size = 25
# Takes the latest uploaded image by name
image_id = data.openstack_images_image_ids_v2.images.ids[0]
}
resource "random_password" "db_password" {
length = 20
special = true
override_special = "*-_=+<>"
}
resource "openstack_compute_instance_v2" "db_server" {
name = "db_server"
flavor_name = var.flavor
key_pair = openstack_compute_keypair_v2.admin_keypair.name
user_data = templatefile("./userdata/db.sh.tftpl", {
db_password = random_password.db_password.result,
asciinema_server_ip = local.asciinema_server_private_ip,
my_private_ip = local.db_server_private_ip,
})
block_device {
uuid = openstack_blockstorage_volume_v3.db_boot.id
source_type = "volume"
boot_index = 0
destination_type = "volume"
}
network {
name = "public"
}
network {
uuid = openstack_networking_network_v2.asciinema_private_network.id
# Allows to specify a specific IP in the private subnet (can be easier to reference elsewhere):
fixed_ip_v4 = local.db_server_private_ip
}
}
# Asciinema server
resource "openstack_networking_port_v2" "asciinema_server_port" {
network_id = openstack_networking_network_v2.asciinema_private_network.id
}
resource "openstack_blockstorage_volume_v3" "asciinema_server_boot" {
name = "asciinema_server_boot"
size = 25
# Takes the latest uploaded image by name
image_id = data.openstack_images_image_ids_v2.images.ids[0]
}
resource "openstack_compute_instance_v2" "asciinema_server" {
name = "asciinema_server"
flavor_name = var.flavor
key_pair = openstack_compute_keypair_v2.admin_keypair.name
user_data = templatefile("./userdata/asciinema.sh.tftpl", {
asciinema_version = var.asciinema_version,
server_host = "${var.asciinema_server_subdomain}.${var.asciinema_server_domain_apex}",
db_password = random_password.db_password.result,
db_server_private_ip = local.db_server_private_ip,
})
block_device {
uuid = openstack_blockstorage_volume_v3.asciinema_server_boot.id
source_type = "volume"
boot_index = 0
destination_type = "volume"
}
network {
name = "public"
}
network {
# Specifying a network uuid lets OpenStack choose an IP in one of its v4 subnet(s)
uuid = openstack_networking_network_v2.asciinema_private_network.id
}
}