Skip to content

Commit 8d54b41

Browse files
committed
Celery config plugin.
1 parent 71d74e5 commit 8d54b41

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

providers/celery.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

resources/celery.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# Author:: Noah Kantrowitz <noah@opscode.com>
3+
# Cookbook Name:: application_python
4+
# Resource:: celery
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::Resource::ApplicationBase
22+
23+
attribute :config, :kind_of => [String, NilClass], :default => nil
24+
attribute :template, :kind_of => [String, NilClass], :default => nil
25+
attribute :django, :kind_of => [TrueClass, FalseClass], :default => false
26+
attribute :celeryd, :kind_of => [TrueClass, FalseClass], :default => true
27+
attribute :celerybeat, :kind_of => [TrueClass, FalseClass], :default => false
28+
attribute :celerycam, :kind_of => [TrueClass, FalseClass], :default => false
29+
attribute :camera_class, :kind_of => [String, NilClass], :default => nil
30+
31+
def config_base
32+
config.split(/[\\\/]/).last
33+
end
34+
35+
def broker(*args, &block)
36+
@broker ||= Mash.new
37+
@broker.update(options_block(*args, &block))
38+
@broker
39+
end
40+
41+
def results(*args, &block)
42+
@results ||= Mash.new
43+
@results.update(options_block(*args, &block))
44+
@results
45+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
BROKER_TRANSPORT = "<%= @broker[:transport] %>"
2+
BROKER_HOST = "<%= @broker[:host] %>"
3+
<% %w{port user password vhost}.each do |key| %>
4+
<% if @broker[key] %>
5+
BROKER_<%= key.upper %> = "<%= @broker[key] %>"
6+
<% end %>
7+
<% end %>
8+
<% %w{pool_limit connection_timeout connection_retry connection_max_retries}.each do |key| %>
9+
<% if @broker[key] %>
10+
BROKER_<%= key.upper %> = <%= @broker[key] %>
11+
<% end %>
12+
<% end %>
13+
<% if @broker[:use_ssl] %>
14+
BROKER_USE_SSL = True
15+
<% end %>

0 commit comments

Comments
 (0)