Class: DevDNSd::Application

Inherits:
RExec::Daemon::Base
  • Object
show all
Includes:
DevDNSd::ApplicationMethods::Aliases, DevDNSd::ApplicationMethods::Server, DevDNSd::ApplicationMethods::System, Lazier::I18n
Defined in:
lib/devdnsd/application.rb

Overview

The main DevDNSd application.

Constant Summary collapse

ANY_REQUEST =

Class for ANY DNS request.

Resolv::DNS::Resource::IN::ANY
ANY_CLASSES =

List of classes handled in case of DNS request with resource class ANY.

[Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA, Resolv::DNS::Resource::IN::ANY, Resolv::DNS::Resource::IN::CNAME, Resolv::DNS::Resource::IN::HINFO, Resolv::DNS::Resource::IN::MINFO, Resolv::DNS::Resource::IN::MX, Resolv::DNS::Resource::IN::NS, Resolv::DNS::Resource::IN::PTR, Resolv::DNS::Resource::IN::SOA, Resolv::DNS::Resource::IN::TXT]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DevDNSd::ApplicationMethods::Server

#perform_server, #process_rule, #process_rule_in_classes

Methods included from DevDNSd::ApplicationMethods::Aliases

#compute_addresses, #is_ipv4?, #is_ipv6?, #manage_address, #manage_aliases

Methods included from DevDNSd::ApplicationMethods::System

#action_add, #action_install, #action_remove, #action_start, #action_stop, #action_uninstall, #dns_update, #execute_command, #is_osx?, #launch_agent_path, #resolver_path

Constructor Details

#initialize(command, locale) ⇒ Application

Creates a new application.

Parameters:

  • command (Bovem::Command)

    The current Bovem command.

  • locale (Symbol)

    The locale to use for the application.



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/devdnsd/application.rb', line 598

def initialize(command, locale)
  i18n_setup(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
  self.i18n = locale

  @locale = locale
  @command = command
  options = @command.application.get_options.reject {|_, v| v.nil? }

  # Setup logger
  @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(options["log_file"]) || Bovem::Logger.default_file, Logger::INFO)

  # Open configuration
  read_configuration(options)

  self
end

Instance Attribute Details

#commandBovem::Command (readonly)

Returns The Bovem command.

Returns:

  • (Bovem::Command)

    The Bovem command.



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/devdnsd/application.rb', line 577

class Application < RExec::Daemon::Base
  # Class for ANY DNS request.
  ANY_REQUEST = Resolv::DNS::Resource::IN::ANY

  # List of classes handled in case of DNS request with resource class ANY.
  ANY_CLASSES = [Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA, Resolv::DNS::Resource::IN::ANY, Resolv::DNS::Resource::IN::CNAME, Resolv::DNS::Resource::IN::HINFO, Resolv::DNS::Resource::IN::MINFO, Resolv::DNS::Resource::IN::MX, Resolv::DNS::Resource::IN::NS, Resolv::DNS::Resource::IN::PTR, Resolv::DNS::Resource::IN::SOA, Resolv::DNS::Resource::IN::TXT]

  include Lazier::I18n
  include DevDNSd::ApplicationMethods::System
  include DevDNSd::ApplicationMethods::Aliases
  include DevDNSd::ApplicationMethods::Server

  attr_reader :config
  attr_reader :command
  attr_accessor :logger
  attr_reader :locale

  # Creates a new application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  def initialize(command, locale)
    i18n_setup(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
    self.i18n = locale

    @locale = locale
    @command = command
    options = @command.application.get_options.reject {|_, v| v.nil? }

    # Setup logger
    @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(options["log_file"]) || Bovem::Logger.default_file, Logger::INFO)

    # Open configuration
    read_configuration(options)

    self
  end

  # Gets the current logger of the application.
  #
  # @return [Logger] The current logger of the application.
  def get_logger
    @logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
  end

  # This method is called when the server starts. By default is a no-op.
  #
  # @return [NilClass] `nil`.
  def on_start
  end

  # This method is called when the server stop.
  #
  # @return [NilClass] `nil`.
  def on_stop
  end

  # Returns a unique (singleton) instance of the application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Application] The unique (singleton) instance of the application.
  def self.instance(command = nil, locale = nil, force = false)
    @instance = nil if force
    @instance ||= DevDNSd::Application.new(command, locale) if command
    @instance
  end

  # Runs the application in foreground.
  #
  # @see #perform_server
  def self.run
    instance.perform_server
  end

  # Stops the application.
  def self.quit
    begin
      EM.add_timer(0.1) { ::EM.stop }
    rescue
    end
  end

  # Check if the current implementation supports DevDNSd.
  def self.check_ruby_implementation
    if defined?(JRuby) then
      Kernel.puts(Lazier::Localizer.new(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n.no_jruby)
      Kernel.exit(0)
    end
  end

  private
    # Reads configuration.
    #
    # @param options [Hash] The configuration to read.
    def read_configuration(options)
      begin
        @config = DevDNSd::Configuration.new(options["configuration"], options, @logger)
        @logger = nil
        @logger = get_logger
      rescue Bovem::Errors::InvalidConfiguration => e
        @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal(i18n.logging_failed(log_file))
        raise ::SystemExit
      end
    end
end

#configConfiguration (readonly)

Returns The Configuration of this application.

Returns:



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/devdnsd/application.rb', line 577

class Application < RExec::Daemon::Base
  # Class for ANY DNS request.
  ANY_REQUEST = Resolv::DNS::Resource::IN::ANY

  # List of classes handled in case of DNS request with resource class ANY.
  ANY_CLASSES = [Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA, Resolv::DNS::Resource::IN::ANY, Resolv::DNS::Resource::IN::CNAME, Resolv::DNS::Resource::IN::HINFO, Resolv::DNS::Resource::IN::MINFO, Resolv::DNS::Resource::IN::MX, Resolv::DNS::Resource::IN::NS, Resolv::DNS::Resource::IN::PTR, Resolv::DNS::Resource::IN::SOA, Resolv::DNS::Resource::IN::TXT]

  include Lazier::I18n
  include DevDNSd::ApplicationMethods::System
  include DevDNSd::ApplicationMethods::Aliases
  include DevDNSd::ApplicationMethods::Server

  attr_reader :config
  attr_reader :command
  attr_accessor :logger
  attr_reader :locale

  # Creates a new application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  def initialize(command, locale)
    i18n_setup(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
    self.i18n = locale

    @locale = locale
    @command = command
    options = @command.application.get_options.reject {|_, v| v.nil? }

    # Setup logger
    @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(options["log_file"]) || Bovem::Logger.default_file, Logger::INFO)

    # Open configuration
    read_configuration(options)

    self
  end

  # Gets the current logger of the application.
  #
  # @return [Logger] The current logger of the application.
  def get_logger
    @logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
  end

  # This method is called when the server starts. By default is a no-op.
  #
  # @return [NilClass] `nil`.
  def on_start
  end

  # This method is called when the server stop.
  #
  # @return [NilClass] `nil`.
  def on_stop
  end

  # Returns a unique (singleton) instance of the application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Application] The unique (singleton) instance of the application.
  def self.instance(command = nil, locale = nil, force = false)
    @instance = nil if force
    @instance ||= DevDNSd::Application.new(command, locale) if command
    @instance
  end

  # Runs the application in foreground.
  #
  # @see #perform_server
  def self.run
    instance.perform_server
  end

  # Stops the application.
  def self.quit
    begin
      EM.add_timer(0.1) { ::EM.stop }
    rescue
    end
  end

  # Check if the current implementation supports DevDNSd.
  def self.check_ruby_implementation
    if defined?(JRuby) then
      Kernel.puts(Lazier::Localizer.new(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n.no_jruby)
      Kernel.exit(0)
    end
  end

  private
    # Reads configuration.
    #
    # @param options [Hash] The configuration to read.
    def read_configuration(options)
      begin
        @config = DevDNSd::Configuration.new(options["configuration"], options, @logger)
        @logger = nil
        @logger = get_logger
      rescue Bovem::Errors::InvalidConfiguration => e
        @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal(i18n.logging_failed(log_file))
        raise ::SystemExit
      end
    end
end

#localeSymbol|nil (readonly)

Returns The current application locale.

Returns:

  • (Symbol|nil)

    The current application locale.



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/devdnsd/application.rb', line 577

class Application < RExec::Daemon::Base
  # Class for ANY DNS request.
  ANY_REQUEST = Resolv::DNS::Resource::IN::ANY

  # List of classes handled in case of DNS request with resource class ANY.
  ANY_CLASSES = [Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA, Resolv::DNS::Resource::IN::ANY, Resolv::DNS::Resource::IN::CNAME, Resolv::DNS::Resource::IN::HINFO, Resolv::DNS::Resource::IN::MINFO, Resolv::DNS::Resource::IN::MX, Resolv::DNS::Resource::IN::NS, Resolv::DNS::Resource::IN::PTR, Resolv::DNS::Resource::IN::SOA, Resolv::DNS::Resource::IN::TXT]

  include Lazier::I18n
  include DevDNSd::ApplicationMethods::System
  include DevDNSd::ApplicationMethods::Aliases
  include DevDNSd::ApplicationMethods::Server

  attr_reader :config
  attr_reader :command
  attr_accessor :logger
  attr_reader :locale

  # Creates a new application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  def initialize(command, locale)
    i18n_setup(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
    self.i18n = locale

    @locale = locale
    @command = command
    options = @command.application.get_options.reject {|_, v| v.nil? }

    # Setup logger
    @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(options["log_file"]) || Bovem::Logger.default_file, Logger::INFO)

    # Open configuration
    read_configuration(options)

    self
  end

  # Gets the current logger of the application.
  #
  # @return [Logger] The current logger of the application.
  def get_logger
    @logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
  end

  # This method is called when the server starts. By default is a no-op.
  #
  # @return [NilClass] `nil`.
  def on_start
  end

  # This method is called when the server stop.
  #
  # @return [NilClass] `nil`.
  def on_stop
  end

  # Returns a unique (singleton) instance of the application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Application] The unique (singleton) instance of the application.
  def self.instance(command = nil, locale = nil, force = false)
    @instance = nil if force
    @instance ||= DevDNSd::Application.new(command, locale) if command
    @instance
  end

  # Runs the application in foreground.
  #
  # @see #perform_server
  def self.run
    instance.perform_server
  end

  # Stops the application.
  def self.quit
    begin
      EM.add_timer(0.1) { ::EM.stop }
    rescue
    end
  end

  # Check if the current implementation supports DevDNSd.
  def self.check_ruby_implementation
    if defined?(JRuby) then
      Kernel.puts(Lazier::Localizer.new(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n.no_jruby)
      Kernel.exit(0)
    end
  end

  private
    # Reads configuration.
    #
    # @param options [Hash] The configuration to read.
    def read_configuration(options)
      begin
        @config = DevDNSd::Configuration.new(options["configuration"], options, @logger)
        @logger = nil
        @logger = get_logger
      rescue Bovem::Errors::InvalidConfiguration => e
        @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal(i18n.logging_failed(log_file))
        raise ::SystemExit
      end
    end
end

#loggerBovem::Logger

Returns The logger for this application.

Returns:

  • (Bovem::Logger)

    The logger for this application.



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/devdnsd/application.rb', line 577

class Application < RExec::Daemon::Base
  # Class for ANY DNS request.
  ANY_REQUEST = Resolv::DNS::Resource::IN::ANY

  # List of classes handled in case of DNS request with resource class ANY.
  ANY_CLASSES = [Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA, Resolv::DNS::Resource::IN::ANY, Resolv::DNS::Resource::IN::CNAME, Resolv::DNS::Resource::IN::HINFO, Resolv::DNS::Resource::IN::MINFO, Resolv::DNS::Resource::IN::MX, Resolv::DNS::Resource::IN::NS, Resolv::DNS::Resource::IN::PTR, Resolv::DNS::Resource::IN::SOA, Resolv::DNS::Resource::IN::TXT]

  include Lazier::I18n
  include DevDNSd::ApplicationMethods::System
  include DevDNSd::ApplicationMethods::Aliases
  include DevDNSd::ApplicationMethods::Server

  attr_reader :config
  attr_reader :command
  attr_accessor :logger
  attr_reader :locale

  # Creates a new application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  def initialize(command, locale)
    i18n_setup(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
    self.i18n = locale

    @locale = locale
    @command = command
    options = @command.application.get_options.reject {|_, v| v.nil? }

    # Setup logger
    @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(options["log_file"]) || Bovem::Logger.default_file, Logger::INFO)

    # Open configuration
    read_configuration(options)

    self
  end

  # Gets the current logger of the application.
  #
  # @return [Logger] The current logger of the application.
  def get_logger
    @logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
  end

  # This method is called when the server starts. By default is a no-op.
  #
  # @return [NilClass] `nil`.
  def on_start
  end

  # This method is called when the server stop.
  #
  # @return [NilClass] `nil`.
  def on_stop
  end

  # Returns a unique (singleton) instance of the application.
  #
  # @param command [Bovem::Command] The current Bovem command.
  # @param locale [Symbol] The locale to use for the application.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Application] The unique (singleton) instance of the application.
  def self.instance(command = nil, locale = nil, force = false)
    @instance = nil if force
    @instance ||= DevDNSd::Application.new(command, locale) if command
    @instance
  end

  # Runs the application in foreground.
  #
  # @see #perform_server
  def self.run
    instance.perform_server
  end

  # Stops the application.
  def self.quit
    begin
      EM.add_timer(0.1) { ::EM.stop }
    rescue
    end
  end

  # Check if the current implementation supports DevDNSd.
  def self.check_ruby_implementation
    if defined?(JRuby) then
      Kernel.puts(Lazier::Localizer.new(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n.no_jruby)
      Kernel.exit(0)
    end
  end

  private
    # Reads configuration.
    #
    # @param options [Hash] The configuration to read.
    def read_configuration(options)
      begin
        @config = DevDNSd::Configuration.new(options["configuration"], options, @logger)
        @logger = nil
        @logger = get_logger
      rescue Bovem::Errors::InvalidConfiguration => e
        @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal(i18n.logging_failed(log_file))
        raise ::SystemExit
      end
    end
end

Class Method Details

.check_ruby_implementationObject

Check if the current implementation supports DevDNSd.



662
663
664
665
666
667
# File 'lib/devdnsd/application.rb', line 662

def self.check_ruby_implementation
  if defined?(JRuby) then
    Kernel.puts(Lazier::Localizer.new(:devdnsd, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n.no_jruby)
    Kernel.exit(0)
  end
end

.instance(command = nil, locale = nil, force = false) ⇒ Application

Returns a unique (singleton) instance of the application.

Parameters:

  • command (Bovem::Command) (defaults to: nil)

    The current Bovem command.

  • locale (Symbol) (defaults to: nil)

    The locale to use for the application.

  • force (Boolean) (defaults to: false)

    If to force recreation of the instance.

Returns:

  • (Application)

    The unique (singleton) instance of the application.



640
641
642
643
644
# File 'lib/devdnsd/application.rb', line 640

def self.instance(command = nil, locale = nil, force = false)
  @instance = nil if force
  @instance ||= DevDNSd::Application.new(command, locale) if command
  @instance
end

.quitObject

Stops the application.



654
655
656
657
658
659
# File 'lib/devdnsd/application.rb', line 654

def self.quit
  begin
    EM.add_timer(0.1) { ::EM.stop }
  rescue
  end
end

.runObject

Runs the application in foreground.



649
650
651
# File 'lib/devdnsd/application.rb', line 649

def self.run
  instance.perform_server
end

Instance Method Details

#get_loggerLogger

Gets the current logger of the application.

Returns:

  • (Logger)

    The current logger of the application.



618
619
620
# File 'lib/devdnsd/application.rb', line 618

def get_logger
  @logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
end

#on_startNilClass

This method is called when the server starts. By default is a no-op.

Returns:

  • (NilClass)

    nil.



625
626
# File 'lib/devdnsd/application.rb', line 625

def on_start
end

#on_stopNilClass

This method is called when the server stop.

Returns:

  • (NilClass)

    nil.



631
632
# File 'lib/devdnsd/application.rb', line 631

def on_stop
end