#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
- name: Update packages
  apt:
    update_cache: yes

- name: Remove ufw
  apt:
    name: ufw
    state: absent
    update_cache: yes

- name: Upgrade packages
  apt:
    update_cache: yes
    upgrade: yes

- name: Install common packages
  apt:
    name: [ 'cloud-init', 'ntp', 'unzip', 'safe-rm', 'net-tools', 'supervisor', 'emacs-nox', 'screen', 'xtail', 'fail2ban' ]
    state: present
    update_cache: yes

- name: Install screenrc file
  copy:
    src: dot-screenrc
    dest: /root/.screenrc

- name: Install packer key as only authorized key
  copy:
    src: packer_rsa
    dest: /root/.ssh/authorized_keys
    owner: root
    group: root
    mode: 0600

- name: Install supervisor ulimit file
  copy:
    src: supervisor_ulimit.conf
    dest: /etc/supervisor/conf.d/ulimit.conf
    owner: root
    group: root
    mode: 0644

- name: Install ensure_file_exists.sh
  copy:
    src: ensure_file_exists.sh
    dest: /usr/local/bin/ensure_file_exists.sh
    owner: root
    group: root
    mode: 0500

- name: Start common services
  service:
    name: '{{ item }}'
    state: restarted
  with_items:
    - fail2ban

- name: Create bubble-log group
  group:
    name: bubble-log

- name: Add root user to newly created group
  user:
    name: root
    groups: bubble-log
    append: yes

- name: Create bubble user
  user:
    name: bubble
    comment: bubble user
    shell: /bin/bash
    system: yes
    home: /home/bubble
    groups: bubble-log

- name: Creates bubble logs dir
  file:
    path: /var/log/bubble
    owner: bubble
    group: bubble-log
    mode: 0770
    state: directory

- name: Create bubble flexrouting user
  user:
    name: bubble-flex
    comment: bubble flexrouting user
    shell: /bin/false
    system: yes
    home: /home/bubble-flex
  when: install_type == 'node'
