Class: Ipswitch::Runner

Inherits:
Thor
  • Object
show all
Defined in:
lib/ipswitch/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject

default options for all tasks



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ipswitch/runner.rb', line 8

def self.default_options
  method_option 'user',      :type => :string,  :desc => 'ssh username', :default => 'root'
  method_option 'port',      :type => :numeric, :desc => 'ssh port', :default => 22
  method_option 'interface', :type => :string,  :desc => 'interface name', :default => 'eth0'
  method_option 'family',    :type => :string,  :desc => 'protocol family [inet|inet6]', :default => 'inet'
  method_option 'ip',        :type => :string,  :desc => 'IP address to remove/add/migrate'
  method_option 'broadcast', :type => :boolean, :desc => 'use arping to broadcast new IP (IPv4 only)', :default => true
  method_option 'count',     :type => :numeric, :desc => 'number of broadcasts to send', :default => 3
  method_option 'debug',     :type => :boolean, :desc => 'talk a lot while running', :default => false
  method_option 'dryrun',    :type => :boolean, :desc => 'do not actually perform anything', :default => false
end

Instance Method Details

#add(host) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ipswitch/runner.rb', line 44

def add(host)
  abort "#{'Error:'.foreground(:red)} --ip argument is required" unless options[:ip]
  ip = IPAddress(options[:ip])

  Ipswitch::Ssh.new(host, options) do |ssh|
    ssh.ip_add(ip)
    ssh.broadcast(ip) if options[:broadcast]
  end
end

#del(host) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ipswitch/runner.rb', line 56

def del(host)
  abort "#{'Error:'.foreground(:red)} --ip argument is required" unless options[:ip]
  ip = IPAddress(options[:ip])

  Ipswitch::Ssh.new(host, options) do |ssh|
    ssh.ip_del(ip)
  end
end

#migrate(source, target) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ipswitch/runner.rb', line 22

def migrate(source, target)
  ssh_source = Ipswitch::Ssh.new(source, options)
  ssh_target = Ipswitch::Ssh.new(target, options)

  # either use ip specified by --ip option
  # or try getting it from source host
  if options[:ip]
    ip = IPAddress(options[:ip])
  else
    ip = ssh_source.get_ip
  end

  # assign IP to target
  ssh_target.ip_add(ip)
  ssh_target.broadcast(ip) if options[:broadcast]

  # remove from source
  ssh_source.ip_del(ip)
end