django.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_file_name = app[:local_settings_file_name] || 'settings_local.py'
  28. ## Create required directories
  29. directory app['deploy_to'] do
  30. owner app['owner']
  31. group app['group']
  32. mode '0755'
  33. recursive true
  34. end
  35. directory "#{app['deploy_to']}/shared" do
  36. owner app['owner']
  37. group app['group']
  38. mode '0755'
  39. recursive true
  40. end
  41. ## Create a virtualenv for the app
  42. ve = python_virtualenv "#{app['id']}_env" do
  43. path "#{app['deploy_to']}/shared/env"
  44. action :create
  45. end
  46. ## First, install any application specific packages
  47. if app['packages']
  48. app['packages'].each do |pkg,ver|
  49. package pkg do
  50. action :install
  51. version ver if ver && ver.length > 0
  52. end
  53. end
  54. end
  55. ## Next, install any application specific gems
  56. if app['pips']
  57. app['pips'].each do |pip,ver|
  58. python_pip pip do
  59. version ver if ver && ver.length > 0
  60. virtualenv ve.path
  61. action :install
  62. end
  63. end
  64. end
  65. if app.has_key?("deploy_key")
  66. ruby_block "write_key" do
  67. block do
  68. f = ::File.open("#{app['deploy_to']}/id_deploy", "w")
  69. f.print(app["deploy_key"])
  70. f.close
  71. end
  72. not_if do ::File.exists?("#{app['deploy_to']}/id_deploy"); end
  73. end
  74. file "#{app['deploy_to']}/id_deploy" do
  75. owner app['owner']
  76. group app['group']
  77. mode '0600'
  78. end
  79. template "#{app['deploy_to']}/deploy-ssh-wrapper" do
  80. source "deploy-ssh-wrapper.erb"
  81. owner app['owner']
  82. group app['group']
  83. mode "0755"
  84. variables app.to_hash
  85. end
  86. end
  87. if app["database_master_role"]
  88. dbm = nil
  89. # If we are the database master
  90. if node.run_list.roles.include?(app["database_master_role"][0])
  91. dbm = node
  92. else
  93. # Find the database master
  94. results = search(:node, "run_list:role\\[#{app["database_master_role"][0]}\\] AND app_environment:#{node[:app_environment]}", nil, 0, 1)
  95. rows = results[0]
  96. if rows.length == 1
  97. dbm = rows[0]
  98. end
  99. end
  100. # we need the django version to render the correct type of settings.py file
  101. django_version = 1.2
  102. if app['pips'].has_key?('django') && !app['pips']['django'].blank?
  103. django_version = app['pips']['django'].to_f
  104. end
  105. # Assuming we have one...
  106. if dbm
  107. # local_settings.py
  108. template "#{app['deploy_to']}/shared/settings_local.py" do
  109. source "settings.py.erb"
  110. owner app["owner"]
  111. group app["group"]
  112. mode "644"
  113. variables(
  114. :host => dbm['fqdn'],
  115. :database => app['databases'][node.app_environment],
  116. :django_version => django_version
  117. )
  118. end
  119. else
  120. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, settings_local.py not rendered!")
  121. end
  122. end
  123. ## Then, deploy
  124. deploy_revision app['id'] do
  125. revision app['revision'][node.app_environment]
  126. repository app['repository']
  127. user app['owner']
  128. group app['group']
  129. deploy_to app['deploy_to']
  130. action app['force'][node.app_environment] ? :force_deploy : :deploy
  131. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  132. purge_before_symlink([])
  133. create_dirs_before_symlink([])
  134. symlinks({})
  135. before_migrate do
  136. requirements_file = nil
  137. # look for requirements.txt files in common locations
  138. if ::File.exists?(::File.join(release_path, "requirements", "#{node[:app_environment]}.txt"))
  139. requirements_file = ::File.join(release_path, "requirements", "#{node[:app_environment]}.txt")
  140. elsif ::File.exists?(::File.join(release_path, "requirements.txt"))
  141. requirements_file = ::File.join(release_path, "requirements.txt")
  142. end
  143. if requirements_file
  144. Chef::Log.info("Installing pips using requirements file: #{requirements_file}")
  145. execute "pip install -E #{ve.path} -r #{requirements_file}" do
  146. ignore_failure true
  147. cwd release_path
  148. end
  149. end
  150. end
  151. symlink_before_migrate({
  152. "settings_local.py" => local_settings_file_name
  153. })
  154. if app['migrate'][node.app_environment] && node[:apps][app['id']][node.app_environment][:run_migrations]
  155. migrate true
  156. migration_command app['migration_command'] || "#{::File.join(ve.path, "bin", "python")} manage.py migrate"
  157. else
  158. migrate false
  159. end
  160. before_symlink do
  161. ruby_block "remove_run_migrations" do
  162. block do
  163. if node.role?("#{app['id']}_run_migrations")
  164. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  165. node.run_list.remove("role[#{app['id']}_run_migrations]")
  166. end
  167. end
  168. end
  169. end
  170. end