main.tf 3.72 KB
Newer Older
Alex Hanselka's avatar
Alex Hanselka committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
resource "azurerm_availability_set" "web" {
  name                         = "${format("web-%v", var.environment)}"
  location                     = "${var.location}"
  managed                      = true
  platform_update_domain_count = 20
  platform_fault_domain_count  = 3
  resource_group_name          = "${var.resource_group_name}"
}

resource "azurerm_network_interface" "web" {
  count                   = "${var.count}"
  name                    = "${format("web-%02d-%v-%v", count.index + 1, var.tier, var.environment)}"
  internal_dns_name_label = "${format("web-%02d-%v-%v", count.index + 1, var.tier, var.environment)}"
  location                = "${var.location}"
  resource_group_name     = "${var.resource_group_name}"

  ip_configuration {
    name                          = "${format("web-%02d-%v", count.index + 1, var.environment)}"
    subnet_id                     = "${var.subnet_id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${join(".", slice(split(".", var.address_prefix), 0, 3))}.${count.index + 101}"
  }
}

resource "aws_route53_record" "web" {
  count   = "${var.count}"
  zone_id = "${var.gitlab_com_zone_id}"
  name    = "${format("web-%02d.%v.%v.gitlab.com.", count.index + 1, var.tier, var.environment == "prod" ? "prd" : var.environment)}"
  type    = "A"
  ttl     = "300"
  records = ["${azurerm_network_interface.web.*.private_ip_address[count.index]}"]
}

data "template_file" "chef-bootstrap-web" {
  count    = "${var.count}"
  template = "${file("${path.root}/../../templates/chef-bootstrap-packer.tpl")}"

  vars {
    ip_address    = "${azurerm_network_interface.web.*.private_ip_address[count.index]}"
    hostname      = "${format("web-%02d.%v.%v.gitlab.com", count.index + 1, var.tier, var.environment == "prod" ? "prd" : var.environment)}"
    chef_version  = "${var.chef_version}"
    chef_repo_dir = "${var.chef_repo_dir}"
    ssh_user      = "${var.ssh_user}"
    ssh_key       = "${var.ssh_key}"
    chef_vaults   = "${var.chef_vaults}"
  }
}

resource "azurerm_virtual_machine" "web" {
  count                         = "${var.count}"
  name                          = "${format("web-%02d.%v.%v.gitlab.com", count.index + 1, var.tier, var.environment == "prod" ? "prd" : var.environment)}"
  location                      = "${var.location}"
  resource_group_name           = "${var.resource_group_name}"
  availability_set_id           = "${azurerm_availability_set.web.id}"
  network_interface_ids         = ["${azurerm_network_interface.web.*.id[count.index]}"]
  primary_network_interface_id  = "${azurerm_network_interface.web.*.id[count.index]}"
  vm_size                       = "${var.instance_type}"
  delete_os_disk_on_termination = true

  storage_image_reference {
    id = "${var.source_image}"
  }

  storage_os_disk {
    name              = "${format("osdisk-web-%02d-%v", count.index + 1, var.environment)}"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "${format("web-%02d.%v.%v.gitlab.com", count.index + 1, var.tier, var.environment == "prod" ? "prd" : var.environment)}"
    admin_username = "${var.ssh_user}"
    admin_password = "${var.ssh_key}"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  provisioner "local-exec" {
    command = "${data.template_file.chef-bootstrap-web.*.rendered[count.index]}"
  }

  provisioner "remote-exec" {
    inline = ["nohup bash -c 'sudo chef-client &'"]

    connection {
      type        = "ssh"
      host        = "${azurerm_network_interface.web.*.private_ip_address[count.index]}"
      user        = "${var.ssh_user}"
      private_key = "${file("${var.ssh_key}")}"
      timeout     = "10s"
    }
  }
}