django.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. # Find the database master
  95. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_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'].strip.empty?
  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.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  116. :database => app['databases'][node.chef_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.chef_environment]
  127. repository app['repository']
  128. user app['owner']
  129. group app['group']
  130. deploy_to app['deploy_to']
  131. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  132. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  133. shallow_clone true
  134. purge_before_symlink([])
  135. create_dirs_before_symlink([])
  136. symlinks({})
  137. before_migrate do
  138. requirements_file = nil
  139. # look for requirements.txt files in common locations
  140. if ::File.exists?(::File.join(release_path, "requirements", "#{node.chef_environment}.txt"))
  141. requirements_file = ::File.join(release_path, "requirements", "#{node.chef_environment}.txt")
  142. elsif ::File.exists?(::File.join(release_path, "requirements.txt"))
  143. requirements_file = ::File.join(release_path, "requirements.txt")
  144. end
  145. if requirements_file
  146. Chef::Log.info("Installing pips using requirements file: #{requirements_file}")
  147. pip_cmd = File.join(ve.path, "bin", "pip")
  148. execute "#{pip_cmd} install -r #{requirements_file}" do
  149. ignore_failure true
  150. cwd release_path
  151. end
  152. end
  153. end
  154. symlink_before_migrate({
  155. local_settings_file_name => local_settings_full_path
  156. })
  157. if app['migrate'][node.chef_environment] && node[:apps][app['id']][node.chef_environment][:run_migrations]
  158. migrate true
  159. migration_command app['migration_command'] || "#{::File.join(ve.path, "bin", "python")} manage.py migrate"
  160. else
  161. migrate false
  162. end
  163. before_symlink do
  164. ruby_block "remove_run_migrations" do
  165. block do
  166. if node.role?("#{app['id']}_run_migrations")
  167. Chef::Log.info("Migrations were run, removing role[#{app['id']}_run_migrations]")
  168. node.run_list.remove("role[#{app['id']}_run_migrations]")
  169. end
  170. end
  171. end
  172. end
  173. end