forked from stackhpc/terraform-aufn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openstack-device.tf
157 lines (130 loc) · 4.3 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
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" {
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_ipv6
}
network {
name = var.lab_net_ipv4
}
timeouts {
create = "30m"
}
}
resource "openstack_compute_floatingip_associate_v2" "bastion" {
floating_ip = var.lab_fip
instance_id = openstack_compute_instance_v2.bastion.id
}
resource "null_resource" "bastion" {
connection {
host = openstack_compute_floatingip_associate_v2.bastion.floating_ip
user = "centos"
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",
]
}
}
resource "openstack_compute_instance_v2" "registry" {
name = "${var.lab_prefix}-registry"
image_name = var.image_name
flavor_name = var.registry_flavor
key_pair = openstack_compute_keypair_v2.ufn_lab_key.name
security_groups = ["default"]
network {
name = var.lab_net_ipv4
}
}
resource "null_resource" "registry" {
connection {
bastion_user = "centos"
bastion_private_key = tls_private_key.default.private_key_pem
bastion_host = openstack_compute_floatingip_associate_v2.bastion.floating_ip
user = "centos"
private_key = tls_private_key.default.private_key_pem
agent = false
timeout = "300s"
host = openstack_compute_instance_v2.registry.access_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
security_groups = ["default"]
network {
name = var.lab_net_ipv4
}
depends_on = [openstack_compute_keypair_v2.ufn_lab_key]
}
resource "null_resource" "lab" {
count = var.lab_count
connection {
bastion_user = "centos"
bastion_private_key = tls_private_key.default.private_key_pem
bastion_host = openstack_compute_floatingip_associate_v2.bastion.floating_ip
user = "centos"
private_key = tls_private_key.default.private_key_pem
agent = false
timeout = "300s"
host = openstack_compute_instance_v2.lab[count.index].access_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 ip link set mtu ${self.triggers.mtu} dev eth0",
"sudo install /tmp/a-universe-from-seed.sh /home/lab",
"sudo usermod -p `echo ${self.triggers.host_id} | openssl passwd -1 -stdin` lab",
"sudo yum install -y git tmux",
"# 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",
]
}
}