Skip to content

Commit

Permalink
Add flexible Linux box support for Vagrant
Browse files Browse the repository at this point in the history
Determining the SSH service restart command needed based on
the Vagrant box name containing one of the expected sequences
is problematic. With this change the required service restart
command should be automatically determined for at least all
vaguely recent Debian, Ubuntu, EL, or Suse systems, regardless
of box name.
  • Loading branch information
seanmil committed Sep 29, 2023
1 parent 32c285e commit 1172ab7
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions tasks/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ def configure_remoting(platform, remoting_config_path, password)
command = "vagrant ssh-config > \"#{remoting_config_path}\""
run_local_command(command, @vagrant_env)
remoting_config = Net::SSH::Config.load(remoting_config_path, 'default')
case platform
when %r{debian.*|ubuntu.*}
restart_command = 'service ssh restart'
when %r{sles-11.*|(centos|redhat|oracle)-[56].*}
restart_command = 'service sshd restart'
when %r{centos.*|redhat.*|oracle.*|sles.*}
restart_command = 'systemctl restart sshd.service'
else
raise ArgumentError, "Unsupported Platform: '#{platform}'"
end
# Pre-configure sshd on the platform prior to handing back
ssh_params = {
port: remoting_config['port'],
Expand All @@ -104,6 +94,15 @@ def configure_remoting(platform, remoting_config_path, password)
) do |session|
session.exec!('sudo su -c "cp -r .ssh /root/."')
session.exec!('sudo su -c "sed -i \"s/.*PermitUserEnvironment\s.*/PermitUserEnvironment yes/g\" /etc/ssh/sshd_config"')
systemctl = session.exec!('which systemctl 2>/dev/null')
if systemctl.strip.empty?

Check failure on line 98 in tasks/vagrant.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

Check failure on line 98 in tasks/vagrant.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
# Debian and Ubuntu use 'ssh' and the EL/Suse family use 'sshd'. This will catch either.
restart_command = 'service ssh restart || service sshd restart'
else
# On Debian/Ubuntu sshd is an alias to the 'ssh' service, and on the EL/Suse families
# 'sshd' is the service name, so 'sshd.service' works for all:
restart_command = 'systemctl restart sshd.service'
end
session.exec!("sudo su -c \"#{restart_command}\"")
end
else
Expand Down

0 comments on commit 1172ab7

Please sign in to comment.