diff --git a/README.md b/README.md index cf146e90e810813fe084f3bf9a6195aba352f8c8..d9ccc03ff89449477c2671bf752cfbecd4e2684e 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,16 @@ unzip terraform_0.12.3_linux_amd64.zip -d ~/bin - `$terraform apply "$HOME/terraform-first-instance/terraform-plan.tf"` + +### There's a target parameter that will let you specify just one module as defined in the root main file. Run it via plan like so: + +- `$terraform plan -out $HOME/terraform-first-instance/name-of-plan.tf -target=module.name-of-module-to-run` + +**_(Note)_** You can also target multiple modules. + +- `$terraform plan -out $HOME/terraform-first-instance/name-of-plan.tf -target=module.name-of-module -target=module.name-of-other-module` + + ### Destroy Terraform Instance: - `$terraform destroy` diff --git a/datasources.tf b/datasources.tf deleted file mode 100644 index 3942142017a02f7c8deff5270b5b33d18997e2b9..0000000000000000000000000000000000000000 --- a/datasources.tf +++ /dev/null @@ -1,3 +0,0 @@ -data "openstack_networking_network_v2" "public-network" { - name = var.public-network-name -} diff --git a/external-network/main.tf b/external-network/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..b2a15f24ad63043e833682d38f923c8fc6ebebd6 --- /dev/null +++ b/external-network/main.tf @@ -0,0 +1,49 @@ +# is created as a datasource this module and called in root module +variable "public_network_name" {type = "string"} + +variable "name" {default = "dmz"} +variable "admin_state_up" {} +variable "enable_dhcp" {} + + + +data "openstack_networking_network_v2" "public_network" {name = var.public_network_name} + +# creates dmznet +resource "openstack_networking_network_v2" "external_network" { + name = "${var.name}net" + admin_state_up = var.admin_state_up +} + +resource "openstack_networking_subnet_v2" "external_subnet" { + name = "${var.name}subnet" + network_id = openstack_networking_network_v2.external_network.id + cidr = "192.168.100.0/24" + ip_version = 4 + dns_nameservers = ["8.8.8.8"] + enable_dhcp = var.enable_dhcp +} + +# defines the router dmzrouter using floating ip defined in datasource above to create the external network id +resource "openstack_networking_router_v2" "router" { + name = "${var.name}router" + admin_state_up = var.admin_state_up + external_network_id = data.openstack_networking_network_v2.public_network.id +} + +resource "openstack_networking_router_interface_v2" "router" { + router_id = openstack_networking_router_v2.router.id + subnet_id = openstack_networking_subnet_v2.external_subnet.id +} + +output "external_network_id" { + value = data.openstack_networking_network_v2.public_network.id +} + +output "external_subnet_id" { + value = openstack_networking_subnet_v2.external_subnet.id +} + +output "router_id" { + value = openstack_networking_router_v2.router.id +} \ No newline at end of file diff --git a/floating-ip/floating-main.tf b/floating-ip/floating-main.tf new file mode 100644 index 0000000000000000000000000000000000000000..a9581cfc9f299dd9ae31fcfe91cc2512f796ca57 --- /dev/null +++ b/floating-ip/floating-main.tf @@ -0,0 +1,19 @@ +variable "public_network_name" {} + + +# defines where floating ip will come from using variable public_network_name defined in root module +resource "openstack_networking_floatingip_v2" "ohpc_ip" { + pool = var.public_network_name +} + +resource "openstack_networking_floatingip_v2" "ood_ip" { + pool = var.public_network_name +} + +output "ohpc_address" { + value = openstack_networking_floatingip_v2.ohpc_ip.address +} + +output "ood_address" { + value = openstack_networking_floatingip_v2.ood_ip.address +} diff --git a/internal-network/main.tf b/internal-network/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..c3ad09bafadfded0e1305f5a0532d5373780e240 --- /dev/null +++ b/internal-network/main.tf @@ -0,0 +1,27 @@ +variable "name" {default = "cluster"} +variable "admin_state_up" { } +variable "enable_dhcp" {} + +# creates clusternet +resource "openstack_networking_network_v2" "internal_network" { + name = "${var.name}net" + admin_state_up = var.admin_state_up +} + +# creates clustersubnet +# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack) +resource "openstack_networking_subnet_v2" "internal_subnet" { + name = "${var.name}subnet" + network_id = openstack_networking_network_v2.internal_network.id + cidr = "10.1.1.0/24" + ip_version = 4 + enable_dhcp = var.enable_dhcp +} + +output "internal_network_id" { + value = openstack_networking_network_v2.internal_network.id +} + +output "internal_subnet_id" { + value = openstack_networking_subnet_v2.internal_subnet.id +} \ No newline at end of file diff --git a/key-pair/main.tf b/key-pair/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..c65cda4ffc656ab1241197ed6d0a15533547bdaf --- /dev/null +++ b/key-pair/main.tf @@ -0,0 +1,15 @@ +variable "keypair_name" {} +variable "ssh_public_key" {} + +resource "openstack_compute_keypair_v2" "keypair" { + name = var.keypair_name + public_key = file(var.ssh_public_key) +} + +output "keypair_name" { + value = var.keypair_name +} + +output "public_key" { + value = file(var.ssh_public_key) +} diff --git a/main.tf b/main.tf index f936ad13e2e0f850eabc4313cb0d863658a1d7d8..128e223650c2dc8e457d1531cb493ea1a049c435 100644 --- a/main.tf +++ b/main.tf @@ -1,146 +1,111 @@ -resource "openstack_compute_keypair_v2" "keypair" { - name = var.keypair-name - public_key = file(var.ssh-public-key) +# runs the external-network module +module "dmz-network" { + source = "./external-network" + # Default name var is in the module main file + admin_state_up = var.admin_state_up + enable_dhcp = var.enable_dhcp + public_network_name = var.public_network_name } - -# creates dmznet -resource "openstack_networking_network_v2" "external_net" { - name = var.external-net - admin_state_up = var.admin-state-up +# calls the outputs defined in the external-network module +output "external_network_id" { + value = "${module.dmz-network.external_network_id}" } -resource "openstack_networking_subnet_v2" "external_subnet" { - name = var.external-subnet - network_id = openstack_networking_network_v2.external_net.id - cidr = "192.168.100.0/24" - ip_version = 4 - dns_nameservers = ["8.8.8.8"] - enable_dhcp = var.enable-dhcp +output "router_id" { + value = "${module.dmz-network.router_id}" } -# defines the router borderrouter using floating ip defined in datasources.tf to create the external network id -resource "openstack_networking_router_v2" "router" { - name = var.router - admin_state_up = var.admin-state-up - external_network_id = data.openstack_networking_network_v2.public-network.id +# runs the internal-network module +module "cluster-network" { + source = "./internal-network" + # Default name var is in the module main file + admin_state_up = var.admin_state_up + enable_dhcp = var.enable_dhcp } - -resource "openstack_networking_router_interface_v2" "router" { - router_id = openstack_networking_router_v2.router.id - subnet_id = openstack_networking_subnet_v2.external_subnet.id +# calls the outputs defined in the internal-network module +output "internal_network_id" { + value = "${module.cluster-network.internal_network_id}" } -# creates clusternet -resource "openstack_networking_network_v2" "internal_net" { - name = var.internal-net - admin_state_up = var.admin-state-up +# runs the floating-ip module - uses public network name defined above +module "floating-ip-address" { + source = "./floating-ip" + public_network_name = var.public_network_name } -# creates clustersubnet -# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack) -resource "openstack_networking_subnet_v2" "internal_subnet" { - name = var.internal-subnet - network_id = openstack_networking_network_v2.internal_net.id - cidr = "10.1.1.0/24" - ip_version = 4 - enable_dhcp = var.enable-dhcp +# calls the outputs defined in the floating-ip module +output "floating_ip_ohpc" { + value = "${module.floating-ip-address.ohpc_address}" } -# defines where floating ip will come from using variable from vars.tf -resource "openstack_networking_floatingip_v2" "ohpc_ip" { - pool = var.public-network-name +output "floating_ip_ood" { + value = "${module.floating-ip-address.ood_address}" } -resource "openstack_networking_floatingip_v2" "ood_ip" { - pool = var.public-network-name +# runs the key-pair module - imports local public key into openstack and give it the name defined above in the variables +module "import-keypair" { + source = "./key-pair" + keypair_name = var.keypair_name + ssh_public_key = var.ssh_public_key } -# creates details for the OHPC instance using variables defined in vars.tf -resource "openstack_compute_instance_v2" "ohpc" { - depends_on = [openstack_networking_subnet_v2.external_subnet] - name = var.ohpc-instance-name - image_name = var.image_ohpc - flavor_name = var.flavor - key_pair = openstack_compute_keypair_v2.keypair.name - security_groups = ["default"] - -# defines the networks of the instance - network { - name = var.external-net - } - network { - name = var.internal-net - } +# calls the outputs defined in the key-pair module +output "keypair_name" { + value = "${module.import-keypair.keypair_name}" } -# associates floating ip with the OHPC instance and run the ansible playbook -resource "openstack_compute_floatingip_associate_v2" "ohpc" { - floating_ip = openstack_networking_floatingip_v2.ohpc_ip.address - instance_id = openstack_compute_instance_v2.ohpc.id - -# defines ssh connection - connection { - host = format( - var.host-prefix, - element( - split(".", openstack_networking_floatingip_v2.ohpc_ip.address), - 3, - ), - ) - user = var.ohpc-user - private_key = file(var.ssh-private-key) - } +# runs the ohpc-instance module - creates ohpc instance using variables defined above +# calls functions from dmz-network, import-keypair, and floating-ip-address modules to get values created there for use +module "create-ohpc-instance" { + external_subnet_id = "${module.dmz-network.external_subnet_id}" + source = "./ohpc-instance" + ohpc_instance_name = var.ohpc_instance_name + image_ohpc = var.image_ohpc + flavor = var.flavor + key_pair = "${module.import-keypair.keypair_name}" + external_network = var.external_network + internal_network = var.internal_network + floating_ip_ohpc = "${module.floating-ip-address.ohpc_address}" + host_prefix = var.host_prefix + ohpc_user = var.ohpc_user + ssh_private_key = var.ssh_private_key } -# creates details for the OOD instance using variables defined in vars.tf -resource "openstack_compute_instance_v2" "ood" { - depends_on = [openstack_networking_subnet_v2.external_subnet] - name = var.ood-instance-name - image_name = var.image_ood - flavor_name = var.flavor - key_pair = openstack_compute_keypair_v2.keypair.name - security_groups = ["default"] - -# defines the networks of the instance - network { - name = var.external-net - } - network { - name = var.internal-net - } +# runs the ood-instance module - creates ood instance using variables defined above +# calls functions from cluster-network, import-keypair, and floating-ip-address modules to get values created there for use +module "create-ood-instance" { + internal_subnet_id = "${module.cluster-network.internal_subnet_id}" + source = "./ood-instance" + ood_instance_name = var.ood_instance_name + image_ood = var.image_ood + flavor = var.flavor + key_pair = "${module.import-keypair.keypair_name}" + internal_network = var.internal_network + external_network = var.external_network + floating_ip_ood = "${module.floating-ip-address.ood_address}" + host_prefix = var.host_prefix + ood_user = var.ood_user + ssh_private_key = var.ssh_private_key } -# associates floating ip with the OOD instance and run the ansible playbook -resource "openstack_compute_floatingip_associate_v2" "ood" { - floating_ip = openstack_networking_floatingip_v2.ood_ip.address - instance_id = openstack_compute_instance_v2.ood.id - -# defines ssh connection - connection { - host = format( - var.host-prefix, - element( - split(".", openstack_networking_floatingip_v2.ood_ip.address), - 3, - ), - ) - user = var.ood-user - private_key = file(var.ssh-private-key) - } +# runs the nodes module - creates nodes using variables defined above +# calls functions from cluster-network and import-keypair modules to get values created there for use +module "nodes" { + internal_subnet_id = "${module.cluster-network.internal_subnet_id}" + source = "./nodes" + image_compute = var.image_compute + flavor = var.flavor + key_pair = "${module.import-keypair.keypair_name}" + compute_node_count = var.compute_node_count + internal_network = var.internal_network } -# creates compute node -resource "openstack_compute_instance_v2" "c0" { - depends_on = [openstack_networking_subnet_v2.internal_subnet] - name = "c${count.index}" - image_name = var.image_compute - flavor_name = var.flavor - key_pair = openstack_compute_keypair_v2.keypair.name - security_groups = ["default"] - count = var.compute_node_count +# calls the outputs defined in the ohpc-instance module +output "ohpc-ssh_host" { + value = "${module.create-ohpc-instance.ssh_host}" +} -# defines the networks of the instance - network { - name = var.internal-net - } +# calls the outputs defined in the ood-instance module +output "ood-ssh_host" { + value = "${module.create-ood-instance.ssh_host}" } \ No newline at end of file diff --git a/nodes/main.tf b/nodes/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..5fdb441aacee4bcd7c26596cc5d0be28ce5c2ef6 --- /dev/null +++ b/nodes/main.tf @@ -0,0 +1,29 @@ +# is created in internal-network module and called in root module +variable "internal_subnet_id" {type = "string"} + +variable "image_compute" {} +variable "flavor" {} + +# is created in key-pair module and called in root module +variable "key_pair" {type = "string"} + +variable "compute_node_count" { } +variable "internal_network" {} + + + +# creates compute node +resource "openstack_compute_instance_v2" "c0" { + depends_on = [var.internal_subnet_id] + name = "c${count.index}" + image_name = var.image_compute + flavor_name = var.flavor + key_pair = var.key_pair + security_groups = ["default"] + count = var.compute_node_count + +# defines the networks of the instance + network { + name = var.internal_network + } +} \ No newline at end of file diff --git a/ohpc-instance/main.tf b/ohpc-instance/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..c41ad802d3b981b170eb7e956d73043d0d2e7817 --- /dev/null +++ b/ohpc-instance/main.tf @@ -0,0 +1,55 @@ +# is created in external-network module and called in root module +variable "external_subnet_id" {type = "string"} + +variable "ohpc_instance_name" {} +variable "image_ohpc" {} +variable "flavor" {} + +# is created in key-pair module and called in root module +variable "key_pair" {type = "string"} + +variable "internal_network" {} +variable "external_network" {} + +# is created in floating-ip module and called in root module +variable "floating_ip_ohpc" {type = "string"} + +variable "host_prefix" {} +variable "ohpc_user" {} +variable "ssh_private_key" {} + + +# creates details for the OHPC instance +resource "openstack_compute_instance_v2" "ohpc" { + depends_on = [var.external_subnet_id] + name = var.ohpc_instance_name + image_name = var.image_ohpc + flavor_name = var.flavor + key_pair = var.key_pair + security_groups = ["default"] + +# defines the networks of the instance + network { + name = var.external_network + } + network { + name = var.internal_network + } +} + +# associates floating ip with the OHPC instance +resource "openstack_compute_floatingip_associate_v2" "ohpc" { + floating_ip = var.floating_ip_ohpc + instance_id = openstack_compute_instance_v2.ohpc.id + +# defines ssh connection + connection { + host = format(var.host_prefix,element(split(".", var.floating_ip_ohpc),3,),) + user = var.ohpc_user + private_key = file(var.ssh_private_key) + } +} + +output "ssh_host" { + value = format(var.host_prefix,element(split(".", var.floating_ip_ohpc),3,),) +} \ No newline at end of file diff --git a/ood-instance/main.tf b/ood-instance/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..09e8ce5cdb33954e567c588b47de5e34b9dce5d2 --- /dev/null +++ b/ood-instance/main.tf @@ -0,0 +1,57 @@ +# is created in internal-network module and called in root module +variable "internal_subnet_id" {type = "string"} + +variable "ood_instance_name" {} +variable "image_ood" {} +variable "flavor" {} + +# is created in key-pair module and called in root module +variable "key_pair" {type = "string"} + +variable "internal_network" {} +variable "external_network" {} + +# is created in floating-ip module and called in root module +variable "floating_ip_ood" {type = "string"} + +variable "host_prefix" {} +variable "ood_user" {} +variable "ssh_private_key" {} + + +# creates details for the OOD instance +resource "openstack_compute_instance_v2" "ood" { + depends_on = [var.internal_subnet_id] + name = var.ood_instance_name + image_name = var.image_ood + flavor_name = var.flavor + key_pair = var.key_pair + security_groups = ["default"] + +# defines the networks of the instance + network { + name = var.external_network + } + network { + name = var.internal_network + } +} + +# associates floating ip with the OOD instance +resource "openstack_compute_floatingip_associate_v2" "ood" { + floating_ip = var.floating_ip_ood + instance_id = openstack_compute_instance_v2.ood.id + +# defines ssh connection + connection { + host = format(var.host-prefix,element(split(".", var.floating_ip_ood),3,),) + user = var.ood_user + private_key = file(var.ssh_private_key) + } +} + + + +output "ssh_host"{ + value = format(var.host_prefix,element(split(".", var.floating_ip_ood),3,),) +} \ No newline at end of file diff --git a/output.tf b/output.tf deleted file mode 100644 index d88cb0b904da4fa22928d7b9a01aa99b862308ab..0000000000000000000000000000000000000000 --- a/output.tf +++ /dev/null @@ -1,16 +0,0 @@ -output "ohpc_address" { - value = openstack_networking_floatingip_v2.ohpc_ip.address -} - -output "ood_address" { - value = openstack_networking_floatingip_v2.ood_ip.address -} - -output "external_network_id" { - value = openstack_networking_network_v2.external_net.id -} - -output "internal_network_id" { - value = openstack_networking_network_v2.internal_net.id -} - diff --git a/vars.tf b/vars.tf index 4ae6e96c60ac36d448e3319c38c4cf70161b4c71..b95a6f264ddc1c3fbf8746f8cbf152902294b758 100644 --- a/vars.tf +++ b/vars.tf @@ -1,87 +1,31 @@ -variable "admin-state-up" { - description = "whether admin state in enabled. defualt is true" - default = true -} - -variable "enable-dhcp" { - description = "whether dhcp in enabled. defualt is true" - default = true -} - -variable "external-net" { - default = "dmznet" -} - -variable "external-subnet" { - default = "dmzsubnet" -} - -variable "flavor" { - default = "m1.medium" -} - -variable "host-prefix" { - description = "prefix of host id." - default = "164.111.161.%s" -} - -variable "internal-net" { - default = "clusternet" -} - -variable "internal-subnet" { - default = "clustersubnet" -} - -variable "image_ohpc" { - default = "CentOS-7-x86_64-GenericCloud-1905" -} - -variable "image_ood" { - default = "CentOS-7-x86_64-GenericCloud-1905" -} - -variable "image_compute" { - default = "CentOS-7-x86_64-GenericCloud-1905" -} -variable "compute_node_count" { - default = 2 -} - -variable "keypair-name" { - default = "os-gen-keypair" -} - -variable "ohpc-instance-name" { - default = "ohpc" -} - -variable "ohpc-user" { - default = "centos" -} - -variable "ood-instance-name" { - default = "ood" -} - -variable "ood-user" { - default = "centos" -} - -variable "public-network-name" { - default = "bright-external-flat-externalnet" -} - -variable "router" { - default = "borderrouter" -} - -variable "ssh-private-key" { - description = "Path to file containing private key" - default = "~/.ssh/id_rsa" -} - -variable "ssh-public-key" { - description = "Path to file containing public key" - default = "~/.ssh/id_rsa.pub" -} +# These are the defined variables used in all the modules. They are referenced in the modules, and defined here. +# This is where you would change any variable values. + +# variables for networks and router +variable "admin_state_up" {default = true} +variable "enable_dhcp" {default = true} + +# variable for floating-ip - also used in exrernal network creation +variable "public_network_name" {default = "bright-external-flat-externalnet"} + +# variables for keypair module +variable "keypair_name" {default = "os-gen-keypair"} +variable "ssh_public_key" {default = "~/.ssh/id_rsa.pub"} + +# variables for instance modules +variable "ohpc_instance_name" {default = "ohpc"} +variable "ood_instance_name" { default = "ood"} +variable "image_ohpc" {default = "CentOS-7-x86_64-GenericCloud-1905"} +variable "image_ood" {default = "CentOS-7-x86_64-GenericCloud-1905"} +variable "flavor" {default = "m1.medium"} +variable "internal_network" {default = "clusternet"} +variable "external_network" {default = "dmznet"} +variable "host_prefix" {default = "164.111.161.%s"} +variable "ohpc_user" {default = "centos"} +variable "ood_user" {default = "centos"} +variable "ssh_private_key" {default = "~/.ssh/id_rsa"} + + +# variables for node creation module +variable "image_compute" {default = "CentOS-7-x86_64-GenericCloud-1905"} +variable "compute_node_count" {default = 2}