Module: DevDNSd::ApplicationMethods::Aliases

Extended by:
ActiveSupport::Concern
Included in:
DevDNSd::Application
Defined in:
lib/devdnsd/application.rb

Overview

Methods to handle interfaces aliases.

Instance Method Summary collapse

Instance Method Details

#compute_addresses(type = :all) ⇒ Array

Computes the list of address to manage.

Parameters:

  • type (Symbol) (defaults to: :all)

    The type of addresses to consider. Valid values are :ipv4, :ipv6, otherwise all addresses are considered.

Returns:

  • (Array)

    The list of addresses to add or remove from the interface.



406
407
408
409
# File 'lib/devdnsd/application.rb', line 406

def compute_addresses(type = :all)
  config = self.config
  config.addresses.present? ? filter_addresses(config, type) : generate_addresses(config, type)
end

#is_ipv4?(address) ⇒ Boolean

Checks if an address is a valid IPv4 address.

Parameters:

  • address (String)

    The address to check.

Returns:

  • (Boolean)

    true if the address is a valid IPv4 address, false otherwise.



415
416
417
418
419
420
# File 'lib/devdnsd/application.rb', line 415

def is_ipv4?(address)
  address = address.ensure_string

  mo = /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/.match(address)
  (mo && mo.captures.all? {|i| i.to_i < 256}) ? true : false
end

#is_ipv6?(address) ⇒ Boolean

Checks if an address is a valid IPv6 address.

Parameters:

  • address (String)

    The address to check.

Returns:

  • (Boolean)

    true if the address is a valid IPv6 address, false otherwise.



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/devdnsd/application.rb', line 426

def is_ipv6?(address)
  address = address.ensure_string

  catch(:valid) do
    # IPv6 (normal)
    throw(:valid, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ address
    throw(:valid, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ address
    throw(:valid, true) if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ address
    # IPv6 (IPv4 compat)
    throw(:valid, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ address && is_ipv4?($')
    throw(:valid, true) if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ address && is_ipv4?($')
    throw(:valid, true) if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ address && is_ipv4?($')

    false
  end
end

#manage_address(type, address, dry_run = false) ⇒ Boolean

Adds or removes an alias from the interface.

Parameters:

  • type (Symbol)

    The operation to execute. Can be :add or :remove.

  • address (String)

    The address to manage.

  • dry_run (Boolean) (defaults to: false)

    If only show which modifications will be done.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/devdnsd/application.rb', line 386

def manage_address(type, address, dry_run = false)
  locale = i18n
  rv, command, prefix = setup_management(type, address)

  # Now execute
  if rv then
    if !dry_run then
      execute_manage(command, prefix, type, address, self.config)
    else
      log_management(:dry_run, prefix, type, locale.remove, locale.add, address, config)
    end
  end

  rv
end

#manage_aliases(operation, message, options) ⇒ Boolean

Manages aliases.

Parameters:

  • operation (Symbol)

    The type of operation. Can be :add or :remove.

  • message (String)

    The message to show if no addresses are found.

  • options (Hash)

    The options provided by the user.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/devdnsd/application.rb', line 365

def manage_aliases(operation, message, options)
  config = self.config
  options.each { |k, v| config.send("#{k}=", v) if config.respond_to?("#{k}=") }

  addresses = compute_addresses

  if addresses.present? then
    # Now, for every address, call the command
    addresses.all? {|address| manage_address(operation, address, options[:dry_run]) }
  else
    @logger.error(message)
    false
  end
end