PagerDuty / blender

A modular orchestration engine
https://github.com/PagerDuty/blender
Other
183 stars 8 forks source link

Running Blender from Ruby instead of DSL? #19

Closed arioch closed 9 years ago

arioch commented 9 years ago

What's the preferred way to run Blender tasks from plain Ruby or Rake instead of using the Thor DSL? I'd like to integrate Blender in a different project. This is what I have so far but I'm running into errors.

require 'blender'
include Blender::SchedulerDSL

list = %w(host1 host2 host3)

ssh_task 'update' do
  execute 'sudo apt-get update'
  members list
end

ssh_task 'upgrade' do
  execute 'sudo apt-get upgrade -y'
  members list
end

concurrency 2
~/.rvm/gems/ruby-1.9.3-p194/gems/pd-blender-0.0.1/lib/blender/scheduler/dsl.rb:84:in `build_task': undefined local variable or method `metadata' for main:Object (NameError)
    from ~/.rvm/gems/ruby-1.9.3-p194/gems/pd-blender-0.0.1/lib/blender/scheduler/dsl.rb:114:in `ssh_task'
    from test.rb:6:in `<main>'

Thanks in advance!

ranjib commented 9 years ago

@arioch if you are using it from another script use the scheduler class which include's the DSL. like this,

require 'blender'
list = %w(host1 host2 host3)
Blender.blend('test') do |sched|
  sched.ssh_task 'update' do
    execute 'sudo apt-get update'
    members list
  end
  sched.ssh_task 'upgrade' do
    execute 'sudo apt-get upgrade -y'
    members list
  end
end

if you want to run a standalone job, you can use the script that you have specified , via `blend -f /path/to/file', just remove the require and include statement.

arioch commented 9 years ago

Just what I was looking for! Thanks!

Maybe just one more question, I couldn't find this in the docs either... Is there a way to retrieve the exit status for each host?

At the moment I'm using Net::SSH with exceptions to accomplish this. However I'm looking for something that allows parallel execution.

ranjib commented 9 years ago

currently not. for ssh_task non-zero exit code will raise ExecutionFailed exception, which can be controlled by the ignore_failure attribute. Ignore failure is available per task or globally. If you want to tap in to failed jobs and run some code, look at the event handlers. you can add failure specific custom handler with the on DSL method.

on :job_failed do |job, error|
   # your code goes here
end
ranjib commented 9 years ago

@arioch im closing this due to inactivity, feel free to raise another issue if you have any queries