Class: Brocade::SAN::Provisioning::Agent

Inherits:
Switch
  • Object
show all
Defined in:
lib/brocadesan/provisioning.rb,
lib/brocadesan/provisioning.rb

Overview

Agent class, used for provisioning tasks

Under development - do not use

TODO: need to properly test it live (partialy done)

Defined Under Namespace

Classes: Error, Transaction

Constant Summary

Constants inherited from Switch

Switch::CMD_MAPPING, Switch::NAME_RULE, Switch::PARSER_MAPPING

Constants included from SshDevice

SshDevice::DEFAULT_QUERY_PROMPT

Instance Attribute Summary

Attributes inherited from Switch

#configuration, #fid

Attributes included from SshDevice

#prompt

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Switch

#aliases, attributes, #effective_configuration, #fabric, #find, #find_alias, #find_aliases, #find_by_member, #find_wwn, #find_zone, #find_zones, #get, #override_vf, #query, #set_context, verify_name, #vf, #wwns, #zone_configurations, #zones

Methods included from SshDevice

#get_mode, #interactive_mode, #query, #script_mode, #session, #set_mode

Class Method Details

.create(*params) ⇒ Object

Creates a Brocade::SAN::Provisioning::Agent instance, tests a connection and test if user has enough rights for provisioning. Raises Error otherwise

Checks as well if the switch is virtual fabric enabled since that defines the way it will be queried further.



35
36
37
38
39
40
# File 'lib/brocadesan/provisioning.rb', line 35

def self.create(*params)
  agent=new(*params)
  #TODO revisit this once the v7.3 is installed
  agent.override_vf
  agent
end

Instance Method Details

#abort_transactionObject

returns true if transaction was aborted and false if there was no transaction to abort and raises error if it is not owner of the transaction



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/brocadesan/provisioning.rb', line 394

def abort_transaction
  response = script_mode do
    query("cfgtransabort")
  end
  #empty response is ok
  case
  when  response.data.split("\n").size==1
    return true
  when  response.data.match(/#{Replies::NO_TRANSACTION}/)
    return false
  when response.data.match(/#{Replies::OUTSTANDING_TRANSACTION}/)
    raise Agent::Error.new(Agent::Error::TRANS_NOTOWNER)
  else
    error = response.data.split("\n").delete_if {|item| item.match(/^#{@prompt}/)}.join("\n")
    raise Agent::Error.new(error)
  end
end

#alias_add(al, member) ⇒ Object

Adds alias member to alias

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

member must be instance of Wwn or String matching Alias::MEMBER_RULE (WWN or D,P)

al must be Alias and must exist in switch configuration, the member does not need to, it has only to match the rule

raises error if al does not exist, member does not match the rule, dirrefent transaction is running or response is unexpected

Returns Alias



329
330
331
# File 'lib/brocadesan/provisioning.rb', line 329

def alias_add(al,member)
  obj_add(al,Alias,member)
end

#alias_change(al) ⇒ Object

Changes alias and saves config

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

al must be of class Alias. alias with the Alias#name will be removed and it will be created anew as it is with all the members defined in al instance

raises error if alias does not exist or different transaction is running or response is unexpected

this is shorthand method that instead of modifiyng the alias removes the alias and recreates it

use alias_remove and alias_add if the above is not an option

Returns Alias



198
199
200
# File 'lib/brocadesan/provisioning.rb', line 198

def alias_change(al)
  obj_change al, Alias
end

#alias_create(al) ⇒ Object

Creates alias and saves config

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

al must be of class Alias. It will be created as it is with all the members of al instance

raises error if alias exists or different transaction is running or response is unexpected

Returns Alias



97
98
99
# File 'lib/brocadesan/provisioning.rb', line 97

def alias_create(al)
  obj_create al, Alias
end

#alias_delete(al) ⇒ Object

Deletes alias and saves config

NOTE: This command checks if every member exists before creating the cfg, if the cfg has many members it will take lot of time, however cfg creation is on daily task.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

al must be of class Alias.

raises error if alias does not exist or transaction is running or response is unexpected

this is low level command and it only removes the alias

it will not remove alias reference from zones, see Agent::#alias_purge that removes all

Returns nil if deletion is successful



149
150
151
# File 'lib/brocadesan/provisioning.rb', line 149

def alias_delete(al)
  obj_delete al, Alias
end

#alias_purge(al) ⇒ Object

Purges the Alias al completely, along with all references.

al must be instance of Alias.

The method first removes the alias from all zones it is member of. Then deletes the alias and saves configuration.

Should return nil if the al was purged.



281
282
283
# File 'lib/brocadesan/provisioning.rb', line 281

def alias_purge(al)
   obj_purge(al,Alias)
end

#alias_remove(al, member) ⇒ Object

Removes member from alias al

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

al must be instance of Alias and member must minstance of Wwn or String matching Alias::MEMBER_RULE

raises error if alias does not exist, member does not match the rule or different transaction is running or response is unexpected

Returns Alias or nil if the removed member was last one (it removes the Alias as well)



229
230
231
# File 'lib/brocadesan/provisioning.rb', line 229

def alias_remove(al,member)
  obj_remove(al,Alias,member)
end

#cfg_add(cfg, member) ⇒ Object

Adds zone cofiguration member to zone configuration.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

member must be instance of Zone and cfg instance of class ZoneConfiguration.

Zone and zone configuration must both exist.

Raises error if cfg or member do not exist or different transaction is running or response is unexpected.

Returns ZoneConfiguration



297
298
299
# File 'lib/brocadesan/provisioning.rb', line 297

def cfg_add(cfg,member)
  obj_add(cfg,ZoneConfiguration,member)
end

#cfg_create(cfg) ⇒ Object

Creates zone configuration and saves fabric configuration.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

cfg must be of class ZoneConfiguration. It will be created as it is with all the members of cfg.

Members must exist on the switch as zones.

Raises error if +cfg already exists or some of the members do not exist or different transaction is running or response is unexpected.

Returns ZoneConfiguration



129
130
131
# File 'lib/brocadesan/provisioning.rb', line 129

def cfg_create(cfg)
  obj_create cfg, ZoneConfiguration
end

#cfg_delete(cfg) ⇒ Object

Removes zone configuration and saves fabric configuration.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

cfg must be of class ZoneConfiguration. It will delete only zone config, keep the members intact.

Raises error if zone configuration does not exist or different transaction is running or response is unexpected.

Returns nil if deletion is successful



180
181
182
# File 'lib/brocadesan/provisioning.rb', line 180

def cfg_delete(cfg)
  obj_delete cfg, ZoneConfiguration
end

#cfg_enable(cfg) ⇒ Object

Enables zoning configuration cfg.

cfg must be ZoneConfiguration instance, will only use it’s name and ignore its members.

Raises erros if cancelled, nothing saved or unexpected result, otherwise returns true.

Raises:



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/brocadesan/provisioning.rb', line 354

def cfg_enable(cfg)
  raise Agent::Error.new(Agent::Error::CFG_BAD) if !cfg.kind_of? ZoneConfiguration
  
  response = interactive_mode do 
    query("cfgenable \"#{cfg}\"","y")
  end
  
  case
  when response.data.match(/#{Replies::OPERATION_CANCELLED}/)
    raise Agent::Error.new(Agent::Error::CFGSAVE_CANC)
  when response.data.match(/#{Replies::FLASH_UPDT}/)
    # if any transaction was in progres it is closed now
    @transaction=nil
    return true
  else
    raise Agent::Error.new(response.data)
  end
end

#cfg_remove(cfg, member) ⇒ Object

Remove zone member from configuration cfg.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

member must be of class Zone and cfg of class ZoneConfiguration.

Raises error if cfg does not exist or different transaction is running or response is unexpected.

Returns ZoneConfiguration or nil if the removed member was last one (it removes the ZoneConfiguration as well)



257
258
259
# File 'lib/brocadesan/provisioning.rb', line 257

def cfg_remove(cfg,member)
  obj_remove(cfg,ZoneConfiguration,member)
end

#check_for_running_transactionObject

Check if there is different zoning transaction in progress.

Returns true or false.

if started in within transactino block it will simply return false.



378
379
380
381
382
383
384
385
386
# File 'lib/brocadesan/provisioning.rb', line 378

def check_for_running_transaction
  # ignore this command when within transaction block
  # as this command is started at the begining of transaction
  return false if @transaction
  response = script_mode do
    query("cfgtransshow")
  end
  response.data.match(/#{Replies::NO_ZONING_TRANSACTION}/) ? false : true
end

#exist?(str, opts = {}) ⇒ Boolean

Checks if object exists in fabric. Finds even objects not stored in memory but as well object created in open transaction.

str is name of the object

opts :object => :zone, :alias, :cfg

if no :object is specified :zone is used by default

Returns:

  • (Boolean)


420
421
422
423
424
425
426
427
# File 'lib/brocadesan/provisioning.rb', line 420

def exist?(str,opts={})
  obj = !opts[:object].nil? && [:zone,:alias,:cfg].include?(opts[:object]) ? opts[:object] : :zone
  
  response = script_mode do 
    query("#{show_cmd(obj)} \"#{str}\"")
  end
  return response.data.match(/#{Replies::DOES_NOT_EXIST}/) ? false : true
end

#get_transactionObject

Queries the agent for ongoing transaction.

Retruns Transaction instance or false if there is no transaction.

Raises Error when transaction details could not be obtained

Raises:



47
48
49
50
51
52
# File 'lib/brocadesan/provisioning.rb', line 47

def get_transaction
  trans=Transaction.new(cfg_transaction(true))
  return false if trans.id==-1
  raise Error.new(Error::TRANS_UNEXPECTED) if trans.id.nil?
  trans
end

#pull(str, opts = {}) ⇒ Object

Pulls object matching str from switch configuration, even objects not yet saved

Returns the object or nil

options

:object

:zone (default) :alias :cfg :all



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/brocadesan/provisioning.rb', line 438

def pull(str,opts={})
  obj = !opts[:object].nil? && [:zone,:alias,:cfg, :all].include?(opts[:object]) ? opts[:object] : :zone
  
  response=nil
  
  session do
    if obj == :all
      [:cfg, :zone, :alias].each do |type|
        obj = type
        response = script_mode do 
          query("#{show_cmd(type)} \"#{str}\"")
        end
        response.parse
        break if !response.parsed[:base].nil?
      end
    else
      response = script_mode do 
        query("#{show_cmd(obj)} \"#{str}\"")
      end
      response.parse
    end 
  end
  return nil if response.parsed[:base].nil?
  
  object = response.parsed[:base][obj] #hash with single key
  name = object.keys[0]
  case obj
  when :zone
    item = Zone.new(name)
  when :alias
    item = Alias.new(name)
  when :cfg 
    item = ZoneConfiguration.new(name)
  end
  object[name].each do |member|
    item.add_member(member)
  end
     
  return response.data.match(/#{Replies::DOES_NOT_EXIST}/) ? nil : item
end

#rename_object(object, newname) ⇒ Object

Renames the object to newname

object and +newname can be instance of Alias, Zone, ZoneConfiguration or String

Returns renamed object or raises error



338
339
340
341
342
343
344
345
346
347
# File 'lib/brocadesan/provisioning.rb', line 338

def rename_object(object,newname)
  session do
    response = script_mode do
      query("zoneobjectrename \"#{object}\", \"#{newname}\"")
    end
    
    validate_and_save(response)
    pull newname.to_s, :object => get_obj_type(object)
  end
end

#transaction(opts = {:auto_enable => false}) ⇒ Object

Opens a provisioning transaction.

Transaction runs in 1 session.

Command allows transaction within transaction.

cfg_save will be run at the end of the transaction is there was no error raised and this is the top-most transaction block.

cfg_save wil not run as result of the transaction, you can however run it as last command in transaction



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/brocadesan/provisioning.rb', line 64

def transaction(opts={:auto_enable => false})
  @transaction_level||=0
  @transaction_level+=1
  session do
    raise_if_transaction_running
    @transaction ||= true
    raise Error.cannot_obtain_transaction_lock if not lock_transaction
    begin
      yield
      # get_transaction in case cfgsave or cfgenable was run in transaction block
      # if there is no transaction we do not need to run it
      # if there is transaction but opend by someone else then t
      cfg_save if @transaction_level==1 && get_transaction
    rescue => e
      abort_transaction
      raise e
    end  
  end
ensure
  @transaction_level-=1
  @transaction = nil if @transaction_level==0
end

#zone_add(zone, member) ⇒ Object

Adds zone member to zone.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns

member must be of class Alias or Wwn or Strign matching Alias::MEMBER_RULE and zone of class Zone.

Zone and member must exist. Exception is if member is Wwn.

Raises error if zone or member does not exist or transaction is running or response is unexpected.

Returns Zone



313
314
315
# File 'lib/brocadesan/provisioning.rb', line 313

def zone_add(zone,member)
  obj_add(zone,Zone,member)
end

#zone_change(zone) ⇒ Object

Changes zone and saves config.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

zone must be of class Zone. zone with the name will be removed and it will be created anew as it is with all the zone members.

Raises error if member does not exist (WWN and D,P members are exception) or different transaction is running or response is unexpected.

This is shorthand method that instead of modifiyng the zone removes the zone and then recreates it.

Returns Zone



215
216
217
# File 'lib/brocadesan/provisioning.rb', line 215

def zone_change(zone)
  obj_change zone, Zone
end

#zone_create(zone) ⇒ Object

Creates zone and saves config.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

zone must be of class Zone. It will be created as it is with all the zone members.

members must exist. Exaception is members matching WWN and D,P notation.

Raises error if zone exists already or different transaction is running or response is unexpected.

Returns Zone



113
114
115
# File 'lib/brocadesan/provisioning.rb', line 113

def zone_create(zone)
  obj_create zone, Zone
end

#zone_delete(zone) ⇒ Object

Removes zone and saves config.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

zone must be of class Zone.

Raises error if zone does not exist or different transaction is running or response is unexpected.

This is low level command, it will remove the zone record but not zone references from zone configurations, use #zone_purge for this purpose.

Returns nil if deletion is successful



166
167
168
# File 'lib/brocadesan/provisioning.rb', line 166

def zone_delete(zone)
  obj_delete zone, Zone
end

#zone_purge(zone) ⇒ Object

Purges the Zone zone completely, along with all references.

zone must me instance of Zone. The method first removes the zone from all zone configurations it is member of. Then deletes the zone and saves configuration.

Should return nil if the zone was purged.



269
270
271
# File 'lib/brocadesan/provisioning.rb', line 269

def zone_purge(zone)
  obj_purge(zone,Zone)
end

#zone_remove(zone, member) ⇒ Object

Remove member from zone.

If started outside transaction block it runs as single command transaction, otherwise it is not commited when the command returns.

zone must be of class Zone and member of class Alias or Wwn or String matching Alias::MEMBER_RULE

Raises error if zone does not exist or different transaction is running or response is unexpected.

Returns Zone or nil if the removed member was last one (it removes the Zone as well)



243
244
245
# File 'lib/brocadesan/provisioning.rb', line 243

def zone_remove(zone,member)
  obj_remove(zone,Zone,member)
end