Class: ConcertoConfig::StaticAddressing

Inherits:
Object
  • Object
show all
Defined in:
lib/concerto_client/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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ StaticAddressing

Returns a new instance of StaticAddressing.



313
314
315
316
317
318
# File 'lib/concerto_client/netconfig.rb', line 313

def initialize(args={})
    @nameservers = args['nameservers']
    @address = args['address']
    @netmask = args['netmask']
    @gateway = args['gateway']
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



354
355
356
# File 'lib/concerto_client/netconfig.rb', line 354

def address
  @address
end

#gatewayObject

Returns the value of attribute gateway.



354
355
356
# File 'lib/concerto_client/netconfig.rb', line 354

def gateway
  @gateway
end

#nameserversObject

Returns the value of attribute nameservers.



354
355
356
# File 'lib/concerto_client/netconfig.rb', line 354

def nameservers
  @nameservers
end

#netmaskObject

Returns the value of attribute netmask.



354
355
356
# File 'lib/concerto_client/netconfig.rb', line 354

def netmask
  @netmask
end

Class Method Details

.descriptionObject



350
351
352
# File 'lib/concerto_client/netconfig.rb', line 350

def self.description
    "Static Addressing"
end

Instance Method Details

#addressing_typeObject



320
321
322
# File 'lib/concerto_client/netconfig.rb', line 320

def addressing_type
    'static'
end

#argsObject



324
325
326
327
328
329
330
331
# File 'lib/concerto_client/netconfig.rb', line 324

def args
    {
        'address' => @address,
        'netmask' => @netmask,
        'gateway' => @gateway,
        'nameservers' => @nameservers
    }
end

#interfaces_linesObject



333
334
335
336
337
338
339
# File 'lib/concerto_client/netconfig.rb', line 333

def interfaces_lines
    [
        "address #{@address}",
        "netmask #{@netmask}",
        "gateway #{@gateway}"
    ]
end

#nameservers_flatObject



397
398
399
# File 'lib/concerto_client/netconfig.rb', line 397

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.



385
386
387
388
389
390
391
392
393
394
395
# File 'lib/concerto_client/netconfig.rb', line 385

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_assignObject



356
357
358
# File 'lib/concerto_client/netconfig.rb', line 356

def safe_assign
    [ :address, :netmask, :gateway, :nameservers_flat ]
end

#validateObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/concerto_client/netconfig.rb', line 360

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_configsObject



342
343
344
345
346
347
348
# File 'lib/concerto_client/netconfig.rb', line 342

def write_configs
    File.open('/etc/resolv.conf','w') do |resolvconf|
        @nameservers.each do |nameserver|
            resolvconf.puts("nameserver #{nameserver}");
        end
    end
end