|
| 1 | +# |
| 2 | +# Author:: Noah Kantrowitz <noah@opscode.com> |
| 3 | +# Cookbook Name:: application_python |
| 4 | +# Provider:: django |
| 5 | +# |
| 6 | +# Copyright:: 2011, Opscode, Inc <legal@opscode.com> |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +include Chef::Mixin::LanguageIncludeRecipe |
| 22 | + |
| 23 | +action :before_compile do |
| 24 | + |
| 25 | + include_recipe "supervisor" |
| 26 | + |
| 27 | + raise "You must specify an application module to load" unless new_resource.config |
| 28 | + |
| 29 | + if !new_resource.restart_command |
| 30 | + new_resource.restart_command do |
| 31 | + run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celeryd").run_action(:restart) if new_resource.celeryd |
| 32 | + run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celerybeat").run_action(:restart) if new_resource.celerybeat |
| 33 | + run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celerycam").run_action(:restart) if new_resource.celerycam |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + new_resource.symlink_before_migrate.update({ |
| 38 | + new_resource.config_base => new_resource.config, |
| 39 | + }) |
| 40 | + |
| 41 | + new_resource.broker[:transport] ||= "amqplib" |
| 42 | + new_resource.broker[:host_role] ||= "#{new_resource.application.name}_task_broker" |
| 43 | + new_resource.broker[:host] ||= begin |
| 44 | + host = new_resource.find_matching_role(new_resource.broker[:host_role]) |
| 45 | + raise "No task broker host found" unless host |
| 46 | + host.attribute?('cloud') ? host['cloud']['local_ipv4'] : host['ipaddress'] |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +action :before_deploy do |
| 51 | + |
| 52 | + new_resource = @new_resource |
| 53 | + |
| 54 | + template ::File.join(new_resource.application.path, "shared", new_resource.config_base) do |
| 55 | + source new_resource.template || "celeryconfig.py.erb" |
| 56 | + cookbook new_resource.template ? new_resource.cookbook_name : "application_python" |
| 57 | + owner new_resource.owner |
| 58 | + group new_resource.group |
| 59 | + mode "644" |
| 60 | + variables :broker => new_resource.broker, :results => new_resource.results |
| 61 | + end |
| 62 | + |
| 63 | + cmds = {} |
| 64 | + cmds[:celeryd] = "celeryd #{new_resource.celerycam ? "-E" : ""}" if new_resource.celeryd |
| 65 | + cmds[:celerybeat] = "celerybeat" if new_resource.celerycam |
| 66 | + if new_resource.celerycam |
| 67 | + if new_resource.django |
| 68 | + cmd = "celerycam" |
| 69 | + else |
| 70 | + raise "No camera class specified" unless new_resource.camera_class |
| 71 | + cmd = "celeryev --camera=\"#{new_resource.camera_class}\"" |
| 72 | + end |
| 73 | + cmds[:celerycam] = cmd |
| 74 | + end |
| 75 | + |
| 76 | + cmds.each do |type, cmd| |
| 77 | + supervisor_service "#{new_resource.application.name}-#{type}" do |
| 78 | + action :enable |
| 79 | + if new_resource.django |
| 80 | + django_resource = new_resource.application.sub_resources.select{|res| res.type == :django}.first |
| 81 | + raise "No Django deployment resource found" unless django_resource |
| 82 | + command "#{::File.join(django_resource.virtualenv, "bin", "python")} manage.py #{cmd}" |
| 83 | + else |
| 84 | + command cmd |
| 85 | + environment 'CELERY_CONFIG_MODULE' => new_resource.config |
| 86 | + end |
| 87 | + directory ::File.join(new_resource.path, "current") |
| 88 | + autostart false |
| 89 | + user new_resource.owner |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | +end |
| 94 | + |
| 95 | +action :before_migrate do |
| 96 | +end |
| 97 | + |
| 98 | +action :before_symlink do |
| 99 | +end |
| 100 | + |
| 101 | +action :before_restart do |
| 102 | +end |
| 103 | + |
| 104 | +action :after_restart do |
| 105 | +end |
0 commit comments