# This playbook will launch an ec2 instance in a VPC.
# This instance will have an autogenerated key.
#
# required variables for this playbook:
#   - base_ami_id                  - The base base AMI-ID
#   - ec2_vpc_subnet_id            - The Subnet ID to bring up the instance
#   - ec2_security_group_id        - The security group ID to use
#   - ec2_instance_profile_name    - The instance profile that should be used to launch this AMI
#
# Other Variables:
#   - ec2_region              - The region the server should be brought up in
#   - ec2_instance_type       - The instance type to use
#   - ebs_volume_size         - Size in GB for the root volume
#   - ec2_timeout             - Time in seconds to wait for an ec2 instance become available
#   - ec2_assign_public_ip    - (yes/no) should the instance have a public IP address?
#
# This playbook generates a directory with 2 artifact files:
#   - launch_template.yml     - A yaml file with information such as the instance ID and internal IP address of the instance launched
#   - key.pem                  - The private key file for the newly generated keypair
#
# Example command line to run this playbook:
#    ansible-playbook -i "localhost," -c local -e @overrides.yml launch_instance.yml
#

- hosts: all
  vars:
    artifact_path: /tmp/ansible-runtime
    ec2_region: us-east-1
    ec2_instance_type: t2.medium
    ebs_volume_size: 8
    ec2_timeout: 500
    ec2_assign_public_ip: no
    automation_prefix: "gocd automation run -- {{ ansible_date_time.iso8601 }} -- "
  gather_facts: True
  connection: local
  tasks:

  - name: Generate UUID for keypair
    command: cat /proc/sys/kernel/random/uuid
    register: unique_key_name

  - name: Generate ec2 keypair to use for this instance
    ec2_key:
      name: "{{ automation_prefix }} {{ unique_key_name.stdout }}"
      region: "{{ ec2_region }}"
    register: ssh_key_register

  - name: Ensure artifact directory exists
    file:
      path: "{{ artifact_path }}"
      state: directory
      force: yes

  - name: Launch EC2 instance
    ec2:
      instance_tags: {"Name" : "{{ automation_prefix }} {{ unique_key_name.stdout }}"}
      region: "{{ ec2_region }}"
      key_name: "{{ automation_prefix }} {{ unique_key_name.stdout }}"
      instance_type: "{{ ec2_instance_type }}"
      image: "{{ base_ami_id }}"
      wait: yes
      group_id: "{{ ec2_security_group_id }}"
      count: 1
      vpc_subnet_id: "{{ ec2_vpc_subnet_id }}"
      assign_public_ip: "{{ ec2_assign_public_ip }}"
      volumes:
        - device_name: /dev/sdf
          volume_type: 'gp2'
          volume_size: "{{ ebs_volume_size }}"
      wait: yes
      wait_timeout: "{{ ec2_timeout }}"
      instance_profile_name: "{{ ec2_instance_profile_name }}"
    register: ec2_instance_register

  - name: Wait for SSH to come up
    wait_for:
      host: "{{ ec2_instance_register.instances[0].private_ip }}"
      port: 22
      delay: 60
      timeout: "{{ ec2_timeout }}"
      state: started

  - name: Generate artifact for jobs down stream
    template:
      src: templates/local/launch_template.yml.j2
      dest: "{{ artifact_path }}/launch_info.yml"
      mode: 0600

  - name: Generate key material artifact for jobs down stream
    template:
      src: templates/local/key.pem.j2
      dest: "{{ artifact_path }}/key.pem"
      mode: 0600

  - name: Generate ansible inventory file
    template:
      src: templates/local/inventory.j2
      dest: "{{ artifact_path }}/ansible_inventory"
      mode: 0600