rails.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #
  2. # Cookbook Name:: application
  3. # Recipe:: rails
  4. #
  5. # Copyright 2009-2011, 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. Chef::Log.warn "The application::rails recipe is deprecated and will go away on Aug 1st. See the README for migration information."
  20. app = node.run_state[:current_app]
  21. # make the _default chef_environment look like the Rails production environment
  22. rails_env = (node.chef_environment =~ /_default/ ? "production" : node.chef_environment)
  23. node.run_state[:rails_env] = rails_env
  24. ###
  25. # You really most likely don't want to run this recipe from here - let the
  26. # default application recipe work it's mojo for you.
  27. ###
  28. node.default[:apps][app['id']][node.chef_environment][:run_migrations] = false
  29. ## First, install any application specific packages
  30. if app['packages']
  31. app['packages'].each do |pkg,ver|
  32. package pkg do
  33. action :install
  34. version ver if ver && ver.length > 0
  35. end
  36. end
  37. end
  38. ## Next, install any application specific gems
  39. if app['gems']
  40. app['gems'].each do |gem,ver|
  41. gem_package gem do
  42. action :install
  43. version ver if ver && ver.length > 0
  44. end
  45. end
  46. end
  47. directory app['deploy_to'] do
  48. owner app['owner']
  49. group app['group']
  50. mode '0755'
  51. recursive true
  52. end
  53. directory "#{app['deploy_to']}/shared" do
  54. owner app['owner']
  55. group app['group']
  56. mode '0755'
  57. recursive true
  58. end
  59. %w{ log pids system vendor_bundle }.each do |dir|
  60. directory "#{app['deploy_to']}/shared/#{dir}" do
  61. owner app['owner']
  62. group app['group']
  63. mode '0755'
  64. recursive true
  65. end
  66. end
  67. if app.has_key?("deploy_key")
  68. ruby_block "write_key" do
  69. block do
  70. f = ::File.open("#{app['deploy_to']}/id_deploy", "w")
  71. f.print(app["deploy_key"])
  72. f.close
  73. end
  74. not_if do ::File.exists?("#{app['deploy_to']}/id_deploy"); end
  75. end
  76. file "#{app['deploy_to']}/id_deploy" do
  77. owner app['owner']
  78. group app['group']
  79. mode '0600'
  80. end
  81. template "#{app['deploy_to']}/deploy-ssh-wrapper" do
  82. source "deploy-ssh-wrapper.erb"
  83. owner app['owner']
  84. group app['group']
  85. mode "0755"
  86. variables app.to_hash
  87. end
  88. end
  89. if app["database_master_role"]
  90. dbm = nil
  91. # If we are the database master
  92. if node.run_list.roles.include?(app["database_master_role"][0])
  93. dbm = node
  94. else
  95. # Find the database master
  96. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_environment}", nil, 0, 1)
  97. rows = results[0]
  98. if rows.length == 1
  99. dbm = rows[0]
  100. end
  101. end
  102. # Assuming we have one...
  103. if dbm
  104. template "#{app['deploy_to']}/shared/database.yml" do
  105. source "database.yml.erb"
  106. owner app["owner"]
  107. group app["group"]
  108. mode "644"
  109. variables(
  110. :host => (dbm.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  111. :databases => app['databases'],
  112. :rails_env => rails_env
  113. )
  114. end
  115. else
  116. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, database.yml not rendered!")
  117. end
  118. end
  119. if app["memcached_role"]
  120. results = search(:node, "role:#{app["memcached_role"][0]} AND chef_environment:#{node.chef_environment} NOT hostname:#{node[:hostname]}")
  121. if results.length == 0
  122. if node.run_list.roles.include?(app["memcached_role"][0])
  123. results << node
  124. end
  125. end
  126. template "#{app['deploy_to']}/shared/memcached.yml" do
  127. source "memcached.yml.erb"
  128. owner app["owner"]
  129. group app["group"]
  130. mode "644"
  131. variables(
  132. :memcached_envs => app['memcached'],
  133. :hosts => results.sort_by { |r| r.name }
  134. )
  135. end
  136. end
  137. ## Then, deploy
  138. deploy_revision app['id'] do
  139. revision app['revision'][node.chef_environment]
  140. repository app['repository']
  141. user app['owner']
  142. group app['group']
  143. deploy_to app['deploy_to']
  144. environment 'RAILS_ENV' => rails_env
  145. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  146. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  147. shallow_clone true
  148. before_migrate do
  149. if app['gems'].has_key?('bundler')
  150. link "#{release_path}/vendor/bundle" do
  151. to "#{app['deploy_to']}/shared/vendor_bundle"
  152. end
  153. common_groups = %w{development test cucumber staging production}
  154. execute "bundle install --deployment --without #{(common_groups -([node.chef_environment])).join(' ')}" do
  155. ignore_failure true
  156. cwd release_path
  157. end
  158. elsif app['gems'].has_key?('bundler08')
  159. execute "gem bundle" do
  160. ignore_failure true
  161. cwd release_path
  162. end
  163. elsif node.chef_environment && app['databases'].has_key?(node.chef_environment)
  164. # chef runs before_migrate, then symlink_before_migrate symlinks, then migrations,
  165. # yet our before_migrate needs database.yml to exist (and must complete before
  166. # migrations).
  167. #
  168. # maybe worth doing run_symlinks_before_migrate before before_migrate callbacks,
  169. # or an add'l callback.
  170. execute "(ln -s ../../../shared/database.yml config/database.yml && rake gems:install); rm config/database.yml" do
  171. ignore_failure true
  172. cwd release_path
  173. end
  174. end
  175. end
  176. symlink_before_migrate({
  177. "database.yml" => "config/database.yml",
  178. "memcached.yml" => "config/memcached.yml"
  179. })
  180. if app['migrate'][node.chef_environment] && node[:apps][app['id']][node.chef_environment][:run_migrations]
  181. migrate true
  182. migration_command app['migration_command'] || "rake db:migrate"
  183. else
  184. migrate false
  185. end
  186. before_symlink do
  187. ruby_block "remove_run_migrations" do
  188. block do
  189. if node.role?("#{app['id']}_run_migrations")
  190. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  191. node.run_list.remove("role[#{app['id']}_run_migrations]")
  192. end
  193. end
  194. end
  195. end
  196. end