---

# Delete ors-upgrade.yml cron which is replaced by ors.yml

  - name: Check if ors-upgrade playbook is installed
    shell: 'crontab -l | grep -q ors-upgrade'
    ignore_errors: yes
    register: ors_upgrade_cron

  - name: Delete ors-upgrade cron
    cron: name="Launch Upgrader with ansible" state=absent
    when: ors_upgrade_cron.rc == 0

  - name: Check if vifib playbook is installed
    shell: 'crontab -l | grep -A1 "#Ansible: Launch Upgrader with ansible" | grep -v ors-upgrade | grep -q "^*/10.*upgrader"'
    ignore_errors: yes
    register: vifib_cron

  - name: Install vifib-base playbook
    shell: ansible-playbook vifib-base.yml --extra-vars '{"period_minute":"*/10", "period_hour":"*"}'
    when: vifib_cron.rc != 0

  - name: Remove old cron
    cron: name="Launch ors playbook every 3 hours" state=absent

  - name: Install periodic cron
    cron: name="Launch ors playbook periodically" minute="*/10" hour="*" job='sleep 10 && flock -w 300 /opt/upgrader/ansible.lock -c "cd /opt/upgrader/playbook && PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ansible-playbook ors.yml -i hosts 2>> /opt/upgrader/latest_ors.log >> /opt/upgrader/latest_ors.log"'

  - name: Install reboot cron
    cron: name="Launch ors playbook at reboot" special_time=reboot job='sleep 10 && flock -w 300 /opt/upgrader/ansible.lock -c "cd /opt/upgrader/playbook && PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ansible-playbook ors.yml -i hosts 2>> /opt/upgrader/startup_ors.log >> /opt/upgrader/startup_ors.log"'

  - name: Install rm-tmp-lte script
    copy: src=rm-tmp-lte dest={{ install_folder }} owner=root mode=770

  - name: Give permission to slapuser to remove tmp files owned by other slapuser
    copy: src=slapuser-rm-tmp dest=/etc/sudoers.d owner=root mode=440

  - name: Give permission to slapuser to launch init-sdr script
    copy: src=slapuser-init-sdr dest=/etc/sudoers.d owner=root mode=440

  - name: Give permission to slapuser to launch init-enb script
    copy: src=slapuser-init-enb dest=/etc/sudoers.d owner=root mode=440

  - name: Check if /opt/amarisoft exists
    stat: path=/opt/amarisoft
    register: opt_amarisoft

  - name: Copy init SDR script
    copy: src=init-sdr dest=/opt/amarisoft owner=root mode=770
    when: opt_amarisoft.stat.exists

  - name: Copy init eNB script
    copy: src=init-enb dest=/opt/amarisoft owner=root mode=770
    when: opt_amarisoft.stat.exists

  - name: Init SDR
    script: "/opt/amarisoft/init-sdr"
    when: opt_amarisoft.stat.exists

  - name: Init eNB
    script: "/opt/amarisoft/init-enb"
    when: opt_amarisoft.stat.exists

  - name: Check if Service Exists
    stat: path=/etc/init.d/init-sdr
    register: init_sdr_service

  - name: Reload daemon
    systemd: daemon_reload=yes
    when: init_sdr_service.stat.exists

  - name: Disable old init-sdr service
    systemd: name=init-sdr enabled=no
    when: init_sdr_service.stat.exists

  - name: Get Amarisoft path
    shell: 'find /opt/amarisoft -maxdepth 1 | grep "^/opt/amarisoft/v[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$" | sort | tail -n1'
    register: amarisoft_path
    when: opt_amarisoft.stat.exists

  - name: Check if lteenb has capabilities
    shell: 'getcap {{ amarisoft_path.stdout }}/enb/lteenb | grep -q cap_sys_nice'
    ignore_errors: yes
    register: lteenb_cap

  - name: Set capabilities on lteenb
    shell: 'patchelf --set-rpath {{ amarisoft_path.stdout }}/enb {{ amarisoft_path.stdout }}/enb/lteenb && setcap cap_sys_nice=+pe {{ amarisoft_path.stdout }}/enb/lteenb'
    when: lteenb_cap.rc != 0

  - name: Check if lteenb-avx2 has capabilities
    shell: 'getcap {{ amarisoft_path.stdout }}/enb/lteenb-avx2 | grep -q cap_sys_nice'
    ignore_errors: yes
    register: lteenb_avx2_cap

  - name: Set capabilities on lteenb-avx2
    shell: 'patchelf --set-rpath {{ amarisoft_path.stdout }}/enb {{ amarisoft_path.stdout }}/enb/lteenb-avx2 && setcap cap_sys_nice=+pe {{ amarisoft_path.stdout }}/enb/lteenb-avx2'
    when: lteenb_avx2_cap.rc != 0

  - name: Configure re6st
    script: configure-re6st

  - name: Configure /etc/network/interfaces
    copy: src=interfaces dest=/etc/network/interfaces owner=root mode=644

  - name: Configure dhcp timeout
    lineinfile: dest=/etc/dhcp/dhclient.conf regexp="^timeout (.*)" line="timeout 15" state=present

  - name: Configure journald log size
    lineinfile: dest=/etc/systemd/journald.conf regexp="^SystemMaxUse=(.*)" line="SystemMaxUse=1G" state=present

  - name: Configure ssh
    lineinfile: dest=/etc/ssh/sshd_config regexp="^PermitRootLogin (.*)" line="PermitRootLogin yes" state=present

  - name: Configure ssh
    lineinfile: dest=/etc/ssh/sshd_config regexp="^PasswordAuthentication (.*)" line="PasswordAuthentication yes" state=present

  - name: Configure slapos
    script: configure-slapos.py

  - name: Create .amarisoft directory for SR
    file: path=/opt/amarisoft/.amarisoft state=directory

  - name: Copy keys for SR
    copy: src=/root/.amarisoft dest=/opt/amarisoft owner=root mode=644

  - name: Add kernel parameter
    script: configure-grub
