# 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' {{ host_mapping_file_path }} > {{ temp_host_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_host_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_host_mapping_file }} {{ role_path }}/files/backup_host_mapping_file.csv| tr -d \>|tr -d \<| grep -E -- ', & :| '
      register: diff_output
      changed_when: false
      failed_when: false

    - name: Status of new nodes
      set_fact:
        new_node_status: true
      when: diff_output.stdout
  rescue:
    - name: No new nodes
      debug:
        msg: "No new nodes to add"
        verbosity: 2
  when: backup_map_status

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

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

- name: Create a backup file
  copy:
    src: "{{ temp_host_mapping_file }}"
    dest: "{{ role_path }}/files/backup_host_mapping_file.csv"
    mode: 0644

- name: Get cobbler pod name
  command: 'kubectl get pod -n cobbler -l app=cobbler -o jsonpath="{.items[0].metadata.name}"'
  changed_when: false
  register: cobbler_pod_name
  when: cobbler_container_status
  tags: install

- name: Copy the dhcp.template inside container
  command: 'kubectl exec --stdin --tty -n cobbler {{ cobbler_pod_name.stdout }} \
    -- cp /root/omnia/control_plane/roles/provision_cobbler/files/dhcp.template /etc/cobbler/dhcp.template'
  when:  ( cobbler_container_status ) and ( new_node_status )

- name: Cobbler sync for adding new nodes
  command: 'kubectl exec --stdin --tty -n cobbler {{ cobbler_pod_name.stdout }} -- cobbler sync'
  when:  ( cobbler_container_status ) and ( new_node_status )

- name: Restart dhcpd
  command: 'kubectl exec --stdin --tty -n cobbler {{ cobbler_pod_name.stdout }} -- systemctl restart dhcpd'
  when:  ( cobbler_container_status ) and ( new_node_status )