# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# 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.
# limitations under the License.
---

- name: Remove blank lines
  shell:  set -o pipefail && awk -F, 'length>NF+1' {{ mngmnt_mapping_file_path }} > {{ temp_mgmt_mapping_file }}
  changed_when: false
  tags: install

- name: Remove blank spaces
  shell:  set -o pipefail && sed -i.bak -E 's/(^|,)[[:blank:]]+/\1/g; s/[[:blank:]]+(,|$)/\1/g'  {{ temp_mgmt_mapping_file }}
  args:
    warn: no
  changed_when: false
  tags: install

- name: Compare the file for new nodes
  block:
    - name: Check difference
      shell: set -o pipefail && diff {{ temp_mgmt_mapping_file }} {{ role_path }}/files/backup_mngmnt_mapping_file.csv| tr -d \>|tr -d \<| grep -E -- ', & :| '
      register: diff_mngmnt_output
      changed_when: false
      failed_when: false

    - name: Status of new nodes
      set_fact:
        new_mngmnt_node_status: true
      when: diff_mngmnt_output.stdout
  rescue:
    - name: No new nodes
      debug:
        msg: "No new nodes to add"
        verbosity: 2
  when: backup_mngmnt_map_status

- name: Fetch inputs from mapping file
  command: awk 'NR > 1 { print }' {{ temp_mgmt_mapping_file }}
  changed_when: false
  register: fetch_mgmt_mapping_file

- name: Fetch input
  blockinfile:
    path: "{{ role_path }}/files/dhcpd.conf"
    insertafter: '^#insert the static DHCP leases for configuration here'
    block: |
      host {{ item.split(',')[1] }} {
        hardware ethernet {{ item.split(',')[0] }};
        fixed-address {{ item.split(',')[1] }};
      }
    marker: "# {mark} DHCP BLOCK OF {{ item.split(',')[0] }}"
  when: (not mngmnt_network_container_image_status) or (new_mngmnt_node_status)
  failed_when: false
  with_items: "{{ fetch_mgmt_mapping_file.stdout_lines }}"
  tags: install

- name: Create a backup file
  copy:
    src: "{{ temp_mgmt_mapping_file }}"
    dest: "{{ role_path }}/files/backup_mngmnt_mapping_file.csv"

- name: Get mngmnt container pod name
  command: 'kubectl get pod -n network-config -l app=mngmnt-network -o jsonpath="{.items[0].metadata.name}"'
  changed_when: false
  register: mngmnt_pod_name
  when:  mngmnt_network_container_status
  tags: install

- name: Copy the dhcp.template inside container
  command: 'kubectl exec --stdin --tty -n network-config {{ mngmnt_pod_name.stdout }} \
    -- cp /root/omnia/control_plane/roles/control_plane_device/files/dhcp.template /etc/dhcpd/dhcpd.conf'
  when:  (  mngmnt_network_container_status ) and ( new_mngmnt_node_status  )

- name: Restart dhcpd
  command: 'kubectl exec --stdin --tty -n network-config {{ mngmnt_pod_name.stdout }} -- systemctl restart dhcpd'
  when:  (  mngmnt_network_container_status ) and ( new_mngmnt_node_status )