provider "libvirt" {
  #uri = "qemu+ssh://deploys@ams-kvm-remote-host/system"
  uri = "qemu+ssh://tobias@localhost/system"
}

resource "libvirt_pool" "tf_pool" {
  name = "tf_pool"
  type = "dir"
  path = var.libvirt_disk_path
}

resource "libvirt_volume" "ubuntu-qcow2" {
  name = "ubuntu-qcow2"
  pool = libvirt_pool.tf_pool.name
  #source = var.ubuntu_18_img_url
  source = var.ubuntu_20_img_url
  format = "qcow2"
  #size = var.vm_disk_size  # not allowed if source is specified
}

# Create a 5GB root volume
#resource "libvirt_volume" "rootfs" {
#  name = "rootfs"
#  pool = libvirt_pool.tf_pool.name
#  #base_volume_id = "..."
#  size = "5120"
#}


data "template_file" "user_data" {
  template = file("${path.module}/config/cloud_init.yml")
  vars = {
    vm_hostname = var.vm_hostname
  }
}

data "template_file" "network_config" {
  template = file("${path.module}/config/network_config.yml")
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name           = "commoninit.iso"
  user_data      = data.template_file.user_data.rendered
  network_config = data.template_file.network_config.rendered
  pool           = libvirt_pool.tf_pool.name
}

resource "libvirt_domain" "domain-ubuntu" {
  qemu_agent = true 
  name   = var.vm_name
  memory = "4096"
  vcpu   = 2 

  cloudinit = libvirt_cloudinit_disk.commoninit.id

  network_interface {
    #network_name   = "host-bridge"
    bridge   = "br0"
    #wait_for_lease = true
    hostname       = var.vm_name
  }

  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  disk {
    volume_id = libvirt_volume.ubuntu-qcow2.id
  }

  graphics {
    type        = "spice"
    listen_type = "address"
    autoport    = true
  }

  provisioner "remote-exec" {
    inline = [
      "echo 'Hello World'"
    ]

    connection {
      type                = "ssh"
      user                = var.ssh_username
      host                = libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]
      private_key         = file(var.ssh_private_key)
      #bastion_host        = "my-jump-host."
      #bastion_user        = "deploys"
      #bastion_private_key = file("~/.ssh/deploys")
      timeout             = "4m"
    }
  }

  provisioner "local-exec" {
    command = <<EOT
      echo "[k8s_master]" > inventory.ini
      echo "${libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]} node_ip=${libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]}  vm_hostname=${var.vm_hostname}" >> inventory.ini
      echo "[k8s_master:vars]" >> inventory.ini
      echo "ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -q hqt\"'" >> inventory.ini
      echo "[k8s_slaves:vars]" >> inventory.ini
      echo "ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -q hqt\"'" >> inventory.ini
      echo "[k8s_slaves]" >> inventory.ini
      #ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i nginx.ini ansible/playbook.yml
      ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i inventory.ini ansible/k8s-master-playbook.yml
      EOT
  }
}

resource "libvirt_volume" "k8sslaves-qcow2" {
  count  = var.slaves
  name   = "k8sslaves-${count.index}.qcow2"
  pool = libvirt_pool.tf_pool.name
  #source = "${path.module}/sources/${var.distros[count.index]}.qcow2"
  source = var.ubuntu_20_img_url
  format = "qcow2"
}

data "template_file" "slaves_user_data" {
  count = var.slaves
  template = file("${path.module}/config/cloud_init.yml")
  vars = {
    vm_hostname = "${var.vm_slave_hostname}${count.index}"
  }
}


resource "libvirt_cloudinit_disk" "slaves_commoninit" {
  count  = var.slaves
  name           = "slaves-commoninit-${count.index}.iso"
  user_data      = data.template_file.slaves_user_data[count.index].rendered
  network_config = data.template_file.network_config.rendered
  pool           = libvirt_pool.tf_pool.name
}

resource "libvirt_domain" "domain-k8s-slave" {
  count  = var.slaves
  qemu_agent = true 
  name   = "${var.vm_slave_name}-${count.index}"
  memory = "4096"
  vcpu   = 2 

  cloudinit = libvirt_cloudinit_disk.slaves_commoninit[count.index].id

  network_interface {
    #network_name   = "host-bridge"
    bridge   = "br0"
    #wait_for_lease = true
    hostname   = "${var.vm_slave_name}-${count.index}"
    #hostname       = var.vm_slave_name
  }

  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  disk {
    volume_id = element(libvirt_volume.k8sslaves-qcow2.*.id, count.index)
  }

  graphics {
    type        = "spice"
    listen_type = "address"
    autoport    = true
  }

  provisioner "remote-exec" {
    inline = [
      "echo 'Hello World'"
    ]

    connection {
      type                = "ssh"
      user                = var.ssh_username
      host                = self.network_interface[0].addresses[0]
      private_key         = file(var.ssh_private_key)
      #bastion_host        = "my-jump-host."
      #bastion_user        = "deploys"
      #bastion_private_key = file("~/.ssh/deploys")
      timeout             = "4m"
    }
  }

  provisioner "local-exec" {
    command = <<EOT
      echo "${self.network_interface[0].addresses[0]} node_ip=${self.network_interface[0].addresses[0]}" >> inventory.ini
      ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i inventory.ini ansible/k8s-slave-playbook.yml
      EOT
  }
}
