php.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if Chef::Config[:solo]
  88. Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
  89. else
  90. # Find the database master
  91. results = search(:node, "role:#{app["database_master_role"][0]} AND chef_environment:#{node.chef_environment}", nil, 0, 1)
  92. rows = results[0]
  93. if rows.length == 1
  94. dbm = rows[0]
  95. end
  96. end
  97. end
  98. # Assuming we have one...
  99. if dbm
  100. template "#{app['deploy_to']}/shared/#{local_settings_file_name}" do
  101. source "#{local_settings_file_name}.erb"
  102. cookbook app["id"]
  103. owner app["owner"]
  104. group app["group"]
  105. mode "644"
  106. variables(
  107. :path => "#{app['deploy_to']}/current",
  108. :host => (dbm.attribute?('cloud') ? dbm['cloud']['local_ipv4'] : dbm['ipaddress']),
  109. :database => app['databases'][node.chef_environment],
  110. :app => app
  111. )
  112. end
  113. else
  114. Chef::Log.warn("No node with role #{app['database_master_role'][0]}, #{local_settings_file_name} not rendered!")
  115. end
  116. end
  117. ## Then, deploy
  118. deploy_revision app['id'] do
  119. revision app['revision'][node.chef_environment]
  120. repository app['repository']
  121. user app['owner']
  122. group app['group']
  123. deploy_to app['deploy_to']
  124. action app['force'][node.chef_environment] ? :force_deploy : :deploy
  125. ssh_wrapper "#{app['deploy_to']}/deploy-ssh-wrapper" if app['deploy_key']
  126. shallow_clone true
  127. purge_before_symlink([])
  128. create_dirs_before_symlink([])
  129. symlinks({})
  130. symlink_before_migrate({
  131. local_settings_file_name => local_settings_full_path
  132. })
  133. end