# Copyright 2017-present Open Networking Foundation
#
# 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: Ansible repository is available
  apt_repository:
    repo: 'ppa:ansible/ansible'
  tags: [installer]

- name: Ansible is present
  apt:
    name: ansible
    state: latest
    force: yes
  tags: [installer]

- name: Installer files and directories are copied
  synchronize:
    src: "{{ cord_home }}/incubator/voltha/{{ item }}"
    dest: /home/vinstall
    archive: no
    owner: no
    perms: no
    recursive: yes
    links: yes
  with_items:
    - install/installer.sh
    - install/install.cfg
    - install/voltha-swarm-start.sh
    - install/voltha-swarm-stop.sh
    - install/get-logs.sh
    - install/get-host-logs.sh
    - install/sort_packages.sh
    - install/sort_packages.py
    - install/install_packages.sh
    - install/BashLoginTarget.sh
    - install/ansible
    - compose
    - k8s 
    - consul_config/basic.json
  tags: [installer]

- name: Determine if test mode is active
  become: false
  local_action: stat path="{{ cord_home }}/incubator/voltha/install/.test"
  register: file
  ignore_errors: true

- name: Test mode file is copied
  copy:
    src: "{{ cord_home }}/incubator/voltha/install/.test"
    dest: /home/vinstall
  when: file.stat.exists

- name: The installer scripts are made executable
  file:
    path: "/home/vinstall/{{ item }}"
    mode: 0744
  with_items:
    - installer.sh
    - sort_packages.sh
    - sort_packages.py
    - install_packages.sh
  tags: [installer]

- name: Python docker-py {{ docker_py_version }} package source is available
  command: pip download -d /home/vinstall/docker-py "docker-py=={{ docker_py_version }}"
  tags: [installer]

- name: Python netifaces {{ netifaces_version }} package source is available
  command: pip download -d /home/vinstall/netifaces "netifaces=={{ netifaces_version }}"
  tags: [installer]

- name: Deb file directory doesn't exist
  file:
    path: /home/vinstall/deb_files
    state: absent
  tags: [installer]

- name: Deb files are saved.
  command: cp -r /var/cache/apt/archives /home/vinstall
  tags: [installer]

- name: Deb file directory is renamed
  command: mv /home/vinstall/archives /home/vinstall/deb_files
  tags: [installer]

- name: Installer directories are owned by vinstall
  file:
    path: /home/vinstall/{{ item }}
    owner: vinstall
    group: vinstall
    recurse: yes
    follow: no
  with_items:
    - ansible
    - compose
    - .ansible
    - deb_files
    - docker-py
    - netifaces
  tags: [installer]

- name: Installer files are owned by vinstall
  file:
    path: /home/vinstall/{{ item }}
    owner: vinstall
    group: vinstall
    follow: no
  with_items:
    - installer.sh
    - install.cfg
    - voltha-swarm-start.sh
    - voltha-swarm-stop.sh
    - get-logs.sh
    - get-host-logs.sh
    - docker-compose-Linux-x86_64
  tags: [installer]
