rails.rb 5.8 KB

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