Skip to content
This repository has been archived by the owner on Jun 25, 2023. It is now read-only.

Commit

Permalink
Issue #274 Support cases where NetworkManager is running dnsmasq better
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontleon authored and hferentschik committed Sep 5, 2018
1 parent e10f684 commit ad0622e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/landrush/cap/host/linux/create_dnsmasq_config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative '../../../util/dnsmasq'

module Landrush
module Cap
module Linux
Expand All @@ -21,6 +23,7 @@ def info(msg)
end

def config_dir
@config_dir ||= Pathname('/etc/NetworkManager/dnsmasq.d') if Landrush::Util::Dnsmasq.nm_managed?
@config_dir ||= Pathname('/etc/dnsmasq.d')
end

Expand Down
10 changes: 8 additions & 2 deletions lib/landrush/cap/host/redhat/restart_dnsmasq.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative '../../../util/dnsmasq'

module Landrush
module Cap
module Redhat
Expand All @@ -13,8 +15,12 @@ class << self
def restart_dnsmasq(_env)
# TODO: At some stage we might want to make create_dnsmasq_config host specific and add the resolv.conf
# changes there which seems more natural
system(SED_COMMAND) unless system("cat /etc/resolv.conf | grep 'nameserver 127.0.0.1' > /dev/null 2>&1")
system('sudo systemctl restart dnsmasq')
if Landrush::Util::Dnsmasq.nm_managed?
system('sudo systemctl reload NetworkManager')
else
system(SED_COMMAND) unless system("cat /etc/resolv.conf | grep 'nameserver 127.0.0.1' > /dev/null 2>&1")
system('sudo systemctl restart dnsmasq')
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/landrush/util/dnsmasq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Landrush
module Util
class Dnsmasq
def self.nm_managed?
nm_config = Pathname('/etc/NetworkManager/NetworkManager.conf')
File.exist?(nm_config) && File.readlines(nm_config).grep(/^dns=dnsmasq$/).any?
end
end
end
end
42 changes: 42 additions & 0 deletions test/landrush/util/dnsmasq_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative '../../test_helper'

module Landrush
module Util
describe Dnsmasq do
MANAGED_DNSMASQ = '# Configuration file for NetworkManager.
[main]
dns=dnsmasq
[logging]
'.split('\n')

UNMANAGED_DNSMASQ = '# Configuration file for NetworkManager.
[main]
#dns=dnsmasq
[logging]
'.split('\n')

describe 'nm_managed?' do
it 'No NetworkManager config exists' do
File.expects(:exist?).returns(false)
Dnsmasq.nm_managed?.must_equal false
end

it 'NetworkManager manages dnsmasq' do
File.expects(:exist?).returns(true)
File.expects(:readlines).returns(MANAGED_DNSMASQ)

Dnsmasq.nm_managed?.must_equal true
end

it 'NetworkManager does not manage dnsmasq' do
File.expects(:exist?).returns(true)
File.expects(:readlines).returns(UNMANAGED_DNSMASQ)

Dnsmasq.nm_managed?.must_equal false
end
end
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'landrush/cap/host/windows/configure_visibility_on_host'
require 'landrush/cap/host/linux/configure_visibility_on_host'
require 'landrush/util/retry'
require 'landrush/util/dnsmasq'

require 'minitest/autorun'
require 'mocha/minitest'
Expand Down

0 comments on commit ad0622e

Please sign in to comment.