Skip to content
Snippets Groups Projects
compute.tf 4.96 KiB
Newer Older
locals {
  local_base_image_visibility = var.base_image_visibility == "null" ? null : var.base_image_visibility
}

# data for reference image
Chris King's avatar
Chris King committed

data "openstack_images_image_v2" "base_image" {
  name       = var.base_image_name
  visibility = local.local_base_image_visibility
}

# data for different instance sizes 

data "openstack_compute_flavor_v2" "m1_small" {
  name = "m1.small"
}

Chris King's avatar
Chris King committed
# admin node
Chris King's avatar
Chris King committed

Chris King's avatar
Chris King committed
resource "openstack_compute_instance_v2" "admin" {
  name      = "admin"
  image_id  = data.openstack_images_image_v2.base_image.id
  flavor_id = data.openstack_compute_flavor_v2.m1_small.id
  key_pair  = var.ssh_keypair
  security_groups = [
    "default",
    openstack_compute_secgroup_v2.allow_ssh.name
  ]

  user_data = templatefile("${path.module}/templates/cloud-init-admin.yml",
    {
Chris King's avatar
Chris King committed
      sles_rmt_host        = var.sles_rmt_host
      sles_rmt_fingerprint = var.sles_rmt_fingerprint
      b64_json = base64encode(templatefile("${path.module}/templates/cluster.json", {
        mon_bootstrap_ip = openstack_compute_instance_v2.mon[0].network[0].fixed_ip_v4
        additional_hosts = openstack_compute_instance_v2.app.*.name
        }
      ))
    }
  )
Chris King's avatar
Chris King committed

  block_device {
    # this is the image to clone from
    uuid                  = data.openstack_images_image_v2.base_image.id
    source_type           = "image"
    destination_type      = "local"
    boot_index            = 0
    delete_on_termination = true
  }

  network {
    uuid = openstack_networking_network_v2.public_network.id
  }
  lifecycle {
    ignore_changes = [
      user_data # ignore changes to user_data after instance creation
    ]
  }
Chris King's avatar
Chris King committed
resource "openstack_compute_floatingip_associate_v2" "admin_association" {
  floating_ip = openstack_compute_floatingip_v2.floating_ip.address
Chris King's avatar
Chris King committed
  instance_id = openstack_compute_instance_v2.admin.id
}

# OSD nodes

resource "openstack_compute_instance_v2" "osd" {
  count = var.osd_node_count

  name      = format("osd%02d", count.index + 1)
  image_id  = data.openstack_images_image_v2.base_image.id
  flavor_id = data.openstack_compute_flavor_v2.m1_small.id
  key_pair  = var.ssh_keypair
  security_groups = [
    "default"
  ]

  user_data = templatefile("${path.module}/templates/cloud-init.yml",
    {
Chris King's avatar
Chris King committed
      sles_rmt_host        = var.sles_rmt_host
      sles_rmt_fingerprint = var.sles_rmt_fingerprint
Chris King's avatar
Chris King committed

  block_device {
    # this is the image to clone from
    uuid                  = data.openstack_images_image_v2.base_image.id
    source_type           = "image"
    destination_type      = "local"
    boot_index            = 0
    delete_on_termination = true
  }

  dynamic "block_device" { # JBOD disks
    for_each = var.osd_disk_sizes

    content {
      source_type           = "blank"
      destination_type      = "volume"
      volume_size           = block_device.value
      boot_index            = -1
      delete_on_termination = true
    }
Chris King's avatar
Chris King committed
    uuid           = openstack_networking_network_v2.public_network.id
    access_network = true
  }

  network {
    uuid = openstack_networking_network_v2.cluster_network.id
  }
}

resource "openstack_compute_instance_v2" "mon" {
  count = 1 # TODO: add variables for this later

  name      = format("mon%02d", count.index + 1)
  image_id  = data.openstack_images_image_v2.base_image.id
  flavor_id = data.openstack_compute_flavor_v2.m1_small.id
  key_pair  = var.ssh_keypair
  security_groups = [
    "default",
    openstack_compute_secgroup_v2.allow_web_interface.name
  user_data = templatefile("${path.module}/templates/cloud-init.yml",
    {
Chris King's avatar
Chris King committed
      sles_rmt_host        = var.sles_rmt_host
      sles_rmt_fingerprint = var.sles_rmt_fingerprint
Chris King's avatar
Chris King committed

  block_device {
    # this is the image to clone from
    uuid                  = data.openstack_images_image_v2.base_image.id
    source_type           = "image"
    destination_type      = "local"
    boot_index            = 0
    delete_on_termination = true
  }

  network {
    uuid = openstack_networking_network_v2.public_network.id
  }
}

resource "openstack_compute_floatingip_associate_v2" "mon_association" {
  floating_ip = openstack_compute_floatingip_v2.floating_ip_mon.address
  instance_id = openstack_compute_instance_v2.mon[0].id
}
# App nodes

resource "openstack_compute_instance_v2" "app" {
  count = var.app_instance_count

  name      = format("app%02d", count.index + 1)
  image_id  = data.openstack_images_image_v2.base_image.id
  flavor_id = data.openstack_compute_flavor_v2.m1_small.id
  key_pair  = var.ssh_keypair
  security_groups = [
    "default",
  user_data = templatefile("${path.module}/templates/cloud-init.yml",
    {
Chris King's avatar
Chris King committed
      sles_rmt_host        = var.sles_rmt_host
      sles_rmt_fingerprint = var.sles_rmt_fingerprint
Chris King's avatar
Chris King committed

  block_device {
    uuid                  = data.openstack_images_image_v2.base_image.id
    source_type           = "image"
    destination_type      = "local"
    boot_index            = 0
    delete_on_termination = true
  }

  network {
    uuid = openstack_networking_network_v2.public_network.id
  }
}