rails.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. directory "#{app['deploy_to']}/shared/log" do
  68. owner app['owner']
  69. group app['group']
  70. mode '0755'
  71. recursive true
  72. end
  73. directory "#{app['deploy_to']}/shared/pids" do
  74. owner app['owner']
  75. group app['group']
  76. mode '0755'
  77. recursive true
  78. end
  79. if app.has_key?("deploy_key")
  80. ruby_block "write_key" do
  81. block do
  82. f = File.open("#{app['deploy_to']}/id_deploy", "w")
  83. f.print(app["deploy_key"])
  84. f.close
  85. end
  86. not_if do File.exists?("#{app['deploy_to']}/id_deploy"); end
  87. end
  88. file "#{app['deploy_to']}/id_deploy" do
  89. owner app['owner']
  90. group app['group']
  91. mode '0600'
  92. end
  93. template "#{app['deploy_to']}/deploy-ssh-wrapper" do
  94. source "deploy-ssh-wrapper.erb"
  95. owner app['owner']
  96. group app['group']
  97. mode "0755"
  98. variables app.to_hash
  99. end
  100. end
  101. if app["database_master_role"]
  102. dbm = nil
  103. # If we are the database master
  104. if node.run_list.roles.include?(app["database_master_role"][0])
  105. dbm = node
  106. else
  107. # Find the database master
  108. results = search(:node, "run_list:role\\[#{app["database_master_role"][0]}\\] AND app_environment:#{node[:app_environment]}", nil, 0, 1)
  109. rows = results[0]
  110. if rows.length == 1
  111. dbm = rows[0]
  112. end
  113. end
  114. # Assuming we have one...
  115. if dbm
  116. template "#{app['deploy_to']}/shared/database.yml" do
  117. source "database.yml.erb"
  118. owner app["owner"]
  119. group app["group"]
  120. mode "644"
  121. variables(
  122. :host => dbm['fqdn'],
  123. :databases => app['databases']
  124. )
  125. end
  126. else
  127. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, database.yml not rendered!")
  128. end
  129. end
  130. if app["memcached_role"]
  131. results = search(:node, "role:#{app["memcached_role"][0]} AND app_environment:#{node[:app_environment]} NOT hostname:#{node[:hostname]}")
  132. if results.length == 0
  133. if node.run_list.roles.include?(app["memcached_role"][0])
  134. results << node
  135. end
  136. end
  137. template "#{app['deploy_to']}/shared/memcached.yml" do
  138. source "memcached.yml.erb"
  139. owner app["owner"]
  140. group app["group"]
  141. mode "644"
  142. variables(
  143. :memcached_envs => app['memcached'],
  144. :hosts => results
  145. )
  146. end
  147. end
  148. ## Then, deploy
  149. deploy_revision app['id'] do
  150. revision app['revision'][node.app_environment]
  151. repository app['repository']
  152. user app['owner']
  153. group app['group']
  154. deploy_to app['deploy_to']
  155. environment 'RAILS_ENV' => node.app_environment
  156. action app['force'][node.app_environment] ? :force_deploy : :deploy
  157. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  158. before_migrate do
  159. if app['gems'].has_key?('bundler')
  160. execute "bundle install" do
  161. ignore_failure true
  162. cwd release_path
  163. end
  164. elsif app['gems'].has_key?('bundler08')
  165. execute "gem bundle" do
  166. ignore_failure true
  167. cwd release_path
  168. end
  169. elsif node.app_environment && app['databases'].has_key?(node.app_environment)
  170. # chef runs before_migrate, then symlink_before_migrate symlinks, then migrations,
  171. # yet our before_migrate needs database.yml to exist (and must complete before
  172. # migrations).
  173. #
  174. # maybe worth doing run_symlinks_before_migrate before before_migrate callbacks,
  175. # or an add'l callback.
  176. execute "(ln -s ../../../shared/database.yml config/database.yml && rake gems:install); rm config/database.yml" do
  177. ignore_failure true
  178. cwd release_path
  179. end
  180. end
  181. end
  182. symlink_before_migrate({
  183. "database.yml" => "config/database.yml",
  184. "memcached.yml" => "config/memcached.yml"
  185. })
  186. if app['migrate'][node.app_environment] && node[:apps][app['id']][node.app_environment][:run_migrations]
  187. migrate true
  188. migration_command "rake db:migrate"
  189. else
  190. migrate false
  191. end
  192. end