Check if interface exists before shutting it down

This commit is contained in:
sophia 2021-02-10 10:41:57 -06:00
parent 81b7d1524b
commit b24d4252c5
2 changed files with 3 additions and 3 deletions

View File

@ -45,7 +45,7 @@ module VagrantPlugins
# each specifically, we avoid reconfiguring eth0 (the NAT interface) so
# SSH never dies.
interfaces.each do |interface|
comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null")
comm.sudo("if [[ $(/sbin/ip a show eth#{interface} | grep UP) ]]; then /sbin/ifdown eth#{interface} 2> /dev/null; fi")
comm.sudo("/sbin/ip addr flush dev eth#{interface} 2> /dev/null")
end

View File

@ -23,9 +23,9 @@ describe 'VagrantPlugins::GuestAlpine::Cap::ConfigureNetworks' do
expect(communicator).to receive(:sudo).with("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre")
expect(communicator).to receive(:sudo).with("sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tail -n +2 > /tmp/vagrant-network-interfaces.post")
expect(communicator).to receive(:sudo).with('/sbin/ifdown eth0 2> /dev/null')
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth0/)
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth0 2> /dev/null')
expect(communicator).to receive(:sudo).with('/sbin/ifdown eth1 2> /dev/null')
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth1/)
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth1 2> /dev/null')
expect(communicator).to receive(:sudo).with('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces')
expect(communicator).to receive(:sudo).with('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post')