- name: Install Dependencies
  apt: pkg={{ item }}
  with_items:
    - build-essential
    - couchdb
    - git
    - libxml2-dev
    - libxslt-dev
    - python
    - python-dev
    - python-imaging
    - python-imaging
    - python-pip
    - python-pycurl
    - python-setuptools
    - python-lxml
    - supervisor
  tags:
    - dependencies

- name: Install Newebe
  pip: name='git+https://github.com/gelnior/newebe.git#egg=newebe'

- name: Add group Newebe
  group: name=newebe

- name: Add user Newebe
  user: name=newebe groups=newebe shell=/usr/sbin/nologin

- name: Create Newebe Config folder
  file: path=/usr/local/etc/newebe/
        owner=newebe
        group=newebe
        state=directory

- name: Create Newebe folder
  file: path=/usr/local/var/newebe/
        owner=newebe
        group=newebe
        state=directory

- name: Create Newebe log folder
  file: path=/usr/local/var/log/newebe/
        owner=newebe
        group=newebe
        state=directory

- name: Set Newebe config file
  template: src=usr_local_etc_newebe_config.j2
            dest=/usr/local/etc/newebe/config.yaml
            owner=newebe
            group=newebe

- name: Set Supervisor config file
  copy: src=newebe.conf dest=/etc/supervisor/conf.d/newebe.conf

- name: Set Newebe Supervisor config file
  copy: src=supervisor.conf dest=/etc/supervisor/supervisor.conf
  notify: restart supervisor

- name: Ensure Supervisor is running
  service: name=supervisor state=running

- name: Ensure that newebe is started
  supervisorctl: name=newebe state=started

- name: Add mod_proxy module to Apache
  apache2_module: state=present name=proxy

- name: Add proxy_http module to Apache
  apache2_module: state=present name=proxy_http

- name: Rename existing Apache newebe virtualhost
  command: mv /etc/apache2/sites-available/newebe /etc/apache2/sites-available/newebe.conf removes=/etc/apache2/sites-available/newebe

- name: Remove old sites-enabled/newebe symlink (new one will be created by a2ensite)
  file: path=/etc/apache2/sites-enabled/newebe state=absent

- name: Configure the Apache HTTP server for Newebe
  template: src=etc_apache2_sites-available_newebe.j2
            dest=/etc/apache2/sites-available/newebe.conf
            group=root
            owner=root

- name: Enable the site
  command: a2ensite newebe.conf creates=/etc/apache2/sites-enabled/newebe.conf
  notify: restart apache