Class: IscsiadmWrapper::Iscsiadm

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

Constant Summary collapse

DEFAULT_COMMAND =
'/sbin/iscsiadm'
DEFAULT_TYPE =
'sendtargets'
DEFAULT_HOST =
'localhost'
DEFAULT_PORT =
3260
DEFAULT_INTERFACE =
1
COMMANDS =
{
  # misc
  :version       => { :cmd => "%s --version", 
                      :filter => %r{^iscsiadm version (.*)$} },
  # discovery mode
  :discover      => { :cmd => "%s --mode discovery --type %s --portal %s:%s", 
                      :filter => %r{^.* (.*)$} },
  :list_portals  => { :cmd => "%s --mode discovery", 
                      :filter => %r{^(.*) via (.*)$} },
  # node mode
  :login         => { :cmd => "%s --mode node --target %s --portal %s --login", 
                      :filter => %r{^iscsiadm version (.*)$} },
  :logout        => { :cmd => "%s --mode node --target %s --portal %s --logout", 
                      :filter => %r{^iscsiadm version (.*)$} },
  :list_nodes    => { :cmd => "%s --mode node", 
                      :filter => %r{^(.*) (.*)$} },
  # session mode
  :list_sessions => { :cmd => "%s --mode session", 
                      :filter => %r{^tcp: \[(\d+)\] (.*) (.*)$} },
  # config db
  :show_config   => { :cmd => "%s --mode node --target %s --portal %s --op show", 
                      :filter => %r{^(.*) = (.*)$} },
  :new_record    => { :cmd => "%s --mode node --target %s --portal %s --op new --name %s --value %s", 
                      :filter => %r{^(.*)$} },
  :delete_record => { :cmd => "%s --mode node --target %s --portal %s --op delete --name %s", 
                      :filter => %r{^(.*)$} },
  :update_record => { :cmd => "%s --mode node --target %s --portal %s --op update --name %s --value %s", 
                      :filter => %r{^(.*)$} },
  # iscsiadm doesn't support this one really, see below
  :show_record   => { :cmd => "%s --mode node --target %s --portal %s --op show --name %s", 
                      :filter => %r{^.* = (.*)$} },
}
COMPAT_VERSION =
'2.0-865'
DEFAULT_BINARY =
'/sbin/iscsiadm'

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Iscsiadm

Instance Methods



66
67
68
69
70
71
72
# File 'lib/iscsiadm/iscsiadm.rb', line 66

def initialize(args = {})
  @command = args[:command] || DEFAULT_COMMAND
  @use_unsupported_version = args[:use_unsupported_version] || false
  @debug = args[:debug] || false

  puts "Running in debug mode, outputting commands to stdout." if @debug
end

Instance Method Details

#delete_record(args = {}) ⇒ Object

Delete a named config entry for a given target Returns ?

Should not be used on a running session so says the man page.



239
240
241
242
243
244
245
246
247
# File 'lib/iscsiadm.rb', line 239

def delete_record(args = {})
  target = args[:node].target
  portal = args[:node].portal
  name = args[:name]

  Common::callout(
    COMMANDS[:delete_record][:cmd] % [@command, target, portal, name], 
    COMMANDS[:delete_record][:filter], @debug)
end

#discover(args = {}) ⇒ Object

Runs iscsi discovery against a target portal Returns nodes.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/iscsiadm.rb', line 117

def discover(args = {})
  type = args[:type] || DEFAULT_TYPE 
  ip = args[:ip] || DEFAULT_HOST 
  port = args[:port] || DEFAULT_PORT
  interface = args[:interface] || DEFAULT_INTERFACE

  Common::callout(
    COMMANDS[:discover][:cmd] % [@command, type, ip, port], 
    COMMANDS[:discover][:filter], @debug)

  refresh_portals
  refresh_nodes

  list_nodes("#{ip}:#{port},#{interface}")
end

#list_nodes(args = {}) ⇒ Object

Returns an array of Node objects. Return all by default



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/iscsiadm.rb', line 177

def list_nodes(args = {})
  portal = args[:portal]
  ret = [] 

  Common::callout(
    COMMANDS[:list_nodes][:cmd] % [@command], 
    COMMANDS[:list_nodes][:filter], @debug) do |p,t|
  
    next if portal.nil? == false and portal != p

    ret << Node.new(:portal => p, :target => t)
  end

  ret
end

#list_portalsObject

Returns an array of portal objects.



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/iscsiadm.rb', line 134

def list_portals
  ret = []

  Common::callout(
    COMMANDS[:list_portals][:cmd] % [@command], 
    COMMANDS[:list_portals][:filter], @debug) do |p,t|

    ret << Portal.parse(p,t)
  end

  ret
end

#list_sessions(args = {}) ⇒ Object

Returns an array of Session objects. Return all by default.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/iscsiadm.rb', line 197

def list_sessions(args = {})
  portal = args[:portal]
  ret = []

  Common::callout(
    COMMANDS[:list_sessions][:cmd] % [@command], 
    COMMANDS[:list_sessions][:filter], @debug) do |r,p,t|
  
    next if portal.nil? == false and portal != p

    ret << Session.new(:portal => p, :target => t, :id => r)
  end

  ret
end

#login(args = {}) ⇒ Object

Attempt to login to a given node. Returns session



152
153
154
155
156
157
158
159
160
161
# File 'lib/iscsiadm.rb', line 152

def (args = {}) 
  target = args[:node].target
  portal = args[:node].portal

  Common::callout(
    COMMANDS[:login][:cmd] % [@command, target, portal], 
    COMMANDS[:login][:filter], @debug)

  session(args)
end

#logout(args = {}) ⇒ Object

Attempt to login to a given node. Returns ?



165
166
167
168
169
170
171
172
173
174
# File 'lib/iscsiadm.rb', line 165

def logout(args = {}) 
  target = args[:node].target
  portal = args[:node].portal

  Common::callout(
    COMMANDS[:logout][:cmd] % [@command, target, portal], 
    COMMANDS[:logout][:filter], @debug)

  refresh_sessions
end

#new_record(args = {}) ⇒ Object

Create a new config entry for a given target Returns ?

Why would you want to create new records again..?



253
254
255
256
257
258
259
260
261
262
# File 'lib/iscsiadm.rb', line 253

def new_record(args = {})
  target = args[:node].target
  portal = args[:node].portal
  name = args[:name]
  value = args[:value]

  Common::callout(
    COMMANDS[:new_record][:cmd] % [@command, target, portal, name, value], 
    COMMANDS[:new_record][:filter], @debug)
end

#nodesObject



80
# File 'lib/iscsiadm.rb', line 80

def nodes; refresh_nodes; @nodes; end

#portalsObject



81
# File 'lib/iscsiadm.rb', line 81

def portals; refresh_portals; @portals; end

#refresh_allObject

Misc



77
78
79
80
81
# File 'lib/iscsiadm/iscsiadm.rb', line 77

def refresh_all
  refresh_sessions
  refresh_nodes
  refresh_portals
end

#refresh_nodesObject



77
# File 'lib/iscsiadm.rb', line 77

def refresh_nodes;    @nodes    = list_nodes; end

#refresh_portalsObject



78
# File 'lib/iscsiadm.rb', line 78

def refresh_portals;  @portals  = list_portals; end

#refresh_sessionsObject



76
# File 'lib/iscsiadm.rb', line 76

def refresh_sessions; @sessions = list_sessions; end

#session(args = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/iscsiadm.rb', line 83

def session(args = {})
  target = args[:node].target
  portal = args[:node].portal

  refresh_sessions

  @sessions.each do |s|
    return s if s.target == target && s.portal == portal
  end
end

#session_established?(args = {}) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/iscsiadm.rb', line 93

def session_established?(args = {})
  target = args[:node].target
  portal = args[:node].portal

  refresh_sessions

  @sessions.each do |s|
    return true if s.target == target && s.portal == portal
  end
  false
end

#sessionsObject



79
# File 'lib/iscsiadm.rb', line 79

def sessions; refresh_sessions; @sessions; end

#show_config(args = {}) ⇒ Object

Returns a hash of all config key => value pairs for a given target



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/iscsiadm.rb', line 218

def show_config(args = {})
  target = args[:node].target
  portal = args[:node].portal

  conf = {}

  Common::callout(
    COMMANDS[:show_config][:cmd] % [@command, target, portal], 
    COMMANDS[:show_config][:filter], @debug) do |k,v|

    # easier to deal with nil
    conf[k] = (v == '<empty>' ? nil : v)
  end

  conf
end

#show_record(args = {}) ⇒ Object

Takes a Node object and entry name as args.

Returns String.



282
283
284
285
286
287
288
289
290
291
# File 'lib/iscsiadm.rb', line 282

def show_record(args = {})
  target = args[:node].target
  portal = args[:node].portal
  name = args[:name]

  # iscsiadm doesn't support show by specific name, fake it
  Common::callout(
    COMMANDS[:show_record][:cmd] % [@command, target, portal, name], 
    %r{#{name} = (.*)$}, @debug)
end

#update_record(args = {}) ⇒ Object

Update a named config entry for a given target Returns ?



267
268
269
270
271
272
273
274
275
276
# File 'lib/iscsiadm.rb', line 267

def update_record(args = {})
  target = args[:node].target
  portal = args[:node].portal
  name = args[:name]
  value = args[:value]

  Common::callout(
    COMMANDS[:update_record][:cmd] % [@command, target, portal, name, value], 
    COMMANDS[:update_record][:filter], @debug)
end

#versionObject

Returns iscsiadm’s version



106
107
108
109
110
# File 'lib/iscsiadm.rb', line 106

def version
  Common::callout(
    COMMANDS[:version][:cmd] % @command, 
    COMMANDS[:version][:filter]).to_s
end