-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
203 lines (166 loc) · 5.05 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
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
195
196
197
198
199
200
201
202
203
terraform {
backend "swift" {}
}
provider openstack {
cloud = var.cloud_name
region = var.region_name
}
resource tls_private_key priv {
algorithm = "ECDSA"
ecdsa_curve = "P384"
}
resource openstack_compute_keypair_v2 keypair {
name = var.ssh_keypair_name
public_key = tls_private_key.priv.public_key_openssh
}
#
# Network objects
#
data openstack_networking_network_v2 ext_net {
name = "Ext-Net"
tenant_id = ""
}
data openstack_images_image_v2 image {
name = var.image_name
most_recent = true
}
resource openstack_networking_secgroup_v2 secgroup {
name = var.name
description = "Security gropup for Jitsi"
}
resource openstack_networking_secgroup_rule_v2 ingress_https_jitsi {
for_each = var.allowed_prefixes
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = each.key
security_group_id = openstack_networking_secgroup_v2.secgroup.id
}
resource openstack_networking_secgroup_rule_v2 ingress_http_01_challenge {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 80 # required for http letsencrypt challenge
port_range_max = 80
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup.id
}
resource openstack_networking_secgroup_rule_v2 ingress_tcp_ssh {
for_each = var.allowed_prefixes
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = each.key
security_group_id = openstack_networking_secgroup_v2.secgroup.id
}
resource openstack_networking_port_v2 fip {
name = var.name
network_id = data.openstack_networking_network_v2.ext_net.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup.id]
}
locals {
jitsi = [
for ip in openstack_networking_port_v2.fip.all_fixed_ips :
ip
if length(replace(ip, "/[[:alnum:]]+:[^,]+/", "")) > 0
][0]
ip_parts = split(".", local.jitsi)
fqdn = var.fqdn != "" ? var.fqdn : format("ip%s.ip-%s-%s-%s.eu", local.ip_parts[3], local.ip_parts[0], local.ip_parts[1], local.ip_parts[2])
user_data = {
hostname = local.fqdn
fqdn = local.fqdn
}
}
resource openstack_compute_instance_v2 vm {
name = var.name
image_id = data.openstack_images_image_v2.image.id
flavor_name = var.flavor_name
key_pair = openstack_compute_keypair_v2.keypair.name
user_data = <<EOF
#cloud-config
${yamlencode(local.user_data)}
EOF
network {
port = openstack_networking_port_v2.fip.id
access_network = true
}
}
data template_file install_letsencrypt_cert {
template = file("${path.module}/install-letsencrypt-cert.sh")
vars = {
DOMAIN = local.fqdn
EMAIL = var.email
}
}
resource null_resource provision {
triggers = {
jitsi = openstack_compute_instance_v2.vm.id
provision = md5(file("${path.module}/provision.sh"))
}
provisioner "file" {
connection {
user = "ubuntu"
host = local.jitsi
private_key = tls_private_key.priv.private_key_pem
}
content = data.template_file.install_letsencrypt_cert.rendered
destination = "/tmp/install-letsencrypt-cert.sh"
}
provisioner "remote-exec" {
connection {
user = "ubuntu"
host = local.jitsi
private_key = tls_private_key.priv.private_key_pem
}
script = "${path.module}/provision.sh"
}
}
data template_file workbook {
template = file("${path.module}/mistral-wb.yml.tpl")
vars = {
name = var.name
id = openstack_compute_instance_v2.vm.id
region = var.region_name
}
}
resource local_file workbook {
count = var.cronjob_enabled ? 1 : 0
content = data.template_file.workbook.rendered
filename = "${path.module}/${var.name}.wb.yml"
}
# the mistral jobs are set using the openstack CLI as
# mistral resources arent available in the terraform openstack provider
resource null_resource openstack_mistral_setup {
count = var.cronjob_enabled ? 1 : 0
triggers = {
server_id = openstack_compute_instance_v2.vm.id
}
provisioner "local-exec" {
working_dir = path.module
environment = {
OS_CLOUD = var.cloud_name
}
command = <<EOF
set -e
sudo pip3 install -r ${path.module}/requirements.txt
if openstack workbook show ${var.name} > /dev/null 2>&1; then
openstack workbook update ${local_file.workbook.0.filename}
else
openstack workbook create ${local_file.workbook.0.filename}
fi
if openstack cron trigger show ${var.name}.unshelve > /dev/null 2>&1; then
openstack cron trigger delete ${var.name}.unshelve
fi
if openstack cron trigger show ${var.name}.shelve > /dev/null 2>&1; then
openstack cron trigger delete ${var.name}.shelve
fi
openstack cron trigger create --pattern='${var.cronjob_unshelve}' --utc ${var.name}.unshelve ${var.name}.unshelve "{}"
openstack cron trigger create --pattern='${var.cronjob_shelve}' --utc ${var.name}.shelve ${var.name}.shelve "{}"
EOF
}
}