Closed khaije1 closed 5 years ago
testing on v0.18.0
I commented out the '_gateway_for_ip' function body:
# Poor man's gateway; strip the last octet and jam a 1 on there.
def _gateway_for_ip(ip)
# ip.split('.').tap(&:pop).push(1).join('.')
end
which helped expose a different error ... looks like the hostname -I
(note capital 'i') should use a lowercase 'i' in CentOS v5. I'll see if I can find locate where to make the change and submit a PR, but anyone should feel free to beat me to it ;-)
==> test-s1: Forwarding ports...
test-s1: -- 22 => 2222
==> test-s1: Setting hostname...
==> test-s1: Configuring network adapters within the VM...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
hostname -I
Stdout from the command:
Stderr from the command:
hostname: invalid option -- I
Usage: hostname [-v] {hostname|-F file} set hostname (from file)
domainname [-v] {nisdomain|-F file} set NIS domainname (from file)
hostname [-v] [-d|-f|-s|-a|-i|-y|-n] display formatted name
hostname [-v] display hostname
hostname -V|--version|-h|--help print info and exit
dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y
-s, --short short host name
-a, --alias alias names
-i, --ip-address addresses for the hostname
-f, --fqdn, --long long host name (FQDN)
-d, --domain DNS domain name
-y, --yp, --nis NIS/YP domainname
-F, --file read hostname or NIS domainname from given file
This command can read or set the hostname or the NIS domainname. You can
also read the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.
Unfortunately as you noticed hostname -I
is not working on all platforms.
We can have a CentOS-specific solution (hostname -i
). Also there's a discussion about having a custom binary to read the IP address here: https://github.com/phinze/landrush/issues/114
Cheers @njam !
I was able to get my case working w/ these changes.
diff --git a/lib/landrush/action/redirect_dns.rb b/lib/landrush/action/redirect_dns.rb
index c2c92e0..8e4eb45 100644
--- a/lib/landrush/action/redirect_dns.rb
+++ b/lib/landrush/action/redirect_dns.rb
@@ -34,6 +34,8 @@ module Landrush
# Poor man's gateway; strip the last octet and jam a 1 on there.
def _gateway_for_ip(ip)
+ ip = machine.guest.capability(:read_host_visible_ip_address) if ip.nil?
+ ip = ip.first if ip.instance_of?(Array)
ip.split('.').tap(&:pop).push(1).join('.')
end
end
diff --git a/lib/landrush/cap/linux/read_host_visible_ip_address.rb b/lib/landrush/cap/linux/read_host_visible_ip_address.rb
index e34e08f..01f3ba7 100644
--- a/lib/landrush/cap/linux/read_host_visible_ip_address.rb
+++ b/lib/landrush/cap/linux/read_host_visible_ip_address.rb
@@ -39,7 +39,7 @@ module Landrush
end
def self.command
- %Q(hostname -I)
+ %Q(hostname -I || /sbin/ip route | awk 'NR==/ src /&&/ dev /{print $NF}')
end
end
end
This made it possible for me to unblock myself but wasn't extensively considered. No doubt the solution in #114 is a better long term solution.
Please feel free to close this issue unless it looks suitable to merge, in which case I'd be happy to submit a PR.
On second thought, please hold off on closing this. While the OS is now booting, the landrush-specific functionality isn't working.
The iptables
redirect rule isn't being set, or at least is absent when I check for it after boot.
When the redirect rule is manually added, DNS works as expected and the problem resolves. Now to figure why it's not being setup ...
Here are the possible factors that I'm aware of to check while evaluating for a fix (not necessarily a list of established problems):
iptables
not in default system PATH iptables -C
, the "check" subcommand isn't implemented in CentOSv5Any pointers or anything else to look out for?
Hm the rule should be added in https://github.com/phinze/landrush/blob/master/lib/landrush/cap/linux/add_iptables_rule.rb - any errors should be printed.
Maybe you could remove that 2> /dev/null
and run vagrant up
again?
A workaround for this is to override hostname
on the guest system at the top of your provisioning script:
echo 'hostname () { if [ "$1" = "-I" ]; then /bin/hostname -i; else /bin/hostname "$@"; fi }' >> .bashrc
Then only run landrush if you're not provisioning:
if !ENV['SETUP']
config.landrush.enabled = true
end
On first run:
SETUP=1 vagrant up
vagrant reload
This snippet, (changed in ~/.vagrant.d/gems/gems/landrush-0.19.0/lib/landrush/cap/linux/read_host_visible_ip_address.rb
), has been helpful for me:
def self.command
%Q(hostname -I 2>/dev/null || /sbin/ip ad | sed -ne 's/^\\W*inet \\([0-9.]*\\).* global .*$/\\1/p' 2>/dev/null)
end
So if the issue is the use of hostname in _read_host_visible_ipaddress.rb, then this might be getting addressed by this pull request - #193. I'll give it a go.
So, after some quick testing it seems indeed that pull request #193 will take care of this.
So I tried the following. Two directories with the following Vagrantfile in each:
Vagrant.configure('2') do |config|
config.vm.box = 'centos/7'
config.vm.synced_folder '.', '/vagrant', disabled: true
config.landrush.enabled = true
config.landrush.tld = 'foo'
end
I can boot each VM and then ssh into it. On each VM I can ping the name of the other VM and I can see in the Landrush log files on my host that the DNS queries are directed to Landrush. Looking good so far.
If I add 'config.vm.hostname = 'my-host'
==> default: Setting hostname...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
# Update sysconfig
sed -i 's/\(HOSTNAME=\).*/\1my-host/' /etc/sysconfig/network
# Update DNS
sed -i 's/\(DHCP_HOSTNAME=\).*/\1"my-host"/' /etc/sysconfig/network-scripts/ifcfg-*
# Set the hostname - use hostnamectl if available
echo 'my-host' > /etc/hostname
if command -v hostnamectl; then
hostnamectl set-hostname 'my-host'
else
hostname 'my-host'
fi
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Prepend ourselves to /etc/hosts
grep -w 'my-host' /etc/hosts || {
sed -i'' '1i 127.0.0.1\tmy-host\tmy-host' /etc/hosts
}
# Restart network
service network restart
Stdout from the command:
/bin/hostnamectl
Restarting network (via systemctl): [FAILED]
Stderr from the command:
Job for network.service failed. See 'systemctl status network.service' and 'journalctl -xn' for details.
For:
Vagrant.configure('2') do |config|
config.vm.box = 'bento/centos-5.11'
config.vm.synced_folder '.', '/vagrant', disabled: true
config.landrush.enabled = true
end
I get:
==> default: Configuring and enabling network interfaces...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
# Down the interface before munging the config file. This might
# fail if the interface is not actually set up yet so ignore
# errors.
/sbin/ifdown '' || true
# Move new config into place
mv '/tmp/vagrant-network-entry--1466676309-0' '/etc/sysconfig/network-scripts/ifcfg-'
# Bring the interface up
ARPCHECK=no /sbin/ifup ''
Stdout from the command:
Stderr from the command:
usage: ifdown <device name>
Usage: ifup <device name>
So definitely more work needed, but it probably needs to be guest specific.
This issue cannot be reproduced anymore.
I get a similar error with any CentOSv5 VM I've tried. The below error happens while using this public CentOSv5 VM, but my packer-generated VM experiences the exact same problem.
With minor testing it appears that the 'ip' variable is being passed to '_gateway_for_ip(ip)' as a nil.