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.

353 lines
12 KiB

  1. # Define CloudComp group number
  2. variable "group_number" {
  3. type = string
  4. default = "30"
  5. }
  6. # Define OpenStack credentials, project config etc.
  7. locals {
  8. auth_url = "https://private-cloud.informatik.hs-fulda.de:5000/v3"
  9. user_name = "CloudComp${var.group_number}"
  10. user_password = "demo"
  11. tenant_name = "CloudComp${var.group_number}"
  12. network_name = "CloudComp${var.group_number}-net"
  13. image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  14. flavor_name = "m1.small"
  15. region_name = "RegionOne"
  16. }
  17. # Define OpenStack provider
  18. terraform {
  19. required_version = ">= 0.14.0"
  20. required_providers {
  21. openstack = {
  22. source = "terraform-provider-openstack/openstack"
  23. version = ">= 1.40.0"
  24. }
  25. }
  26. }
  27. # Configure the OpenStack Provider
  28. provider "openstack" {
  29. user_name = local.user_name
  30. tenant_name = local.tenant_name
  31. password = local.user_password
  32. auth_url = local.auth_url
  33. region = local.region_name
  34. use_octavia = true
  35. }
  36. # import keypair
  37. resource "openstack_compute_keypair_v2" "terraform-keypair" {
  38. name = "my-terraform-pubkey"
  39. #public_key = file("~/.ssh/id_rsa.pub")
  40. }
  41. # create api security group
  42. resource "openstack_networking_secgroup_v2" "terraform-secgroup-api" {
  43. name = "my-terraform-secgroup-api"
  44. description = "for API services only"
  45. }
  46. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-api-rule-http" {
  47. direction = "ingress"
  48. ethertype = "IPv4"
  49. protocol = "tcp"
  50. port_range_min = 80
  51. port_range_max = 80
  52. #remote_ip_prefix = "0.0.0.0/0"
  53. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-api.id
  54. }
  55. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-api-rule-ssh" {
  56. direction = "ingress"
  57. ethertype = "IPv4"
  58. protocol = "tcp"
  59. port_range_min = 22
  60. port_range_max = 22
  61. #remote_ip_prefix = "0.0.0.0/0"
  62. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-api.id
  63. }
  64. # create worker security group
  65. resource "openstack_networking_secgroup_v2" "terraform-secgroup-worker" {
  66. name = "my-terraform-secgroup-worker"
  67. description = "for services that run on a worker node"
  68. }
  69. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-worker-rule-ssh" {
  70. direction = "ingress"
  71. ethertype = "IPv4"
  72. protocol = "tcp"
  73. port_range_min = 22
  74. port_range_max = 22
  75. #remote_ip_prefix = "0.0.0.0/0"
  76. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-worker.id
  77. }
  78. resource "openstack_networking_secgroup_v2" "terraform-secgroup-control" {
  79. name = "my-terraform-secgroup-control"
  80. description = "for services that run on a control node"
  81. }
  82. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-control-rule-ssh" {
  83. direction = "ingress"
  84. ethertype = "IPv4"
  85. protocol = "tcp"
  86. port_range_min = 22
  87. port_range_max = 22
  88. #remote_ip_prefix = "0.0.0.0/0"
  89. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-control.id
  90. }
  91. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-control-rule-http" {
  92. direction = "ingress"
  93. ethertype = "IPv4"
  94. protocol = "tcp"
  95. port_range_min = 80
  96. port_range_max = 80
  97. #remote_ip_prefix = "0.0.0.0/0"
  98. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-control.id
  99. }
  100. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-control-rule-rabbitmq" {
  101. direction = "ingress"
  102. ethertype = "IPv4"
  103. protocol = "tcp"
  104. port_range_min = 5672
  105. port_range_max = 5672
  106. remote_group_id = openstack_networking_secgroup_v2.terraform-secgroup-worker.id
  107. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-control.id
  108. }
  109. # create services security group
  110. resource "openstack_networking_secgroup_v2" "terraform-secgroup-services" {
  111. name = "my-terraform-secgroup-services"
  112. description = "for DB and AMQP services only"
  113. }
  114. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-services-rule-ssh" {
  115. direction = "ingress"
  116. ethertype = "IPv4"
  117. protocol = "tcp"
  118. port_range_min = 22
  119. port_range_max = 22
  120. #remote_ip_prefix = "0.0.0.0/0"
  121. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-services.id
  122. }
  123. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-services-rule-http" {
  124. direction = "ingress"
  125. ethertype = "IPv4"
  126. protocol = "tcp"
  127. port_range_min = 80
  128. port_range_max = 80
  129. #remote_ip_prefix = "0.0.0.0/0"
  130. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-services.id
  131. }
  132. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-services-rule-mysql-api" {
  133. direction = "ingress"
  134. ethertype = "IPv4"
  135. protocol = "tcp"
  136. port_range_min = 3306
  137. port_range_max = 3306
  138. remote_group_id = openstack_networking_secgroup_v2.terraform-secgroup-api.id
  139. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-services.id
  140. }
  141. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-services-rule-rabbitmq-worker" {
  142. direction = "ingress"
  143. ethertype = "IPv4"
  144. protocol = "tcp"
  145. port_range_min = 5672
  146. port_range_max = 5672
  147. remote_group_id = openstack_networking_secgroup_v2.terraform-secgroup-worker.id
  148. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-services.id
  149. }
  150. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-services-rule-rabbitmq-api" {
  151. direction = "ingress"
  152. ethertype = "IPv4"
  153. protocol = "tcp"
  154. port_range_min = 5672
  155. port_range_max = 5672
  156. remote_group_id = openstack_networking_secgroup_v2.terraform-secgroup-api.id
  157. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup-services.id
  158. }
  159. ###########################################################################
  160. #
  161. # create app-services instance
  162. #
  163. ###########################################################################
  164. resource "openstack_compute_instance_v2" "terraform-instance-app-services" {
  165. name = "my-terraform-app-services"
  166. image_name = local.image_name
  167. flavor_name = local.flavor_name
  168. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  169. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup-services.name]
  170. network {
  171. name = local.network_name
  172. }
  173. user_data = <<-EOF
  174. #!/usr/bin/env bash
  175. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  176. -i database -i messaging
  177. rabbitmqctl add_user faafo guest
  178. rabbitmqctl set_user_tags faafo administrator
  179. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  180. EOF
  181. }
  182. ###########################################################################
  183. #
  184. # create app-api instances
  185. #
  186. ###########################################################################
  187. resource "openstack_compute_instance_v2" "terraform-instance-app-api-1" {
  188. name = "my-terraform-app-api-1"
  189. image_name = local.image_name
  190. flavor_name = local.flavor_name
  191. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  192. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup-api.name]
  193. network {
  194. name = local.network_name
  195. }
  196. user_data = <<-EOF
  197. #!/usr/bin/env bash
  198. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  199. -i faafo -r api -m 'amqp://faafo:guest@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:5672/' \
  200. -d 'mysql+pymysql://faafo:password@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:3306/faafo'
  201. EOF
  202. }
  203. resource "openstack_compute_instance_v2" "terraform-instance-app-api-2" {
  204. name = "my-terraform-app-api-2"
  205. image_name = local.image_name
  206. flavor_name = local.flavor_name
  207. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  208. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup-api.id]
  209. network {
  210. name = local.network_name
  211. }
  212. user_data = <<-EOF
  213. #!/usr/bin/env bash
  214. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  215. -i faafo -r api -m 'amqp://faafo:guest@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:5672/' \
  216. -d 'mysql+pymysql://faafo:password@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:3306/faafo'
  217. EOF
  218. }
  219. ###########################################################################
  220. #
  221. # create worker instances
  222. #
  223. ###########################################################################
  224. resource "openstack_compute_instance_v2" "terraform-instance-app-worker-1" {
  225. name = "my-terraform-app-worker-1"
  226. image_name = local.image_name
  227. flavor_name = local.flavor_name
  228. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  229. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup-worker.id]
  230. network {
  231. name = local.network_name
  232. }
  233. user_data = <<-EOF
  234. #!/usr/bin/env bash
  235. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  236. -i faafo -r worker -e 'http://${openstack_compute_instance_v2.terraform-instance-app-api-1.access_ip_v4}' -m 'amqp://faafo:guest@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:5672/'
  237. EOF
  238. }
  239. resource "openstack_compute_instance_v2" "terraform-instance-app-worker-2" {
  240. name = "my-terraform-app-worker-2"
  241. image_name = local.image_name
  242. flavor_name = local.flavor_name
  243. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  244. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup-worker.id]
  245. network {
  246. name = local.network_name
  247. }
  248. user_data = <<-EOF
  249. #!/usr/bin/env bash
  250. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  251. -i faafo -r worker -e 'http://${openstack_compute_instance_v2.terraform-instance-app-api-1.access_ip_v4}' -m 'amqp://faafo:guest@${openstack_compute_instance_v2.terraform-instance-app-services.access_ip_v4}:5672/'
  252. EOF
  253. }
  254. ###########################################################################
  255. #
  256. # create load balancer
  257. #
  258. ###########################################################################
  259. data "openstack_networking_network_v2" "network_1" {
  260. name = local.network_name
  261. }
  262. resource "openstack_lb_loadbalancer_v2" "lb_1" {
  263. vip_subnet_id = data.openstack_networking_network_v2.network_1.subnets[0]
  264. }
  265. resource "openstack_lb_listener_v2" "listener_1" {
  266. protocol = "HTTP"
  267. protocol_port = 80
  268. loadbalancer_id = openstack_lb_loadbalancer_v2.lb_1.id
  269. connection_limit = 1024
  270. }
  271. resource "openstack_lb_pool_v2" "pool_1" {
  272. protocol = "HTTP"
  273. lb_method = "ROUND_ROBIN"
  274. listener_id = openstack_lb_listener_v2.listener_1.id
  275. }
  276. resource "openstack_lb_members_v2" "members_1" {
  277. pool_id = openstack_lb_pool_v2.pool_1.id
  278. member {
  279. address = openstack_compute_instance_v2.terraform-instance-app-api-1.access_ip_v4
  280. protocol_port = 80
  281. }
  282. member {
  283. address = openstack_compute_instance_v2.terraform-instance-app-api-2.access_ip_v4
  284. protocol_port = 80
  285. }
  286. }
  287. resource "openstack_lb_monitor_v2" "monitor_1" {
  288. pool_id = openstack_lb_pool_v2.pool_1.id
  289. type = "HTTP"
  290. delay = 5
  291. timeout = 5
  292. max_retries = 3
  293. http_method = "GET"
  294. url_path = "/"
  295. expected_codes = 200
  296. }
  297. ###########################################################################
  298. #
  299. # assign floating ip to load balancer
  300. #
  301. ###########################################################################
  302. resource "openstack_networking_floatingip_v2" "fip_1" {
  303. pool = "public1"
  304. port_id = openstack_lb_loadbalancer_v2.lb_1.vip_port_id
  305. }
  306. output "loadbalancer_vip_addr" {
  307. value = openstack_networking_floatingip_v2.fip_1
  308. }