Class: Deltacloud::DSL::Task
- Inherits:
-
Object
- Object
- Deltacloud::DSL::Task
- Defined in:
- lib/task.rb
Instance Attribute Summary (collapse)
-
- (Object) log
readonly
Returns the value of attribute log.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) state_machine
readonly
Returns the value of attribute state_machine.
Instance Method Summary (collapse)
- - (Object) define(&block)
-
- (Task) initialize(name, &block)
constructor
A new instance of Task.
-
- (Object) instances(configuration = {}, &block)
Configure group of instances that would be launched using specified configuration.
-
- (Object) method_missing(name, *args)
Declare a set of methods dynamically:.
-
- (Object) on(state, &block)
Handler for callback definition:.
-
- (Object) perform(s, item = nil)
Syntax sugar for performing callbacks.
-
- (Object) start!
Start executing given task.
-
- (Object) state(state)
Syntax sugar for @state_machine callbacks.
Constructor Details
- (Task) initialize(name, &block)
A new instance of Task
11 12 13 14 15 16 |
# File 'lib/task.rb', line 11 def initialize(name, &block) @name = name @state_machine = {} @log = Logger.new(STDERR) define &block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *args)
Declare a set of methods dynamically:
find_instances_by_ => returns InstanceDefinition objects that
match the value of attribute provided in method name
[driver]_instances => return InstanceDefinition objects that correspond
to given driver
[driver]_instance => return Instance object that match with name
eg. mock_instance 'test-mock-1' will find this
instance.
instances_ => return Instance objects with corresponding state
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/task.rb', line 62 def method_missing(name, *args) if name.to_s =~ /^find_instances_by_([\w_]+)$/ @instances.select { |i| i.configuration.respond_to?($1.intern) && args.include?(i.configuration.send($1.intern)) } elsif name.to_s =~ /^(\w+)_instances$/ find_instances_by_driver($1.intern) elsif name.to_s =~ /^(\w+)_instance$/ inst = find_instances_by_driver($1.intern).map { |instances| instances.find { |i| args.include?(i.name) } }.flatten.compact.first yield inst if block_given? inst elsif name.to_s =~ /^instances_(\w+)\?$/ @instances.any? { |d| d.map { |i| (i.update! rescue true) && i.instance_state == $1.intern }.include? true } else super end end |
Instance Attribute Details
- (Object) log (readonly)
Returns the value of attribute log
9 10 11 |
# File 'lib/task.rb', line 9 def log @log end |
- (Object) name (readonly)
Returns the value of attribute name
9 10 11 |
# File 'lib/task.rb', line 9 def name @name end |
- (Object) state_machine (readonly)
Returns the value of attribute state_machine
9 10 11 |
# File 'lib/task.rb', line 9 def state_machine @state_machine end |
Instance Method Details
- (Object) define(&block)
18 19 20 |
# File 'lib/task.rb', line 18 def define(&block) instance_eval(&block) if block_given? end |
- (Object) instances(configuration = {}, &block)
Configure group of instances that would be launched using specified configuration.
Configuration:
:driver - Deltacloud API driver to use (default: mock) :provider - API_PROVIDER (eg. URL to RHEV-M API) :username - API key (default: mockuser) :password - API secret (default: mockpassword) :url - Deltacloud API url (default: localhost:3001/api)
The block should define single instances:
instances(:driver => :ec2, :username => API_KEY, :password => API_SECRET) do
instance 'instance-1' do
# instance configuration here
end
end
41 42 43 44 45 46 |
# File 'lib/task.rb', line 41 def instances(configuration={}, &block) return find_instances_by_driver(configuration) if configuration.kind_of? Symbol @instances ||= [] @instances << Deltacloud::DSL::Instances(configuration, &block) if block_given? @instances end |
- (Object) on(state, &block)
Handler for callback definition:
on(:running) do end
on(:instance_started) do |instance| end
122 123 124 |
# File 'lib/task.rb', line 122 def on(state, &block) @state_machine[state] = block if block_given? end |
- (Object) perform(s, item = nil)
Syntax sugar for performing callbacks
134 135 136 137 |
# File 'lib/task.rb', line 134 def perform(s, item=nil) return unless @state_machine.keys.include?(s) state(s).call(item) end |
- (Object) start!
Start executing given task.
This method will try to start all instances defined for all drivers. Then it will wait for instances to become running.
Various callback can be provided for different state:
on(:running) callback is triggered when all instances are running on(:instance_started) callback is triggered for every instance when become running
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/task.rb', line 88 def start! task = self EventMachine::run do inst_arr = [] task.instances.each do |definition| inst_arr += definition.instances definition.instances.each do |instance| Thread.new do begin instance.create(definition).wait_for_running! perform(:instance_started, instance) log.info "Instance #{instance.name}[#{definition.configuration.driver}] successfully started." rescue => e log.error "ERROR: #{e.}" ensure inst_arr.delete(instance) end end end end until inst_arr.empty?; end perform(:running) EM.stop end end |
- (Object) state(state)
Syntax sugar for @state_machine callbacks
128 129 130 |
# File 'lib/task.rb', line 128 def state(state) @state_machine[state] end |