Kanrisuru

Cluster

Kanrusuru::Remote::Cluster encapsulates a collection of remote hosts. This class can add and remove hosts from the cluster, as well as run commands across the cluster all at once, or conditionally depending on some condition on a per host basis.

Public Class Methods

new

Create a new cluster instance. Can pass in an array of hosts to add into the cluster.

cluster = Kanrisuru::Remote::Cluster.new(host1, {
  host: 'localhost', 
  username: 'ubuntu', 
  keys: ['~/.ssh/id_rsa']
})

Public Instance Methods

[]

Access a single host in the cluster by hostname.

cluster['localhost']
<Kanrisuru::Remote::Host:0x0000559a65169ae0 
  @host="localhost", 
  @username="ubuntu", 
  @login_user="ubuntu", 
  @port=22, 
  @keys=["~/.ssh/id_rsa"],
  @shell="/bin/bash", 
  @current_dir=""
>

«

Add a new host to the cluster. Can be an object hash of host params or a host instance.

cluster << Kanrisuru::Remote::Host.new(host: 'centos', username: 'centos', keys: ['~/.ssh/id_rsa'])
cluster.count
3

execute

Execute a remote command across all hosts in the cluster. Returns array of command results grouped with the hostname.

cluster.execute('whoami')
[
  [0] {
    :host => "localhost",
    :result => #<Kanrisuru::Command:0x0000559a650d5b60 @valid_exit_codes=[0], @raw_command="whoami", @raw_result=["ubuntu\n"], @exit_status=0>
  },
  [1] {
    :host => "centos-host",
    :result => #<Kanrisuru::Command:0x0000559a650bb558 @valid_exit_codes=[0], @raw_command="whoami", @raw_result=["centos\n"], @exit_status=0>
  },
  [2] {
    :host => "fedora-host",
    :result => #<Kanrisuru::Command:0x0000559a65093f80 @valid_exit_codes=[0], @raw_command="whoami", @raw_result=["fedora\n"], @exit_status=0>
  }
]

execute_shell

Execute a remote command with shell access across all hosts in the cluster. Returns array of command results grouped with the hostname.

cluster.su('root')
cluster.execute_shell('du /etc -s')
[
  [0] {
    :host => "localhost",
    :result => #<Kanrisuru::Command:0x0000559a658531a8 @valid_exit_codes=[0], @raw_command="du /etc -s", @raw_result=["5712\t/etc\n"], @exit_status=0, @remote_user="root", @remote_shell="/bin/bash", @remote_path="", @remote_env="">
  },
  [1] {
    :host => "centos-host",
    :result => #<Kanrisuru::Command:0x0000559a65844e50 @valid_exit_codes=[0], @raw_command="du /etc -s", @raw_result=["36164\t/etc\n"], @exit_status=0, @remote_user="root", @remote_shell="/bin/bash", @remote_path="", @remote_env="">
  },
  [2] {
    :host => "fedora-host",
    :result => #<Kanrisuru::Command:0x0000559a65836850 @valid_exit_codes=[0], @raw_command="du /etc -s", @raw_result=["23540\t/etc\n"], @exit_status=0, @remote_user="root", @remote_shell="/bin/bash", @remote_path="", @remote_env="">
  }
]

each

Iterate sequentially through each host in the cluster. Can conditionally perform commands on a host-by-host basis.

cluster.each do |host|
  case host.os.release
  when 'debian', 'ubuntu'
    host.apt('update')
  when 'fedora', 'redhat', 'centos'
    host.yum('update')
  when 'sles', 'opensuse_leap'
    host.zypper('update')
  end
end

delete

Removes a host from the cluster.

cluster.delete('centos-host')
cluster.count
2

hostname

Gets the hostname for each host.

cluster.hostname
[
  [0] {
    :host => "localhost",
    :result => "ubuntu"
  },
  [1] {
    :host => "centos-host",
    :result => "centos-host"
  },
  [2] {
    :host => "fedora-host",
    :result => "fedora-host"
  }
]

ping?

Returns true for each host if they can be pinged or not.

cluster.ping?
[
  [0] {
    :host => "localhost",
    :result => true
  },
  [1] {
    :host => "centos-host",
    :result => true
  },
  [2] {
    :host => "fedora-host",
    :result => true
  }
]

su

Switch user for all the hosts in the cluster.

cluster.su('root')
cluster.whoami
[
  [0] {
    :host => "localhost",
    :result => #<Kanrisuru::Result:0x1800 @status=0 @data=#<struct Kanrisuru::Core::Path::UserName user="root"> @command=sudo -u root /bin/bash -c "whoami">
  },
  [1] {
    :host => "centos-host",
    :result => #<Kanrisuru::Result:0x1840 @status=0 @data=#<struct Kanrisuru::Core::Path::UserName user="root"> @command=sudo -u root /bin/bash -c "whoami">
  },
  [2] {
    :host => "fedora-host",
    :result => #<Kanrisuru::Result:0x1880 @status=0 @data=#<struct Kanrisuru::Core::Path::UserName user="root"> @command=sudo -u root /bin/bash -c "whoami">
  }
]

chdir

Alias for cd command

cd

Change directories for all hosts.

cluster.cd('/etc')
cluster.pwd
[
  [0] {
    :host => "localhost",
    :result => #<Kanrisuru::Result:0x2200 @status=0 @data=#<struct Kanrisuru::Core::Path::FilePath path="/etc"> @command=sudo -u root /bin/bash -c "cd /etc && pwd">
  },
  [1] {
    :host => "centos-host",
    :result => #<Kanrisuru::Result:0x2240 @status=0 @data=#<struct Kanrisuru::Core::Path::FilePath path="/etc"> @command=sudo -u root /bin/bash -c "cd /etc && pwd">
  },
  [2] {
    :host => "fedora-host",
    :result => #<Kanrisuru::Result:0x2280 @status=0 @data=#<struct Kanrisuru::Core::Path::FilePath path="/etc"> @command=sudo -u root /bin/bash -c "cd /etc && pwd">
  }
]

disconnect

Disconnect all of the hosts’ ssh sessions.