django.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #
  2. # Cookbook Name:: application
  3. # Recipe:: wsgi
  4. #
  5. # Copyright 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. include_recipe "python"
  21. ###
  22. # You really most likely don't want to run this recipe from here - let the
  23. # default application recipe work it's mojo for you.
  24. ###
  25. node.default[:apps][app['id']][node.app_environment][:run_migrations] = false
  26. # the Django split-settings file name varies from project to project...+1 for standardization
  27. local_settings_full_path = app['local_settings_file'] || 'settings_local.py'
  28. local_settings_file_name = local_settings_full_path.split(/[\\\/]/).last
  29. ## Create required directories
  30. directory app['deploy_to'] do
  31. owner app['owner']
  32. group app['group']
  33. mode '0755'
  34. recursive true
  35. end
  36. directory "#{app['deploy_to']}/shared" do
  37. owner app['owner']
  38. group app['group']
  39. mode '0755'
  40. recursive true
  41. end
  42. ## Create a virtualenv for the app
  43. ve = python_virtualenv app['id'] do
  44. path "#{app['deploy_to']}/shared/env"
  45. action :create
  46. end
  47. ## First, install any application specific packages
  48. if app['packages']
  49. app['packages'].each do |pkg,ver|
  50. package pkg do
  51. action :install
  52. version ver if ver && ver.length > 0
  53. end
  54. end
  55. end
  56. ## Next, install any application specific gems
  57. if app['pips']
  58. app['pips'].each do |pip,ver|
  59. python_pip pip do
  60. version ver if ver && ver.length > 0
  61. virtualenv ve.path
  62. action :install
  63. end
  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, "run_list:role\\[#{app["database_master_role"][0]}\\] AND app_environment:#{node[:app_environment]}", nil, 0, 1)
  96. rows = results[0]
  97. if rows.length == 1
  98. dbm = rows[0]
  99. end
  100. end
  101. # we need the django version to render the correct type of settings.py file
  102. django_version = 1.2
  103. if app['pips'].has_key?('django') && !app['pips']['django'].blank?
  104. django_version = app['pips']['django'].to_f
  105. end
  106. # Assuming we have one...
  107. if dbm
  108. # local_settings.py
  109. template "#{app['deploy_to']}/shared/#{local_settings_file_name}" do
  110. source "settings.py.erb"
  111. owner app["owner"]
  112. group app["group"]
  113. mode "644"
  114. variables(
  115. :host => dbm['fqdn'],
  116. :database => app['databases'][node.app_environment],
  117. :django_version => django_version
  118. )
  119. end
  120. else
  121. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, #{local_settings_file_name} not rendered!")
  122. end
  123. end
  124. ## Then, deploy
  125. deploy_revision app['id'] do
  126. revision app['revision'][node.app_environment]
  127. repository app['repository']
  128. user app['owner']
  129. group app['group']
  130. deploy_to app['deploy_to']
  131. action app['force'][node.app_environment] ? :force_deploy : :deploy
  132. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  133. purge_before_symlink([])
  134. create_dirs_before_symlink([])
  135. symlinks({})
  136. before_migrate do
  137. requirements_file = nil
  138. # look for requirements.txt files in common locations
  139. if ::File.exists?(::File.join(release_path, "requirements", "#{node[:app_environment]}.txt"))
  140. requirements_file = ::File.join(release_path, "requirements", "#{node[:app_environment]}.txt")
  141. elsif ::File.exists?(::File.join(release_path, "requirements.txt"))
  142. requirements_file = ::File.join(release_path, "requirements.txt")
  143. end
  144. if requirements_file
  145. Chef::Log.info("Installing pips using requirements file: #{requirements_file}")
  146. execute "pip install -E #{ve.path} -r #{requirements_file}" do
  147. ignore_failure true
  148. cwd release_path
  149. end
  150. end
  151. end
  152. symlink_before_migrate({
  153. local_settings_file_name => local_settings_full_path
  154. })
  155. if app['migrate'][node.app_environment] && node[:apps][app['id']][node.app_environment][:run_migrations]
  156. migrate true
  157. migration_command app['migration_command'] || "#{::File.join(ve.path, "bin", "python")} manage.py migrate"
  158. else
  159. migrate false
  160. end
  161. before_symlink do
  162. ruby_block "remove_run_migrations" do
  163. block do
  164. if node.role?("#{app['id']}_run_migrations")
  165. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  166. node.run_list.remove("role[#{app['id']}_run_migrations]")
  167. end
  168. end
  169. end
  170. end
  171. end