From 690963669cd8d760cab53cea45adfe39f3ca49c9 Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Sat, 3 Aug 2019 20:59:41 +0530 Subject: [PATCH] Added fall through logic with error messages if OS is unrecognized. This will print an error message but still exit with 0 if an ephemeral hostname change was made, which seems more defenisve, and user friendly to me. --- plugins/guests/alt/cap/change_host_name.rb | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/plugins/guests/alt/cap/change_host_name.rb b/plugins/guests/alt/cap/change_host_name.rb index 6b73d27ad..1991b29f2 100644 --- a/plugins/guests/alt/cap/change_host_name.rb +++ b/plugins/guests/alt/cap/change_host_name.rb @@ -16,13 +16,6 @@ module VagrantPlugins NEW_HOSTNAME_FULL='#{name}' NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}" - # Update sysconfig - if [ -f /etc/sysconfig/network ]; then - sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network - elif [ -f /etc/hostname ]; then - sed -i 's/.*/#{name}/' /etc/hostname - fi - # Set the hostname - use hostnamectl if available if command -v hostnamectl; then hostnamectl set-hostname --static '#{name}' @@ -39,12 +32,27 @@ module VagrantPlugins sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT\(\s\)/\1$NEW_HOSTNAME_SHORT\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT$/\1$NEW_HOSTNAME_SHORT/g" /etc/hosts fi - # Restart network - if command -v hostnamectl; then + # Persist hostname change across reboots + if [ -f /etc/sysconfig/network ]; then + sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network + elif [ -f /etc/hostname ]; then + sed -i 's/.*/#{name}/' /etc/hostname + else + echo 'Unrecognized system. Hostname change may not persist across reboots.' + exit 0 + fi + + # Restart the network if we find a recognized SYS V init script + if command -v service; then if [ -f /etc/init.d/network ]; then service network restart elif [ -f /etc/init.d/networking ]; then service networking restart + elif [ -f /etc/init.d/NetworkManager ]; then + service NetworkManager restart + else + echo 'Unrecognized system. Networking was not restarted following hostname change.' + exit 0 fi fi