github.com/SUSE/skuba@v1.4.17/ci/infra/vmware/lb-instance.tf (about) 1 variable "lbs" { 2 default = 1 3 description = "Number of load-balancer nodes" 4 } 5 6 variable "lb_cpus" { 7 default = 1 8 description = "Number of CPUs used on load-balancer node" 9 } 10 11 variable "lb_memory" { 12 default = 2048 13 description = "Amount of memory used on load-balancer node" 14 } 15 16 variable "lb_disk_size" { 17 default = 40 18 description = "Size of the root disk in GB on load-balancer node" 19 } 20 21 variable "lb_repositories" { 22 type = map(string) 23 24 default = { 25 sle_server_pool = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Products/SLE-Product-SLES/15-SP1/x86_64/product/" 26 basesystem_pool = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Products/SLE-Module-Basesystem/15-SP1/x86_64/product/" 27 ha_pool = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Products/SLE-Product-HA/15-SP1/x86_64/product/" 28 ha_updates = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Updates/SLE-Product-HA/15-SP1/x86_64/update/" 29 sle_server_updates = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Updates/SLE-Product-SLES/15-SP1/x86_64/update/" 30 basesystem_updates = "http://ibs-mirror.prv.suse.net/ibs/SUSE/Updates/SLE-Module-Basesystem/15-SP1/x86_64/update/" 31 } 32 } 33 34 data "template_file" "lb_repositories_template" { 35 count = length(var.lb_repositories) 36 template = file("cloud-init/repository.tpl") 37 38 vars = { 39 repository_url = element(values(var.lb_repositories), count.index) 40 repository_name = element(keys(var.lb_repositories), count.index) 41 } 42 } 43 44 data "template_file" "haproxy_apiserver_backends_master" { 45 count = var.masters 46 template = "server $${fqdn} $${ip}:6443\n" 47 48 vars = { 49 fqdn = element(vsphere_virtual_machine.master.*.name, count.index) 50 ip = element( 51 vsphere_virtual_machine.master.*.default_ip_address, 52 count.index, 53 ) 54 } 55 56 depends_on = [vsphere_virtual_machine.master] 57 } 58 59 data "template_file" "haproxy_gangway_backends_master" { 60 count = var.masters 61 template = "server $${fqdn} $${ip}:32001\n" 62 63 vars = { 64 fqdn = element(vsphere_virtual_machine.master.*.name, count.index) 65 ip = element( 66 vsphere_virtual_machine.master.*.default_ip_address, 67 count.index, 68 ) 69 } 70 71 depends_on = [vsphere_virtual_machine.master] 72 } 73 74 data "template_file" "haproxy_dex_backends_master" { 75 count = var.masters 76 template = "server $${fqdn} $${ip}:32000\n" 77 78 vars = { 79 fqdn = element(vsphere_virtual_machine.master.*.name, count.index) 80 ip = element( 81 vsphere_virtual_machine.master.*.default_ip_address, 82 count.index, 83 ) 84 } 85 86 depends_on = [vsphere_virtual_machine.master] 87 } 88 89 data "template_file" "lb_cloud_init_metadata" { 90 template = file("cloud-init/metadata.tpl") 91 92 vars = { 93 network_config = base64gzip(data.local_file.network_cloud_init.content) 94 instance_id = "${var.stack_name}-lb" 95 } 96 } 97 98 data "template_file" "lb_haproxy_cfg" { 99 template = file("cloud-init/haproxy.cfg.tpl") 100 101 vars = { 102 apiserver_backends = join( 103 " ", 104 data.template_file.haproxy_apiserver_backends_master.*.rendered, 105 ) 106 gangway_backends = join( 107 " ", 108 data.template_file.haproxy_gangway_backends_master.*.rendered, 109 ) 110 dex_backends = join( 111 " ", 112 data.template_file.haproxy_dex_backends_master.*.rendered, 113 ) 114 } 115 } 116 117 data "template_file" "lb_cloud_init_userdata" { 118 template = file("cloud-init/lb.tpl") 119 count = var.lbs 120 121 vars = { 122 authorized_keys = join("\n", formatlist(" - %s", var.authorized_keys)) 123 repositories = join("\n", data.template_file.lb_repositories_template.*.rendered) 124 packages = join("\n", formatlist(" - %s", var.packages)) 125 ntp_servers = join("\n", formatlist(" - %s", var.ntp_servers)) 126 hostname = "${var.stack_name}-lb-${count.index}" 127 hostname_from_dhcp = var.hostname_from_dhcp == true ? "yes" : "no" 128 } 129 } 130 131 resource "vsphere_virtual_machine" "lb" { 132 count = var.lbs 133 name = "${var.stack_name}-lb-${count.index}" 134 num_cpus = var.lb_cpus 135 memory = var.lb_memory 136 guest_id = var.guest_id 137 firmware = var.firmware 138 scsi_type = data.vsphere_virtual_machine.template.scsi_type 139 resource_pool_id = data.vsphere_resource_pool.pool.id 140 datastore_id = (var.vsphere_datastore == null ? null : data.vsphere_datastore.datastore[0].id) 141 datastore_cluster_id = (var.vsphere_datastore_cluster == null ? null : data.vsphere_datastore_cluster.datastore[0].id) 142 folder = var.cpi_enable == true ? vsphere_folder.folder[0].path : null 143 144 clone { 145 template_uuid = data.vsphere_virtual_machine.template.id 146 } 147 148 hardware_version = var.vsphere_hardware_version 149 150 disk { 151 label = "disk0" 152 size = var.lb_disk_size 153 } 154 155 extra_config = { 156 "guestinfo.metadata" = base64gzip(data.template_file.lb_cloud_init_metadata.rendered) 157 "guestinfo.metadata.encoding" = "gzip+base64" 158 "guestinfo.userdata" = base64gzip(data.template_file.lb_cloud_init_userdata[count.index].rendered) 159 "guestinfo.userdata.encoding" = "gzip+base64" 160 } 161 162 network_interface { 163 network_id = data.vsphere_network.network.id 164 } 165 166 depends_on = [ 167 vsphere_folder.folder, 168 vsphere_virtual_machine.master, 169 ] 170 } 171 172 resource "null_resource" "lb_wait_cloudinit" { 173 depends_on = [vsphere_virtual_machine.lb] 174 count = var.lbs 175 176 connection { 177 host = element( 178 vsphere_virtual_machine.lb.*.guest_ip_addresses.0, 179 count.index, 180 ) 181 user = var.username 182 type = "ssh" 183 agent = true 184 } 185 186 provisioner "remote-exec" { 187 inline = [ 188 "cloud-init status --wait > /dev/null", 189 ] 190 } 191 } 192 193 resource "null_resource" "lb_push_haproxy_cfg" { 194 depends_on = [null_resource.lb_wait_cloudinit] 195 count = var.lbs 196 197 triggers = { 198 master_count = var.masters 199 } 200 201 connection { 202 host = element( 203 vsphere_virtual_machine.lb.*.guest_ip_addresses.0, 204 count.index, 205 ) 206 user = var.username 207 type = "ssh" 208 agent = true 209 } 210 211 provisioner "file" { 212 content = data.template_file.lb_haproxy_cfg.rendered 213 destination = "/tmp/haproxy.cfg" 214 } 215 216 provisioner "remote-exec" { 217 inline = [ 218 "sudo mv /tmp/haproxy.cfg /etc/haproxy/haproxy.cfg", 219 "sudo systemctl enable haproxy && sudo systemctl restart haproxy", 220 ] 221 } 222 }