Commit 6a0a1e36 authored by Ryan Randles Jones's avatar Ryan Randles Jones
Browse files

Merge branch 'make-modular' into 'master'

Make modular

See merge request rrand11/terraform-openstack!27
parents 1cd3ecf6 1251f46e
......@@ -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`
data "openstack_networking_network_v2" "public-network" {
name = var.public-network-name
}
# 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
output "ohpc_address" {
value = openstack_networking_floatingip_v2.ohpc_ip.address
}
variable "public_network_name" {}
output "ood_address" {
value = openstack_networking_floatingip_v2.ood_ip.address
# 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
}
output "external_network_id" {
value = openstack_networking_network_v2.external_net.id
resource "openstack_networking_floatingip_v2" "ood_ip" {
pool = var.public_network_name
}
output "internal_network_id" {
value = openstack_networking_network_v2.internal_net.id
output "ohpc_address" {
value = openstack_networking_floatingip_v2.ohpc_ip.address
}
output "ood_address" {
value = openstack_networking_floatingip_v2.ood_ip.address
}
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
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)
}
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
# 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
# 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
# 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
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"}