rails.rb 5.4 KB

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