Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
213 lines (182 sloc) 6.08 KB
--- # Deploy
# Remove old rollbacks so they aren't used accidentally after errors.
- name: remove the old rollback
file:
dest={{ rollback_path }}
state=absent
tags: clone
- name: remove the old rollback database
file:
dest={{ rollback_sql_path }}
state=absent
tags: clone
#----------------
# Build codebase
- name: remove the old build path
file:
dest={{ build_path }}
state=absent
tags: clone
- name: build codebase from git branch
git:
repo: "{{ git_repo }}"
dest: "{{ build_path }}"
version: "{{ git_version }}"
refspec: "+refs/pull/*/merge:refs/remotes/origin/pull-request-*"
force: yes
tags: clone
- name: detach head for git push deployment
shell: git checkout `git rev-parse HEAD`
args:
chdir: "{{ build_path }}"
tags: skip_ansible_lint
# TODO: Ugly hack until we have better configuration management
- name: symlink into the repo
file:
src={{ item.src }}
dest={{ item.dest }}
owner={{ unicorn_user }}
state=link
force=yes
with_items:
- { src: "{{ assets_path }}", dest: "{{ build_path }}/public/assets" }
- { src: "{{ system_path }}", dest: "{{ build_path }}/public/system" }
- { src: "{{ images_path }}", dest: "{{ build_path }}/public/images" }
- { src: "{{ spree_path }}", dest: "{{ build_path }}/public/spree" }
- { src: "{{ config_path }}/database.yml", dest: "{{ build_path }}/config/database.yml" }
- { src: "{{ config_path }}/application.yml", dest: "{{ build_path }}/config/application.yml" }
- { src: "{{ l10n_path }}/states.yml", dest: "{{ build_path }}/db/default/spree/states.yml" }
- { src: "{{ config_path }}/post-receive", dest: "{{ build_path }}/.git/hooks/post-receive" }
tags: symlink
- name: symlink the bugsnag file into the config folder
file:
src="{{ config_path }}/bugsnag.rb"
dest={{ build_path }}/config/initializers/bugsnag.rb
state=link
force=yes
owner={{ unicorn_user }}
mode=0775
when: bugsnag_key != "none"
tags: symlink
- name: symlink the db2fog file into the config folder
file:
src="{{ config_path }}/db2fog.rb"
dest={{ build_path }}/config/initializers/db2fog.rb
state=link
force=yes
owner={{ unicorn_user }}
mode=0775
when: s3_backups_bucket != ""
tags: symlink
- name: install bundler
# This needs to be run inside a bash shell to initialise rbenv
# See http://stackoverflow.com/questions/22115936/install-bundler-gem-using-ansible
command: bash -lc "./script/install-bundler --no-ri --no-rdoc"
args:
chdir: "{{ build_path }}"
register: bundler
changed_when: "bundler.stdout != ''"
- name: bundle install app dependencies
#TODO make the "--without development" part conditional on rails_env
# Note: the 'LANG=...' is a fix for broken rubygems utf8 handling.
command: bash -lc "bundle install --gemfile {{ build_path }}/Gemfile --path /home/{{ unicorn_user }}/.gem --deployment" #--without development test"
environment:
LANG: "{{ language }}"
LC_ALL: "{{ language }}"
LC_COLLATE: "{{ language }}"
tags:
- bundle_app
- skip_ansible_lint
notify:
- precompile assets
- restart unicorn
#--------------------
# Get timestamp
- name: define a timestamp
set_fact:
timestamp: "{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"
#--------------------------
# Create a rollback release
- name: create a database rollback version
shell: pg_dump -h {{ db_host }} -U {{ db_user }} {{ db }} | gzip > {{ rollback_sql_path }}
args:
creates: "{{ rollback_sql_path }}"
- name: create a database backup version
command: cp {{ rollback_sql_path }} {{ releases_path }}/{{ timestamp }}.sql.gz
tags: skip_ansible_lint
- name: create a repo rollback version
command: mv {{ current_path }} {{ rollback_path }} removes={{ current_path }}
tags: skip_ansible_lint
- name: create a repo backup version
command: cp -r {{ rollback_path }} {{ releases_path }}/{{ timestamp }} removes={{ rollback_path }}
tags: skip_ansible_lint
#-------------------------
# Move new code into place
- name: move build to current
command: mv {{ build_path }} {{ current_path }} removes={{ build_path }}
become: yes
become_user: "{{ unicorn_user }}"
#--------------------
# Update the database
- name: check database status
command: psql -h {{ db_host }} -U {{ db_user }} -d {{ db }} -c "SELECT true FROM pg_tables WHERE tablename = 'order_cycles';"
register: table_exists
ignore_errors: yes
tags:
- rake
- skip_ansible_lint
- debug: msg="{{ table_exists.stdout }}"
tags: rake
- debug: msg="{{ table_exists.stderr }}"
tags: rake
- name: create database if it doesn't exist
command: bash -lc "bundle exec rake db:create RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ current_path }}"
when: table_exists.stderr.find('does not exist') != -1
tags:
- rake
- skip_ansible_lint
- name: load schema if it isn't loaded
command: bash -lc "bundle exec rake db:schema:load RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ current_path }}"
when: table_exists.stderr.find('does not exist') != -1 or table_exists.stdout.find('0 rows') != -1
tags:
- rake
- skip_ansible_lint
- name: migrate database
command: bash -lc "bundle exec rake db:migrate RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ current_path }}"
tags:
- rake
- skip_ansible_lint
notify:
- precompile assets
- restart unicorn
- restart delayed job service
- name: seed database
# We run a shell script that passes the default email and password to rake with an EOF block, so we don't hang on the prompts.
command: bash -lc "{{ config_path }}/seed.sh RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ current_path }}"
# when: table_exists.stderr.find('does not exist') != -1
tags:
- seed
- skip_ansible_lint
notify:
- precompile assets
- restart unicorn
# tag to trigger a unicorn restart
- name: reset
# needs to do something here
command: echo 1
tags:
- reset
- skip_ansible_lint
notify:
- restart unicorn
- restart delayed job service
- update whenever
- notify slack