rails.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. # Find the database master
  95. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_environment}", nil, 0, 1)
  96. rows = results[0]
  97. if rows.length == 1
  98. dbm = rows[0]
  99. end
  100. end
  101. # Assuming we have one...
  102. if dbm
  103. template "#{app['deploy_to']}/shared/database.yml" do
  104. source "database.yml.erb"
  105. owner app["owner"]
  106. group app["group"]
  107. mode "644"
  108. variables(
  109. :host => (dbm.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  110. :databases => app['databases'],
  111. :rails_env => rails_env
  112. )
  113. end
  114. else
  115. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, database.yml not rendered!")
  116. end
  117. end
  118. if app["memcached_role"]
  119. results = search(:node, "role:#{app["memcached_role"][0]} AND chef_environment:#{node.chef_environment} NOT hostname:#{node[:hostname]}")
  120. if results.length == 0
  121. if node.run_list.roles.include?(app["memcached_role"][0])
  122. results << node
  123. end
  124. end
  125. template "#{app['deploy_to']}/shared/memcached.yml" do
  126. source "memcached.yml.erb"
  127. owner app["owner"]
  128. group app["group"]
  129. mode "644"
  130. variables(
  131. :memcached_envs => app['memcached'],
  132. :hosts => results.sort_by { |r| r.name }
  133. )
  134. end
  135. end
  136. ## Then, deploy
  137. deploy_revision app['id'] do
  138. revision app['revision'][node.chef_environment]
  139. repository app['repository']
  140. user app['owner']
  141. group app['group']
  142. deploy_to app['deploy_to']
  143. environment 'RAILS_ENV' => rails_env
  144. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  145. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  146. shallow_clone true
  147. before_migrate do
  148. if app['gems'].has_key?('bundler')
  149. link "#{release_path}/vendor/bundle" do
  150. to "#{app['deploy_to']}/shared/vendor_bundle"
  151. end
  152. common_groups = %w{development test cucumber staging production}
  153. execute "bundle install --deployment --without #{(common_groups -([node.chef_environment])).join(' ')}" do
  154. ignore_failure true
  155. cwd release_path
  156. end
  157. elsif app['gems'].has_key?('bundler08')
  158. execute "gem bundle" do
  159. ignore_failure true
  160. cwd release_path
  161. end
  162. elsif node.chef_environment && app['databases'].has_key?(node.chef_environment)
  163. # chef runs before_migrate, then symlink_before_migrate symlinks, then migrations,
  164. # yet our before_migrate needs database.yml to exist (and must complete before
  165. # migrations).
  166. #
  167. # maybe worth doing run_symlinks_before_migrate before before_migrate callbacks,
  168. # or an add'l callback.
  169. execute "(ln -s ../../../shared/database.yml config/database.yml && rake gems:install); rm config/database.yml" do
  170. ignore_failure true
  171. cwd release_path
  172. end
  173. end
  174. end
  175. symlink_before_migrate({
  176. "database.yml" => "config/database.yml",
  177. "memcached.yml" => "config/memcached.yml"
  178. })
  179. if app['migrate'][node.chef_environment] && node[:apps][app['id']][node.chef_environment][:run_migrations]
  180. migrate true
  181. migration_command app['migration_command'] || "rake db:migrate"
  182. else
  183. migrate false
  184. end
  185. before_symlink do
  186. ruby_block "remove_run_migrations" do
  187. block do
  188. if node.role?("#{app['id']}_run_migrations")
  189. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  190. node.run_list.remove("role[#{app['id']}_run_migrations]")
  191. end
  192. end
  193. end
  194. end
  195. end