# Copyright (c) 2018 Intel Corporation.
#
# 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.
---
# This script is based on https://docs.openstack.org/kolla-ansible/pike/user/quickstart.html
- name: Include variables
  include_vars:
    file: "{{ rs_file }}"
    name: infra_deploy_vars

- set_fact:
    traffic_ip: "{{ item.interfaces[1].ip }}"
  when: item.hostname == ansible_host
  with_items: "{{ infra_deploy_vars.nodes }}"

- name: Get neutron iface
  set_fact:
    neutron_iface: "{{ item }}"
  when:
    - hostvars[ansible_host]['ansible_' + item.replace('-', '_')].ipv4 is defined
    - hostvars[ansible_host]['ansible_' + item.replace('-', '_')].ipv4.address is defined
    - hostvars[ansible_host]['ansible_' + item.replace('-', '_')].ipv4.address == traffic_ip
  with_items: "{{ hostvars[ansible_host].ansible_interfaces }}"

- name: Create a registry container
  docker_container:
    name: registry
    image: registry:2
    restart_policy: always
    ports:
    - "4000:5000"

- name: Download and install Kolla
  include_tasks: install_kolla.yml

- name: Configure Kolla
  include_tasks: configure_kolla.yml

- name: Configure Open Stack
  include_tasks: configure_openstack.yml

- name: Ramp up Open Stack
  include_tasks: rampup_openstack.yml

- name: Update admin-openrc.sh
  lineinfile:
    path: /etc/kolla/admin-openrc.sh
    regexp: "{{ item.find }}"
    line: "{{ item.add }}"
  with_items:
   - { find: 'EXTERNAL_NETWORK', add: 'export EXTERNAL_NETWORK=public' }
   - { find: 'OS_AUTH_TYPE', add: 'export OS_AUTH_TYPE=password' }

- name: Copy env file
  shell: cp /etc/kolla/admin-openrc.sh /tmp/admin-openrc.yaml

- name: Rework as env vars
  replace:
    path: /tmp/admin-openrc.yaml
    regexp: 'export\s+(.*)=(.*)'
    replace: '\1: \2'

- name: Download OpenStack env file
  fetch:
    src: /tmp/admin-openrc.yaml
    dest: /tmp/
    flat: yes

- include_vars:
    file: /tmp/admin-openrc.yaml
    name: ostack_env

- name: Re-assign IP address
  shell: ip address show {{ neutron_iface }} | awk '/inet/ {print $2}'
  when: neutron_iface is defined
  register: ip_netmask

- shell: >
    ip addr del dev {{ neutron_iface }} {{ ip_netmask.stdout }} &&
    ip addr add dev br-ex {{ infra_deploy_vars.networks[1].host_ip }}/{{ ip_netmask.stdout_lines[0].split('/')[1] }}
  when:
    - neutron_iface is defined
    - ip_netmask.stdout | length > 0

- name: Create external network
  os_network:
    name: public
    external: yes
    provider_physical_network: physnet1
    provider_network_type: flat
  environment:
    - no_proxy: "{{ lookup('env', 'no_proxy') + ',' + ansible_host + ',' + hostvars[ansible_host].ansible_default_ipv4.address }}"
    - "{{ ostack_env }}"

- name: Create sub-network
  os_subnet:
    name: public-subnet
    network_name: public
    cidr: "{{ ip_netmask.stdout }}"
    allocation_pool_start: "{{ infra_deploy_vars.networks[1].dhcp_ip_start }}"
    allocation_pool_end: "{{ infra_deploy_vars.networks[1].dhcp_ip_stop }}"
    gateway_ip: "{{ infra_deploy_vars.networks[1].host_ip }}"
    enable_dhcp: no
  environment:
    - no_proxy: "{{ lookup('env', 'no_proxy') + ',' + ansible_host + ',' + hostvars[ansible_host].ansible_default_ipv4.address }}"
    - "{{ ostack_env }}"

- name: Upload OpenStack env file to Yardstick VM
  copy:
    src: /etc/kolla/admin-openrc.sh
    dest: '/tmp/admin-openrc.sh'
    delegate_to: "{{ item }}"
  when: "groups['yardstickG'] is defined"
  with_items:
    - "{{ groups['yardstickG'] }}"