rails.rb 6.2 KB

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