---
# Include this play once for each container you want to create and use as a test host.
#
# Optional parameters on the include are as follows:
# * scenario = unique name for the container to be started
# * image = name of the image to start in the container
# * command = command to run in the container
# * l_groups = host groups that the container should be added to
# * l_host_vars = any variables that should be added to the host

- name: Start container for specified test host
  gather_facts: no
  hosts: localhost
  connection: local
  tasks:

    - set_fact:
        # This is a little weird but if we use a var instead of a fact,
        # a different random value is generated for each task. See:
        # https://opensolitude.com/2015/05/27/ansible-lookups-variables-vs-facts.html
        container_name: openshift_ansible_test_{{ scenario | default(100000000000000 | random) }}

    - name: start container
      docker_container:
        name: "{{ container_name }}"
        image: "{{ lookup('env', 'IMAGE_PREFIX') | default('openshift-ansible-integration-', true) }}{{ image | default('test-target-base') }}"
        command: "{{ command | default('sleep 1800') }}"
        recreate: yes
        # NOTE: When/if we need to run containers that are docker hosts as well:
        # volumes: [ "/var/run/docker.sock:/var/run/docker.sock:z" ]

    - name: add container as host in inventory
      add_host:
        ansible_connection: docker
        name: "{{ container_name }}"
        groups: '{{ l_groups | default("masters,nodes,etcd") }}'

    # There ought to be a better way to transfer the host vars, but see:
    # https://groups.google.com/forum/#!topic/Ansible-project/Jwx8RYhqxPA
    - name: set host facts per test parameters
      set_fact:
        "{{ item.key }}": "{{ item.value }}"
      delegate_facts: True
      delegate_to: "{{ container_name }}"
      with_dict: "{{ l_host_vars | default({}) }}"

- hosts: all
  tasks:

    # run before openshift_version to prevent it breaking
    - include: preflight/playbooks/tasks/enable_repo.yml
      vars: { repo_name: "ose-3.2" }

- include: ../../../playbooks/byo/openshift-cluster/initialize_groups.yml
- include: ../../../playbooks/common/openshift-cluster/std_include.yml

- hosts: all
  tasks:

    # put it back like it was for the tests
    - include: preflight/playbooks/tasks/enable_repo.yml
      vars: { repo_name: "ose-3.2", enabled: False }
