Merge pull request #7921 from chrisroberts/fix/key-fixups

Remove `set -e` usage for better shell compatibility
This commit is contained in:
Chris Roberts 2016-10-25 14:43:04 -07:00 committed by GitHub
commit be3fa85853
18 changed files with 80 additions and 100 deletions

View File

@ -8,18 +8,16 @@ module VagrantPlugins
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0] basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, "") comm.sudo <<-EOH.gsub(/^ {14}/, "")
set -e # Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Set hostname # Set hostname
hostnamectl set-hostname '#{basename}' hostnamectl set-hostname '#{basename}'
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Prepend ourselves to /etc/hosts # Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || { test $? -eq 0 && (grep -w '#{name}' /etc/hosts || {
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
} })
EOH EOH
end end
end end

View File

@ -12,10 +12,9 @@ module VagrantPlugins
def self.configure_networks(machine, networks) def self.configure_networks(machine, networks)
comm = machine.communicate comm = machine.communicate
commands = []
commands = ["set -e"]
interfaces = machine.guest.capability(:network_interfaces) interfaces = machine.guest.capability(:network_interfaces)
networks.each.with_index do |network, i| networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]] network[:device] = interfaces[network[:interface]]
@ -42,15 +41,15 @@ module VagrantPlugins
commands << <<-EOH.gsub(/^ {14}/, '') commands << <<-EOH.gsub(/^ {14}/, '')
# Configure #{network[:device]} # Configure #{network[:device]}
mv '#{remote_path}' '/etc/netctl/#{network[:device]}' mv '#{remote_path}' '/etc/netctl/#{network[:device]}' &&
ip link set '#{network[:device]}' down ip link set '#{network[:device]}' down &&
netctl restart '#{network[:device]}' netctl restart '#{network[:device]}' &&
netctl enable '#{network[:device]}' netctl enable '#{network[:device]}'
EOH EOH
end end
# Run all the network modification commands in one communicator call. # Run all the network modification commands in one communicator call.
comm.sudo(commands.join("\n")) comm.sudo(commands.join(" && \n"))
end end
end end
end end

View File

@ -15,8 +15,7 @@ module VagrantPlugins
# https://bbs.archlinux.org/viewtopic.php?id=193410 # https://bbs.archlinux.org/viewtopic.php?id=193410
# #
comm.sudo <<-EOH.gsub(/^ {12}/, "") comm.sudo <<-EOH.gsub(/^ {12}/, "")
set -e systemctl enable rpcbind &&
systemctl enable rpcbind
systemctl start rpcbind systemctl start rpcbind
EOH EOH
end end
@ -24,8 +23,7 @@ module VagrantPlugins
def self.nfs_client_install(machine) def self.nfs_client_install(machine)
comm = machine.communicate comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {12}/, "") comm.sudo <<-EOH.gsub(/^ {12}/, "")
set -e pacman --noconfirm -Syy &&
pacman --noconfirm -Syy
pacman --noconfirm -S nfs-utils ntp pacman --noconfirm -S nfs-utils ntp
EOH EOH
end end

View File

@ -5,9 +5,9 @@ module VagrantPlugins
def self.rsync_install(machine) def self.rsync_install(machine)
comm = machine.communicate comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {12}/, '') comm.sudo <<-EOH.gsub(/^ {12}/, '')
set -e
pacman -Sy --noconfirm pacman -Sy --noconfirm
pacman -S --noconfirm rsync pacman -S --noconfirm rsync
exit $?
EOH EOH
end end
end end

View File

@ -8,18 +8,16 @@ module VagrantPlugins
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0] basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, "") comm.sudo <<-EOH.gsub(/^ {14}/, "")
set -e # Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Set hostname # Set hostname
hostnamectl set-hostname '#{basename}' hostnamectl set-hostname '#{basename}'
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Prepend ourselves to /etc/hosts # Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || { test $? -eq 0 && (grep -w '#{name}' /etc/hosts || {
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
} })
EOH EOH
end end
end end

View File

@ -11,10 +11,8 @@ module VagrantPlugins
def self.mount_nfs_folder(machine, ip, folders) def self.mount_nfs_folder(machine, ip, folders)
comm = machine.communicate comm = machine.communicate
folders.each do |name, opts|
# Mount each folder separately so we can retry. # Mount each folder separately so we can retry.
commands = ["set -e"] folders.each do |name, opts|
# Shellescape the paths in case they do not have special characters. # Shellescape the paths in case they do not have special characters.
guest_path = Shellwords.escape(opts[:guestpath]) guest_path = Shellwords.escape(opts[:guestpath])
host_path = Shellwords.escape(opts[:hostpath]) host_path = Shellwords.escape(opts[:hostpath])
@ -29,14 +27,14 @@ module VagrantPlugins
mount_opts = mount_opts.join(",") mount_opts = mount_opts.join(",")
# Make the directory on the guest. # Make the directory on the guest.
commands << "mkdir -p #{guest_path}" machine.communicate.sudo("mkdir -p #{guest_path}")
# Perform the mount operation. # Perform the mount operation.
commands << "/sbin/mount -t nfs -o '#{mount_opts}' #{ip}:#{host_path} #{guest_path}" command = "/sbin/mount -t nfs -o '#{mount_opts}' #{ip}:#{host_path} #{guest_path}"
# Run the command, raising a specific error. # Run the command, raising a specific error.
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do
machine.communicate.sudo(commands.join("\n"), machine.communicate.sudo(command,
error_class: Vagrant::Errors::NFSMountFailed, error_class: Vagrant::Errors::NFSMountFailed,
shell: "sh", shell: "sh",
) )

View File

@ -22,14 +22,13 @@ module VagrantPlugins
# Use execute (not sudo) because we want to execute this as the SSH # Use execute (not sudo) because we want to execute this as the SSH
# user (which is "vagrant" by default). # user (which is "vagrant" by default).
comm.execute <<-EOH.gsub(/^ {12}/, "") comm.execute <<-EOH.gsub(/^ {12}/, "")
set -e
mkdir -p ~/.ssh mkdir -p ~/.ssh
chmod 0700 ~/.ssh chmod 0700 ~/.ssh &&
cat '#{remote_path}' >> ~/.ssh/authorized_keys cat '#{remote_path}' >> ~/.ssh/authorized_keys &&
chmod 0600 ~/.ssh/authorized_keys chmod 0600 ~/.ssh/authorized_keys
result=$?
rm -f '#{remote_path}' rm -f '#{remote_path}'
exit $result
EOH EOH
end end
@ -49,15 +48,16 @@ module VagrantPlugins
# Use execute (not sudo) because we want to execute this as the SSH # Use execute (not sudo) because we want to execute this as the SSH
# user (which is "vagrant" by default). # user (which is "vagrant" by default).
comm.execute <<-EOH.sub(/^ {12}/, "") comm.execute <<-EOH.sub(/^ {12}/, "")
set -e result=0
if test -f ~/.ssh/authorized_keys; then if test -f ~/.ssh/authorized_keys; then
grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp &&
mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys &&
chmod 0600 ~/.ssh/authorized_keys chmod 0600 ~/.ssh/authorized_keys
result=$?
fi fi
rm -f '#{remote_path}' rm -f '#{remote_path}'
exit $result
EOH EOH
end end
end end

View File

@ -8,16 +8,17 @@ module VagrantPlugins
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0] basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
set -e
# Set hostname
scutil --set ComputerName '#{name}'
scutil --set HostName '#{name}'
# LocalHostName should not contain dots - it is used by Bonjour and # LocalHostName should not contain dots - it is used by Bonjour and
# visible through file sharing services. # visible through file sharing services.
comm.sudo <<-EOH.gsub(/^ */, '')
# Set hostname
scutil --set ComputerName '#{name}' &&
scutil --set HostName '#{name}' &&
scutil --set LocalHostName '#{basename}' scutil --set LocalHostName '#{basename}'
result=$?
if [ $result -ne 0 ]; then
exit $result
fi
hostname '#{name}' hostname '#{name}'
@ -27,7 +28,7 @@ module VagrantPlugins
# Prepend ourselves to /etc/hosts - sed on bsd is sad # Prepend ourselves to /etc/hosts - sed on bsd is sad
grep -w '#{name}' /etc/hosts || { grep -w '#{name}' /etc/hosts || {
echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts &&
mv /tmp/tmp-hosts /etc/hosts mv /tmp/tmp-hosts /etc/hosts
} }
EOH EOH

View File

@ -8,9 +8,6 @@ module VagrantPlugins
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0] basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '') comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Ensure exit on command error
set -e
# Set the hostname # Set the hostname
echo '#{basename}' > /etc/hostname echo '#{basename}' > /etc/hostname
hostname -F /etc/hostname hostname -F /etc/hostname

View File

@ -11,7 +11,7 @@ module VagrantPlugins
def self.configure_networks(machine, networks) def self.configure_networks(machine, networks)
comm = machine.communicate comm = machine.communicate
commands = ["set -e"] commands = []
entries = [] entries = []
interfaces = machine.guest.capability(:network_interfaces) interfaces = machine.guest.capability(:network_interfaces)

View File

@ -5,9 +5,9 @@ module VagrantPlugins
def self.nfs_client_install(machine) def self.nfs_client_install(machine)
comm = machine.communicate comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {12}/, '') comm.sudo <<-EOH.gsub(/^ {12}/, '')
set -e
apt-get -yqq update apt-get -yqq update
apt-get -yqq install nfs-common portmap apt-get -yqq install nfs-common portmap
exit $?
EOH EOH
end end
end end

View File

@ -5,7 +5,6 @@ module VagrantPlugins
def self.rsync_install(machine) def self.rsync_install(machine)
comm = machine.communicate comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {14}/, '') comm.sudo <<-EOH.gsub(/^ {14}/, '')
set -e
apt-get -yqq update apt-get -yqq update
apt-get -yqq install rsync apt-get -yqq install rsync
EOH EOH

View File

@ -8,8 +8,6 @@ module VagrantPlugins
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0] basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, "") comm.sudo <<-EOH.gsub(/^ {14}/, "")
set -e
# Set the hostname # Set the hostname
hostname '#{basename}' hostname '#{basename}'
echo "hostname=#{basename}" > /etc/conf.d/hostname echo "hostname=#{basename}" > /etc/conf.d/hostname
@ -20,7 +18,7 @@ module VagrantPlugins
# Prepend ourselves to /etc/hosts # Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || { grep -w '#{name}' /etc/hosts || {
echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts &&
mv /tmp/tmp-hosts /etc/hosts mv /tmp/tmp-hosts /etc/hosts
} }
EOH EOH

View File

@ -13,10 +13,8 @@ module VagrantPlugins
def self.mount_nfs_folder(machine, ip, folders) def self.mount_nfs_folder(machine, ip, folders)
comm = machine.communicate comm = machine.communicate
folders.each do |name, opts|
# Mount each folder separately so we can retry. # Mount each folder separately so we can retry.
commands = ["set -e"] folders.each do |name, opts|
# Shellescape the paths in case they do not have special characters. # Shellescape the paths in case they do not have special characters.
guest_path = Shellwords.escape(opts[:guestpath]) guest_path = Shellwords.escape(opts[:guestpath])
host_path = Shellwords.escape(opts[:hostpath]) host_path = Shellwords.escape(opts[:hostpath])
@ -30,22 +28,24 @@ module VagrantPlugins
end end
mount_opts = mount_opts.join(",") mount_opts = mount_opts.join(",")
# Make the directory on the guest. machine.communicate.sudo("mkdir -p #{guest_path}")
commands << "mkdir -p #{guest_path}"
# Perform the mount operation. # Perform the mount operation and emit mount event if applicable
commands << "mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}" command = <<-EOH.gsub(/^ */, '')
mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}
# Emit a mount event result=$?
commands << <<-EOH.gsub(/^ {14}/, '') if test $result -eq 0; then
if test -x /sbin/initctl && command -v /sbin/init && /sbin/init --version | grep upstart; then if test -x /sbin/initctl && command -v /sbin/init && /sbin/init --version | grep upstart; then
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path} /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
fi fi
else
exit $result
fi
EOH EOH
# Run the command, raising a specific error. # Run the command, raising a specific error.
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do
machine.communicate.sudo(commands.join("\n"), machine.communicate.sudo(command,
error_class: Vagrant::Errors::NFSMountFailed, error_class: Vagrant::Errors::NFSMountFailed,
) )
end end

View File

@ -21,15 +21,13 @@ module VagrantPlugins
# Use execute (not sudo) because we want to execute this as the SSH # Use execute (not sudo) because we want to execute this as the SSH
# user (which is "vagrant" by default). # user (which is "vagrant" by default).
comm.execute <<-EOH.gsub(/^ {12}/, "") comm.execute <<-EOH.gsub(/^ */, "")
set -e
mkdir -p ~/.ssh mkdir -p ~/.ssh
chmod 0700 ~/.ssh chmod 0700 ~/.ssh
cat '#{remote_path}' >> ~/.ssh/authorized_keys cat '#{remote_path}' >> ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys result=$?
rm -f '#{remote_path}' rm -f '#{remote_path}'
exit $result
EOH EOH
end end
@ -48,16 +46,14 @@ module VagrantPlugins
# Use execute (not sudo) because we want to execute this as the SSH # Use execute (not sudo) because we want to execute this as the SSH
# user (which is "vagrant" by default). # user (which is "vagrant" by default).
comm.execute <<-EOH.sub(/^ {12}/, "") comm.execute <<-EOH.sub(/^ */, "")
set -e
if test -f ~/.ssh/authorized_keys; then if test -f ~/.ssh/authorized_keys; then
grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp
mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys result=$?
fi fi
rm -f '#{remote_path}' rm -f '#{remote_path}'
exit $result
EOH EOH
end end
end end

View File

@ -5,9 +5,8 @@ module VagrantPlugins
def self.change_host_name(machine, name) def self.change_host_name(machine, name)
if !machine.communicate.test("hostname -s | grep '^#{name}$'") if !machine.communicate.test("hostname -s | grep '^#{name}$'")
machine.communicate.sudo(<<CMDS, {shell: "sh"}) machine.communicate.sudo(<<CMDS, {shell: "sh"})
set -e sed -e 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf > /tmp/rc.conf.vagrant_changehostname_#{name} &&
sed -e 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf > /tmp/rc.conf.vagrant_changehostname_#{name} mv /tmp/rc.conf.vagrant_changehostname_#{name} /etc/rc.conf &&
mv /tmp/rc.conf.vagrant_changehostname_#{name} /etc/rc.conf
hostname #{name} hostname #{name}
CMDS CMDS
end end

View File

@ -31,10 +31,9 @@ describe "VagrantPlugins::GuestBSD::Cap::NFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/set -e/)
expect(comm.received_commands[0]).to match(/mkdir -p \/guest/) expect(comm.received_commands[0]).to match(/mkdir -p \/guest/)
expect(comm.received_commands[0]).to match(/mount -t nfs/) expect(comm.received_commands[1]).to match(/mount -t nfs/)
expect(comm.received_commands[0]).to match(/1.2.3.4:\/host \/guest/) expect(comm.received_commands[1]).to match(/1.2.3.4:\/host \/guest/)
end end
it "mounts with options" do it "mounts with options" do
@ -49,7 +48,7 @@ describe "VagrantPlugins::GuestBSD::Cap::NFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/mount -t nfs -o 'nfsv2,mntudp,banana'/) expect(comm.received_commands[1]).to match(/mount -t nfs -o 'nfsv2,mntudp,banana'/)
end end
it "escapes host and guest paths" do it "escapes host and guest paths" do
@ -61,8 +60,8 @@ describe "VagrantPlugins::GuestBSD::Cap::NFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/host\\\'s/) expect(comm.received_commands[1]).to match(/host\\\'s/)
expect(comm.received_commands[0]).to match(/guest\\\ with\\\ spaces/) expect(comm.received_commands[1]).to match(/guest\\\ with\\\ spaces/)
end end
end end
end end

View File

@ -43,7 +43,7 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/mkdir -p #{guestpath}/) expect(comm.received_commands[0]).to match(/mkdir -p #{guestpath}/)
expect(comm.received_commands[0]).to match(/1.2.3.4:#{hostpath} #{guestpath}/) expect(comm.received_commands[1]).to match(/1.2.3.4:#{hostpath} #{guestpath}/)
end end
it "mounts with options" do it "mounts with options" do
@ -58,7 +58,7 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/mount -o vers=2,udp/) expect(comm.received_commands[1]).to match(/mount -o vers=2,udp/)
end end
it "emits an event" do it "emits an event" do
@ -71,7 +71,7 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to include( expect(comm.received_commands[1]).to include(
"/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guestpath}") "/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guestpath}")
end end
@ -84,8 +84,8 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
} }
cap.mount_nfs_folder(machine, ip, folders) cap.mount_nfs_folder(machine, ip, folders)
expect(comm.received_commands[0]).to match(/host\\\'s/) expect(comm.received_commands[1]).to match(/host\\\'s/)
expect(comm.received_commands[0]).to match(/guest\\\ with\\\ spaces/) expect(comm.received_commands[1]).to match(/guest\\\ with\\\ spaces/)
end end
end end
end end