root/lib/cyborghood/cyborg/dsl.rb @ d9ef7de9
7bc7e62a | Marc Dequènes (Duck) | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|||
#
|
|||
# This program is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
require 'active_support/basic_object'
|
|||
f6fd4b56 | Marc Dequènes (Duck) | require 'set'
|
|
7bc7e62a | Marc Dequènes (Duck) | ||
module CyborgHood
|
|||
4d9f2962 | Marc Dequènes (Duck) | module DSL
|
|
7bc7e62a | Marc Dequènes (Duck) | class BaseDSL < ActiveSupport::BasicObject
|
|
def initialize(&block)
|
|||
self.instance_eval(&block)
|
|||
_start_dsl
|
|||
end
|
|||
612572f9 | Marc Dequènes (Duck) | ||
4d9f2962 | Marc Dequènes (Duck) | reveal :class
|
|
reveal :logger
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
class Task < BaseDSL
|
|||
4d9f2962 | Marc Dequènes (Duck) | attr_reader :bot, :name, :errors, :results
|
|
7bc7e62a | Marc Dequènes (Duck) | ||
4d9f2962 | Marc Dequènes (Duck) | @@task_wip = 0
|
|
def self.idle?
|
|||
@@task_wip == 0
|
|||
end
|
|||
7bc7e62a | Marc Dequènes (Duck) | ||
7619e2d8 | Marc Dequènes (Duck) | def initialize(bot, name = nil, previous_step_data = {}, &block)
|
|
7bc7e62a | Marc Dequènes (Duck) | @bot = bot
|
|
@name = name
|
|||
4d9f2962 | Marc Dequènes (Duck) | @@task_wip += 1
|
|
7bc7e62a | Marc Dequènes (Duck) | ||
7619e2d8 | Marc Dequènes (Duck) | @errors = (previous_step_data[:errors] || {}).freeze
|
|
@results = (previous_step_data[:results] || {}).freeze
|
|||
f6fd4b56 | Marc Dequènes (Duck) | @dsl_runing = false
|
|
7bc7e62a | Marc Dequènes (Duck) | @subtasks = []
|
|
@cancel_on_start_error = false
|
|||
@cancel_run = false
|
|||
@error_cb = nil
|
|||
@success_cb = nil
|
|||
@notification_name = "task/#{@name}"
|
|||
begin
|
|||
super(&block)
|
|||
rescue
|
|||
4d9f2962 | Marc Dequènes (Duck) | logger.error "Task '#{@name}': error in DSL: " + $!.to_s
|
|
612572f9 | Marc Dequènes (Duck) | logger.debug $!.backtrace.join("\n")
|
|
7bc7e62a | Marc Dequènes (Duck) | @cancel_run = true
|
|
end
|
|||
end
|
|||
# may return a Hash of results
|
|||
def schedule(&job)
|
|||
612572f9 | Marc Dequènes (Duck) | _add_subtask("job/#{job.hash}") do |subtask|
|
|
logger.debug "Task '#{@name}': Scheduling job"
|
|||
7bc7e62a | Marc Dequènes (Duck) | cb = Proc.new do
|
|
612572f9 | Marc Dequènes (Duck) | subtask.finish
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
d9ef7de9 | Marc Dequènes (Duck) | job_wrapper = Proc.new do
|
|
job.call subtask
|
|||
end
|
|||
@bot.schedule_task(cb, job_wrapper)
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
def send_notification(name, data)
|
|||
947e1d12 | Marc Dequènes (Duck) | name = @notification_name if name == :task
|
|
7bc7e62a | Marc Dequènes (Duck) | ||
612572f9 | Marc Dequènes (Duck) | _add_subtask("notification/#{name}/out") do |subtask|
|
|
logger.debug "Task '#{@name}': Sending notification to '#{name}'"
|
|||
7bc7e62a | Marc Dequènes (Duck) | chan = @bot.get_channel(name)
|
|
chan << data
|
|||
612572f9 | Marc Dequènes (Duck) | subtask.finish
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
def wait_notification(name, criterias = {}, timeout = nil, &cb)
|
|||
947e1d12 | Marc Dequènes (Duck) | name = @notification_name if name == :task
|
|
7bc7e62a | Marc Dequènes (Duck) | ||
612572f9 | Marc Dequènes (Duck) | _add_subtask("notification/#{name}/in") do |subtask|
|
|
logger.debug "Task '#{@name}': Waiting notification on '#{name}'"
|
|||
7bc7e62a | Marc Dequènes (Duck) | subcription_id = @bot.get_channel(name).subscribe do |msg|
|
|
if _notification_criterias_match(msg, criterias)
|
|||
612572f9 | Marc Dequènes (Duck) | cb.call(subtask, msg)
|
|
@bot.get_channel(name).unsubscribe(subcription_id) if subtask.finished?
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
if timeout
|
|||
EventMachine.add_timer(timeout) do
|
|||
@bot.get_channel(name).unsubscribe(subcription_id)
|
|||
612572f9 | Marc Dequènes (Duck) | subtask.finish
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
end
|
|||
end
|
|||
def wait_timer(timeout, repeat = false, &cb)
|
|||
612572f9 | Marc Dequènes (Duck) | _add_subtask("timer/#{timeout}/#{cb.hash}") do |subtask|
|
|
7bc7e62a | Marc Dequènes (Duck) | periodic_timer_signature = nil
|
|
timer_cb = Proc.new do
|
|||
if repeat
|
|||
612572f9 | Marc Dequènes (Duck) | cb.call(subtask)
|
|
EventMachine.cancel_timer(periodic_timer_signature) if subtask.finished?
|
|||
7bc7e62a | Marc Dequènes (Duck) | else
|
|
612572f9 | Marc Dequènes (Duck) | cb.call(subtask)
|
|
subtask.finish
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
if repeat
|
|||
periodic_timer_signature = EventMachine.add_periodic_timer(timeout, timer_cb)
|
|||
else
|
|||
EventMachine.add_timer(timeout, timer_cb)
|
|||
end
|
|||
end
|
|||
end
|
|||
def task(name = nil, &block)
|
|||
612572f9 | Marc Dequènes (Duck) | _add_subtask("task/#{name}") do |subtask|
|
|
4d9f2962 | Marc Dequènes (Duck) | self.class.new(@bot, name || @name, &block)
|
|
612572f9 | Marc Dequènes (Duck) | subtask.finish
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
f6fd4b56 | Marc Dequènes (Duck) | def meet(task_list, meetpoint, retry_time = 2)
|
|
task_list = [task_list] unless task_list.is_a? Array
|
|||
task_list = Set.new(task_list)
|
|||
notification_name = "meeting_point/#{meetpoint}"
|
|||
notifications_received = Set.new
|
|||
meet_ok = false
|
|||
wait_notification(notification_name, {:topic => "MEET"}) do |subtask, msg|
|
|||
notifications_received << msg[:from] if task_list.include?(msg[:from])
|
|||
if task_list == notifications_received
|
|||
meet_ok = true
|
|||
subtask.finish
|
|||
end
|
|||
end
|
|||
wait_timer(retry_time, true) do |subtask|
|
|||
# in this order: send a last message before leaving
|
|||
# (so we are sure the peer received a reply to its message)
|
|||
send_notification notification_name, {:topic => "MEET", :from => @name}
|
|||
subtask.finish if meet_ok
|
|||
end
|
|||
end
|
|||
7bc7e62a | Marc Dequènes (Duck) | # error when adding subtasks, as we cannot check callbacks
|
|
# only applies to subsequently added tasks (wanted behavior)
|
|||
def cancel_on_start_error
|
|||
@cancel_on_start_error = true
|
|||
end
|
|||
def on_error(&cb)
|
|||
@error_cb = cb
|
|||
end
|
|||
def on_success(&cb)
|
|||
@success_cb = cb
|
|||
end
|
|||
def stop_bot(condition)
|
|||
612572f9 | Marc Dequènes (Duck) | _add_subtask('stop') do |subtask|
|
|
7bc7e62a | Marc Dequènes (Duck) | case condition
|
|
when :when_finished
|
|||
# try to stop gracefully
|
|||
@bot.try_stop
|
|||
when :at_once
|
|||
# it won't wait for anything to finish…
|
|||
@bot.stop
|
|||
end
|
|||
612572f9 | Marc Dequènes (Duck) | subtask.finish
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
end
|
|||
protected
|
|||
612572f9 | Marc Dequènes (Duck) | class Subtask
|
|
attr_reader :name
|
|||
attr_accessor :results, :errors
|
|||
7bc7e62a | Marc Dequènes (Duck) | ||
4d9f2962 | Marc Dequènes (Duck) | def initialize(task, name, &block)
|
|
@task = task
|
|||
612572f9 | Marc Dequènes (Duck) | @name = name
|
|
@block = block
|
|||
7bc7e62a | Marc Dequènes (Duck) | ||
612572f9 | Marc Dequènes (Duck) | @finished = false
|
|
@results = {}
|
|||
@errors = []
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
612572f9 | Marc Dequènes (Duck) | def do_it
|
|
@block.call self
|
|||
rescue
|
|||
4d9f2962 | Marc Dequènes (Duck) | msg = "Task '#{@task.name}': error: " + $!.to_s
|
|
612572f9 | Marc Dequènes (Duck) | logger.error msg
|
|
@errors << CyberError.new(:unrecoverable, "botnet/client/dsl", msg)
|
|||
4d9f2962 | Marc Dequènes (Duck) | logger.debug $!.backtrace.join("\n")
|
|
612572f9 | Marc Dequènes (Duck) | finish
|
|
end
|
|||
def finish
|
|||
if @finished
|
|||
4d9f2962 | Marc Dequènes (Duck) | raise CyberError.new(:unrecoverable, "botnet/client/dsl", "Task '#{@task.name}': subtask should have ended, but it lied")
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
612572f9 | Marc Dequènes (Duck) | @finished = true
|
|
4d9f2962 | Marc Dequènes (Duck) | logger.debug "Task '#{@task.name}': subtask '#{@name}' finished"
|
|
@task.__send__(:_check_finished)
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
612572f9 | Marc Dequènes (Duck) | def finished?
|
|
@finished
|
|||
end
|
|||
end
|
|||
7bc7e62a | Marc Dequènes (Duck) | ||
612572f9 | Marc Dequènes (Duck) | def _add_subtask(name, cb = nil, &block)
|
|
f6fd4b56 | Marc Dequènes (Duck) | subtask = Subtask.new(self, name, &block)
|
|
@subtasks << subtask
|
|||
subtask.do_it if @dsl_runing
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
def _subtasks_finished?
|
|||
@subtasks.each do |subtask|
|
|||
612572f9 | Marc Dequènes (Duck) | return false unless subtask.finished?
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
return true
|
|||
end
|
|||
def _check_finished
|
|||
return unless _subtasks_finished?
|
|||
f6fd4b56 | Marc Dequènes (Duck) | # avoid race: no subtask will be run in this task now
|
|
@dsl_runing = false
|
|||
7619e2d8 | Marc Dequènes (Duck) | next_step_data = {
|
|
4d9f2962 | Marc Dequènes (Duck) | :errors => {},
|
|
:results => {}
|
|||
7bc7e62a | Marc Dequènes (Duck) | }
|
|
@subtasks.each do |subtask|
|
|||
4d9f2962 | Marc Dequènes (Duck) | next_step_data[:errors][subtask.name] = subtask.errors unless subtask.errors.empty?
|
|
7619e2d8 | Marc Dequènes (Duck) | next_step_data[:results].merge!(subtask.results)
|
|
7bc7e62a | Marc Dequènes (Duck) | end
|
|
7619e2d8 | Marc Dequènes (Duck) | ||
f6fd4b56 | Marc Dequènes (Duck) | cb = next_step_data[:errors].empty? ? @success_cb : @error_cb
|
|
4d9f2962 | Marc Dequènes (Duck) | if cb
|
|
self.class.new(@bot, @name, next_step_data, &cb)
|
|||
else
|
|||
logger.debug "Task '#{@name}': finished"
|
|||
end
|
|||
@@task_wip -= 1
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
def _notification_criterias_match(msg, criterias)
|
|||
criterias.each_pair do |key, expected_val|
|
|||
val = msg[key]
|
|||
if expected_val.is_a? Regexp
|
|||
return false if val !~ expected_val
|
|||
else
|
|||
return false if val != expected_val
|
|||
end
|
|||
end
|
|||
true
|
|||
end
|
|||
def _start_dsl
|
|||
return false if @cancel_run
|
|||
4d9f2962 | Marc Dequènes (Duck) | logger.debug "Task '#{@name}': begining step"
|
|
if @subtasks.empty?
|
|||
_check_finished
|
|||
else
|
|||
f6fd4b56 | Marc Dequènes (Duck) | @dsl_runing = true
|
|
4d9f2962 | Marc Dequènes (Duck) | @subtasks.each do |subtask|
|
|
# skip broken or canceled subtasks
|
|||
next if subtask.finished?
|
|||
subtask.do_it
|
|||
end
|
|||
7bc7e62a | Marc Dequènes (Duck) | end
|
|
true
|
|||
end
|
|||
end
|
|||
end
|
|||
end
|