---
- set_fact:
    created_by: "{{ lookup('env', 'LOGNAME')|default(cluster, true) }}"
    docker_vol_ephemeral: "{{ lookup('env', 'os_docker_vol_ephemeral') | default(false, true) }}"
    cluster: "{{ cluster_id }}"
    env: "{{ cluster_env }}"
    host_type: "{{ type }}"
    sub_host_type: "{{ g_sub_host_type }}"

- set_fact:
    ec2_instance_type: "{{ lookup('env', 'ec2_master_instance_type') | default(deployment_vars[deployment_type].type, true) }}"
    ec2_security_groups: "{{ lookup('env', 'ec2_master_security_groups') | default(deployment_vars[deployment_type].security_groups, true) }}"
  when: host_type == "master" and sub_host_type == "default"

- set_fact:
    ec2_instance_type: "{{ lookup('env', 'ec2_etcd_instance_type') | default(deployment_vars[deployment_type].type, true) }}"
    ec2_security_groups: "{{ lookup('env', 'ec2_etcd_security_groups') | default(deployment_vars[deployment_type].security_groups, true) }}"
  when: host_type == "etcd" and sub_host_type == "default"

- set_fact:
    ec2_instance_type: "{{ lookup('env', 'ec2_infra_instance_type') | default(deployment_vars[deployment_type].type, true) }}"
    ec2_security_groups: "{{ lookup('env', 'ec2_infra_security_groups') | default(deployment_vars[deployment_type].security_groups, true) }}"
  when: host_type == "node" and sub_host_type == "infra"

- set_fact:
    ec2_instance_type: "{{ lookup('env', 'ec2_node_instance_type') | default(deployment_vars[deployment_type].type, true) }}"
    ec2_security_groups: "{{ lookup('env', 'ec2_node_security_groups') | default(deployment_vars[deployment_type].security_groups, true) }}"
  when: host_type == "node" and sub_host_type == "compute"

- set_fact:
    ec2_instance_type: "{{ deployment_vars[deployment_type].type }}"
  when: ec2_instance_type is not defined
- set_fact:
    ec2_security_groups: "{{ deployment_vars[deployment_type].security_groups }}"
  when: ec2_security_groups is not defined

- name: Find amis for deployment_type
  ec2_ami_find:
    region: "{{ deployment_vars[deployment_type].region }}"
    ami_id: "{{ deployment_vars[deployment_type].image }}"
    name: "{{ deployment_vars[deployment_type].image_name }}"
  register: ami_result

- fail: msg="Could not find requested ami"
  when: not ami_result.results

- set_fact:
    latest_ami: "{{ ami_result.results | oo_ami_selector(deployment_vars[deployment_type].image_name) }}"
    volume_defs:
      etcd:
        root:
          volume_size: "{{ lookup('env', 'os_etcd_root_vol_size') | default(25, true) }}"
          device_type: "{{ lookup('env', 'os_etcd_root_vol_type') | default('gp2', true) }}"
          iops: "{{ lookup('env', 'os_etcd_root_vol_iops') | default(500, true) }}"
      master:
        root:
          volume_size: "{{ lookup('env', 'os_master_root_vol_size') | default(25, true) }}"
          device_type: "{{ lookup('env', 'os_master_root_vol_type') | default('gp2', true) }}"
          iops: "{{ lookup('env', 'os_master_root_vol_iops') | default(500, true) }}"
        docker:
          volume_size: "{{ lookup('env', 'os_docker_vol_size') | default(10, true) }}"
          device_type: "{{ lookup('env', 'os_docker_vol_type') | default('gp2', true) }}"
          iops: "{{ lookup('env', 'os_docker_vol_iops') | default(500, true) }}"
      node:
        root:
          volume_size: "{{ lookup('env', 'os_node_root_vol_size') | default(85, true) }}"
          device_type: "{{ lookup('env', 'os_node_root_vol_type') | default('gp2', true) }}"
          iops: "{{ lookup('env', 'os_node_root_vol_iops') | default(500, true) }}"
        docker:
          volume_size: "{{ lookup('env', 'os_docker_vol_size') | default(32, true) }}"
          device_type: "{{ lookup('env', 'os_docker_vol_type') | default('gp2', true) }}"
          iops: "{{ lookup('env', 'os_docker_vol_iops') | default(500, true) }}"

- set_fact:
    volumes: "{{ volume_defs | oo_ec2_volume_definition(host_type, docker_vol_ephemeral | bool) }}"

- name: Launch instance(s)
  ec2:
    state: present
    region: "{{ deployment_vars[deployment_type].region }}"
    keypair: "{{ deployment_vars[deployment_type].keypair }}"
    group: "{{ deployment_vars[deployment_type].security_groups }}"
    instance_type: "{{ ec2_instance_type }}"
    image: "{{ deployment_vars[deployment_type].image }}"
    count: "{{ instances | length }}"
    vpc_subnet_id: "{{ deployment_vars[deployment_type].vpc_subnet }}"
    assign_public_ip: "{{ deployment_vars[deployment_type].assign_public_ip }}"
    user_data: "{{ lookup('template', '../templates/user_data.j2') }}"
    wait: yes
    instance_tags:
      created-by: "{{ created_by }}"
      clusterid: "{{ cluster }}"
      environment: "{{ cluster_env }}"
      host-type: "{{ host_type }}"
      sub-host-type: "{{ sub_host_type }}"
    volumes: "{{ volumes }}"
  register: ec2

- name: Add Name tag to instances
  ec2_tag: resource={{ item.1.id }} region={{ deployment_vars[deployment_type].region }} state=present
  with_together:
  - "{{ instances }}"
  - "{{ ec2.instances }}"
  args:
    tags:
      Name: "{{ item.0 }}"

- set_fact:
    instance_groups: >
      tag_created-by_{{ created_by }}, tag_clusterid_{{ cluster }},
      tag_environment_{{ cluster_env }}, tag_host-type_{{ host_type }},
      tag_sub-host-type_{{ sub_host_type }}

- set_fact:
    node_label:
      region: "{{ deployment_vars[deployment_type].region }}"
      type: "{{sub_host_type}}"
  when: host_type == "node"

- set_fact:
    node_label:
      region: "{{ deployment_vars[deployment_type].region }}"
      type: "{{host_type}}"
  when: host_type != "node"

- set_fact:
    logrotate:
        - name: syslog
          path: |
            /var/log/cron
            /var/log/maillog
            /var/log/messages
            /var/log/secure
            /var/log/spooler"
          options:
            - daily
            - rotate 7
            - compress
            - sharedscripts
            - missingok
          scripts:
            postrotate: "/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true"

- name: Add new instances groups and variables
  add_host:
    hostname: "{{ item.0 }}"
    ansible_ssh_host: "{{ item.1.dns_name }}"
    ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
    ansible_become: "{{ deployment_vars[deployment_type].become }}"
    groups: "{{ instance_groups }}"
    ec2_private_ip_address: "{{ item.1.private_ip }}"
    ec2_ip_address: "{{ item.1.public_ip }}"
    ec2_tag_sub-host-type: "{{ sub_host_type }}"
    openshift_node_labels: "{{ node_label }}"
    logrotate_scripts: "{{ logrotate }}"
  with_together:
  - "{{ instances }}"
  - "{{ ec2.instances }}"

- name: Add new instances to nodes_to_add group if needed
  add_host:
    hostname: "{{ item.0 }}"
    ansible_ssh_host: "{{ item.1.dns_name }}"
    ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
    ansible_become: "{{ deployment_vars[deployment_type].become }}"
    groups: nodes_to_add
    ec2_private_ip_address: "{{ item.1.private_ip }}"
    ec2_ip_address: "{{ item.1.public_ip }}"
    openshift_node_labels: "{{ node_label }}"
    logrotate_scripts: "{{ logrotate }}"
  with_together:
  - "{{ instances }}"
  - "{{ ec2.instances }}"
  when: oo_extend_env is defined and oo_extend_env | bool

- name: Wait for ssh
  wait_for: "port=22 host={{ item.dns_name }}"
  with_items: "{{ ec2.instances }}"

- name: Wait for user setup
  command: "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null {{ hostvars[item.0].ansible_ssh_user }}@{{ item.1.dns_name }} echo {{ hostvars[item.0].ansible_ssh_user }} user is setup"
  register: result
  until: result.rc == 0
  retries: 20
  delay: 10
  with_together:
  - "{{ instances }}"
  - "{{ ec2.instances }}"
