landrush 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +12 -178
- data/.travis.yml +6 -1
- data/CHANGELOG.md +18 -1
- data/CONTRIBUTING.adoc +112 -0
- data/Gemfile +6 -9
- data/Guardfile +10 -0
- data/README.adoc +100 -0
- data/Rakefile +14 -2
- data/appveyor.yml +20 -0
- data/doc/Development.adoc +112 -0
- data/doc/ProxyMobile.adoc +66 -0
- data/doc/Troubleshooting.adoc +42 -0
- data/doc/Usage.adoc +271 -0
- data/features/commands.feature +35 -0
- data/features/dns_resolution.feature +6 -5
- data/features/{landrush-ip.feature → landrush_ip.feature} +0 -0
- data/features/step_definitions/landrush_custom_steps.rb +48 -0
- data/features/support/env.rb +25 -1
- data/landrush.gemspec +3 -3
- data/lib/landrush/action/common.rb +3 -11
- data/lib/landrush/action/install_prerequisites.rb +2 -3
- data/lib/landrush/action/redirect_dns.rb +1 -1
- data/lib/landrush/action/setup.rb +25 -30
- data/lib/landrush/action/teardown.rb +8 -11
- data/lib/landrush/cap/guest/all/read_host_visible_ip_address.rb +28 -4
- data/lib/landrush/cap/guest/linux/add_iptables_rule.rb +1 -1
- data/lib/landrush/cap/guest/linux/redirect_dns.rb +2 -2
- data/lib/landrush/cap/guest/suse/add_iptables_rule.rb +20 -0
- data/lib/landrush/cap/guest/suse/install_iptables.rb +14 -0
- data/lib/landrush/cap/guest/suse/iptables_installed.rb +11 -0
- data/lib/landrush/cap/host/suse/dnsmasq_installed.rb +11 -0
- data/lib/landrush/cap/host/suse/install_dnsmasq.rb +14 -0
- data/lib/landrush/cap/host/suse/restart_dnsmasq.rb +21 -0
- data/lib/landrush/cap/host/windows/configure_visibility_on_host.rb +1 -1
- data/lib/landrush/command.rb +42 -17
- data/lib/landrush/config.rb +29 -14
- data/lib/landrush/plugin.rb +30 -0
- data/lib/landrush/server.rb +96 -138
- data/lib/landrush/start_server.rb +11 -0
- data/lib/landrush/store.rb +6 -2
- data/lib/landrush/util/path.rb +32 -0
- data/lib/landrush/util/process_helper.rb +46 -0
- data/lib/landrush/util/retry.rb +2 -2
- data/lib/landrush/version.rb +1 -1
- data/test/landrush/action/setup_test.rb +19 -25
- data/test/landrush/action/teardown_test.rb +18 -15
- data/test/landrush/cap/guest/all/read_host_visible_ip_address_test.rb +35 -1
- data/test/landrush/cap/guest/linux/configured_dns_servers_test.rb +8 -8
- data/test/landrush/cap/guest/linux/redirect_dns_test.rb +4 -4
- data/test/landrush/config_test.rb +23 -2
- data/test/landrush/dependent_vms_test.rb +5 -5
- data/test/landrush/issues/255.rb +115 -0
- data/test/landrush/server_test.rb +22 -4
- data/test/landrush/store_test.rb +28 -13
- data/test/support/test_server_daemon.rb +2 -4
- data/test/test_helper.rb +37 -14
- metadata +30 -15
- data/CONTRIBUTING.md +0 -103
- data/NOTES.md +0 -28
- data/README.md +0 -406
- data/doc/proxy-mobile/README.md +0 -50
- data/features/step_definitions/dns.rb +0 -19
- data/features/step_definitions/ip.rb +0 -13
@@ -0,0 +1,14 @@
|
|
1
|
+
module Landrush
|
2
|
+
module Cap
|
3
|
+
module Suse
|
4
|
+
module InstallDnsmasq
|
5
|
+
class << self
|
6
|
+
def install_dnsmasq(_env)
|
7
|
+
system('sudo zypper -q clean > /dev/null 2>&1')
|
8
|
+
system('sudo zypper -n -q install dnsmasq > /dev/null 2>&1')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Landrush
|
2
|
+
module Cap
|
3
|
+
module Suse
|
4
|
+
module RestartDnsmasq
|
5
|
+
class << self
|
6
|
+
SED_COMMAND = <<-EOF.gsub(/^ +/, '')
|
7
|
+
sudo sed -i.orig '1 i\
|
8
|
+
# Added by landrush, a vagrant plugin \\
|
9
|
+
nameserver 127.0.0.1 \\
|
10
|
+
' /etc/resolv.conf
|
11
|
+
EOF
|
12
|
+
|
13
|
+
def restart_dnsmasq(_env)
|
14
|
+
system(SED_COMMAND) unless system("cat /etc/resolv.conf | grep 'nameserver 127.0.0.1' > /dev/null 2>&1")
|
15
|
+
system('sudo systemctl restart dnsmasq')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -180,6 +180,6 @@ end
|
|
180
180
|
|
181
181
|
# Only run the following code when this file is the main file being run
|
182
182
|
# instead of having been required or loaded by another file
|
183
|
-
if __FILE__ == $
|
183
|
+
if __FILE__ == $PROGRAM_NAME
|
184
184
|
Landrush::Cap::Windows::ConfigureVisibilityOnHost.configure_visibility_on_host(nil, ARGV[0], ARGV[1])
|
185
185
|
end
|
data/lib/landrush/command.rb
CHANGED
@@ -3,33 +3,27 @@ module Landrush
|
|
3
3
|
DAEMON_COMMANDS = %w(start stop restart status).freeze
|
4
4
|
|
5
5
|
def self.synopsis
|
6
|
-
|
6
|
+
'manages DNS for both guest and host'
|
7
7
|
end
|
8
8
|
|
9
9
|
def execute
|
10
10
|
# Make sure we use the right data directory for Landrush
|
11
11
|
Server.working_dir = File.join(@env.data_dir, 'landrush')
|
12
|
+
Server.gems_dir = File.join(@env.gems_path, 'gems')
|
13
|
+
Server.ui = @env.ui
|
12
14
|
|
13
15
|
ARGV.shift # flush landrush from ARGV
|
14
16
|
command = ARGV.first || 'help'
|
15
17
|
if DAEMON_COMMANDS.include?(command)
|
16
18
|
Server.send(command)
|
17
19
|
elsif command == 'dependentvms' || command == 'vms'
|
18
|
-
|
19
|
-
@env.ui.info(DependentVMs.list.map { |dvm| " - #{dvm}" }.join("\n"))
|
20
|
-
else
|
21
|
-
@env.ui.info("No dependent VMs")
|
22
|
-
end
|
20
|
+
dependent_vms
|
23
21
|
elsif command == 'ls' || command == 'list'
|
24
|
-
|
25
|
-
printf "%-30s %s\n", key, value
|
26
|
-
end
|
22
|
+
store_ls
|
27
23
|
elsif command == 'set'
|
28
|
-
|
29
|
-
Landrush::Store.hosts.set(host, ip)
|
24
|
+
store_set
|
30
25
|
elsif command == 'del' || command == 'rm'
|
31
|
-
|
32
|
-
Landrush::Store.hosts.delete(key)
|
26
|
+
store_del
|
33
27
|
elsif command == 'help'
|
34
28
|
@env.ui.info(help)
|
35
29
|
else
|
@@ -40,7 +34,7 @@ module Landrush
|
|
40
34
|
end
|
41
35
|
|
42
36
|
def boom(msg)
|
43
|
-
raise Vagrant::Errors::CLIInvalidOptions, :
|
37
|
+
raise Vagrant::Errors::CLIInvalidOptions, help: usage(msg)
|
44
38
|
end
|
45
39
|
|
46
40
|
def usage(msg); <<-EOS.gsub(/^ /, '')
|
@@ -62,12 +56,43 @@ module Landrush
|
|
62
56
|
list vms currently dependent on the landrush server
|
63
57
|
set { <host> <ip> | <alias> <host> }
|
64
58
|
adds the given host-to-ip or alias-to-hostname mapping.
|
65
|
-
|
66
|
-
rm, del { <host> | <alias> }
|
67
|
-
delete the given hostname or alias from the server
|
59
|
+
Existing host ip addresses will be overwritten
|
60
|
+
rm, del { <host> | <alias> | --all }
|
61
|
+
delete the given hostname or alias from the server.
|
62
|
+
--all removes all entries
|
68
63
|
help
|
69
64
|
you're lookin at it!
|
70
65
|
EOS
|
71
66
|
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def dependent_vms
|
71
|
+
if DependentVMs.any?
|
72
|
+
@env.ui.info(DependentVMs.list.map { |dvm| " - #{dvm}" }.join("\n"))
|
73
|
+
else
|
74
|
+
@env.ui.info('No dependent VMs')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def store_del
|
79
|
+
key = ARGV[1]
|
80
|
+
if key == '--all'
|
81
|
+
Landrush::Store.hosts.clear!
|
82
|
+
else
|
83
|
+
Landrush::Store.hosts.delete(key)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def store_set
|
88
|
+
host, ip = ARGV[1, 2]
|
89
|
+
Landrush::Store.hosts.set(host, ip)
|
90
|
+
end
|
91
|
+
|
92
|
+
def store_ls
|
93
|
+
Landrush::Store.hosts.each do |key, value|
|
94
|
+
printf "%-30s %s\n", key, value
|
95
|
+
end
|
96
|
+
end
|
72
97
|
end
|
73
98
|
end
|
data/lib/landrush/config.rb
CHANGED
@@ -7,18 +7,23 @@ module Landrush
|
|
7
7
|
attr_accessor :host_ip_address
|
8
8
|
attr_accessor :guest_redirect_dns
|
9
9
|
attr_accessor :host_interface
|
10
|
+
attr_accessor :host_interface_class
|
10
11
|
attr_accessor :host_interface_excludes
|
11
12
|
attr_accessor :host_redirect_dns
|
12
13
|
|
14
|
+
INTERFACE_CLASSES = [:any, :ipv4, :ipv6].freeze
|
15
|
+
INTERFACE_CLASS_INVALID = "Invalid interface class, should be one of: #{INTERFACE_CLASSES.join(', ')}".freeze
|
16
|
+
|
13
17
|
DEFAULTS = {
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:
|
18
|
+
enabled: false,
|
19
|
+
tld: 'vagrant.test',
|
20
|
+
upstream_servers: [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]],
|
21
|
+
host_ip_address: nil,
|
22
|
+
guest_redirect_dns: true,
|
23
|
+
host_interface: nil,
|
24
|
+
host_interface_excludes: [/lo[0-9]*/, /docker[0-9]+/, /tun[0-9]+/],
|
25
|
+
host_interface_class: :ipv4,
|
26
|
+
host_redirect_dns: true
|
22
27
|
}.freeze
|
23
28
|
|
24
29
|
def initialize
|
@@ -30,6 +35,7 @@ module Landrush
|
|
30
35
|
@guest_redirect_dns = UNSET_VALUE
|
31
36
|
@host_interface = UNSET_VALUE
|
32
37
|
@host_interface_excludes = UNSET_VALUE
|
38
|
+
@host_interface_class = UNSET_VALUE
|
33
39
|
@host_redirect_dns = UNSET_VALUE
|
34
40
|
end
|
35
41
|
|
@@ -53,14 +59,12 @@ module Landrush
|
|
53
59
|
@host_redirect_dns
|
54
60
|
end
|
55
61
|
|
56
|
-
def host(hostname, ip_address=nil)
|
62
|
+
def host(hostname, ip_address = nil)
|
57
63
|
@hosts[hostname] = ip_address
|
58
64
|
end
|
59
65
|
|
60
|
-
def upstream(ip, port=53, protocol=nil)
|
61
|
-
if @upstream_servers == UNSET_VALUE
|
62
|
-
@upstream_servers = []
|
63
|
-
end
|
66
|
+
def upstream(ip, port = 53, protocol = nil)
|
67
|
+
@upstream_servers = [] if @upstream_servers == UNSET_VALUE
|
64
68
|
|
65
69
|
if !protocol
|
66
70
|
@upstream_servers.push [:udp, ip, port]
|
@@ -84,9 +88,20 @@ module Landrush
|
|
84
88
|
end
|
85
89
|
end
|
86
90
|
|
87
|
-
def validate(
|
91
|
+
def validate(_machine)
|
88
92
|
errors = _detected_errors
|
93
|
+
errors.concat validate_host_interface_class
|
94
|
+
|
89
95
|
{ 'landrush' => errors }
|
90
96
|
end
|
97
|
+
|
98
|
+
def validate_host_interface_class
|
99
|
+
@host_interface_class = @host_interface_class.intern if @host_interface_class.is_a? String
|
100
|
+
|
101
|
+
return [] if INTERFACE_CLASSES.include? @host_interface_class
|
102
|
+
|
103
|
+
# TODO: Should really be using I18n; makes testing a lot cleaner too.
|
104
|
+
[INTERFACE_CLASS_INVALID, fields: 'host_interface_class']
|
105
|
+
end
|
91
106
|
end
|
92
107
|
end
|
data/lib/landrush/plugin.rb
CHANGED
@@ -87,6 +87,21 @@ module Landrush
|
|
87
87
|
Cap::Redhat::InstallIptables
|
88
88
|
end
|
89
89
|
|
90
|
+
guest_capability('suse', 'add_iptables_rule') do
|
91
|
+
require_relative 'cap/guest/suse/add_iptables_rule'
|
92
|
+
Cap::Suse::AddIptablesRule
|
93
|
+
end
|
94
|
+
|
95
|
+
guest_capability('suse', 'iptables_installed') do
|
96
|
+
require_relative 'cap/guest/suse/iptables_installed'
|
97
|
+
Cap::Suse::IptablesInstalled
|
98
|
+
end
|
99
|
+
|
100
|
+
guest_capability('suse', 'install_iptables') do
|
101
|
+
require_relative 'cap/guest/suse/install_iptables'
|
102
|
+
Cap::Suse::InstallIptables
|
103
|
+
end
|
104
|
+
|
90
105
|
guest_capability('linux', 'configured_dns_servers') do
|
91
106
|
require_relative 'cap/guest/linux/configured_dns_servers'
|
92
107
|
Cap::Linux::ConfiguredDnsServers
|
@@ -166,5 +181,20 @@ module Landrush
|
|
166
181
|
require_relative 'cap/host/redhat/restart_dnsmasq'
|
167
182
|
Cap::Redhat::RestartDnsmasq
|
168
183
|
end
|
184
|
+
|
185
|
+
host_capability('suse', 'dnsmasq_installed') do
|
186
|
+
require_relative 'cap/host/suse/dnsmasq_installed'
|
187
|
+
Cap::Suse::DnsmasqInstalled
|
188
|
+
end
|
189
|
+
|
190
|
+
host_capability('suse', 'install_dnsmasq') do
|
191
|
+
require_relative 'cap/host/suse/install_dnsmasq'
|
192
|
+
Cap::Suse::InstallDnsmasq
|
193
|
+
end
|
194
|
+
|
195
|
+
host_capability('suse', 'restart_dnsmasq') do
|
196
|
+
require_relative 'cap/host/suse/restart_dnsmasq'
|
197
|
+
Cap::Suse::RestartDnsmasq
|
198
|
+
end
|
169
199
|
end
|
170
200
|
end
|
data/lib/landrush/server.rb
CHANGED
@@ -1,29 +1,56 @@
|
|
1
1
|
require 'rubydns'
|
2
2
|
require 'ipaddr'
|
3
|
-
require '
|
4
|
-
|
3
|
+
require 'win32/process' unless (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil? # only require on Windows
|
5
4
|
require_relative 'store'
|
5
|
+
require_relative 'util/path'
|
6
|
+
require_relative 'util/process_helper'
|
6
7
|
|
7
8
|
module Landrush
|
8
9
|
class Server
|
10
|
+
extend Landrush::Util::ProcessHelper
|
11
|
+
|
9
12
|
Name = Resolv::DNS::Name
|
10
13
|
IN = Resolv::DNS::Resource::IN
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
class << self
|
16
|
+
attr_reader :gems_dir
|
17
|
+
|
18
|
+
def gems_dir=(gems_dir)
|
19
|
+
@gems_dir = Pathname(gems_dir)
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :ui
|
23
|
+
attr_writer :ui
|
24
|
+
|
25
|
+
def working_dir
|
26
|
+
# TODO, https://github.com/vagrant-landrush/landrush/issues/178
|
27
|
+
# Due to the fact that the whole server is just a bunch of static methods,
|
28
|
+
# there is no initalize method to ensure that the working directory is
|
29
|
+
# set prior to making calls to this method. Things work, since at the appropriate
|
30
|
+
# Vagrant plugin integration points (e.g. setup.rb) we set the working dir based
|
31
|
+
# on the enviroment passed to us.
|
32
|
+
if @working_dir.nil?
|
33
|
+
raise 'The Server\s working directory needs to be explicitly set prior to calling this method'
|
34
|
+
end
|
35
|
+
@working_dir
|
21
36
|
end
|
22
|
-
@working_dir
|
23
|
-
end
|
24
37
|
|
25
|
-
|
26
|
-
|
38
|
+
def working_dir=(working_dir)
|
39
|
+
@working_dir = Pathname(working_dir).tap(&:mkpath)
|
40
|
+
end
|
41
|
+
|
42
|
+
def port
|
43
|
+
@port unless @port.nil?
|
44
|
+
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil?
|
45
|
+
# Default Landrush port for non Windows OS
|
46
|
+
100_53
|
47
|
+
else
|
48
|
+
# On Windows we need to use the default DNS port, since there seems to be no way to configure it otherwise
|
49
|
+
53
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
attr_writer :port
|
27
54
|
end
|
28
55
|
|
29
56
|
def self.log_directory
|
@@ -34,22 +61,9 @@ module Landrush
|
|
34
61
|
File.join(log_directory, 'landrush.log')
|
35
62
|
end
|
36
63
|
|
37
|
-
def self.port
|
38
|
-
if Vagrant::Util::Platform.windows?
|
39
|
-
# On Windows we need to use the default DNS port, since there seems to be no way to configure it otherwise
|
40
|
-
@port ||= 53
|
41
|
-
else
|
42
|
-
@port ||= 100_53
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.port=(port)
|
47
|
-
@port = port
|
48
|
-
end
|
49
|
-
|
50
64
|
def self.upstream_servers
|
51
65
|
# Doing collect to cast protocol to symbol because JSON store doesn't know about symbols
|
52
|
-
@upstream_servers ||= Store.config.get('upstream').collect {|i| [i[0].to_sym, i[1], i[2]]}
|
66
|
+
@upstream_servers ||= Store.config.get('upstream').collect { |i| [i[0].to_sym, i[1], i[2]] }
|
53
67
|
end
|
54
68
|
|
55
69
|
def self.interfaces
|
@@ -65,7 +79,14 @@ module Landrush
|
|
65
79
|
|
66
80
|
# Used to start the Landrush DNS server as a child process using ChildProcess gem
|
67
81
|
def self.start
|
68
|
-
|
82
|
+
# On a machine with just Vagrant installed there might be no other Ruby except the
|
83
|
+
# one bundled with Vagrant. Let's make sure the embedded bin directory containing
|
84
|
+
# the Ruby executable is added to the PATH.
|
85
|
+
Landrush::Util::Path.ensure_ruby_on_path
|
86
|
+
|
87
|
+
ruby_bin = Landrush::Util::Path.embedded_vagrant_ruby.nil? ? 'ruby' : Landrush::Util::Path.embedded_vagrant_ruby
|
88
|
+
start_server_script = Pathname(__dir__).join('start_server.rb').to_s
|
89
|
+
@ui.detail("[landrush] '#{ruby_bin} #{start_server_script} #{port} #{working_dir} #{gems_dir}'") unless @ui.nil?
|
69
90
|
if Vagrant::Util::Platform.windows?
|
70
91
|
# Need to handle Windows differently. Kernel.spawn fails to work, if
|
71
92
|
# the shell creating the process is closed.
|
@@ -79,31 +100,33 @@ module Landrush
|
|
79
100
|
# inherited by default, but if any filehandle is passed to
|
80
101
|
# a spawned process then all files that are
|
81
102
|
# set as inheritable will get inherited. In another project this
|
82
|
-
# created a problem (see:
|
83
|
-
# https://github.com/dustymabe/vagrant-sshfs/issues/41).
|
103
|
+
# created a problem (see: https://github.com/dustymabe/vagrant-sshfs/issues/41).
|
84
104
|
#
|
85
105
|
# Today we don't pass any filehandles, so it isn't a problem.
|
86
106
|
# Future self, make sure this doesn't become a problem.
|
87
|
-
|
88
|
-
|
89
|
-
:
|
90
|
-
:
|
91
|
-
:
|
92
|
-
:cwd => working_dir.to_path)
|
107
|
+
info = Process.create(command_line: "#{ruby_bin} #{start_server_script} #{port} #{working_dir} #{gems_dir}",
|
108
|
+
creation_flags: Process::DETACHED_PROCESS,
|
109
|
+
process_inherit: false,
|
110
|
+
thread_inherit: true,
|
111
|
+
cwd: working_dir.to_path)
|
93
112
|
pid = info.process_id
|
94
113
|
else
|
95
114
|
# Fix https://github.com/vagrant-landrush/landrush/issues/249)
|
96
115
|
# by turning of filehandle inheritance with :close_others => true
|
97
116
|
# and by explicitly closing STDIN, STDOUT, and STDERR
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
:
|
102
|
-
:
|
117
|
+
pid = spawn(ruby_bin, start_server_script, port.to_s, working_dir.to_s, gems_dir.to_s,
|
118
|
+
in: :close,
|
119
|
+
out: :close,
|
120
|
+
err: :close,
|
121
|
+
close_others: true,
|
122
|
+
chdir: working_dir.to_path,
|
123
|
+
pgroup: true)
|
103
124
|
Process.detach pid
|
104
125
|
end
|
105
126
|
|
106
|
-
write_pid(pid)
|
127
|
+
write_pid(pid, pid_file)
|
128
|
+
# As of Vagrant 1.8.6 this additonal sleep is needed, otherwise the child process dies!?
|
129
|
+
sleep 1
|
107
130
|
end
|
108
131
|
|
109
132
|
def self.stop
|
@@ -115,7 +138,7 @@ module Landrush
|
|
115
138
|
return
|
116
139
|
end
|
117
140
|
|
118
|
-
pid = read_pid
|
141
|
+
pid = read_pid(pid_file)
|
119
142
|
|
120
143
|
# Check if the daemon is already stopped...
|
121
144
|
unless running?
|
@@ -132,7 +155,7 @@ module Landrush
|
|
132
155
|
end
|
133
156
|
|
134
157
|
# Otherwise the daemon has been stopped.
|
135
|
-
delete_pid_file
|
158
|
+
delete_pid_file(pid_file)
|
136
159
|
end
|
137
160
|
|
138
161
|
def self.restart
|
@@ -141,11 +164,13 @@ module Landrush
|
|
141
164
|
end
|
142
165
|
|
143
166
|
def self.pid
|
144
|
-
IO.read(pid_file).to_i
|
167
|
+
IO.read(pid_file).to_i
|
168
|
+
rescue
|
169
|
+
nil
|
145
170
|
end
|
146
171
|
|
147
172
|
def self.running?
|
148
|
-
pid = read_pid
|
173
|
+
pid = read_pid(pid_file)
|
149
174
|
return false if pid.nil?
|
150
175
|
if Vagrant::Util::Platform.windows?
|
151
176
|
begin
|
@@ -155,20 +180,24 @@ module Landrush
|
|
155
180
|
raise e unless e.class.name.start_with?('Errno::ENXIO')
|
156
181
|
end
|
157
182
|
else
|
158
|
-
|
183
|
+
begin
|
184
|
+
!!Process.kill(0, pid)
|
185
|
+
rescue
|
186
|
+
false
|
187
|
+
end
|
159
188
|
end
|
160
189
|
end
|
161
190
|
|
162
191
|
def self.status
|
163
|
-
case process_status
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
192
|
+
case process_status(pid_file)
|
193
|
+
when :running
|
194
|
+
puts "Daemon status: running pid=#{read_pid(pid_file)}"
|
195
|
+
when :stopped
|
196
|
+
puts 'Daemon status: stopped'
|
197
|
+
else
|
198
|
+
puts 'Daemon status: unknown'
|
199
|
+
puts "#{pid_file} exists, but process is not running"
|
200
|
+
puts "Check log file: #{log_file_path}"
|
172
201
|
end
|
173
202
|
end
|
174
203
|
|
@@ -184,7 +213,7 @@ module Landrush
|
|
184
213
|
@logger.level = Logger::INFO
|
185
214
|
|
186
215
|
# Start the DNS server
|
187
|
-
run_dns_server(:
|
216
|
+
run_dns_server(listen: interfaces, logger: @logger) do
|
188
217
|
match(/.*/, IN::A) do |transaction|
|
189
218
|
host = Store.hosts.find(transaction.name)
|
190
219
|
if host
|
@@ -215,7 +244,7 @@ module Landrush
|
|
215
244
|
server = RubyDNS::RuleBasedServer.new(options, &block)
|
216
245
|
|
217
246
|
EventMachine.run do
|
218
|
-
trap(
|
247
|
+
trap('INT') do
|
219
248
|
EventMachine.stop
|
220
249
|
end
|
221
250
|
|
@@ -227,94 +256,23 @@ module Landrush
|
|
227
256
|
|
228
257
|
def self.check_a_record(host, transaction)
|
229
258
|
value = Store.hosts.get(host)
|
230
|
-
if value.nil?
|
231
|
-
return
|
232
|
-
end
|
259
|
+
return if value.nil?
|
233
260
|
|
234
|
-
if
|
261
|
+
if begin
|
262
|
+
IPAddr.new(value)
|
263
|
+
rescue
|
264
|
+
nil
|
265
|
+
end
|
235
266
|
name = transaction.name =~ /#{host}/ ? transaction.name : host
|
236
|
-
transaction.respond!(value, :
|
267
|
+
transaction.respond!(value, ttl: 0, name: name)
|
237
268
|
else
|
238
269
|
transaction.respond!(Name.create(value), resource_class: IN::CNAME, ttl: 0)
|
239
270
|
check_a_record(value, transaction)
|
240
271
|
end
|
241
272
|
end
|
242
273
|
|
243
|
-
# private methods
|
244
|
-
def self.write_pid(pid)
|
245
|
-
ensure_path_exits(pid_file)
|
246
|
-
File.open(pid_file, 'w') {|f| f << pid.to_s}
|
247
|
-
end
|
248
|
-
|
249
|
-
def self.read_pid
|
250
|
-
IO.read(pid_file).to_i rescue nil
|
251
|
-
end
|
252
|
-
|
253
|
-
def self.delete_pid_file
|
254
|
-
if File.exist? pid_file
|
255
|
-
FileUtils.rm(pid_file)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
274
|
def self.pid_file
|
260
275
|
File.join(working_dir, 'run', 'landrush.pid')
|
261
276
|
end
|
262
|
-
|
263
|
-
def self.process_status
|
264
|
-
if File.exist? pid_file
|
265
|
-
return running? ? :running : :unknown
|
266
|
-
else
|
267
|
-
return :stopped
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def self.ensure_path_exits(file_name)
|
272
|
-
dirname = File.dirname(file_name)
|
273
|
-
unless File.directory?(dirname)
|
274
|
-
FileUtils.mkdir_p(dirname)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
def self.terminate_process(pid)
|
279
|
-
# Kill/Term loop - if the daemon didn't die easily, shoot
|
280
|
-
# it a few more times.
|
281
|
-
attempts = 5
|
282
|
-
while running? && attempts > 0
|
283
|
-
sig = (attempts >= 2) ? 'KILL' : 'TERM'
|
284
|
-
|
285
|
-
puts "Sending #{sig} to process #{pid}..."
|
286
|
-
Process.kill(sig, pid)
|
287
|
-
|
288
|
-
attempts -= 1
|
289
|
-
sleep 1
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
# On a machine with just Vagrant installed there might be no other Ruby except the
|
294
|
-
# one bundled with Vagrant. Let's make sure the embedded bin directory containing
|
295
|
-
# the Ruby executable is added to the PATH.
|
296
|
-
def self.ensure_ruby_on_path
|
297
|
-
vagrant_binary = Vagrant::Util::Which.which('vagrant')
|
298
|
-
vagrant_binary = File.realpath(vagrant_binary) if File.symlink?(vagrant_binary)
|
299
|
-
# in a Vagrant installation the Ruby executable is in ../embedded/bin relative to the vagrant executable
|
300
|
-
# we don't use File.join here, since even on Cygwin we want a Windows path - see https://github.com/vagrant-landrush/landrush/issues/237
|
301
|
-
if Vagrant::Util::Platform.windows?
|
302
|
-
separator = '\\'
|
303
|
-
else
|
304
|
-
separator = '/'
|
305
|
-
end
|
306
|
-
embedded_bin_dir = File.dirname(File.dirname(vagrant_binary)) + separator + 'embedded' + separator + 'bin'
|
307
|
-
ENV['PATH'] = embedded_bin_dir + File::PATH_SEPARATOR + ENV['PATH'] if File.exist?(embedded_bin_dir)
|
308
|
-
end
|
309
|
-
|
310
|
-
private_class_method :write_pid, :read_pid, :delete_pid_file, :pid_file, :process_status, :ensure_path_exits,
|
311
|
-
:terminate_process, :ensure_ruby_on_path
|
312
277
|
end
|
313
278
|
end
|
314
|
-
|
315
|
-
# Only run the following code when this file is the main file being run
|
316
|
-
# instead of having been required or loaded by another file
|
317
|
-
if __FILE__ == $0
|
318
|
-
# TODO, Add some argument checks
|
319
|
-
Landrush::Server.run(ARGV[0], ARGV[1])
|
320
|
-
end
|