Module: DevDNSd::Aliases

Extended by:
ActiveSupport::Concern
Included in:
Application
Defined in:
lib/devdnsd/aliases.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.



59
60
61
62
# File 'lib/devdnsd/aliases.rb', line 59

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

#ipv4?(address) ⇒ Boolean Also known as: is_ipv4?

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.



68
69
70
71
72
73
# File 'lib/devdnsd/aliases.rb', line 68

def 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

#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.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/devdnsd/aliases.rb', line 80

def ipv6?(address)
  address = address.ensure_string

  catch(:valid) do
    # IPv6 (normal)
    check_normal_ipv6(address)
    # IPv6 (IPv4 compat)
    check_compat_ipv6(address)

    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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/devdnsd/aliases.rb', line 40

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

  # Now execute
  if rv
    if !dry_run
      execute_manage(command, prefix, type, address, config)
    else
      log_management(:dry_run, prefix, type, i18n.remove, i18n.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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/devdnsd/aliases.rb', line 19

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?
    # Now, for every address, call the command
    addresses.all? { |address| manage_address(operation, address, options[:dry_run]) }
  else
    @logger.error(message)
    false
  end
end