php.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #
  2. # Cookbook Name:: application
  3. # Recipe:: php
  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 "php"
  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 PHP projects have no standard local settings file name..or path in the project
  27. local_settings_full_path = app['local_settings_file'] || 'LocalSettings.php'
  28. local_settings_file_name = local_settings_full_path.split(/[\\\/]/).last
  29. ## First, install any application specific packages
  30. if app['packages']
  31. app['packages'].each do |pkg,ver|
  32. package pkg do
  33. action :install
  34. version ver if ver && ver.length > 0
  35. end
  36. end
  37. end
  38. ## Next, install any application specific gems
  39. if app['pears']
  40. app['pears'].each do |pear,ver|
  41. php_pear pear do
  42. action :install
  43. version ver if ver && ver.length > 0
  44. end
  45. end
  46. end
  47. directory app['deploy_to'] do
  48. owner app['owner']
  49. group app['group']
  50. mode '0755'
  51. recursive true
  52. end
  53. directory "#{app['deploy_to']}/shared" do
  54. owner app['owner']
  55. group app['group']
  56. mode '0755'
  57. recursive true
  58. end
  59. if app.has_key?("deploy_key")
  60. ruby_block "write_key" do
  61. block do
  62. f = ::File.open("#{app['deploy_to']}/id_deploy", "w")
  63. f.print(app["deploy_key"])
  64. f.close
  65. end
  66. not_if do ::File.exists?("#{app['deploy_to']}/id_deploy"); end
  67. end
  68. file "#{app['deploy_to']}/id_deploy" do
  69. owner app['owner']
  70. group app['group']
  71. mode '0600'
  72. end
  73. template "#{app['deploy_to']}/deploy-ssh-wrapper" do
  74. source "deploy-ssh-wrapper.erb"
  75. owner app['owner']
  76. group app['group']
  77. mode "0755"
  78. variables app.to_hash
  79. end
  80. end
  81. if app["database_master_role"]
  82. dbm = nil
  83. # If we are the database master
  84. if node.run_list.roles.include?(app["database_master_role"][0])
  85. dbm = node
  86. else
  87. # Find the database master
  88. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_environment}", nil, 0, 1)
  89. rows = results[0]
  90. if rows.length == 1
  91. dbm = rows[0]
  92. end
  93. end
  94. # Assuming we have one...
  95. if dbm
  96. template "#{app['deploy_to']}/shared/#{local_settings_file_name}" do
  97. source "#{local_settings_file_name}.erb"
  98. cookbook app["id"]
  99. owner app["owner"]
  100. group app["group"]
  101. mode "644"
  102. variables(
  103. :path => "#{app['deploy_to']}/current",
  104. :host => (dbm.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  105. :database => app['databases'][node.chef_environment],
  106. :app => app
  107. )
  108. end
  109. else
  110. Chef::Log.warn("No node with role #{app['database_master_role'][0]}, #{local_settings_file_name} not rendered!")
  111. end
  112. end
  113. ## Then, deploy
  114. deploy_revision app['id'] do
  115. revision app['revision'][node.chef_environment]
  116. repository app['repository']
  117. user app['owner']
  118. group app['group']
  119. deploy_to app['deploy_to']
  120. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  121. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  122. shallow_clone true
  123. purge_before_symlink([])
  124. create_dirs_before_symlink([])
  125. symlinks({})
  126. symlink_before_migrate({
  127. local_settings_file_name => local_settings_full_path
  128. })
  129. end