class Dory::Dnsmasq
Public Class Methods
address(addr)
click to toggle source
# File lib/dory/dnsmasq.rb, line 114 def self.address(addr) Dory::Dinghy.match?(addr) ? Dory::Dinghy.ip : addr end
answer_from_settings()
click to toggle source
# File lib/dory/dnsmasq.rb, line 222 def self.answer_from_settings # This `== true` is important because kill_others could be # 'no' which would be a truthy value despite the fact that it # should be falsey if self.kill_others == true || self.kill_others =~ /yes/i 'Y' elsif self.kill_others == false || self.kill_others =~ /no/i 'N' else nil end end
ask_about_killing?()
click to toggle source
# File lib/dory/dnsmasq.rb, line 214 def self.ask_about_killing? !self.answer_from_settings end
container_name()
click to toggle source
# File lib/dory/dnsmasq.rb, line 98 def self.container_name Dory::Config.settings[:dory][:dnsmasq][:container_name] end
dnsmasq_image_name()
click to toggle source
# File lib/dory/dnsmasq.rb, line 13 def self.dnsmasq_image_name setting = Dory::Config.settings[:dory][:dnsmasq][:image] setting ? setting : 'freedomben/dory-dnsmasq:1.1.0' end
domain_addr_arg_string()
click to toggle source
# File lib/dory/dnsmasq.rb, line 118 def self.domain_addr_arg_string if self.old_domain "#{Shellwords.escape(self.old_domain)} #{Shellwords.escape(self.address(self.old_address))}" else self.domains.map do |domain| "#{Shellwords.escape(domain[:domain])} #{Shellwords.escape(self.address(domain[:address]))}" end.join(" ") end end
domains()
click to toggle source
# File lib/dory/dnsmasq.rb, line 102 def self.domains Dory::Config.settings[:dory][:dnsmasq][:domains] end
down_systemd_services()
click to toggle source
# File lib/dory/dnsmasq.rb, line 170 def self.down_systemd_services puts "[DEBUG] Putting systemd services down" if Dory::Config.debug? conf = if ask_about_killing? puts "You have some systemd services running that will race against us \n" \ "to bind to port 53 (and usually they win):".yellow puts "\n #{self.systemd_services.join(', ')}\n".yellow puts "If we don't stop these services temporarily while putting up the \n" \ "dnsmasq container, starting it will likely fail.".yellow print "Would you like me to put them down while we start dns \n" \ "(I'll put them back up when finished)? (Y/N): ".yellow STDIN.gets.chomp else answer_from_settings end if conf =~ /y/i if self.systemd_services.all? { |service| Dory::Systemd.set_systemd_service(service: service, up: false) } puts "Putting down services succeeded".green else puts "One or more services failed to stop".red end else puts 'OK, not putting down the services'.yellow set_systemd_services([]) end end
first_attempt_failed?()
click to toggle source
# File lib/dory/dnsmasq.rb, line 18 def self.first_attempt_failed? @@first_attempt_failed ||= false if @first_attempt_failed.nil? @@first_attempt_failed end
handle_error(_command_output)
click to toggle source
# File lib/dory/dnsmasq.rb, line 67 def self.handle_error(_command_output) puts "[DEBUG] handling dnsmasq start error" if Dory::Config.debug? # If we've already tried to handle failure, prevent infinite recursion if first_attempt_failed? puts "[DEBUG] Attempt to kill conflicting service failed" if Dory::Config.debug? return false else if Dory::Config.debug? puts "[DEBUG] First attempt to start dnsmasq failed." \ "There is probably a conflicting service present" end set_first_attempt_failed(true) self.start(handle_error: false) end end
has_services_that_block_dnsmasq?()
click to toggle source
# File lib/dory/dnsmasq.rb, line 160 def self.has_services_that_block_dnsmasq? !self.running_services_that_block_dnsmasq.empty? end
ip_from_dinghy?()
click to toggle source
# File lib/dory/dnsmasq.rb, line 83 def self.ip_from_dinghy? Dory::Dinghy.match?(self.address(self.old_address)) || self.domains.any?{ |domain| Dory::Dinghy.match?(self.address(domain[:address])) } end
kill_others()
click to toggle source
# File lib/dory/dnsmasq.rb, line 218 def self.kill_others Dory::Config.settings[:dory][:dnsmasq][:kill_others] end
offer_to_kill(listener_list, answer: nil)
click to toggle source
# File lib/dory/dnsmasq.rb, line 135 def self.offer_to_kill(listener_list, answer: nil) listener_list.each do |process| puts "Process '#{process.command}' with PID '#{process.pid}' is listening on #{process.node} port #{self.port}.".yellow end pids = listener_list.uniq(&:pid).map(&:pid) pidstr = pids.join(' and ') print "This interferes with Dory's dnsmasq container. Would you like me to kill PID #{pidstr}? (Y/N): ".yellow conf = answer ? answer : answer_from_settings conf = STDIN.gets.chomp unless conf if conf =~ /y/i puts "Requesting sudo to kill PID #{pidstr}".green return Sh.run_command("sudo kill #{pids.join(' ')}").success? else puts "OK, not killing PID #{pidstr}. Please kill manually and try starting dory again.".red return false end end
old_address()
click to toggle source
# File lib/dory/dnsmasq.rb, line 110 def self.old_address Dory::Config.settings[:dory][:dnsmasq][:address] end
old_domain()
click to toggle source
# File lib/dory/dnsmasq.rb, line 106 def self.old_domain Dory::Config.settings[:dory][:dnsmasq][:domain] end
port()
click to toggle source
# File lib/dory/dnsmasq.rb, line 88 def self.port return 53 unless Os.macos? p = Dory::Config.settings[:dory][:dnsmasq][:port] p.nil? || p == 0 ? 19323 : self.sanitize_port(p) end
run_command()
click to toggle source
# File lib/dory/dnsmasq.rb, line 128 def self.run_command "docker run -d -p #{self.port}:#{self.port}/tcp -p #{self.port}:#{self.port}/udp " \ "--name=#{Shellwords.escape(self.container_name)} " \ "--cap-add=NET_ADMIN #{Shellwords.escape(self.dnsmasq_image_name)} " \ "#{self.domain_addr_arg_string}" end
run_postconditions()
click to toggle source
# File lib/dory/dnsmasq.rb, line 62 def self.run_postconditions puts "[DEBUG] dnsmasq service running postconditions" if Dory::Config.debug? self.up_systemd_services if self.systemd_services? end
run_preconditions()
click to toggle source
# File lib/dory/dnsmasq.rb, line 41 def self.run_preconditions puts "[DEBUG] dnsmasq service running preconditions" if Dory::Config.debug? # we don't want to hassle the user with checking the port unless necessary if first_attempt_failed? self.set_systemd_services(self.running_services_that_block_dnsmasq) self.down_systemd_services if self.systemd_services? puts "[DEBUG] First attempt failed. Checking port #{self.port}" if Dory::Config.debug? listener_list = Dory::PortUtils.check_port(self.port) unless listener_list.empty? return self.offer_to_kill(listener_list) end return false else puts "[DEBUG] Skipping preconditions on first run" if Dory::Config.debug? return true end end
running_services_that_block_dnsmasq()
click to toggle source
# File lib/dory/dnsmasq.rb, line 164 def self.running_services_that_block_dnsmasq self.services_that_block_dnsmasq.select do |service| Dory::Systemd.systemd_service_running?(service) end end
sanitize_port(port)
click to toggle source
# File lib/dory/dnsmasq.rb, line 94 def self.sanitize_port(port) port.to_s.gsub(/\D/, '').to_i end
services_that_block_dnsmasq()
click to toggle source
# File lib/dory/dnsmasq.rb, line 153 def self.services_that_block_dnsmasq %w[ NetworkManager.service systemd-resolved.service ] end
set_first_attempt_failed(failed)
click to toggle source
# File lib/dory/dnsmasq.rb, line 23 def self.set_first_attempt_failed(failed) @@first_attempt_failed = failed end
set_systemd_services(services)
click to toggle source
# File lib/dory/dnsmasq.rb, line 37 def self.set_systemd_services(services) @@systemd_services = services end
systemd_services()
click to toggle source
# File lib/dory/dnsmasq.rb, line 32 def self.systemd_services @@systemd_services ||= [] @@systemd_services end
systemd_services?()
click to toggle source
# File lib/dory/dnsmasq.rb, line 27 def self.systemd_services? return false unless self.systemd_services self.systemd_services.count > 0 end
up_systemd_services()
click to toggle source
# File lib/dory/dnsmasq.rb, line 199 def self.up_systemd_services if self.systemd_services? puts "[DEBUG] Putting systemd services back up: #{self.systemd_services.join(', ')}" if Dory::Config.debug? if self.systemd_services.reverse.all? { |service| Dory::Systemd.set_systemd_service(service: service, up: true) } puts "#{self.systemd_services.join(', ')} were successfully restarted".green else puts "#{self.systemd_services.join(', ')} failed to restart".red end else puts "[DEBUG] Not putting systemd services back up cause array was empty " if Dory::Config.debug? end end