django.rb 5.7 KB

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