provider "aws" {
  access_key = ""
  secret_key = ""
  region = "us-east-1"
}

variable "PEM_KEY_NAME" {
  type = "string"
  default = "dev-mortali-cluster"
  description = "Name of PEM key"
}

variable "PEM_KEY_LOCATION" {
  type = "string"
  default = "/Users/mortali/Downloads/dev-mortali-cluster.pem"
  description = "Absolute path to PEM file"
}

variable "SECURITY_GROUP" {
  type = "string"
  default = "sg-b84745c0"
  description = "Security group identifier"
}

variable "SUBNET_ID" {
  type = "string"
  default = "subnet-b649dfef"
  description = "Subnet Id"
}

variable "INSTANCE_PROFILE_NAME" {
  type = "string"
  default = "cluster-client-instance-profile"
  description = "Name of instance profile"
}

resource "aws_iam_instance_profile" "instance_profile" {
  name = "${var.INSTANCE_PROFILE_NAME}"
  roles = ["ec2-describe-instance"]
}

resource "aws_instance" "server" {
  count = 3
  ami = "ami-08111162"
  instance_type = "t2.small"
  security_groups = ["${var.SECURITY_GROUP}"]
  subnet_id = "${var.SUBNET_ID}"
  key_name = "${var.PEM_KEY_NAME}"
  iam_instance_profile = "${var.INSTANCE_PROFILE_NAME}"

  tags {
    Name = "cluster-client"
  }

  provisioner "file" {
    source = "${path.module}/consul/upstart.conf"
    destination = "/tmp/consul-upstart.conf"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/consul/upstart.conf"
    destination = "/tmp/consul-upstart.conf"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/consul/consul.json"
    destination = "/tmp/consul.json"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/consul/bin"
    destination = "/tmp/consul"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/nomad/upstart.conf"
    destination = "/tmp/nomad-upstart.conf"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/nomad/consul.json"
    destination = "/tmp/nomad-consul.json"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "file" {
    source = "${path.module}/nomad/bin"
    destination = "/tmp/nomad"

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "remote-exec" {
    inline = [
      "aws ec2 describe-instances --filter Name=tag:Name,Values='cluster-master' --region 'us-east-1' --query 'Reservations[*].Instances[*].PrivateDnsName' --output text | grep '[^[:blank:]]' > /tmp/master-server-addr"
    ]

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }

  provisioner "remote-exec" {
    scripts = [
      "${path.module}/requirements.sh",
      "${path.module}/consul/install.sh",
      "${path.module}/nomad/install.sh",
      "${path.module}/consul/service.sh",
      "${path.module}/nomad/service.sh"
    ]

    connection {
      user = "ec2-user"
      private_key = "${var.PEM_KEY_LOCATION}"
    }
  }
}
