Skip to content

Commit

Permalink
Provisioning with vagrant, capistrano and ansible just works
Browse files Browse the repository at this point in the history
  • Loading branch information
chischaschos committed Mar 8, 2014
1 parent ff6b30d commit 52e0d5e
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ blog.db
capybara*
coverage/
.env
provisioning/vars/defaults.yml
28 changes: 28 additions & 0 deletions Capfile
@@ -0,0 +1,28 @@
# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

require 'capistrano/bundler'


# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -15,6 +15,8 @@ few changes:
[maruku](https://github.com/bhollis/maruku/). It is faster and has
better support for embedded html
- Uses [puma](https://github.com/puma/puma)
- Contains provisioning support via [Ansible](http://www.ansible.com/home),
[Vagrant](http://www.vagrantup.com/) and [Capistrano](http://capistranorb.com/)

Made some changes to the default code base in
order to remove empty spaces, fix test suite, upgrade used gems and
Expand All @@ -37,4 +39,12 @@ customize its configuration.
- Try to improve code so that codeclimate becomes happier [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/chischaschos/scanty)

## Provisioning
Based on https://github.com/radar/ansible-rails-app

- Based on https://github.com/radar/ansible-rails-app
- Tips from http://ruby-journal.com/how-to-setup-rails-app-with-puma-and-nginx/
- Depending on https://github.com/dotless-de/vagrant-vbguest

1. Copy provisioning/vars/defaults.yml.example to provisioning/vars/defaults.yml
2. Set you deploy public key in [deploy_public_key](provisioning/vars/defaults.yml)
3. Run ```vagrant up```
4. Run ```cap staging deploy```
2 changes: 1 addition & 1 deletion Vagrantfile
Expand Up @@ -7,7 +7,7 @@ VAGRANTFILE_API_VERSION = '2'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'precise64'

#config.vm.network 'private_network', ip: '192.168.50.4'
config.vm.network 'private_network', ip: '192.168.50.4'

config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provisioning/site.yml'
Expand Down
67 changes: 67 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,67 @@
set :application, 'emmanueldelgado.me'
set :repo_url, 'git@github.com:chischaschos/scanty.git'
set :deploy_to, '/data/site'
set :log_level, :debug
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{tmp/sockets log config/puma public/spree}
set :sockets_path, Pathname.new("#{fetch(:deploy_to)}/shared/tmp/sockets/")

# These puma settings are only here because capistrano-puma is borked.
# See issue #4.
set :puma_roles, :app
set :puma_socket, "unix://#{fetch(:sockets_path).join('puma_' + fetch(:application) + '.sock')}"
set :pumactl_socket, "unix://#{fetch(:sockets_path).join('pumactl_' + fetch(:application) + '.sock')}"
set :puma_state, fetch(:sockets_path).join('puma.state')
set :puma_log, -> { shared_path.join("log/puma-#{fetch(:stage )}.log") }
set :puma_flags, nil

set :bundle_flags, '--deployment'

namespace :deploy do
task :restart do
invoke 'puma:restart'
end
end

namespace :spree_sample do
task :load do
on roles(:app) do
within release_path do
ask(:confirm, "Are you sure you want to delete everything and start again? Type 'yes'")
if fetch(:confirm) == "yes"
execute :rake, "db:reset AUTO_ACCEPT=true"
execute :rake, "spree_sample:load"
end
end
end
end
end

namespace :puma do
desc "Restart puma instance for this application"
task :restart do
on roles fetch(:puma_roles) do
within release_path do
execute :bundle, "exec pumactl -S #{fetch(:puma_state)} restart"
end
end
end

desc "Show status of puma for this application"
task :status do
on roles fetch(:puma_roles) do
within release_path do
execute :bundle, "exec pumactl -S #{fetch(:puma_state)} stats"
end
end
end

desc "Show status of puma for all applications"
task :overview do
on roles fetch(:puma_roles) do
within release_path do
execute :bundle, "exec puma status"
end
end
end
end
39 changes: 39 additions & 0 deletions config/deploy/production.rb
@@ -0,0 +1,39 @@
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
# Don't declare `role :all`, it's a meta role
role :app, %w{deploy@example.com}
role :web, %w{deploy@example.com}
role :db, %w{deploy@example.com}

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value

# you can set custom ssh options
# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options
# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start)
# set it globally
# set :ssh_options, {
# keys: %w(/home/rlisowski/.ssh/id_rsa),
# forward_agent: false,
# auth_methods: %w(password)
# }
# and/or per server
# server 'example.com',
# user: 'user_name',
# roles: %w{web app},
# ssh_options: {
# user: 'user_name', # overrides user setting above
# keys: %w(/home/user_name/.ssh/id_rsa),
# forward_agent: false,
# auth_methods: %w(publickey password)
# # password: 'please use keys'
# }
# setting per server overrides global ssh_options
9 changes: 9 additions & 0 deletions config/deploy/staging.rb
@@ -0,0 +1,9 @@
server '127.0.0.1',
user: 'deploy',
roles: %w{app}

set :ssh_options, {
forward_agent: true,
port: 2222,
#keys: %w(/some/place/private_key),
}
2 changes: 1 addition & 1 deletion provisioning/roles/database/tasks/postgresql.yml
Expand Up @@ -23,7 +23,7 @@
command: service postgresql restart

- name: create the postgresql user for application
postgresql_user: name=deploy
postgresql_user: name=deploy password=deploy
sudo_user: postgres

- name: create the postgresql database for application
Expand Down
5 changes: 4 additions & 1 deletion provisioning/roles/webserver/tasks/deploy.yml
Expand Up @@ -55,7 +55,7 @@
- name: Create deployment user
user: name=deploy comment="Application deployment user"

#- authorized_key: user=deploy key='$FILE(~/.ssh/id_rsa.pub)'
- authorized_key: user=deploy key="{{ lookup('file', deploy_public_key) }}"

- name: Create deployment directory
file: path={{deploy_directory}} owner=deploy group=deploy state=directory
Expand All @@ -75,5 +75,8 @@
- name: Install database.yml
template: src=database.yml.j2 dest={{deploy_directory}}/shared/config/database.yml force=yes mode=755 owner=deploy

- name: Install .env
template: src=data_site_shared_config_env.j2 dest={{deploy_directory}}/shared/config/.env force=yes mode=755 owner=deploy

- name: Install Bundler
command: gem install bundler
@@ -0,0 +1,5 @@
DATABASE_URL=postgres://deploy:deploy@localhost:5432/app_emmanueldelgado.me
BLOG_PASSWD=123
COOKIE_KEY=123
COOKIE_VALUE=123
DISQUS_SHORTNAME=123
1 change: 1 addition & 0 deletions provisioning/roles/webserver/templates/etc_init_puma.j2
Expand Up @@ -33,4 +33,5 @@ script
cd {{deploy_directory}}/current
logger -t puma "Starting server: $app"
exec bundle exec puma -C {{deploy_directory}}/current/config/puma/production.rb
stdout_redirect "/data/site/shared/log/puma.stdout.log", "/data/site/shared/log/puma.stderr.log"
end script
2 changes: 1 addition & 1 deletion provisioning/roles/webserver/templates/puma_production.j2
Expand Up @@ -9,7 +9,7 @@ workers 2
# ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"] || YAML.load_file("#{cwd}/config/database.yml")["production"])
# end

bind 'unix://{{deploy_directory}}/shared/tmp/sockets/puma_spree.sock'
bind 'unix://{{deploy_directory}}/shared/tmp/sockets/puma_emmanueldelgado.me.sock'
environment 'production'
pidfile "{{deploy_directory}}/shared/tmp/puma.pid"
state_path "{{deploy_directory}}/shared/tmp/sockets/puma.state"
Expand Down
1 change: 1 addition & 0 deletions provisioning/site.yml
@@ -1,6 +1,7 @@
---
- hosts: all
sudo: yes

vars_files:
- vars/defaults.yml

Expand Down
9 changes: 6 additions & 3 deletions provisioning/vars/defaults.yml
Expand Up @@ -5,7 +5,10 @@ app_name: emmanueldelgado.me

## stolen from https://github.com/jgrowl/ansible-playbook-ruby-from-src
rubyTmpDir: /usr/local/src
rubyUrl: http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.0.tar.gz
rubyCompressedFile: ruby-2.1.0.tar.gz
rubyName: ruby-2.1.0
rubyUrl: http://cache.ruby-lang.org/pub/ruby/ruby-1.9.3-p545.tar.gz
rubyCompressedFile: ruby-1.9.3-p545.tar.gz
rubyName: ruby-1.9.3-p545
tmpRubyPath: "{{rubyTmpDir}}/{{rubyName}}"

## deploy
deploy_public_key: /your/deploy/public_key
14 changes: 14 additions & 0 deletions provisioning/vars/defaults.yml.example
@@ -0,0 +1,14 @@
## webapp
webserver_name: emmanueldelgado.me
deploy_directory: /data/site
app_name: emmanueldelgado.me

## stolen from https://github.com/jgrowl/ansible-playbook-ruby-from-src
rubyTmpDir: /usr/local/src
rubyUrl: http://cache.ruby-lang.org/pub/ruby/ruby-1.9.3-p545.tar.gz
rubyCompressedFile: ruby-1.9.3-p545.tar.gz
rubyName: ruby-1.9.3-p545
tmpRubyPath: "{{rubyTmpDir}}/{{rubyName}}"

## deploy
deploy_public_key: /your/deploy/public_key

0 comments on commit 52e0d5e

Please sign in to comment.