Class: DRBD

Inherits:
Object
  • Object
show all
Defined in:
lib/drbd.rb

Defined Under Namespace

Classes: Host, Resource, Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, opts = {}) ⇒ DRBD

Returns a new instance of DRBD.



7
8
9
10
11
# File 'lib/drbd.rb', line 7

def initialize host, opts = {}
  connect host
  parse_opts opts
  load!
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



5
6
7
# File 'lib/drbd.rb', line 5

def command
  @command
end

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/drbd.rb', line 5

def connection
  @connection
end

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/drbd.rb', line 5

def resources
  @resources
end

Instance Method Details

#connect(host) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/drbd.rb', line 13

def connect host
  if host.class == String
    @connection = Net::SSH.start(host, ENV['USER'])
  elsif host.class == Net::SSH::Connection::Session
    @connection = host
  else
    raise "Connection must be String with hostname or Net::SSH::Connection::Session object but it is #{host.class}."
  end
end

#find_resource_by_disk(disk_name) ⇒ Object



102
103
104
# File 'lib/drbd.rb', line 102

def find_resource_by_disk disk_name
  @resources.select{|r| r.hosts.inject(false){|sum,h| (h.disk == disk_name && sum == false) ? true  : sum}}.first
end

#find_resource_by_name(given_name) ⇒ Object



98
99
100
# File 'lib/drbd.rb', line 98

def find_resource_by_name given_name
  @resources.select{|r| r.name == given_name}.first
end

#load!Object



75
76
77
78
# File 'lib/drbd.rb', line 75

def load!
  load_resources!
  load_status!
end

#load_resources!Object



80
81
82
83
# File 'lib/drbd.rb', line 80

def load_resources!
  xml = ssh_output("#{@command} dump-xml")
  @resources = Resource.load_config(xml, self)
end

#load_status!Object



85
86
87
88
89
# File 'lib/drbd.rb', line 85

def load_status!
  xml = ssh_output("#{@command} status")
  statuses = Status.new(xml).resources
  set_resources_status statuses
end

#parse_opts(opts) ⇒ Object



22
23
24
# File 'lib/drbd.rb', line 22

def parse_opts opts
  opts[:command].nil? ? @command = "sudo /sbin/drbdadm" : @command = opts[:command]
end

#set_resources_status(statuses) ⇒ Object



91
92
93
94
95
96
# File 'lib/drbd.rb', line 91

def set_resources_status statuses 
  statuses.each do |status|
    resource = find_resource_by_name(status[:name])
    resource.status = status
  end
end

#ssh_exec(command) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drbd.rb', line 50

def ssh_exec(command)
  exit_status = nil
  channel = @connection.open_channel do |ch|
    ch.exec command

    ch.on_data do |c, data|
      puts data
    end
    ch.on_extended_data do |c, type, data|
      puts data
    end

    ch.on_request("exit-status") do |ch, data|
      exit_status = data.read_long
    end
  end
  channel.wait

  if exit_status > 0
    return exit_status
  else
    return true
  end  
end

#ssh_output(command) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/drbd.rb', line 26

def ssh_output(command)
  stdout = ""
  stderr = ""
  channel = @connection.open_channel do |ch|
    ch.exec(command) do |ch, success|
      raise "could not execute command" unless success

      # "on_data" is called when the process writes something to stdout
      ch.on_data do |c, data|
       stdout << data
      end

      # "on_extended_data" is called when the process writes something to stderr
      ch.on_extended_data do |c, type, data|
        stderr << data
      end
      #ch.on_close { puts "done!" }
    end
  end
  channel.wait
  #puts stderr
  return stdout
end