Class: Bandshell::StaticAddressing
- Inherits:
-
Object
- Object
- Bandshell::StaticAddressing
- Defined in:
- lib/bandshell/netconfig.rb
Overview
Static IPv4 addressing. We use the IPAddress gem to validate that the address information is vaguely correct (weeding out errors like the gateway being on another subnet)
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#gateway ⇒ Object
Returns the value of attribute gateway.
-
#nameservers ⇒ Object
Returns the value of attribute nameservers.
-
#netmask ⇒ Object
Returns the value of attribute netmask.
Class Method Summary collapse
Instance Method Summary collapse
- #addressing_type ⇒ Object
- #args ⇒ Object
-
#initialize(args = {}) ⇒ StaticAddressing
constructor
A new instance of StaticAddressing.
- #interfaces_lines ⇒ Object
- #nameservers_flat ⇒ Object
-
#nameservers_flat=(separated_list) ⇒ Object
These next two methods are for the web interface, where it’s more convenient to enter a bunch of nameservers on one line than to have to deal with an array of fields.
- #safe_assign ⇒ Object
- #validate ⇒ Object
- #write_configs ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ StaticAddressing
Returns a new instance of StaticAddressing.
314 315 316 317 318 319 |
# File 'lib/bandshell/netconfig.rb', line 314 def initialize(args={}) @nameservers = args['nameservers'] @address = args['address'] @netmask = args['netmask'] @gateway = args['gateway'] end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
355 356 357 |
# File 'lib/bandshell/netconfig.rb', line 355 def address @address end |
#gateway ⇒ Object
Returns the value of attribute gateway.
355 356 357 |
# File 'lib/bandshell/netconfig.rb', line 355 def gateway @gateway end |
#nameservers ⇒ Object
Returns the value of attribute nameservers.
355 356 357 |
# File 'lib/bandshell/netconfig.rb', line 355 def nameservers @nameservers end |
#netmask ⇒ Object
Returns the value of attribute netmask.
355 356 357 |
# File 'lib/bandshell/netconfig.rb', line 355 def netmask @netmask end |
Class Method Details
.description ⇒ Object
351 352 353 |
# File 'lib/bandshell/netconfig.rb', line 351 def self.description "Static Addressing" end |
Instance Method Details
#addressing_type ⇒ Object
321 322 323 |
# File 'lib/bandshell/netconfig.rb', line 321 def addressing_type 'static' end |
#args ⇒ Object
325 326 327 328 329 330 331 332 |
# File 'lib/bandshell/netconfig.rb', line 325 def args { 'address' => @address, 'netmask' => @netmask, 'gateway' => @gateway, 'nameservers' => @nameservers } end |
#interfaces_lines ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/bandshell/netconfig.rb', line 334 def interfaces_lines [ "address #{@address}", "netmask #{@netmask}", "gateway #{@gateway}" ] end |
#nameservers_flat ⇒ Object
398 399 400 |
# File 'lib/bandshell/netconfig.rb', line 398 def nameservers_flat @nameservers.join(',') end |
#nameservers_flat=(separated_list) ⇒ Object
These next two methods are for the web interface, where it’s more convenient to enter a bunch of nameservers on one line than to have to deal with an array of fields.
386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/bandshell/netconfig.rb', line 386 def nameservers_flat=(separated_list) servers = separated_list.strip.split(/\s*[,|:;\s]\s*/) servers.each do |server| server.strip! p server if not IPAddress.valid? server fail "One or more invalid IP addresses in nameserver list" end end @nameservers = servers end |
#safe_assign ⇒ Object
357 358 359 |
# File 'lib/bandshell/netconfig.rb', line 357 def safe_assign [ :address, :netmask, :gateway, :nameservers_flat ] end |
#validate ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/bandshell/netconfig.rb', line 361 def validate @address.strip! @netmask.strip! @gateway.strip! if not IPAddress.valid_ipv4?(@address) fail "Static address is invalid" end p @netmask if not IPAddress.valid_ipv4_netmask?(@netmask) fail "Static netmask is invalid" end p @netmask subnet = IPAddress::IPv4.new(@address) subnet.netmask = @netmask if not subnet.include? IPAddress::IPv4.new(gateway) fail "Gateway provided is unreachable" end end |
#write_configs ⇒ Object
343 344 345 346 347 348 349 |
# File 'lib/bandshell/netconfig.rb', line 343 def write_configs File.open('/etc/resolv.conf','w') do |resolvconf| @nameservers.each do |nameserver| resolvconf.puts("nameserver #{nameserver}"); end end end |