Module: Cute::TakTuk

Defined in:
lib/cute/taktuk.rb

Overview

Cute::TakTuk is a library for controlling the execution of commands in multiple machines using taktuk tool. It exposes an API similar to that of Net::SSH:Multi, making it simpler to adapt to scripts designed with Net::SSH::Multi. It simplifies the use of taktuk by automating the generation of large command line parameters.

require 'cute/taktuk'

results = {}
Cute::TakTuk.start(['host1','host2','host3'],:user => "root") do |tak|
     tak.exec("df")
     results = tak.exec!("hostname")
     tak.exec("ls -l")
     tak.exec("sleep 20")
     tak.loop()
     tak.exec("tar xvf -")
     tak.input(:file => "test_file.tar")
end
puts results

Understanding exec and exec!

This section explains the differences between exec and exec! with several examples.

Example 1

Cute::TakTuk.start(['host1','host2','host3'],:user => "root") do |tak|
     tak.exec("df")
     tak.exec("ls -l")
     tak.exec("sleep 20")
     tak.exec("tar xvf file.tar")
end

In the previous example all the commands will be executed concurrently on each host*. This will be equivalent to execute the following sequence in bash:

$ df &
$ ls -l &
$ sleep 20 &
$ tar xvf file.tar &
$ wait

The start method waits for all commands, it performs a loop() implicitly. This implicit loop() has the same behaviour as the ‘wait’ command in bash.

Example 2

Cute::TakTuk.start(['host1','host2','host3'],:user => "root") do |tak|
     tak.exec("df")
     tak.exec("ls -l")
     tak.loop()
     tak.exec("sleep 20")
     tak.exec("tar xvf file.tar")
end

This will execute the two first comamnds concurrently and then the remaining commands concurrently. It is equivalent to execute the following sequence in bash:

$ df &
$ ls -l &
$ wait
$ sleep 20 &
$ tar xvf file.tar &
$ wait

Example 3

Cute::TakTuk.start(['host1','host2','host3'],:user => "root") do |tak|
     tak.exec("df")
     tak.exec("ls -l")
     tak.exec!("sleep 20")
     tak.exec("tar xvf file.tar")
end

Notice that we use now the exec! method which will wait for the previous commands and then it will block until the command finishes. It is equivalent to execute the following sequence in bash:

$ df &
$ ls -l &
$ wait
$ sleep 20 &
$ wait
$ tar xvf file.tar &
$ wait

You can go directly to the documentation of the mentioned methods exec, exec! and other useful methods such as: put, input, etc.

Defined Under Namespace

Classes: Commands, Hostlist, Options, StateStream, Stream, TakTuk

Class Method Summary collapse

Class Method Details

.start(host_list, opts = {}) ⇒ Object

It instantiates a new TakTuk. If a block is given, a TakTuk object will be yielded to the block and automatically closed when the block finishes. Otherwise a TakTuk object will be returned.

Parameters:

  • host_list (Array)

    list of hosts where taktuk will execute commands on.

  • opts (Hash) (defaults to: {})

    Options to be directly passed to the TakTuk object.

Options Hash (opts):

  • :user (String)

    Sets the username to login into the machines.

  • :connector (String)

    Defines the connector command used to contact the machines.

  • :keys (Array)

    SSH keys to be used for connecting to the machines.

  • :port (Fixnum)

    SSH port to be used for connecting to the machines.

  • :config (String)

    SSH configuration file

  • :gateway (String)

    Specifies a forward only node



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cute/taktuk.rb', line 119

def self.start(host_list, opts={})
  taktuk_cmd = TakTuk.new(host_list, opts)
  if block_given?
    begin
      yield  taktuk_cmd
      taktuk_cmd.loop unless taktuk_cmd.commands.empty?
      taktuk_cmd.free! if taktuk_cmd
    end
  else
    return taktuk_cmd
  end
end

.taktuk(*args) ⇒ Object

Execution samples:

taktuk('hostfile',:connector => 'ssh -A', :self_propagate => true).broadcast_exec['hostname'].run!

taktuk(['node-1','node-2'],:dynamic => 3).broadcast_put['myfile']['dest'].run!

taktuk(nodes).broadcast_exec['hostname'].seq!.broadcast_exec['df'].run!

taktuk(nodes).broadcast_exec['cat - | fdisk'].seq!.broadcast_input_file['fdiskdump'].run!

tak = taktuk(nodes)
tak.broadcast_exec['hostname']
tak.seq!.broadcast_exec['df']
tak.streams[:output] => OutputStream.new(Template[:line,:rank]),
tak.streams[:info] => ConnectorStream.new(Template[:command,:line])
tak.run!


104
105
106
# File 'lib/cute/taktuk.rb', line 104

def self.taktuk(*args)
  TakTuk.new(*args)
end