Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
iafonov committed Jun 22, 2012
0 parents commit 9721a6a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Rails Ghetto

Description
===========

Chef cookbook intended for managing & deployment of multiple rack-based applications living on one server behind nginx. Supports asset pipeline right from the box.

Uses nginx + Unicorn to run applications.

Requirements
============

Cookbook depends on several opscode's cookbooks:

* [application](https://github.com/opscode-cookbooks/application)
* [application_ruby](https://github.com/opscode-cookbooks/application_ruby)
* [nginx](https://github.com/opscode-cookbooks/nginx)

Recipes
=======

* `rails_ghetto::user` - creates user and group (if they aren't exist) that are used to deploy & run applications
* `rails_ghetto::nginx_sites_enable` - creates and enables nginx configuration files
* `rails_ghetto::deploy` - deploys application. *Caution* - [revision based deploy](http://wiki.opscode.com/display/chef/Deploy+Resource#DeployResource-DeployResource) is used to ensure that chef-client runs will not re-deploy all applications. Make sure you understand how it works.

Attributes
==========

Here is a sample YAML representation of attributes hierarchy:

rails_ghetto:
deployment_user: deploy
deployment_group: deploy
apps_root: '/var/www/apps'
applications:
- sample:
repository: git@github.com:account/application.git
revision: master
deploy_key: '/home/root/.ssh/github'
unicorn_port: 8080
server_name: 'production.example.com'
compile_assets: true
run_migrations: true
rails_environment: 'production'
database: { adapter: mysql2, encoding: 'utf8', database: 'sample', username: 'root', password: 'changeit', socket: '/tmp/mysql.sock' }
- sample2:
repository: git@github.com:account/application.git
revision: master
deploy_key: '/home/root/.ssh/github'
unicorn_port: 8081
server_name: 'staging.example.com'
compile_assets: false
run_migrations: true
rails_environment: 'staging'
database: { adapter: postgresql, encoding: 'utf8', database: 'sample2', username: 'root', password: 'changeit', socket: '/tmp/mysql.sock' }


© 2012 [Igor Afonov](https://iafonov.github.com) MIT License
4 changes: 4 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
default['rails_ghetto']['deployment_user'] = 'deploy'
default['rails_ghetto']['deployment_group'] = 'deploy'

default['rails_ghetto']['apps_root'] = '/var/www/apps/'
13 changes: 13 additions & 0 deletions libraries/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Chef
class Recipe
def rails_applications(&block)
Array(node['rails_ghetto']['applications']).each do |application|
application_name = application.keys.first
application_data = application[application_name]
default = application == Array(node['rails_ghetto']['applications']).first

block.call(application_name, application_data, default)
end
end
end
end
9 changes: 9 additions & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
maintainer "Igor Afonov"
maintainer_email "afonov@gmail.com"
license "MIT"
description "Simple cookbook for managing deployment of several ruby/rails web applications"
version "0.0.1"

depends "application"
depends "application_ruby"
depends "nginx"
3 changes: 3 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_recipe 'rails_ghetto::user'
include_recipe 'rails_ghetto::deploy'
include_recipe 'rails_ghetto::nginx_sites_enable'
57 changes: 57 additions & 0 deletions recipes/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
deployment_user = node['rails_ghetto']['deployment_user']
deployment_group = node['rails_ghetto']['deployment_group']

rails_applications do |name, app|
application name do
path "/var/www/apps/#{name}"
owner deployment_user
group deployment_group
deploy_key File.read(app['deploy_key']) if app['deploy_key']

before_symlink do
# TODO: figure out how to extract common part
if app['run_migrations']
execute 'create_and_migrate_database' do
command 'bundle exec rake db:create db:migrate'
user deployment_user
group deployment_group
cwd release_path
environment ({'RAILS_ENV' => app['rails_environment']})
end
end

if app['compile_assets']
execute 'compile_assets' do
command 'bundle exec rake assets:precompile'
user deployment_user
group deployment_group
cwd release_path
environment ({'RAILS_ENV' => app['rails_environment']})
end
end
end

repository app['repository']
revision app['revision']

rails do
gems ['bundler', 'rake']

if app.has_key? 'database'
database do
app['database'].each do |key, value|
send(key.to_sym, value)
end
end
end
end

unicorn do
port app['unicorn_port'].to_s
restart_command do
execute "/etc/init.d/#{name} restart" do
end
end
end
end
end
19 changes: 19 additions & 0 deletions recipes/nginx_sites_enable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nginx_site "default" do
enable false
end

rails_applications do |name, data, default|
template "/etc/nginx/sites-available/#{name}" do
source "nginx_site.erb"
mode 644
variables(
:application_name => name,
:default => default,
:unicorn_port => data["unicorn_port"]
)
end

nginx_site name do
enable true
end
end
22 changes: 22 additions & 0 deletions recipes/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
group node["rails_ghetto"]["deployment_group"]

user node["rails_ghetto"]["deployment_user"] do
comment "Deployment User"
home "/home/#{node["rails_ghetto"]["deployment_user"]}"
gid node["rails_ghetto"]["deployment_group"]
supports :manage_home => true
end

directory "/home/deploy/.ssh" do
owner node["rails_ghetto"]["deployment_user"]
group node["rails_ghetto"]["deployment_group"]
mode 0700
recursive true
end

file "/home/deploy/.ssh/authorized_keys" do
owner node["rails_ghetto"]["deployment_user"]
group node["rails_ghetto"]["deployment_group"]
mode 0600
content Array(node["rails_ghetto"]["authorized_keys"]).join("\n")
end
26 changes: 26 additions & 0 deletions templates/default/nginx_site.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by stack-rails/chef - do not edit manually
upstream <%= @application_name %>_server {
server 127.0.0.1:<%= @unicorn_port %> fail_timeout=0;
}

server {
listen 80 <%= @default ? "default" : "" %>;
server_name <%= @default ? "_" : @application_name %>;

client_max_body_size 4G;
keepalive_timeout 5;
root /var/www/apps/<%= @application_name %>/current/public;
try_files $uri/index.html $uri.html $uri @app;

location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://<%= @application_name %>_server;
}

error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/apps/sample/current/public;
}
}

0 comments on commit 9721a6a

Please sign in to comment.