django.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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.chef_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. if Chef::Config[:solo]
  95. Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
  96. else
  97. # Find the database master
  98. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_environment}", nil, 0, 1)
  99. rows = results[0]
  100. if rows.length == 1
  101. dbm = rows[0]
  102. end
  103. end
  104. end
  105. # we need the django version to render the correct type of settings.py file
  106. django_version = 1.2
  107. if app['pips'].has_key?('django') && !app['pips']['django'].strip.empty?
  108. django_version = app['pips']['django'].to_f
  109. end
  110. # Assuming we have one...
  111. if dbm
  112. # local_settings.py
  113. template "#{app['deploy_to']}/shared/#{local_settings_file_name}" do
  114. source "settings.py.erb"
  115. owner app["owner"]
  116. group app["group"]
  117. mode "644"
  118. variables(
  119. :host => (dbm.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  120. :database => app['databases'][node.chef_environment],
  121. :django_version => django_version
  122. )
  123. end
  124. else
  125. Chef::Log.warn("No node with role #{app["database_master_role"][0]}, #{local_settings_file_name} not rendered!")
  126. end
  127. end
  128. ## Then, deploy
  129. deploy_revision app['id'] do
  130. revision app['revision'][node.chef_environment]
  131. repository app['repository']
  132. user app['owner']
  133. group app['group']
  134. deploy_to app['deploy_to']
  135. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  136. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  137. shallow_clone true
  138. purge_before_symlink([])
  139. create_dirs_before_symlink([])
  140. symlinks({})
  141. before_migrate do
  142. requirements_file = nil
  143. # look for requirements.txt files in common locations
  144. if ::File.exists?(::File.join(release_path, "requirements", "#{node['chef_environment']}.txt"))
  145. requirements_file = ::File.join(release_path, "requirements", "#{node.chef_environment}.txt")
  146. elsif ::File.exists?(::File.join(release_path, "requirements.txt"))
  147. requirements_file = ::File.join(release_path, "requirements.txt")
  148. end
  149. if requirements_file
  150. Chef::Log.info("Installing pips using requirements file: #{requirements_file}")
  151. pip_cmd = File.join(ve.path, "bin", "pip")
  152. execute "#{pip_cmd} install -r #{requirements_file}" do
  153. ignore_failure true
  154. cwd release_path
  155. end
  156. end
  157. end
  158. symlink_before_migrate({
  159. local_settings_file_name => local_settings_full_path
  160. })
  161. if app['migrate'][node.chef_environment] && node['apps'][app['id']][node.chef_environment]['run_migrations']
  162. migrate true
  163. migration_command app['migration_command'] || "#{::File.join(ve.path, "bin", "python")} manage.py migrate"
  164. else
  165. migrate false
  166. end
  167. before_symlink do
  168. ruby_block "remove_run_migrations" do
  169. block do
  170. if node.role?("#{app['id']}_run_migrations")
  171. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  172. node.run_list.remove("role[#{app['id']}_run_migrations]")
  173. end
  174. end
  175. end
  176. end
  177. end