Permalink
Browse files

Celery config plugin.

  • Loading branch information...
1 parent 71d74e5 commit 8d54b41a208523856114405db8934b2308396aeb @coderanger coderanger committed Sep 7, 2011
Showing with 165 additions and 0 deletions.
  1. +105 −0 providers/celery.rb
  2. +45 −0 resources/celery.rb
  3. +15 −0 templates/default/celeryconfig.py.erb
View
@@ -0,0 +1,105 @@
+#
+# Author:: Noah Kantrowitz <noah@opscode.com>
+# Cookbook Name:: application_python
+# Provider:: django
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include Chef::Mixin::LanguageIncludeRecipe
+
+action :before_compile do
+
+ include_recipe "supervisor"
+
+ raise "You must specify an application module to load" unless new_resource.config
+
+ if !new_resource.restart_command
+ new_resource.restart_command do
+ run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celeryd").run_action(:restart) if new_resource.celeryd
+ run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celerybeat").run_action(:restart) if new_resource.celerybeat
+ run_context.resource_collection.find(:supervisor_service => "#{new_resource.application.name}-celerycam").run_action(:restart) if new_resource.celerycam
+ end
+ end
+
+ new_resource.symlink_before_migrate.update({
+ new_resource.config_base => new_resource.config,
+ })
+
+ new_resource.broker[:transport] ||= "amqplib"
+ new_resource.broker[:host_role] ||= "#{new_resource.application.name}_task_broker"
+ new_resource.broker[:host] ||= begin
+ host = new_resource.find_matching_role(new_resource.broker[:host_role])
+ raise "No task broker host found" unless host
+ host.attribute?('cloud') ? host['cloud']['local_ipv4'] : host['ipaddress']
+ end
+end
+
+action :before_deploy do
+
+ new_resource = @new_resource
+
+ template ::File.join(new_resource.application.path, "shared", new_resource.config_base) do
+ source new_resource.template || "celeryconfig.py.erb"
+ cookbook new_resource.template ? new_resource.cookbook_name : "application_python"
+ owner new_resource.owner
+ group new_resource.group
+ mode "644"
+ variables :broker => new_resource.broker, :results => new_resource.results
+ end
+
+ cmds = {}
+ cmds[:celeryd] = "celeryd #{new_resource.celerycam ? "-E" : ""}" if new_resource.celeryd
+ cmds[:celerybeat] = "celerybeat" if new_resource.celerycam
+ if new_resource.celerycam
+ if new_resource.django
+ cmd = "celerycam"
+ else
+ raise "No camera class specified" unless new_resource.camera_class
+ cmd = "celeryev --camera=\"#{new_resource.camera_class}\""
+ end
+ cmds[:celerycam] = cmd
+ end
+
+ cmds.each do |type, cmd|
+ supervisor_service "#{new_resource.application.name}-#{type}" do
+ action :enable
+ if new_resource.django
+ django_resource = new_resource.application.sub_resources.select{|res| res.type == :django}.first
+ raise "No Django deployment resource found" unless django_resource
+ command "#{::File.join(django_resource.virtualenv, "bin", "python")} manage.py #{cmd}"
+ else
+ command cmd
+ environment 'CELERY_CONFIG_MODULE' => new_resource.config
+ end
+ directory ::File.join(new_resource.path, "current")
+ autostart false
+ user new_resource.owner
+ end
+ end
+
+end
+
+action :before_migrate do
+end
+
+action :before_symlink do
+end
+
+action :before_restart do
+end
+
+action :after_restart do
+end
View
@@ -0,0 +1,45 @@
+#
+# Author:: Noah Kantrowitz <noah@opscode.com>
+# Cookbook Name:: application_python
+# Resource:: celery
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include Chef::Resource::ApplicationBase
+
+attribute :config, :kind_of => [String, NilClass], :default => nil
+attribute :template, :kind_of => [String, NilClass], :default => nil
+attribute :django, :kind_of => [TrueClass, FalseClass], :default => false
+attribute :celeryd, :kind_of => [TrueClass, FalseClass], :default => true
+attribute :celerybeat, :kind_of => [TrueClass, FalseClass], :default => false
+attribute :celerycam, :kind_of => [TrueClass, FalseClass], :default => false
+attribute :camera_class, :kind_of => [String, NilClass], :default => nil
+
+def config_base
+ config.split(/[\\\/]/).last
+end
+
+def broker(*args, &block)
+ @broker ||= Mash.new
+ @broker.update(options_block(*args, &block))
+ @broker
+end
+
+def results(*args, &block)
+ @results ||= Mash.new
+ @results.update(options_block(*args, &block))
+ @results
+end
@@ -0,0 +1,15 @@
+BROKER_TRANSPORT = "<%= @broker[:transport] %>"
+BROKER_HOST = "<%= @broker[:host] %>"
+<% %w{port user password vhost}.each do |key| %>
+<% if @broker[key] %>
+BROKER_<%= key.upper %> = "<%= @broker[key] %>"
+<% end %>
+<% end %>
+<% %w{pool_limit connection_timeout connection_retry connection_max_retries}.each do |key| %>
+<% if @broker[key] %>
+BROKER_<%= key.upper %> = <%= @broker[key] %>
+<% end %>
+<% end %>
+<% if @broker[:use_ssl] %>
+BROKER_USE_SSL = True
+<% end %>

0 comments on commit 8d54b41

Please sign in to comment.