---

- name: Install packages
  apt: name={{ item }} state=present
  with_items:
  - php5-curl
  - php5-fpm
  - php5-gd
  - php5-pgsql
  - postgresql
  - python-psycopg2

- name: Configure PostgreSQL database
  postgresql_db: name={{ owncloud_dbname }} owner={{ owncloud_dbuser }}
  become: true
  become_user: postgres

- name: Configure PostgreSQL user
  postgresql_user: db={{ owncloud_dbname }} name={{ owncloud_dbuser }} password={{ owncloud_dbpass }} priv=ALL state=present
  become: true
  become_user: postgres

- name: Ensure certificates are available
  command: openssl req -x509 -nodes -newkey rsa:2048 -keyout /etc/nginx/ssl/{{ owncloud_domain }}.key -out /etc/nginx/ssl/{{ owncloud_domain }}.crt -days 730 -subj "/CN={{ owncloud_domain }}" creates=/etc/nginx/ssl/{{ owncloud_domain }}.crt
  notify: Restart nginx

- name: Configure certificate manager for owncloud
  template: src=certs.j2 dest=/etc/acme/domains.d/{{ owncloud_domain }}.conf

- name: Create vhost directory
  file: path=/var/www/owncloud state=directory owner=www-data group=www-data

- name: Configure vhost
  template: src=vhost.j2 dest=/etc/nginx/sites-available/owncloud
  notify: Restart nginx

- name: Enable vhosts
  file: src=/etc/nginx/sites-available/owncloud dest=/etc/nginx/sites-enabled/owncloud state=link
  notify: Restart nginx

- name: Start php5-fpm
  service: name=php5-fpm state=started enabled=yes

- name: Start PostgreSQL
  service: name=postgresql state=started enabled=yes