rails_nginx_ree_passenger.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #
  2. # Cookbook Name:: application
  3. # Recipe:: default
  4. #
  5. # Copyright 2009, Opscode, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. include_recipe "ruby_enterprise"
  20. runit_service "nginx"
  21. search(:apps) do |app|
  22. if (app["server_roles"] & node.run_list.roles).length > 0
  23. node.default[:apps][app['id']][node.app_environment][:run_migrations] = false
  24. ## First, install any application specific packages
  25. if app['packages']
  26. app['packages'].each do |pkg,ver|
  27. package pkg do
  28. action :install
  29. version ver if ver && ver.length > 0
  30. end
  31. end
  32. end
  33. ## Next, install any application specific gems
  34. if app['gems']
  35. app['gems'].each do |gem,ver|
  36. ree_gem gem do
  37. action :install
  38. version ver if ver && ver.length > 0
  39. end
  40. end
  41. end
  42. ## Then, configure nginx
  43. template "#{node[:nginx][:dir]}/sites-available/#{app['id']}.conf" do
  44. source "rails_nginx_passenger.conf.erb"
  45. owner "root"
  46. group "root"
  47. mode "0644"
  48. variables(
  49. :app => app['id'],
  50. :docroot => "/srv/#{app['id']}/current/public",
  51. :server_name => "#{app['id']}.#{node[:domain]}",
  52. :server_aliases => [ node[:fqdn], app['id'] ],
  53. :rails_env => app['environment']
  54. )
  55. end
  56. nginx_site "#{app['id']}.conf" do
  57. notifies :restart, resources(:service => "nginx")
  58. end
  59. directory app['deploy_to'] do
  60. owner app['owner']
  61. group app['group']
  62. mode '0755'
  63. recursive true
  64. end
  65. directory "#{app['deploy_to']}/shared" do
  66. owner app['owner']
  67. group app['group']
  68. mode '0755'
  69. recursive true
  70. end
  71. directory "#{app['deploy_to']}/shared/log" do
  72. owner app['owner']
  73. group app['group']
  74. mode '0755'
  75. recursive true
  76. end
  77. if app.has_key?("deploy_key")
  78. ruby_block "write_key" do
  79. block do
  80. f = File.open("#{app['deploy_to']}/id_deploy", "w")
  81. f.print(app["deploy_key"])
  82. f.close
  83. end
  84. not_if do File.exists?("#{app['deploy_to']}/id_deploy"); end
  85. end
  86. file "#{app['deploy_to']}/id_deploy" do
  87. owner app['owner']
  88. group app['group']
  89. mode '0600'
  90. end
  91. template "#{app['deploy_to']}/deploy-ssh-wrapper" do
  92. source "deploy-ssh-wrapper.erb"
  93. owner app['owner']
  94. group app['group']
  95. mode "0755"
  96. variables app.to_hash
  97. end
  98. end
  99. ## Then, deploy
  100. deploy_revision app['id'] do
  101. revision app['revision'][node.app_environment]
  102. repository app['repository']
  103. user app['owner']
  104. group app['group']
  105. deploy_to app['deploy_to']
  106. action app['force'][node.app_environment] ? :force_deploy : :deploy
  107. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  108. if app['migrate'][node.app_environment] && node[:apps][app['id']][node.app_environment][:run_migrations]
  109. migrate true
  110. migration_command "rake db:migrate"
  111. else
  112. migrate false
  113. end
  114. restart_command do
  115. case app["type"]
  116. when /nginx/
  117. service "nginx" do action :restart; end
  118. when /apache/
  119. service "apache" do action :restart; end
  120. end
  121. end
  122. symlink_before_migrate({
  123. "database.yml" => "config/database.yml",
  124. "memcached.yml" => "config/memcached.yml"
  125. })
  126. before_symlink do
  127. if app["database_master_role"]
  128. results = search(:node, "run_list:role\\[#{app["database_master_role"][0]}\\]", nil, 0, 1)
  129. rows = results[0]
  130. if rows.length == 1
  131. dbm = rows[0]
  132. template "#{@new_resource.shared_path}/database.yml" do
  133. source "database.yml.erb"
  134. owner app["owner"]
  135. group app["group"]
  136. mode "644"
  137. variables(
  138. :host => dbm['fqdn'],
  139. :databases => app['databases']
  140. )
  141. end
  142. else
  143. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, database.yml not rendered!")
  144. end
  145. end
  146. if app["memcached_role"]
  147. results = search(:node, "run_list:role\\[#{app["memcached_role"][0]}\\]")
  148. rows = results[0]
  149. template "#{@new_resource.shared_path}/memcached.yml" do
  150. source "memcached.yml.erb"
  151. owner app["owner"]
  152. group app["group"]
  153. mode "644"
  154. variables(
  155. :memcached_envs => app['memcached'],
  156. :hosts => rows
  157. )
  158. end
  159. end
  160. end
  161. end
  162. end
  163. end