-
Notifications
You must be signed in to change notification settings - Fork 5
/
openstack-device.tf
198 lines (167 loc) · 6.09 KB
/
openstack-device.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
resource "openstack_compute_keypair_v2" "ufn_lab_key" {
name = "ufn_lab_key"
public_key = tls_private_key.default.public_key_openssh
}
resource "openstack_compute_instance_v2" "bastion" {
count = var.create_bastion ? 1 : 0
name = "${var.lab_prefix}-bastion"
image_name = var.image_name
flavor_name = var.bastion_flavor
key_pair = openstack_compute_keypair_v2.ufn_lab_key.name
security_groups = ["default"]
network {
name = var.lab_net_ipv4
}
timeouts {
create = "30m"
}
}
resource "openstack_compute_floatingip_associate_v2" "bastion" {
count = var.create_bastion ? 1 : 0
floating_ip = var.bastion_floating_ip
instance_id = openstack_compute_instance_v2.bastion[0].id
}
resource "null_resource" "bastion" {
count = var.create_bastion ? 1 : 0
connection {
host = openstack_compute_floatingip_associate_v2.bastion[0].floating_ip
user = var.image_user
private_key = tls_private_key.default.private_key_pem
agent = false
timeout = "300s"
}
triggers = {
ssh_config = templatefile("ssh-config.tpl", local.template)
}
provisioner "file" {
content = self.triggers.ssh_config
destination = "/tmp/ssh_config"
}
provisioner "remote-exec" {
inline = [
"mkdir -p ~/.ssh; chmod 0700 ~/.ssh; cp /tmp/ssh_config ~/.ssh/config",
]
}
}
# Boot instance with volume attached for Docker Registry
resource "openstack_compute_instance_v2" "registry" {
name = "${var.lab_prefix}-registry"
flavor_name = var.registry_flavor
key_pair = openstack_compute_keypair_v2.ufn_lab_key.name
security_groups = ["default"]
block_device {
uuid = var.image_id
source_type = "image"
volume_size = var.registry_data_vol
boot_index = 0
destination_type = "volume"
delete_on_termination = true
}
network {
name = var.lab_net_ipv4
}
}
resource "openstack_compute_floatingip_v2" "registry" {
count = var.allocate_floating_ips ? 1 : 0
pool = var.floating_ip_external_net
}
resource "openstack_compute_floatingip_associate_v2" "registry" {
count = var.allocate_floating_ips ? 1 : 0
floating_ip = openstack_compute_floatingip_v2.registry[count.index].address
instance_id = openstack_compute_instance_v2.registry.id
}
resource "null_resource" "registry" {
connection {
bastion_user = var.create_bastion ? var.image_user : null
bastion_private_key = var.create_bastion ? tls_private_key.default.private_key_pem : null
bastion_host = var.create_bastion ? openstack_compute_floatingip_associate_v2.bastion[0].floating_ip : null
user = var.image_user
private_key = tls_private_key.default.private_key_pem
agent = false
timeout = "300s"
host = var.allocate_floating_ips ? openstack_compute_floatingip_associate_v2.registry[0].floating_ip : openstack_compute_instance_v2.registry.network.0.fixed_ip_v4
}
triggers = {
pull_retag_push_images = file("pull-retag-push-images.sh")
}
provisioner "file" {
content = self.triggers.pull_retag_push_images
destination = "/tmp/pull-retag-push-images.sh"
}
provisioner "remote-exec" {
inline = [
"bash /tmp/pull-retag-push-images.sh > pull-retag-push-images.out",
]
}
}
resource "openstack_compute_instance_v2" "lab" {
count = var.lab_count
name = format("%s-lab-%02d", var.lab_prefix, count.index)
image_name = var.image_name
flavor_name = var.lab_flavor
key_pair = openstack_compute_keypair_v2.ufn_lab_key.name
dynamic "block_device" {
for_each = var.boot_labs_from_volume ? [1] : []
content {
uuid = var.image_id
source_type = "image"
volume_size = var.lab_data_vol
boot_index = 0
destination_type = "volume"
delete_on_termination = true
}
}
network {
name = var.lab_net_ipv4
}
timeouts {
create = "30m"
}
depends_on = [openstack_compute_keypair_v2.ufn_lab_key, null_resource.registry]
}
resource "openstack_compute_floatingip_v2" "lab" {
count = var.allocate_floating_ips ? var.lab_count : 0
pool = var.floating_ip_external_net
}
resource "openstack_compute_floatingip_associate_v2" "lab" {
count = var.allocate_floating_ips ? var.lab_count : 0
floating_ip = openstack_compute_floatingip_v2.lab[count.index].address
instance_id = openstack_compute_instance_v2.lab[count.index].id
}
resource "null_resource" "lab" {
count = var.lab_count
connection {
bastion_user = var.create_bastion ? var.image_user : null
bastion_private_key = var.create_bastion ? tls_private_key.default.private_key_pem : null
bastion_host = var.create_bastion ? openstack_compute_floatingip_associate_v2.bastion[0].floating_ip : null
user = var.image_user
private_key = tls_private_key.default.private_key_pem
agent = false
timeout = "300s"
host = var.allocate_floating_ips ? openstack_compute_floatingip_associate_v2.lab[count.index].floating_ip : openstack_compute_instance_v2.lab[count.index].network.0.fixed_ip_v4
}
triggers = {
registry_ip = openstack_compute_instance_v2.registry.access_ip_v4
host_id = openstack_compute_instance_v2.lab[count.index].id
mtu = 1500
}
provisioner "remote-exec" {
script = "setup-user.sh"
}
provisioner "file" {
source = "a-seed-from-nothing.sh"
destination = "/tmp/a-seed-from-nothing.sh"
}
provisioner "file" {
source = "a-universe-from-seed.sh"
destination = "/tmp/a-universe-from-seed.sh"
}
provisioner "remote-exec" {
inline = [
"sudo install /tmp/a-seed-from-nothing.sh /home/lab",
"sudo install /tmp/a-universe-from-seed.sh /home/lab",
"sudo usermod -p `echo ${self.triggers.host_id} | openssl passwd -1 -stdin` lab",
"sudo -u lab /home/lab/a-seed-from-nothing.sh ${self.triggers.registry_ip} | sudo -u lab tee -a /home/lab/a-seed-from-nothing.out",
]
}
}