<commit_before>---
- name: install system packages
  apt: pkg={{item}} update_cache=yes
  with_items:
      - build-essential
      - git
      - mercurial
      - python-pycurl
      - python-boto
      - libpq-dev
      - unzip
      - gdal-bin
      - python
      - python-dev
      - virtualenv
      - uwsgi
      - nginx
      - uwsgi-plugin-python
      - libxslt-dev
      - letsencrypt
- name: make project dir
  file: path=/home/openstates state=directory
- name: add project user
  user: name=openstates home=/home/openstates shell=/bin/bash state=present
- name: chown user directory
  file: path=/home/openstates owner=openstates
- name: add user subdirectories
  file: path=/home/openstates/{{item}} owner=openstates state=directory
  with_items:
    - data
    - letsencrypt
  sudo_user: "openstates"
- name: add env_vars for project user
  template: src=env_vars.j2 dest=/home/openstates/env_vars mode=640
  sudo_user: "openstates"

# letsencrypt
- name: add letsencrypt renewal crontab
  cron: job="letsencrypt renew" special_time="daily"

# virtualenv
- name: create virtualenv
  command: virtualenv -p python2.7 /home/openstates/virt{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: checkout project directories
  git: repo=https://github.com/openstates/openstates.org.git dest=/home/openstates/src/openstates.org accept_hostkey=yes version=master
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: add checkouts to python path
  lineinfile: dest=/home/openstates/virt{{ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}/lib/python2.7/site-packages/checkouts.pth create=yes state=present line=/home/openstates/src/openstates.org
  sudo_user: "openstates"
- name: install requirements
  pip: requirements=/home/openstates/src/openstates.org/requirements.txt virtualenv=/home/openstates/virt{{ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: link virtualenv
  file: src=/home/openstates/virt{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }} dest=/home/openstates/virt state=link

# nginx
- name: make certs dir
  file: path=/etc/nginx/certs/ state=directory
- name: generate dh params
  command: openssl dhparam -out /etc/nginx/certs/dhparams.pem 2048 creates=/etc/nginx/certs/dhparams.pem
- name: remove nginx default config
  file: path=/etc/nginx/sites-enabled/default state=absent
- name: copy over htpasswd
  copy: src=../secrets/ansible/htpasswd dest=/home/openstates/htpasswd
- name: write nginx template
  template: src=nginx.j2 dest=/etc/nginx/sites-enabled/openstates
  notify:
    - restart nginx
- name: ensure nginx is running and starts on boot
  service: name=nginx state=restarted enabled=yes

# new relic
- name: generate new relic config
  command: /home/openstates/virt/bin/newrelic-admin generate-config {{ newrelic_key }} /home/openstates/newrelic.ini creates=/home/openstates/newrelic.ini
- name: rename new relic app
  replace: dest=/home/openstates/newrelic.ini regexp='Python Application' replace='openstates.org'

# django
- name: collectstatic
  command: /home/openstates/virt/bin/python manage.py collectstatic --settings=openstates.settings --noinput chdir=/home/openstates/src/openstates.org
  environment: django_environment
- name: syncdb
  command: /home/openstates/virt/bin/python manage.py syncdb --settings=openstates.settings --noinput chdir=/home/openstates/src/openstates.org
  environment: django_environment

# uwsgi
- name: write uwsgi template
  template: src=uwsgi.j2 dest=/etc/uwsgi/apps-enabled/openstates.ini
  notify:
    - restart django-application
- name: start uwsgi
  service: name=uwsgi state=started args=openstates<commit_msg>fix crontab name and create Site, fixes #4<commit_after>---
- name: install system packages
  apt: pkg={{item}} update_cache=yes
  with_items:
      - build-essential
      - git
      - mercurial
      - python-pycurl
      - python-boto
      - libpq-dev
      - unzip
      - gdal-bin
      - python
      - python-dev
      - virtualenv
      - uwsgi
      - nginx
      - uwsgi-plugin-python
      - libxslt-dev
      - letsencrypt
- name: make project dir
  file: path=/home/openstates state=directory
- name: add project user
  user: name=openstates home=/home/openstates shell=/bin/bash state=present
- name: chown user directory
  file: path=/home/openstates owner=openstates
- name: add user subdirectories
  file: path=/home/openstates/{{item}} owner=openstates state=directory
  with_items:
    - data
    - letsencrypt
  sudo_user: "openstates"
- name: add env_vars for project user
  template: src=env_vars.j2 dest=/home/openstates/env_vars mode=640
  sudo_user: "openstates"

# letsencrypt
- name: add letsencrypt renewal crontab
  cron: job="letsencrypt renew" special_time="daily" name='renew certificate'

# virtualenv
- name: create virtualenv
  command: virtualenv -p python2.7 /home/openstates/virt{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: checkout project directories
  git: repo=https://github.com/openstates/openstates.org.git dest=/home/openstates/src/openstates.org accept_hostkey=yes version=master
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: add checkouts to python path
  lineinfile: dest=/home/openstates/virt{{ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}/lib/python2.7/site-packages/checkouts.pth create=yes state=present line=/home/openstates/src/openstates.org
  sudo_user: "openstates"
- name: install requirements
  pip: requirements=/home/openstates/src/openstates.org/requirements.txt virtualenv=/home/openstates/virt{{ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}
  sudo_user: "openstates"
  notify:
    - restart django-application
- name: link virtualenv
  file: src=/home/openstates/virt{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }} dest=/home/openstates/virt state=link

# nginx
- name: make certs dir
  file: path=/etc/nginx/certs/ state=directory
- name: generate dh params
  command: openssl dhparam -out /etc/nginx/certs/dhparams.pem 2048 creates=/etc/nginx/certs/dhparams.pem
- name: remove nginx default config
  file: path=/etc/nginx/sites-enabled/default state=absent
- name: copy over htpasswd
  copy: src=../secrets/ansible/htpasswd dest=/home/openstates/htpasswd
- name: write nginx template
  template: src=nginx.j2 dest=/etc/nginx/sites-enabled/openstates
  notify:
    - restart nginx
- name: ensure nginx is running and starts on boot
  service: name=nginx state=restarted enabled=yes

# new relic
- name: generate new relic config
  command: /home/openstates/virt/bin/newrelic-admin generate-config {{ newrelic_key }} /home/openstates/newrelic.ini creates=/home/openstates/newrelic.ini
- name: rename new relic app
  replace: dest=/home/openstates/newrelic.ini regexp='Python Application' replace='openstates.org'

# django
- name: collectstatic
  command: /home/openstates/virt/bin/python manage.py collectstatic --settings=openstates.settings --noinput chdir=/home/openstates/src/openstates.org
  environment: django_environment
- name: syncdb
  command: /home/openstates/virt/bin/python manage.py syncdb --settings=openstates.settings --noinput chdir=/home/openstates/src/openstates.org
  environment: django_environment
- name: create Site
  command: echo "from django.contrib.sites.models import Site;Site.objects.get_or_create(domain='openstates.org', name='openstates.org')" | /home/openstates/virt/bin/python manage.py shell --settings=openstates.settings chdir=/home/openstates/src/openstates.org
  environment: django_environment

# uwsgi
- name: write uwsgi template
  template: src=uwsgi.j2 dest=/etc/uwsgi/apps-enabled/openstates.ini
  notify:
    - restart django-application
- name: start uwsgi
  service: name=uwsgi state=started args=openstates
{'fraction_non_alphanumeric': 0.1143377885783718, 'fraction_numerical': 0.0035236938031591738, 'mean_word_length': 4.0590043023970495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1042773', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2642, 'n_words': 573}