Skip to content

Commit

Permalink
Various refactoring improvements
Browse files Browse the repository at this point in the history
Moved vars out to default files.
Fixed bugs in seeds.rb.
Removed handling of S3 config.
Backups are stored in /backups sub-folder.
  • Loading branch information
pmackay committed Aug 31, 2015
1 parent 52eb4b8 commit 7298bd7
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ ssl.key
vars.yml
files/*
-files/README.md
production
staging
backups/*
9 changes: 5 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Optimise virtualbox.
vbox.customize [ "modifyvm", :id, "--nictype1", "virtio" ]
vbox.customize [ "modifyvm", :id, "--nictype2", "virtio" ]

# VM network config.
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 44343
config.vm.network "private_network", type: "dhcp"

#config.vm.network "forwarded_port", guest: 443, host: 44343
config.vm.synced_folder ".", "/vagrant", :create => true
config.ssh.forward_agent = true
end
Expand All @@ -30,15 +29,17 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
ansible.playbook = "install.yml"
#ansible.playbook = "deploy.yml"
ansible.host_key_checking = false
ansible.verbose = 'vvvv'
ansible.verbose = ''
#ansible.tags = 'deploy' # uncomment this for running only specific tags with vagrant, good for debugging.
ansible.extra_vars = {
ansible_ssh_user: 'vagrant',
ansible_connection: 'ssh',
ansible_ssh_args: '-o ForwardAgent=yes'
}
ansible.groups = {
"ofn_servers" => ["default"],
"all_groups:children" => ["ofn_servers"]
}
#ansible.start_at_task = 'seed database'
end
end
1 change: 1 addition & 0 deletions backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
hosts: ofn_servers
remote_user: "{{ user }}"
vars_files:
- vars/default_vars.yml
- vars.yml
roles:
- backup
Expand Down
5 changes: 5 additions & 0 deletions backups/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Backups

This directory is a default location for backup files.

Override the `local_backup_path` variable if you wish to change it.
4 changes: 4 additions & 0 deletions deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
- name: deploy
hosts: ofn_servers
remote_user: "{{ user }}"

vars_files:
- vars/default_vars.yml
- vars.yml

roles:
- role: deploy
notify:
- restart unicorn

handlers:
- include: roles/webserver/handlers/main.yml
3 changes: 3 additions & 0 deletions provision.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
- name: provision
hosts: ofn_servers
remote_user: "{{ user }}"

vars_files:
- vars/default_vars.yml
- vars.yml

roles:
- role: common # Install common apps and libraries, and setup shell.
tags: common
Expand Down
13 changes: 6 additions & 7 deletions roles/app/files/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
require 'csv'

# -- Spree
unless Spree::Country.find_by_name(ENV['DEFAULT_COUNTRY'])
unless Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
puts "[db:seed] Seeding Spree"
Spree::Core::Engine.load_seed if defined?(Spree::Core)
Spree::Auth::Engine.load_seed if defined?(Spree::Auth)
end

country = Spree::Country.find_by_name(ENV['DEFAULT_COUNTRY'])
country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
puts "Country is #{country.to_s}"
puts "[db:seed] loading states yaml"
states = YAML::load_file "db/default/spree/states.yml"
Expand All @@ -21,10 +21,9 @@
suburbs_file = File.join ['db', 'suburbs.csv']

# -- Seeding States
puts "[db:seed] Seeding states for " + country.name
puts "[db:seed] Seeding states for " + country.name

states.each do |id,state|
puts id
states.each do |state|
puts "State: " + state.to_s
unless Spree::State.find_by_name(state['name'])
Spree::State.create!({"name"=>state['name'], "abbr"=>state['abbr'], :country=>country}, without_protection: true)
Expand All @@ -47,12 +46,12 @@
name = row[:name]
lat = row[:latitude]
long = row[:longitude]
state_id = states_ids[row[:state]]
state_id = states_ids[row[:state_id]]
statement += "(#{postcode},$$#{name}$$,#{state_id},#{lat},#{long}),"
end
statement[-1] = ';'

puts statement
# puts statement

unless Suburb.find_by_name(name)
puts "[db:seed] Seeding suburbs for country.name"
Expand Down
7 changes: 1 addition & 6 deletions roles/app/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- "{{ pid_path }}"
- "{{ sock_path }}"
- "{{ config_path }}"
ttags: dirs
tags: dirs

- name: copy seeds.rb
copy: src=seeds.rb dest={{ config_path }}/seeds.rb
Expand All @@ -41,11 +41,6 @@
tags: app_templates
when: newrelic_key != "none"

- name: copy the s3 file to the shared folder
template: src=s3.yml dest={{ config_path }}/s3.yml owner={{ user }} mode=775
tags: app_templates
when: s3_bucket != "none"

- name: get i10n repo
git: repo={{ i10n_repo }} dest={{ i10n_path }} force=yes
tags: i10n
Expand Down
2 changes: 1 addition & 1 deletion roles/app/templates/application.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
SECRET_TOKEN: {{ secret_token }}

TIMEZONE: {{ timezone }}
# Default country for dropdowns etc.
# Default country for dropdowns etc. See for codes: http://en.wikipedia.org/wiki/ISO_3166-1
DEFAULT_COUNTRY_CODE: {{ country_code }}
# Locale for translation.
LOCALE: {{ locale }}
Expand Down
3 changes: 3 additions & 0 deletions roles/backup/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
local_backup_path: "{{ playbook_dir }}/backups"
local_rsync_path: "{{ playbook_dir }}/backups"
12 changes: 10 additions & 2 deletions roles/backup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
shell: sudo -u postgres pg_dump {{ db }} | gzip > {{ backup_path }}/{{ current_date.stdout }}.sql.gz

- name: fetch database to local
fetch: src={{ backup_path }}/{{ current_date.stdout }}.sql.gz dest={{ local_backup_path }} fail_on_missing=yes
fetch:
src={{ backup_path }}/{{ current_date.stdout }}.sql.gz
dest={{ local_backup_path }}/{{ ansible_hostname }}/databases/
fail_on_missing=yes
flat=yes

# But we don't bother making a whole new copy of all the files every time, just rsync them.
- name: sync public files to local
synchronize: mode=pull src={{ current_path }}/public dest={{ local_rsync_path }} copy_links=yes
synchronize:
mode=pull
src={{ current_path }}/public
dest={{ local_rsync_path }}/{{ ansible_hostname }}/files
copy_links=yes
tags: rsync

# @TODO just delete anything older than the newest {{ num_remote_backups }} files
Expand Down
2 changes: 2 additions & 0 deletions roles/common/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# home_path: "/home/{{ user }}"
3 changes: 3 additions & 0 deletions roles/deploy/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rollback_path: "{{ app_path }}/rollback"
rollback_sql_path: "{{ app_path }}/rollback.sql.gz"
5 changes: 0 additions & 5 deletions roles/deploy/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
when: newrelic_key != "none"
tags: symlink

- name: symlink the s3 file to the config folder
file: src="{{ config_path }}/s3.yml" dest={{ build_path }}/config/s3.yml state=link force=yes owner={{ user }} mode=775
when: s3_bucket != "none"
tags: symlink

- name: install app dependencies
#TODO make the "--without development" part conditional on rails_env
# Note: the 'LANG=...' is a fix for broken rubygems utf8 handling.
Expand Down
18 changes: 18 additions & 0 deletions roles/webserver/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---

#----------------------------------------------------------------------
# Nginx variables
app_root: "{{ current_path }}"
port: 3000
# domain: is set above

#----------------------------------------------------------------------
# Unicorn variables
unicorn_user: "{{ user }}"
unicorn_config: "{{ config_path }}/unicorn.rb"
unicorn_log: "{{ log_path }}/unicorn.log"
unicorn_pid: "{{ pid_path }}/unicorn.pid"
unicorn_sock: "{{ sock_path }}/unicorn.{{ app }}.sock"
unicorn_workers: 2
unicorn_timeout: 30

4 changes: 4 additions & 0 deletions rollback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

- hosts: ofn_servers
remote_user: "{{ user }}"

vars_files:
- vars/default_vars.yml
- vars.yml

handlers:
- include: roles/webserver/handlers/main.yml
- include: roles/deploy/handlers/main.yml

roles:
- role: rollback
3 changes: 3 additions & 0 deletions user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

- hosts: ofn_servers
remote_user: root

vars_files:
- vars/default_vars.yml
- vars.yml

roles:
- default_user

Expand Down
41 changes: 41 additions & 0 deletions vars/default_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---


#----------------------------------------------------------------------
# Rails variables
ruby: 1.9.3-p392
gem_path: /home/{{ user }}/.gem
bundle_path: /home/{{ user }}/.rbenv/shims/bundle
env:
RAILS_ENV: "{{ rails_env }}"
PATH: "{{ gem_home }}/bin:{{ ansible_env.PATH }}"

#----------------------------------------------------------------------
# Database variables
db: openfoodnetwork
test_db: ofn_test
db_user: ofn_user
db_host: localhost

#----------------------------------------------------------------------
# App variables
app: openfoodnetwork

home_path: "/home/{{ user }}"
app_path: "{{ home_path }}/apps/{{ app }}"
current_path: "{{ app_path }}/current"
repo_path: "{{ app_path }}/repo"
build_path: "{{ app_path }}/build"
shared_path: "{{ app_path }}/shared"
backup_path: "{{ app_path }}/backups"
releases_path: "{{ app_path }}/releases-old"
assets_path: "{{ shared_path }}/assets"
system_path: "{{ shared_path }}/system"
spree_path: "{{ shared_path }}/spree"
config_path: "{{ shared_path }}/config"
log_path: "{{ shared_path }}/log"
pid_path: "{{ shared_path }}/pids"
sock_path: "{{ shared_path }}/sock"
git_post_receive: "{{ current_path }}/.git/hooks/post-receive"
i10n_path: "{{ shared_path }}/i10n"

0 comments on commit 7298bd7

Please sign in to comment.