Class: Puppet::Util::NetworkDevice::Cisco::Device

Inherits:
Base
  • Object
show all
Includes:
IPCalc
Defined in:
lib/puppet/util/network_device/cisco/device.rb

Constant Summary collapse

IF =
{
  :FastEthernet => %w{FastEthernet FastEth Fast FE Fa F},
  :GigabitEthernet => %w{GigabitEthernet GigEthernet GigEth GE Gi G},
  :TenGigabitEthernet => %w{TenGigabitEthernet TE Te},
  :Ethernet => %w{Ethernet Eth E},
  :Serial => %w{Serial Se S},
  :PortChannel => %w{PortChannel Port-Channel Po},
  :POS => %w{POS P},
  :VLAN => %w{VLAN VL V},
  :Loopback => %w{Loopback Loop Lo},
  :ATM => %w{ATM AT A},
  :Dialer => %w{Dialer Dial Di D},
  :VirtualAccess => %w{Virtual-Access Virtual-A Virtual Virt}
}

Constants included from IPCalc

IPCalc::IP, IPCalc::IPv4, IPCalc::IPv6_full, IPCalc::IPv6_partial, IPCalc::Octet

Instance Attribute Summary collapse

Attributes inherited from Base

#transport, #url

Instance Method Summary collapse

Methods included from IPCalc

#bits, #fullmask, #linklocal?, #mask, #netmask, #parse, #prefix_length, #wildmask

Constructor Details

#initialize(url, options = {}) ⇒ Device

Returns a new instance of Device.



15
16
17
18
19
# File 'lib/puppet/util/network_device/cisco/device.rb', line 15

def initialize(url, options = {})
  super(url, options)
  @enable_password = options[:enable_password] || parse_enable(@url.query)
  transport.default_prompt = /[#>]\s?\z/n
end

Instance Attribute Details

#enable_passwordObject

Returns the value of attribute enable_password.



13
14
15
# File 'lib/puppet/util/network_device/cisco/device.rb', line 13

def enable_password
  @enable_password
end

Instance Method Details

#canonalize_ifname(interface) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/puppet/util/network_device/cisco/device.rb', line 102

def canonalize_ifname(interface)
  IF.each do |k,ifnames|
    if found = ifnames.find { |ifname| interface =~ /^#{ifname}\s*\d/i }
      found = /^#{found}(.+)\Z/i.match(interface)
      return "#{k.to_s}#{found[1]}".gsub(/\s+/,'')
    end
  end
  interface
end

#command(cmd = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



41
42
43
44
45
46
47
# File 'lib/puppet/util/network_device/cisco/device.rb', line 41

def command(cmd = nil)
  connect
  out = execute(cmd) if cmd
  yield self if block_given?
  disconnect
  out
end

#connectObject



28
29
30
31
32
33
34
35
# File 'lib/puppet/util/network_device/cisco/device.rb', line 28

def connect
  transport.connect
  
  transport.command("terminal length 0") do |out|
    enable if out =~ />\s?\z/n
  end
  find_capabilities
end

#disconnectObject



37
38
39
# File 'lib/puppet/util/network_device/cisco/device.rb', line 37

def disconnect
  transport.close
end

#enableObject



69
70
71
72
73
# File 'lib/puppet/util/network_device/cisco/device.rb', line 69

def enable
  raise _("Can't issue \"enable\" to enter privileged, no enable password set") unless enable_password
  transport.command("enable", :prompt => /^Password:/)
  transport.command(enable_password)
end

#execute(cmd) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/puppet/util/network_device/cisco/device.rb', line 49

def execute(cmd)
  transport.command(cmd) do |out|
    if out =~ /^%/mo or out =~ /^Command rejected:/mo
      # strip off the command just sent
      error = out.sub(cmd,'')
      Puppet.err _("Error while executing '%{cmd}', device returned: %{error}") % { cmd: cmd, error: error }
    end
  end
end

#factsObject



112
113
114
115
116
117
118
119
# File 'lib/puppet/util/network_device/cisco/device.rb', line 112

def facts
  @facts ||= Puppet::Util::NetworkDevice::Cisco::Facts.new(transport)
  facts = {}
  command do |ng|
    facts = @facts.retrieve
  end
  facts
end

#find_capabilitiesObject



79
80
81
82
83
84
85
# File 'lib/puppet/util/network_device/cisco/device.rb', line 79

def find_capabilities
  out = execute("sh vlan brief")
  lines = out.split("\n")
  lines.shift; lines.pop

  @support_vlan_brief = ! (lines.first =~ /^%/)
end

#interface(name) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/puppet/util/network_device/cisco/device.rb', line 121

def interface(name)
  ifname = canonalize_ifname(name)
  interface = parse_interface(ifname)
  return { :ensure => :absent } if interface.empty?
  interface.merge!(parse_trunking(ifname))
  interface.merge!(parse_interface_config(ifname))
end

#loginObject



59
60
61
62
63
64
65
66
67
# File 'lib/puppet/util/network_device/cisco/device.rb', line 59

def 
  return if transport.handles_login?
  if @url.user != ''
    transport.command(@url.user, :prompt => /^Password:/)
  else
    transport.expect(/^Password:/)
  end
  transport.command(@url.password)
end

#new_interface(name) ⇒ Object



129
130
131
# File 'lib/puppet/util/network_device/cisco/device.rb', line 129

def new_interface(name)
  Puppet::Util::NetworkDevice::Cisco::Interface.new(canonalize_ifname(name), transport)
end

#parse_enable(query) ⇒ Object



21
22
23
24
25
26
# File 'lib/puppet/util/network_device/cisco/device.rb', line 21

def parse_enable(query)
  if query
    params = CGI.parse(query)
    params['enable'].first unless params['enable'].empty?
  end
end

#parse_interface(name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet/util/network_device/cisco/device.rb', line 133

def parse_interface(name)
  resource = {}
  out = execute("sh interface #{name}")
  lines = out.split("\n")
  lines.shift; lines.pop
  lines.each do |l|
    if l =~ /#{name} is (.+), line protocol is /
      resource[:ensure] = ($1 == 'up' ? :present : :absent);
    end
    if l =~ /Auto Speed \(.+\),/ or l =~ /Auto Speed ,/ or l =~ /Auto-speed/
      resource[:speed] = :auto
    end
    if l =~ /, (.+)Mb\/s/
      resource[:speed] = $1
    end
    if l =~ /\s+Auto-duplex \((.{4})\),/
      resource[:duplex] = :auto
    end
    if l =~ /\s+(.+)-duplex/
      resource[:duplex] = $1 == "Auto" ? :auto : $1.downcase.to_sym
    end
    if l =~ /Description: (.+)/
      resource[:description] = $1
    end
  end
  resource
end

#parse_interface_config(name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/puppet/util/network_device/cisco/device.rb', line 161

def parse_interface_config(name)
  resource = Hash.new { |hash, key| hash[key] = Array.new ; }
  out = execute("sh running-config interface #{name} | begin interface")
  lines = out.split("\n")
  lines.shift; lines.pop
  lines.each do |l|
    if l =~ /ip address (#{IP}) (#{IP})\s+secondary\s*$/
      resource[:ipaddress] << [prefix_length(IPAddr.new($2)), IPAddr.new($1), 'secondary']
    end
    if l =~ /ip address (#{IP}) (#{IP})\s*$/
      resource[:ipaddress] << [prefix_length(IPAddr.new($2)), IPAddr.new($1), nil]
    end
    if l =~ /ipv6 address (#{IP})\/(\d+) (eui-64|link-local)/
      resource[:ipaddress] << [$2.to_i, IPAddr.new($1), $3]
    end
    if l =~ /channel-group\s+(\d+)/
      resource[:etherchannel] = $1
    end
  end
  resource
end

#parse_trunking(interface) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/puppet/util/network_device/cisco/device.rb', line 236

def parse_trunking(interface)
  trunking = {}
  out = execute("sh interface #{interface} switchport")
  lines = out.split("\n")
  lines.shift; lines.pop
  lines.each do |l|
    case l
    when /^Administrative mode:\s+(.*)$/i
      case $1
      when "trunk"
        trunking[:mode] = :trunk
      when "static access"
        trunking[:mode] = :access
      when "dynamic auto"
        trunking[:mode] = 'dynamic auto'
      when "dynamic desirable"
        trunking[:mode] = 'dynamic desirable'
      else
        raise _("Unknown switchport mode: %{mode} for %{interface}") % { mode: $1, interface: interface }
      end
    when /^Administrative Trunking Encapsulation:\s+(.*)$/
      case $1
      when "dot1q","isl"
        trunking[:encapsulation] = $1.to_sym if trunking[:mode] != :access
      when "negotiate"
        trunking[:encapsulation] = :negotiate
      else
        raise _("Unknown switchport encapsulation: %{value} for %{interface}") % { value: $1, interface: interface }
      end
    when /^Access Mode VLAN:\s+(.*) \((.*)\)$/
      trunking[:access_vlan] = $1 if $2 != '(Inactive)'
    when /^Trunking Native Mode VLAN:\s+(.*) \(.*\)$/
      trunking[:native_vlan] = $1
    when /^Trunking VLANs Enabled:\s+(.*)$/
      next if trunking[:mode] == :access
      vlans = $1
      trunking[:allowed_trunk_vlans] = case vlans
      when /all/i
        :all
      when /none/i
        :none
      else
        vlans
      end
    end
  end
  trunking
end

#parse_vlansObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/puppet/util/network_device/cisco/device.rb', line 183

def parse_vlans
  vlans = {}
  out = execute(support_vlan_brief? ? "sh vlan brief" : "sh vlan-switch brief")
  lines = out.split("\n")
  lines.shift; lines.shift; lines.shift; lines.pop
  vlan = nil
  lines.each do |l|
    case l
          # vlan    name    status
    when /^(\d+)\s+(\w+)\s+(\w+)\s+([a-zA-Z0-9,\/. ]+)\s*$/
      vlan = { :name => $1, :description => $2, :status => $3, :interfaces => [] }
      if $4.strip.length > 0
        vlan[:interfaces] = $4.strip.split(/\s*,\s*/).map{ |ifn| canonalize_ifname(ifn) }
      end
      vlans[vlan[:name]] = vlan
    when /^\s+([a-zA-Z0-9,\/. ]+)\s*$/
      raise _("invalid sh vlan summary output") unless vlan
      if $1.strip.length > 0
        vlan[:interfaces] += $1.strip.split(/\s*,\s*/).map{ |ifn| canonalize_ifname(ifn) }
      end
    else
    end
  end
  vlans
end

#support_vlan_brief?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/puppet/util/network_device/cisco/device.rb', line 75

def support_vlan_brief?
  !! @support_vlan_brief
end

#update_vlan(id, is = {}, should = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/puppet/util/network_device/cisco/device.rb', line 209

def update_vlan(id, is = {}, should = {})
  if should[:ensure] == :absent
    Puppet.info _("Removing %{id} from device vlan") % { id: id }
    execute("conf t")
    execute("no vlan #{id}")
    execute("exit")
    return
  end

  # Cisco VLANs are supposed to be alphanumeric only
  if should[:description] =~ /[^\w]/
    Puppet.err _("Invalid VLAN name '%{name}' for Cisco device.\nVLAN name must be alphanumeric, no spaces or special characters.") % { name: should[:description] }
    return
  end
  
  # We're creating or updating an entry
  execute("conf t")
  execute("vlan #{id}")
  [is.keys, should.keys].flatten.uniq.each do |property|
    Puppet.debug("trying property: #{property}: #{should[property]}")
    next if property != :description
    execute("name #{should[property]}")
  end
  execute("exit")
  execute("exit")
end