Skip to content

Instantly share code, notes, and snippets.

@rsim
Created September 17, 2009 13:12
Show Gist options
  • Save rsim/188481 to your computer and use it in GitHub Desktop.
Save rsim/188481 to your computer and use it in GitHub Desktop.
include_recipe "passenger_apache2::mod_rails"
if node[:rails][:active_applications]
node[:rails][:active_applications].each do |app, confs|
# confs = [confs] unless confs.is_a?(Array)
rails_app = node[:rails][:applications][app]
if rails_app[:rails_version]
%w{ rails actionmailer actionpack activerecord activesupport activeresource }.each do |rails_gem|
gem_package rails_gem do
version rails_app[:rails_version]
action :install
end
end
end
rails_app[:packages].to_a.each do |package_name|
package package_name
end
rails_app[:gems].to_a.each do |g|
gem_name, gem_version = g
gem_package gem_name do
version gem_version if gem_version
end
end
rails_app[:apache_modules].to_a.each do |mod|
include_recipe "apache2::mod_#{mod}"
end
case rails_app[:database_adapter]
when "mysql"
execute "Set mysql root password" do
command "mysqladmin -u root status >/dev/null && mysqladmin -u root password \"#{@node[:mysql][:server_root_password]}\" || echo \"Password already changed\""
# only if can connect without password
only_if "mysqladmin -u root status"
end
end
confs.each do |conf|
full_name = "#{app}_#{conf[:env]}"
conf_http_port = (conf[:http_port] || 80).to_s
node[:apache][:listen_ports] = node[:apache][:listen_ports] << conf_http_port unless node[:apache][:listen_ports].include?(conf_http_port)
cap_setup "/srv/#{full_name}" do
owner "rails"
group "rails"
mode 0775
end
case rails_app[:database_adapter]
when "mysql"
database = conf[:database] || "#{app}_#{conf[:env]}"
template "/srv/#{full_name}/shared/config/database.yml" do
source "database.yml.erb"
variables :app => app, :database_adapter => rails_app[:database_adapter], :database => database, :conf => conf
owner "rails"
group "rails"
mode 0775
end
execute "Create MySQL database #{database}" do
command "mysql -u root -p#{node[:mysql][:server_root_password]} " <<
" -e 'CREATE DATABASE `#{database}` DEFAULT CHARACTER SET `utf8` COLLATE `#{rails_app[:database_collation] || 'utf8_unicode_ci'}`;'"
not_if { File.directory?("#{node[:mysql][:datadir]}/#{database}") }
end
end
web_app full_name do
docroot "/srv/#{full_name}/current/public"
template "rails_app.conf.erb"
server_name conf[:server_name] || node[:hostname]
server_aliases conf[:server_aliases] if conf[:server_aliases]
http_port conf_http_port
rails_env conf[:env]
rails_spawn_method rails_app[:rails_spawn_method] || 'smart-lv2'
end
logrotate full_name do
files "/srv/#{full_name}/current/log/*.log"
frequency conf[:logrotate_frequency] || "weekly"
rotate_count conf[:logrotate_count] || 10
restart_command "touch /srv/#{full_name}/current/tmp/restart.txt"
end
end
end
else
Chef::Log.info "Add an :active_applications attribute to configure this node's apps"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment