<commit_before>---
- hosts: "biovel-portal:&apt"
  sudo: yes
  tasks:
    - name: "Install portal package dependencies"
      action: apt pkg={{ item }} state=present
      with_items:
        - libcurl4-gnutls-dev
        - libxml2-dev
        - libv8-dev
        - ruby
        - ruby-dev
        - ri
        - rubygems
        - git            # to obtain portal source code
        - python-mysqldb # required for Ansible mysql modules

- hosts: biovel-portal
  sudo: yes
  tasks:
    - name: Install Ruby gem bundler
      gem: name=bundler state=present

    - name: Add BioVeL Portal database
      mysql_db: name={{item}} state=present
      with_items:
        - tlite_test
        - tlite_dev
        - tlite_prod

    - name: Add BioVeL Portal database user
      mysql_user: name={{biovel_portal_db_user}} password={{<PASSWORD>}} state=present
            "priv=tlite_prod.*:ALL/tlite_dev.*:ALL/tlite_test.*:ALL"

    - name: Create portal install directory
      file: name={{biovel_portal_install}} state=directory owner=www-data

    - name: create Portal log directory
      file: path={{biovel_portal_log}} state=directory owner=www-data

    - name: Get portal code
      sudo_user: www-data
      git: repo=https://github.com/BioVeL/portal.git dest={{biovel_portal_install}}
            version={{biovel_portal_git_version}}
      notify: restart portal

    - name: install Ruby dependencies using bundle
      # this command is effectively idempotent, as it does not do anything if the dependencies
      # are already installed, and it is fast when nothing needs to be done
      command: bundle install
          chdir={{biovel_portal_install}}

    # Before 2013-08-08 
    - name: ensure production.rb exists
      sudo_user: www-data
      shell: "[ -r '{{biovel_portal_install}}/config/environments/production.rb' ] || cp '{{biovel_portal_install}}/config/environments/production.example.rb' '{{biovel_portal_install}}/config/environments/production.rb'"

    - name: configure portal password reset link
      sudo_user: www-data
      lineinfile: dest='{{biovel_portal_install}}/config/environments/production.rb'
            regexp='^ *config.action_mailer.default_url_options *='
            line='  config.action_mailer.default_url_options = { :host => "{{external_hostname}}" }'
            state=present backup=yes
      notify: restart portal

    - name: configure portal database connections
      sudo_user: www-data
      template: src=database.yml.j2 dest={{biovel_portal_install}}/config/database.yml mode=0600
      notify: restart portal

    - name: precompile assets
      # TODO - assets only get precompiled when application.js does not exist - it would be nice to
      # re-run it when the Git repository gets updated
      sudo_user: www-data
      command: bundle exec rake assets:precompile
            chdir={{biovel_portal_install}} creates={{biovel_portal_install}}/public/assets/application.js

    - name: Check database migration status
      command: bundle exec rake RAILS_ENV=production db:migrate:status
            chdir={{biovel_portal_install}}
      register: biovel_portal_db_migration_status

    - name: Create database seed file
      sudo_user: www-data
      template: src=seeds.rb.j2 dest={{biovel_portal_install}}/db/seeds.rb mode=0600

    ### Schema loading fails due to over-long index. Commmented out until SERVINF-135 is fixed
    # - name: Initial setup of database
    #   command: bundle exec rake RAILS_ENV=production db:setup
    #         chdir={{biovel_portal_install}}
    #   when: "'Schema migrations table does not exist yet' in biovel_portal_db_migration_status.stdout"

    - name: setup databases
      sudo_user: www-data
      command: bundle exec rake RAILS_ENV=production db:migrate
            chdir={{biovel_portal_install}}

    ### db:setup will run db:seed, so delete this clause when uncommenting above
    - name: Seed database
      command: bundle exec rake RAILS_ENV=production db:seed
            chdir={{biovel_portal_install}}
      when: "'Schema migrations table does not exist yet' in biovel_portal_db_migration_status.stdout"

    - name: setup database verification script
      template: src=dbcreate.sh.j2 dest={{biovel_portal_install}}/dbcreate.sh owner=root mode=0700

    - name: run database verification
      command: "{{biovel_portal_install}}/dbcreate.sh {{item}}"
      with_items:
        #- tlite_dev<commit_msg>Use db:setup to create database rather than relying on db:migrate<commit_after>---
- hosts: "biovel-portal:&apt"
  sudo: yes
  tasks:
    - name: "Install portal package dependencies"
      action: apt pkg={{ item }} state=present
      with_items:
        - libcurl4-gnutls-dev
        - libxml2-dev
        - libv8-dev
        - ruby
        - ruby-dev
        - ri
        - rubygems
        - git            # to obtain portal source code
        - python-mysqldb # required for Ansible mysql modules

- hosts: biovel-portal
  sudo: yes
  tasks:
    - name: Install Ruby gem bundler
      gem: name=bundler state=present

    - name: Add BioVeL Portal database
      mysql_db: name={{item}} state=present
      with_items:
        - tlite_test
        - tlite_dev
        - tlite_prod

    - name: Add BioVeL Portal database user
      mysql_user: name={{biovel_portal_db_user}} password={{bio<PASSWORD>_portal_db_<PASSWORD>}} state=present
            "priv=tlite_prod.*:ALL/tlite_dev.*:ALL/tlite_test.*:ALL"

    - name: Create portal install directory
      file: name={{biovel_portal_install}} state=directory owner=www-data

    - name: create Portal log directory
      file: path={{biovel_portal_log}} state=directory owner=www-data

    - name: Get portal code
      sudo_user: www-data
      git: repo=https://github.com/BioVeL/portal.git dest={{biovel_portal_install}}
            version={{biovel_portal_git_version}}
      notify: restart portal

    - name: install Ruby dependencies using bundle
      # this command is effectively idempotent, as it does not do anything if the dependencies
      # are already installed, and it is fast when nothing needs to be done
      command: bundle install
          chdir={{biovel_portal_install}}

    # From 2013-08-08 production.rb needs to be created from example file
    - name: ensure production.rb exists
      sudo_user: www-data
      shell: "[ -r '{{biovel_portal_install}}/config/environments/production.rb' ] || cp '{{biovel_portal_install}}/config/environments/production.example.rb' '{{biovel_portal_install}}/config/environments/production.rb'"

    - name: configure portal password reset link
      sudo_user: www-data
      lineinfile: dest='{{biovel_portal_install}}/config/environments/production.rb'
            regexp='^ *config.action_mailer.default_url_options *='
            line='  config.action_mailer.default_url_options = { :host => "{{external_hostname}}" }'
            state=present backup=yes
      notify: restart portal

    - name: configure portal database connections
      sudo_user: www-data
      template: src=database.yml.j2 dest={{biovel_portal_install}}/config/database.yml mode=0600
      notify: restart portal

    - name: precompile assets
      # TODO - assets only get precompiled when application.js does not exist - it would be nice to
      # re-run it when the Git repository gets updated
      sudo_user: www-data
      command: bundle exec rake assets:precompile
            chdir={{biovel_portal_install}} creates={{biovel_portal_install}}/public/assets/application.js

    - name: Check database migration status
      command: bundle exec rake RAILS_ENV=production db:migrate:status
            chdir={{biovel_portal_install}}
      register: biovel_portal_db_migration_status

    - name: Create database seed file
      sudo_user: www-data
      template: src=seeds.rb.j2 dest={{biovel_portal_install}}/db/seeds.rb mode=0600

    # Run db:setup only when the database appears to be empty (no migration marker)
    # Note, db:setup runs db:schema:load followed by db:seed
    - name: Initial setup of database
      command: bundle exec rake RAILS_ENV=production db:setup
            chdir={{biovel_portal_install}}
      when: "'Schema migrations table does not exist yet' in biovel_portal_db_migration_status.stdout"

    # Run migrate every time. After db:setup, it should do nothing. Otherwise, we are checking for
    # any changes in the checked out portal
    - name: setup databases
      sudo_user: www-data
      command: bundle exec rake RAILS_ENV=production db:migrate
            chdir={{biovel_portal_install}}

    # A third level of database manipulation - this primarily updates the server passwords if they
    # get changed in Ansible
    - name: setup database verification script
      template: src=dbcreate.sh.j2 dest={{biovel_portal_install}}/dbcreate.sh owner=root mode=0700

    - name: run database verification
      command: "{{biovel_portal_install}}/dbcreate.sh {{item}}"
      with_items:
        #- tlite_dev
{'fraction_non_alphanumeric': 0.09095957352287871, 'fraction_numerical': 0.006108396268325189, 'mean_word_length': 2.9968930315135376, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 18, 'xml': 2}, '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': '652425', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2621, 'n_words': 724}