---
- name: Dependencies installed
  # We install xmlsec and cairocffi using pkg because python-Levenshtein is tricky to install on FreeBSD
  community.general.pkgng:
    name: git,py38-pip,fish,direnv,jpeg-turbo,tiff,webp,lcms2,freetype2,syncthing,yarn-node14,cairo,pango,gmake,libxml2,libxslt,postgresql12-client,py38-xmlsec,py38-cairocffi,py38-sqlite3,bash,py38-libcst
    state: latest

- name: Root shell is fish
  ansible.builtin.user: name=root shell=/usr/local/bin/fish

- name: Tmux config installed
  copy:
    src: tmux.conf
    dest: /root/.tmux.conf
    mode: 0644

- name: Github SSH key installed
  template:
    src: sshkey.j2
    dest: /root/.ssh/id_ed25519
    mode: 0400

- name: Nginx location file installed
  copy:
    src: nginx-location.conf
    dest: /usr/local/etc/nginx/locations/fifteen5.conf
    mode: 0644
  notify: Nginx restarted

- name: Source folder exists
  file:
    path: /usr/local/src
    mode: 0755
    state: directory

- name: Create python bin link for yarn
  file:
    src: /usr/local/bin/python3.8
    dest: /usr/bin/python
    owner: root
    state: link

- name: 15Five source deployed
  git:
    repo: "git@github.com:15five/fifteen5.git"
    dest: /usr/local/src/fifteen5
    accept_hostkey: yes
    update: yes
  ignore_errors: true
  tags:
    - deploy

- name: Syncthing ignore file installed
  copy:
    src: stignore
    dest: /usr/local/src/fifteen5/.stignore
    mode: 0775

- name: Syncthing running as root
  shell:
    cmd: 'sed -i -e ''/^: ${syncthing_user/s/"syncthing"/"root"/'' /usr/local/etc/rc.d/syncthing'
    warn: false
  notify: Syncthing restarted

- name: Environment file installed
  template:
    src: envrc.j2
    dest: /usr/local/src/fifteen5/.envrc
    mode: 0600
  tags:
    - deploy

- name: Allow environment file
  shell:
    cmd: direnv allow
    chdir: /usr/local/src/fifteen5
  tags:
    - deploy

- name: Hack patch for clearbit bullshit
  shell:
    cmd: 'sed -i -e "s/clearbit/#clearbit/g" /usr/local/src/fifteen5/requirements/common.txt'
    warn: false

- name: 15Five requirements up to date
  pip:
    requirements: /usr/local/src/fifteen5/requirements/test.pip
    executable: pip
  tags:
    - deploy

- name: 15Five personal reqs up to date
  pip:
    name:
      - asciitree
      - colorlog
      - logging_tree
      - django-pdb
      - pgcli
      - ipython
      - git+git://github.com/sureapp/sqlformatter
      - python-json-logger
      - git+git://github.com/zensourcer/clearbit-python.git#egg=clearbit
    executable: pip
  tags:
    - deploy

- name: 15Five migrations up to date
  command: bash -lc "direnv exec /usr/local/src/fifteen5 /usr/local/bin/python3.8 /usr/local/src/fifteen5/manage.py migrate"
  tags:
    - deploy

- name: 15Five BE supervisor file installed
  copy:
    src: fifteen5-supervisor.conf
    dest: /usr/local/etc/supervisor/conf.d/uvicorn.conf
    owner: root
    mode: 0644
  notify: Supervisor restarted

- name: 15Five FE supervisor file installed
  copy:
    src: fifteen5-yarn-supervisor.conf
    dest: /usr/local/etc/supervisor/conf.d/yarn.conf
    owner: root
    mode: 0644
  notify: Supervisor restarted

- name: 15Five celery supervisor file installed
  copy:
    src: celery-supervisor.conf
    dest: /usr/local/etc/supervisor/conf.d/celery.conf
    owner: root
    mode: 0644
  notify: Supervisor restarted

- name: 15Five frontend files generated and copied
  command: bash -lc "direnv exec /usr/local/src/fifteen5 /usr/local/bin/yarn"
  args:
    chdir: "/usr/local/src/fifteen5"
  tags:
    - deploy

- name: 15Five frontend files generated
  command: bash -lc "direnv exec /usr/local/src/fifteen5 /usr/local/bin/python3.8 manage.py collectstatic --noinput && direnv exec /usr/local/src/fifteen5 /usr/local/bin/python3.8 manage.py collectstatic_js_reverse"
  args:
    chdir: "/usr/local/src/fifteen5"
  tags:
    - deploy

- name: 15Five reset database
  command: bash -lc "RESET_DB=True direnv exec /usr/local/src/fifteen5 /usr/local/bin/python3.8 /usr/local/src/fifteen5/manage.py run_local_scripts && direnv exec /usr/local/src/fifteen5 /usr/local/bin/python3.8 /usr/local/src/fifteen5/manage.py clearsessions"
  tags:
    - db-refresh
    - never
