---

- name: install git
  pacman: name=git,python2,python2-virtualenv,uwsgi-plugin-python2 state=present

- name: make archweb user
  user: name=archweb shell=/bin/false home="{{ archweb_dir }}" createhome=no groups=uwsgi

- name: fix home permissions
  file: state=directory owner=archweb group=archweb path="{{ archweb_dir }}"

- name: set up nginx
  template: src=nginx.d.conf.j2 dest=/etc/nginx/nginx.d/archweb.conf owner=root group=root mode=644
  notify:
    - restart nginx

- name: make nginx log dir
  file: path=/var/log/nginx/{{ archweb_domain }} state=directory owner=http group=log mode=755

- name: clone archweb repo
  git: repo=git://git.archlinux.org/archweb.git dest="{{ archweb_dir }}"
  become: true
  become_user: archweb

- name: make virtualenv
  command: virtualenv2 "{{ archweb_dir }}"/env creates="{{ archweb_dir }}/env/bin/python"
  become: true
  become_user: archweb

- name: install stuff into virtualenv
  pip: requirements="{{ archweb_dir }}/requirements_prod.txt" virtualenv="{{ archweb_dir }}/env"
  become: true
  become_user: archweb

- name: create media dir
  file: state=directory owner=archweb group=archweb path="{{ archweb_dir }}/media"

- name: fix home permissions
  file: state=directory owner=archweb group=archweb path="{{ archweb_dir }}"

- name: copy archweb services
  copy: src="{{ item }}" dest="/etc/systemd/system/{{ item }}" owner=root group=root mode=0644
  with_items:
    - archweb-reporead.service
    - archweb-mirrorcheck.service
    - archweb-mirrorcheck.timer
  notify:
    - daemon reload
  when: services

- name: copy archweb memcached service
  copy: src="archweb-memcached.service" dest="/etc/systemd/system/archweb-memcached.service" owner=root group=root mode=0644
  notify:
    - daemon reload
  when: not services

- name: configure archweb
  template: src=local_settings.py.j2 dest=/srv/http/archweb/local_settings.py owner=archweb group=archweb mode=0660

- name: deploy archweb
  template: src=archweb.ini.j2 dest=/etc/uwsgi/vassals/archweb.ini owner=archweb group=http mode=0644
  when: not services

- name: start and enable archweb memcached service
  service: name="archweb-memcached.service" enabled=yes state=started
  when: not services

- name: start and enable archweb services
  service: name="{{ item }}" enabled=yes state=started
  with_items:
    - archweb-reporead.service
    - archweb-mirrorcheck.timer
  when: services