---
- hosts: all
  vars:
    lemmy_docker_image: "lemmy:dev"

  # Install python if required
  # https://www.josharcher.uk/code/ansible-python-connection-failure-ubuntu-server-1604/
  gather_facts: False
  pre_tasks:
    - name: install python for Ansible
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-setuptools)
      args:
        executable: /bin/bash
      register: output
      changed_when: output.stdout != ""
    - setup: # gather facts

  tasks:
  - name: install dependencies
    apt:
      pkg: ['nginx', 'docker-compose', 'docker.io', 'certbot', 'python-certbot-nginx']

  - name: request initial letsencrypt certificate
    command: certbot certonly --nginx --agree-tos -d '{{ domain }}' -m '{{ letsencrypt_contact_email }}'
    args:
      creates: '/etc/letsencrypt/live/{{domain}}/privkey.pem'

  - name: create lemmy folder
    file: path={{item.path}} state=directory
    with_items:
      - { path: '/lemmy/' }
      - { path: '/lemmy/volumes/' }

  - block:
    - name:  add template files
      template: src={{item.src}} dest={{item.dest}} mode={{item.mode}}
      with_items:
        - { src: 'templates/docker-compose.yml', dest: '/lemmy/docker-compose.yml', mode: '0600' }
        - { src: 'templates/nginx.conf', dest: '/etc/nginx/sites-enabled/lemmy.conf', mode: '0644' }
        - { src: '../docker/iframely.config.local.js', dest: '/lemmy/iframely.config.local.js', mode: '0600' }

    - name:  add config file (only during initial setup)
      template: src='templates/config.hjson' dest='/lemmy/lemmy.hjson' mode='0600' force='no' owner='1000' group='1000'
    vars:
      postgres_password: "{{ lookup('password', 'passwords/{{ inventory_hostname }}/postgres chars=ascii_letters,digits') }}"
      jwt_password: "{{ lookup('password', 'passwords/{{ inventory_hostname }}/jwt chars=ascii_letters,digits') }}"

  - name: build the dev docker image
    local_action: shell cd .. && sudo docker build . -f docker/dev/Dockerfile -t lemmy:dev
    register: image_build

  - name: find hash of the new docker image
    set_fact:
      image_hash: "{{ image_build.stdout | regex_search('(?<=Successfully built )[0-9a-f]{12}') }}"

  # this does not use become so that the output file is written as non-root user and is easy to delete later
  - name: save dev docker image to file
    local_action: shell sudo docker save lemmy:dev > lemmy-dev.tar

  - name: copy dev docker image to server
    copy: src=lemmy-dev.tar dest=/lemmy/lemmy-dev.tar

  - name: import docker image
    docker_image:
      name: lemmy
      tag: dev
      load_path: /lemmy/lemmy-dev.tar
      source: load
      force_source: yes
    register: image_import

  - name: delete remote image file
    file: path=/lemmy/lemmy-dev.tar state=absent

  - name: delete local image file
    local_action: file path=lemmy-dev.tar state=absent

  - name: enable and start docker service
    systemd:
      name: docker
      enabled: yes
      state: started

  # cant pull here because that fails due to lemmy:dev (without dessalines/) not being on docker hub, but that shouldnt
  # be a problem for testing
  - name: start docker-compose
    docker_compose:
      project_src: /lemmy/
      state: present
      recreate: always
    ignore_errors: yes

  - name: reload nginx with new config
    shell: nginx -s reload

  - name: certbot renewal cronjob
    cron:
      special_time=daily
      name=certbot-renew-lemmy
      user=root
      job="certbot certonly --nginx -d '{{ domain }}' --deploy-hook 'nginx -s reload'"
