mybot

My personal bot

Installation

Add this line to your application's Gemfile:

gem 'mybot'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mybot

Usage

Tasks

It's all about executing tasks. mybot will search for recipes in ~/.mybot/*.rb and Botfile in current directory. Syntax is similar to rake and capistrano

require "mybot"

namespace :foo do
  task :baz do
    puts "foo:baz"
  end

  task :bar => :baz do |options|
    puts options.inspect
  end
end

To run task, use bot command:

% bot foo:bar --no-foo -bar -baz=12
      task foo:bar
      task foo:baz
foo:baz
{:foo=>false, :bar=>true, :baz=>12}

Nodes & interactive commands

mybot operates on nodes which are in fact computers you can ssh into. You can run commands and interactively handle it's output:

require "mybot"

server = node "1.2.3.4", "user", :password => "123"

task :foo do
  wait "Press any key to continue..."
  options = {
    :sudo => true,
    :cwd => "/home",
    :env => {
      "PARAM" => "value"
    }
  }
  server.run "uname -a", options do |cmd|
    cmd.on "Password:" do
      cmd.write! ask "Type password: "
    end
  end

  if yes? "Is that all (y/n): "
    puts "we're done!"
  else
    puts "wait a minute!"
  end
end

and you should see:

% bot foo:bar
      task foo
      wait Press any key to continue...
       run cd /home && sudo PARAM='value' uname -a
    handle Password:
       ask Type password:
     write ***
      time 0.151334
      exit 0
       ask Is that all (y/n):
we're done!

Note that bang version of write would replace sent data with asterisks.

File operations & templates

Couple of most common file operations are available.

require "mybot"

server = node "1.2.3.4", "user", :password => "123"
file_tpl = tpl :file => "/path/to/tpl"

namespace :server do
  task :foo do
    unless server.exists? "/path/to/file"
      server.create "/path/to/file", :content => file_tpl.render({
        :param => "value",
        :param => "value"
      })
    end
  end
end

Upload & download

require "mybot"

server = node "1.2.3.4", "user", :password => "123"

task :foo do
  server.upload "/Users/user/from.txt", "/home/user/to.txt"
  server.download "/home/user/to.txt", "/Users/user/file.txt"
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request