Skip to content

Instantly share code, notes, and snippets.

@antoinelyset
Forked from callumj/1prep.yml
Created July 15, 2014 14:43
Show Gist options
  • Save antoinelyset/397979a884e25bc3b0c2 to your computer and use it in GitHub Desktop.
Save antoinelyset/397979a884e25bc3b0c2 to your computer and use it in GitHub Desktop.
---
# Configures the system for deploying My School Day
- name: Ensure backend directory exists
file: path=/apps/backend state=directory owner=app group=apps mode=0770
- name: Ensure shared directory exists
file: path=/apps/shared state=directory owner=app group=apps mode=0770
- name: Ensure shared sub directories exist
file: path=/apps/shared/{{ item }} state=directory owner=app group=apps mode=0770
with_items:
- log
- pids
- sockets
- system
- name: Ensure shared sub directories exist for public access
file: path=/apps/shared/{{ item }} state=directory owner=app group=apps_public mode=0770
with_items:
- system
- name: Ensure nginx is in the apps directory
user: name=nginx groups=apps append=yes
- name: Generate the versioned directory name
command: date +%Y%m%d_%H%M
register: versioned_directory_name
- name: Create the directory
file: path=/apps/backend/{{ versioned_directory_name.stdout }} state=directory owner=app group=apps mode=0770
- name: Copy the backend into the versioned directory
synchronize: src=files/backend/ dest=/apps/backend/{{ versioned_directory_name.stdout }}/ recursive=yes
- name: Copy the frontend public into the backend public
copy: src=../../../files/frontend/public/ dest=/apps/backend/{{ versioned_directory_name.stdout }}/public/ owner=app group=apps mode=0770
- name: Ensure log is not present in backend
file: path=/apps/backend/{{ versioned_directory_name.stdout }}/log state=absent
- name: Ensure log is shared
file: dest=/apps/backend/{{ versioned_directory_name.stdout }}/log src=/apps/shared/log state=link
- name: Ensure public/system is not present in backend
file: path=/apps/backend/{{ versioned_directory_name.stdout }}/public/system state=absent
- name: Ensure public/system is shared
file: dest=/apps/backend/{{ versioned_directory_name.stdout }}/public/system src=/apps/shared/system state=link
- name: Ensure tmp directory on backend exists
command: mkdir -p /apps/backend/{{ versioned_directory_name.stdout }}/tmp creates=/apps/backend/{{ versioned_directory_name.stdout }}/tmp
- name: Ensure shared directories exist
file: dest=/apps/backend/{{ versioned_directory_name.stdout }}/tmp/{{ item }} src=/apps/shared/{{ item }} state=link
with_items:
- pids
- sockets
- name: Chown on app
command: chown -R app /apps/backend /apps/shared
- name: Chgrp on apps
command: chgrp -R apps /apps/backend /apps/shared
- name: Grant apps_public to nginx
user: name=nginx append=yes groups=apps_public
- name: Chgrp for public to apps_public
command: chgrp -R apps /apps/backend/{{ versioned_directory_name.stdout }}/public
- name: Grant access to access the folder
command: chmod 775 {{ item }}
with_items:
- /apps
- /apps/backend
- /apps/backend/{{ versioned_directory_name.stdout }}
- /apps/backend/{{ versioned_directory_name.stdout }}/public
- /apps/shared
- /apps/shared/sockets
- name: Load default configuration
include_vars: ../variables/default_vhost.yml
- name: Deploy Nginx configuration
template: src=../templates/nginx_vhost.conf dest=/etc/nginx/conf.d/msd.conf
- name: Update Nginx
service: name=nginx state=reloaded
- name: Deploy the Unicorn script
copy: src=unicorn dest=/etc/init/unicorn.conf
- service: name=unicorn enabled=yes
- name: Deploy the worker script
copy: src=worker dest=/etc/init/worker.conf
- service: name=worker enabled=yes
---
# Brings the application up (or restarts as needed etc.)
- name: Stop the workers
service: name=worker state=stopped
- name: Ensure the vendored gems are installed
shell: bundle install --binstubs --deployment --without development test test_pr test_prod chdir=/apps/backend/{{ versioned_directory_name.stdout }}
sudo: yes
sudo_user: app
- name: Ensure executions on bin stubs
shell: chmod +x /apps/backend/{{ versioned_directory_name.stdout }}/bin/*
- name: Check DB state
shell: RAILS_ENV=production ./bin/rake db:migrate:status chdir=/apps/backend/{{ versioned_directory_name.stdout }}
register: migration_status
ignore_errors: true
sudo: yes
sudo_user: app
- name: Creating application DB
shell: RAILS_ENV=production ./bin/rake db:create chdir=/apps/backend/{{ versioned_directory_name.stdout }}
when: migration_status.stdout.find('migrations table does not exist') != -1
sudo: yes
sudo_user: app
- name: Perform the migrations
shell: RAILS_ENV=production ./bin/rake --trace db:migrate chdir=/apps/backend/{{ versioned_directory_name.stdout }}
sudo: yes
sudo_user: app
- name: Generate Rails managed binstubs
shell: yes | ./bin/rake rails:update:bin chdir=/apps/backend/{{ versioned_directory_name.stdout }}
sudo: yes
sudo_user: app
- name: Check the system integrity
shell: RAILS_ENV=production ./bin/rake check:integrity chdir=/apps/backend/{{ versioned_directory_name.stdout }}
sudo: yes
sudo_user: app
- name: Remove the current pointer
file: path=/apps/backend/current state=absent
ignore_errors: true
- name: Create the current pointer
file: src=/apps/backend/{{ versioned_directory_name.stdout }} dest=/apps/backend/current state=link
- name: Restart the application
service: name=unicorn state=restarted
- name: Start the worker
service: name=worker state=restarted
- name: Grant access to current
command: chmod 775 /apps/backend/current
- name: Install the cron jobs
shell: CRON_D_USER=app ./bin/whenever > /etc/cron.d/msd_cron chdir=/apps/backend/{{ versioned_directory_name.stdout }}
sudo: yes
- name: Copy cleanup script
copy: src=cleanup_old_releases.rb dest=/tmp/cleanup_old_releases.rb mode=0755
- name: Run cleanup script
shell: ruby /tmp/cleanup_old_releases.rb /apps/backend chdir=/apps/backend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment