You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.4 KiB

  1. # Define CloudComp group number
  2. variable "group_number" {
  3. type = string
  4. default = "32"
  5. }
  6. ## OpenStack credentials can be used in a more secure way by using
  7. ## cloud.yaml from https://private-cloud.informatik.hs-fulda.de/project/api_access/clouds.yaml/
  8. # or by using env vars exported from openrc here,
  9. # e.g., using 'export TF_VAR_os_password=$OS_PASSWORD'
  10. # Define OpenStack credentials, project config etc.
  11. locals {
  12. auth_url = "https://10.32.4.182:5000/v3"
  13. user_name = "CloudComp32"
  14. user_password = "demo"
  15. tenant_name = "CloudComp${var.group_number}"
  16. #network_name = "CloudComp${var.group_number}-net"
  17. router_name = "CloudComp${var.group_number}-router"
  18. image_name = "ubuntu-22.04-jammy-x86_64"
  19. flavor_name = "m1.small"
  20. region_name = "RegionOne"
  21. floating_net = "ext_net"
  22. dns_nameservers = [ "10.33.16.100" ]
  23. }
  24. # Define OpenStack provider
  25. terraform {
  26. required_version = ">= 0.14.0"
  27. required_providers {
  28. openstack = {
  29. source = "terraform-provider-openstack/openstack"
  30. # last version before 2.0.0, shows octavia/neutron lbaas deprecation warnings
  31. # "~> 1.54.1"
  32. version = ">= 2.0.0"
  33. }
  34. }
  35. }
  36. # Configure the OpenStack Provider
  37. provider "openstack" {
  38. user_name = local.user_name
  39. tenant_name = local.tenant_name
  40. password = local.user_password
  41. auth_url = local.auth_url
  42. region = local.region_name
  43. # due to currenty missing valid certificate
  44. insecure = true
  45. }
  46. ###########################################################################
  47. #
  48. # create keypair
  49. #
  50. ###########################################################################
  51. # import keypair, if public_key is not specified, create new keypair to use
  52. resource "openstack_compute_keypair_v2" "terraform-keypair" {
  53. name = "my-terraform-pubkey"
  54. #public_key = file("~/.ssh/id_rsa.pub")
  55. }
  56. ###########################################################################
  57. #
  58. # create security group
  59. #
  60. ###########################################################################
  61. resource "openstack_networking_secgroup_v2" "terraform-secgroup" {
  62. name = "my-terraform-secgroup"
  63. description = "for terraform instances"
  64. }
  65. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-rule-http" {
  66. direction = "ingress"
  67. ethertype = "IPv4"
  68. protocol = "tcp"
  69. port_range_min = 80
  70. port_range_max = 80
  71. #remote_ip_prefix = "0.0.0.0/0"
  72. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup.id
  73. }
  74. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-rule-ssh" {
  75. direction = "ingress"
  76. ethertype = "IPv4"
  77. protocol = "tcp"
  78. port_range_min = 22
  79. port_range_max = 22
  80. #remote_ip_prefix = "0.0.0.0/0"
  81. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup.id
  82. }
  83. ###########################################################################
  84. #
  85. # create network
  86. #
  87. ###########################################################################
  88. resource "openstack_networking_network_v2" "terraform-network-1" {
  89. name = "my-terraform-network-1"
  90. admin_state_up = "true"
  91. }
  92. resource "openstack_networking_subnet_v2" "terraform-subnet-1" {
  93. name = "my-terraform-subnet-1"
  94. network_id = openstack_networking_network_v2.terraform-network-1.id
  95. cidr = "192.168.255.0/24"
  96. ip_version = 4
  97. dns_nameservers = local.dns_nameservers
  98. }
  99. #new ressource here:
  100. resource "openstack_networking_port_v2" "port_1" {
  101. name = "port_1"
  102. network_id = openstack_networking_network_v2.terraform-network-1.id
  103. admin_state_up = "true"
  104. security_group_ids = [openstack_networking_secgroup_v2.terraform-secgroup.id]
  105. fixed_ip {
  106. subnet_id = openstack_networking_subnet_v2.terraform-subnet-1.id
  107. }
  108. }
  109. data "openstack_networking_router_v2" "router-1" {
  110. name = local.router_name
  111. }
  112. resource "openstack_networking_router_interface_v2" "router_interface_1" {
  113. router_id = data.openstack_networking_router_v2.router-1.id
  114. subnet_id = openstack_networking_subnet_v2.terraform-subnet-1.id
  115. }
  116. ###########################################################################
  117. #
  118. # create instances
  119. #
  120. ###########################################################################
  121. resource "openstack_compute_instance_v2" "terraform-instance-1" {
  122. name = "my-terraform-instance-1"
  123. image_name = local.image_name
  124. flavor_name = local.flavor_name
  125. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  126. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup.name]
  127. depends_on = [openstack_networking_subnet_v2.terraform-subnet-1]
  128. network {
  129. #uuid = openstack_networking_network_v2.terraform-network-1.id
  130. port = openstack_networking_port_v2.port_1.id
  131. }
  132. user_data = <<-EOF
  133. #!/bin/bash
  134. apt-get update
  135. apt-get -y install apache2
  136. rm /var/www/html/index.html
  137. cat > /var/www/html/index.html << INNEREOF
  138. <!DOCTYPE html>
  139. <html>
  140. <body>
  141. <h1>It works!</h1>
  142. <p>hostname</p>
  143. </body>
  144. </html>
  145. INNEREOF
  146. sed -i "s/hostname/terraform-instance-1/" /var/www/html/index.html
  147. sed -i "1s/$/ terraform-instance-1/" /etc/hosts
  148. EOF
  149. }
  150. ###########################################################################
  151. #
  152. # assign floating ip to instance
  153. #
  154. ###########################################################################
  155. resource "openstack_networking_floatingip_v2" "fip_1" {
  156. pool = local.floating_net
  157. }
  158. resource "openstack_networking_floatingip_associate_v2" "terraform-instance-1-ip" {
  159. floating_ip = openstack_networking_floatingip_v2.fip_1.address
  160. port_id = openstack_networking_port_v2.port_1.id
  161. }
  162. # does work, though openstack_compute_floatingip_associate_v2 is deprecated,
  163. # openstack_compute_instance_v2.terraform-instance-1.network[0].port is empty after instance creation: ""
  164. #
  165. #resource "openstack_networking_floatingip_associate_v2" "terraform-instance-1-ip" {
  166. # floating_ip = openstack_networking_floatingip_v2.fip_1.address
  167. # port_id = openstack_compute_instance_v2.terraform-instance-1.network[0].port
  168. #}
  169. #
  170. # even better, as soon as openstack_compute_instance_v2.terraform-instance-1.network[0].port is not "":
  171. #resource "openstack_networking_floatingip_v2" "fip_1" {
  172. # pool = local.floating_net
  173. # port_id = openstack_compute_instance_v2.terraform-instance-1.network[0].port
  174. #}
  175. output "vip_addr" {
  176. value = openstack_networking_floatingip_v2.fip_1
  177. }