---
# Copyright 2016, Rackspace US, Inc.
# Copyright 2017, SUSE LINUX GmbH.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Clone the scenario requirements
  hosts: localhost
  connection: local
  gather_facts: true
  vars_files:
    - ../var/opnfv.yml
  tasks:
    - name: Remove existing scenario directories
      file:
        path: "{{ item[1] }}/{{ item[0].scenario }}"
        state: absent
      with_nested:
        - "{{ scenarios }}"
        - [ "{{ scenario_path_default }}", "{{ role_path_default }}" ]
      loop_control:
        label: "{{ item[0].scenario }}"

    - name: Update scenarios with local overrides
      set_fact:
          scenarios: >
            {%- for z in xci_scenarios_overrides -%}
              {%- for x in scenarios if x.scenario == z.scenario  -%}
                {%- set _ = x.update(z) -%}
              {%- endfor -%}
            {%- endfor -%}
            {{- scenarios -}}
      with_items: "{{ xci_scenarios_overrides }}"
      loop_control:
        label: "{{ item.scenario }}"
      when: xci_scenarios_overrides is defined

    - name: Collect list of known scenarions
      set_fact:
        known_scenarios: >
          {%- set scenario_names = [] -%}
          {%- for x in scenarios -%}
            {%- set _ = scenario_names.append(x.scenario) -%}
          {%- endfor -%}
          {{- scenario_names -}}
      with_items: "{{ scenarios }}"
      loop_control:
        label: "{{ item.scenario }}"

    - name: Fail if 'DEPLOY_SCENARIO' is not defined
      fail:
        msg: "DEPLOY_SCENARIO env variable is not defined so no scenario can be deployed"
      when: deploy_scenario is not defined

    - name: Ensure {{ deploy_scenario }} is a known XCI scenario
      fail:
        msg: "{{ deploy_scenario }} does not exist"
      when: deploy_scenario not in known_scenarios

    - name: Collect scenario information
      set_fact:
        xci_scenario: >
          {%- set xci_scenario = {} -%}
          {%- for x in scenarios if x.scenario == deploy_scenario -%}
          {%-   for z in x.installers if z.installer == installer_type -%}
          {%-     set _ = xci_scenario.update({'flavors': z.flavors}) -%}
          {%-     set _ = xci_scenario.update({'distros': z.distros}) -%}
          {%-   endfor -%}
          {%- set _ = xci_scenario.update({'role': x.role | basename}) -%}
          {%- endfor -%}
          {{ xci_scenario }}

    - name: Ensure local facts directory exists
      file:
        path: "/etc/ansible/facts.d"
        state: directory
      become: true

    - name: Record scenario information
      ini_file:
        create: yes
        section: scenarios
        state: present
        option: role
        value:  "{{ xci_scenario.role | basename }}"
        path: "/etc/ansible/facts.d/xci.fact"
      become: true

    - name: Fail if {{ deploy_scenario }} is not supported
      fail:
        msg:
          - ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          - ERROR! The {{ deploy_scenario }} scenario can't be deployed. This is because
          - the {{ installer_type }} XCI installer or the {{ xci_flavor }} flavor or the {{ xci_distro }}
          - distribution is not supported by this scenario. It may also be possible that
          - this scenario doesn't exist at all or it's not listed in {{ scenario_file }}.
          - ''
          - This is a great chance for you to contribute to XCI ;-)
          - ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          - ''
      when:
          (xci_scenario['flavors'] is defined and xci_flavor not in xci_scenario['flavors']) or
          (xci_scenario['distros'] is defined and xci_distro not in xci_scenario['distros'])

    - name: Clone git repos
      git:
        repo: "{{ item.src }}"
        dest: "{{ scenario_path_default }}/{{ item.scenario | default(item.src | basename) }}"
        version: "{{ item.version | default('master') }}"
        refspec: "{{ item.refspec | default(omit) }}"
        update: true
        force: true
      with_items: "{{ scenarios }}"
      register: git_clone
      until: git_clone | success
      retries: "{{ git_clone_retries }}"
      delay: "{{ git_clone_retry_delay }}"
      loop_control:
        label: "{{ item.scenario }}"

    - name: Plug in the scenario Ansible roles to XCI
      synchronize:
        src: "{{ scenario_path_default }}/{{ item.scenario }}/{{ item.role }}/"
        dest: "{{ role_path_default }}/{{ item.role | basename }}"
      with_items: "{{ scenarios }}"
      loop_control:
        label: "{{ item.scenario }}"

  vars:
    ansible_python_interpreter: "/usr/bin/python"
    scenarios: "{{ lookup('file', scenario_file) | from_yaml }}"
    scenario_file: '../opnfv-scenario-requirements.yml'
    scenario_path_default: "{{ xci_scenarios_cache }}"
    role_path_default: "{{ playbook_dir }}/roles"
    git_clone_retries: 2
    git_clone_retry_delay: 5