d9c3f4e
From 80006251f422a8d534ff9bafa0e0c45d9c98143c Mon Sep 17 00:00:00 2001
d9c3f4e
From: Joe Doss <jdoss@kennasecurity.com>
d9c3f4e
Date: Mon, 17 Sep 2018 13:30:55 -0500
d9c3f4e
Subject: [PATCH 01/10] Add in logic to restart NetworkManager if it is
d9c3f4e
 enabled.
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 8 +++++++-
d9c3f4e
 1 file changed, 7 insertions(+), 1 deletion(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index 55fcdc4b48..8f32650035 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -29,7 +29,13 @@ def self.change_host_name(machine, name)
d9c3f4e
               }
d9c3f4e
 
d9c3f4e
               # Restart network
d9c3f4e
-              service network restart
d9c3f4e
+              if (test -f /etc/init.d/network && /etc/init.d/network status &> /dev/null ); then
d9c3f4e
+                service network restart
d9c3f4e
+              elif (test -f /usr/lib/systemd/system/NetworkManager.service && systemctl is-enabled NetworkManager.service &> /dev/null ); then
d9c3f4e
+                systemctl restart NetworkManager.service
d9c3f4e
+              else
d9c3f4e
+                printf "Could not restart the network to set the new hostname!\n"
d9c3f4e
+              fi
d9c3f4e
             EOH
d9c3f4e
           end
d9c3f4e
         end
d9c3f4e
d9c3f4e
From 94954739b53ee4c6741a35c366c2fe5c9853e0ed Mon Sep 17 00:00:00 2001
d9c3f4e
From: Joe Doss <jdoss@kennasecurity.com>
d9c3f4e
Date: Mon, 17 Sep 2018 14:30:57 -0500
d9c3f4e
Subject: [PATCH 02/10] Simplified if statements.
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 4 ++--
d9c3f4e
 1 file changed, 2 insertions(+), 2 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index 8f32650035..e9c0b1d80e 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -29,9 +29,9 @@ def self.change_host_name(machine, name)
d9c3f4e
               }
d9c3f4e
 
d9c3f4e
               # Restart network
d9c3f4e
-              if (test -f /etc/init.d/network && /etc/init.d/network status &> /dev/null ); then
d9c3f4e
+              if test -f /etc/init.d/network; then
d9c3f4e
                 service network restart
d9c3f4e
-              elif (test -f /usr/lib/systemd/system/NetworkManager.service && systemctl is-enabled NetworkManager.service &> /dev/null ); then
d9c3f4e
+              elif systemctl -q is-enabled NetworkManager.service; then
d9c3f4e
                 systemctl restart NetworkManager.service
d9c3f4e
               else
d9c3f4e
                 printf "Could not restart the network to set the new hostname!\n"
d9c3f4e
d9c3f4e
From c14a4a09f723230682c5ef5f9dc53662e2968b92 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Joe Doss <jdoss@kennasecurity.com>
d9c3f4e
Date: Mon, 17 Sep 2018 14:56:06 -0500
d9c3f4e
Subject: [PATCH 03/10] Switch if statements, check for systemctl, and switch
d9c3f4e
 to is-active.
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 6 +++---
d9c3f4e
 1 file changed, 3 insertions(+), 3 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index e9c0b1d80e..70bd496943 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -29,10 +29,10 @@ def self.change_host_name(machine, name)
d9c3f4e
               }
d9c3f4e
 
d9c3f4e
               # Restart network
d9c3f4e
-              if test -f /etc/init.d/network; then
d9c3f4e
-                service network restart
d9c3f4e
-              elif systemctl -q is-enabled NetworkManager.service; then
d9c3f4e
+              if (test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service); then
d9c3f4e
                 systemctl restart NetworkManager.service
d9c3f4e
+              elif test -f /etc/init.d/network; then
d9c3f4e
+                service network restart
d9c3f4e
               else
d9c3f4e
                 printf "Could not restart the network to set the new hostname!\n"
d9c3f4e
               fi
d9c3f4e
d9c3f4e
From 19aa9578c797c99a8632955a703490d5e6b50a26 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Joe Doss <jdoss@kennasecurity.com>
d9c3f4e
Date: Tue, 18 Sep 2018 13:15:26 -0500
d9c3f4e
Subject: [PATCH 04/10] Exit 1 if we cannot set the hostname.
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 1 +
d9c3f4e
 1 file changed, 1 insertion(+)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index 70bd496943..ae02460156 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -35,6 +35,7 @@ def self.change_host_name(machine, name)
d9c3f4e
                 service network restart
d9c3f4e
               else
d9c3f4e
                 printf "Could not restart the network to set the new hostname!\n"
d9c3f4e
+                exit 1
d9c3f4e
               fi
d9c3f4e
             EOH
d9c3f4e
           end
d9c3f4e
d9c3f4e
From 86ab4533b180009ed476026374110fc0bd79f522 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Joe Doss <jdoss@kennasecurity.com>
d9c3f4e
Date: Tue, 18 Sep 2018 13:16:12 -0500
d9c3f4e
Subject: [PATCH 05/10] Fix the test to check for systemctl restart
d9c3f4e
 NetworkManager.service too.
d9c3f4e
d9c3f4e
---
d9c3f4e
 test/unit/plugins/guests/redhat/cap/change_host_name_test.rb | 2 +-
d9c3f4e
 1 file changed, 1 insertion(+), 1 deletion(-)
d9c3f4e
d9c3f4e
diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
index 7662935458..10f43a3593 100644
d9c3f4e
--- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
+++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
@@ -31,7 +31,7 @@
d9c3f4e
       expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/)
d9c3f4e
       expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/)
d9c3f4e
       expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/)
d9c3f4e
-      expect(comm.received_commands[1]).to match(/service network restart/)
d9c3f4e
+      expect(comm.received_commands[1]).to match(/service network restart|systemctl restart NetworkManager.service/)
d9c3f4e
     end
d9c3f4e
 
d9c3f4e
     it "does not change the hostname if already set" do
d9c3f4e
d9c3f4e
From a12b09b098ea87eec815e166d0e1395f6f47f937 Mon Sep 17 00:00:00 2001
d9c3f4e
From: shotop <samuel.j.hotop@gmail.com>
d9c3f4e
Date: Thu, 20 Sep 2018 14:21:40 -0500
d9c3f4e
Subject: [PATCH 06/10] add specs around network restart logic
d9c3f4e
d9c3f4e
---
d9c3f4e
 .../redhat/cap/change_host_name_test.rb       | 27 +++++++++++++++++++
d9c3f4e
 1 file changed, 27 insertions(+)
d9c3f4e
d9c3f4e
diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
index 10f43a3593..b85802e947 100644
d9c3f4e
--- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
+++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
@@ -39,5 +39,32 @@
d9c3f4e
       cap.change_host_name(machine, name)
d9c3f4e
       expect(comm.received_commands.size).to eq(1)
d9c3f4e
     end
d9c3f4e
+
d9c3f4e
+    context "restarts the network" do
d9c3f4e
+      it "uses systemctl and NetworkManager.service" do
d9c3f4e
+        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
+        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 0)
d9c3f4e
+        cap.change_host_name(machine, name)
d9c3f4e
+        expect(comm.received_commands[1]).to match(/systemctl restart NetworkManager.service/)
d9c3f4e
+      end
d9c3f4e
+
d9c3f4e
+      it "uses the service command" do
d9c3f4e
+        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
+        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1)
d9c3f4e
+        comm.stub_command("test -f /etc/init.d/network", exit_code: 0)
d9c3f4e
+        cap.change_host_name(machine, name)
d9c3f4e
+        expect(comm.received_commands[1]).to match(/service network restart/)
d9c3f4e
+      end
d9c3f4e
+    end
d9c3f4e
+
d9c3f4e
+    context "cannot restart the network" do
d9c3f4e
+      it "prints cannot restart message" do
d9c3f4e
+        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
+        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1)
d9c3f4e
+        comm.stub_command("test -f /etc/init.d/network", exit_code: 1)
d9c3f4e
+        cap.change_host_name(machine, name)
d9c3f4e
+        expect(comm.received_commands[1]).to match(/printf "Could not restart the network to set the new hostname!/)
d9c3f4e
+      end
d9c3f4e
+    end
d9c3f4e
   end
d9c3f4e
 end
d9c3f4e
d9c3f4e
From fb5fc0e657a10ee1eaf046980827cc1802c4d0f9 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Thu, 20 Sep 2018 16:42:39 -0700
d9c3f4e
Subject: [PATCH 07/10] Update guest inspection utility module
d9c3f4e
d9c3f4e
Use sudo option for communicator test command instead of inline sudo
d9c3f4e
to properly use configured sudo value. Use command for checking
d9c3f4e
availability of hostnamectl. Use is-active for determining if a
d9c3f4e
service is being actively managed by systemd.
d9c3f4e
---
d9c3f4e
 lib/vagrant/util/guest_inspection.rb | 19 ++++++++++++++++---
d9c3f4e
 1 file changed, 16 insertions(+), 3 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/lib/vagrant/util/guest_inspection.rb b/lib/vagrant/util/guest_inspection.rb
d9c3f4e
index 86ab1dc69e..5a1902d8da 100644
d9c3f4e
--- a/lib/vagrant/util/guest_inspection.rb
d9c3f4e
+++ b/lib/vagrant/util/guest_inspection.rb
d9c3f4e
@@ -17,22 +17,34 @@ module Linux
d9c3f4e
 
d9c3f4e
         # systemd-networkd.service is in use
d9c3f4e
         #
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def systemd_networkd?(comm)
d9c3f4e
-          comm.test("sudo systemctl status systemd-networkd.service")
d9c3f4e
+          comm.test("systemctl -q is-active systemd-networkd.service", sudo: true)
d9c3f4e
+        end
d9c3f4e
+
d9c3f4e
+        # Check if given service is controlled by systemd
d9c3f4e
+        #
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
+        # @param [String] service_name Name of the service to check
d9c3f4e
+        # @return [Boolean]
d9c3f4e
+        def systemd_controlled?(comm, service_name)
d9c3f4e
+          comm.test("systemctl -q is-active #{service_name}", sudo: true)
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         # systemd hostname set is via hostnamectl
d9c3f4e
         #
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def hostnamectl?(comm)
d9c3f4e
-          comm.test("hostnamectl")
d9c3f4e
+          comm.test("command -v hostnamectl")
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         ## netplan helpers
d9c3f4e
 
d9c3f4e
         # netplan is installed
d9c3f4e
         #
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def netplan?(comm)
d9c3f4e
           comm.test("netplan -h")
d9c3f4e
@@ -42,6 +54,7 @@ def netplan?(comm)
d9c3f4e
 
d9c3f4e
         # nmcli is installed
d9c3f4e
         #
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def nmcli?(comm)
d9c3f4e
           comm.test("nmcli")
d9c3f4e
@@ -49,7 +62,7 @@ def nmcli?(comm)
d9c3f4e
 
d9c3f4e
         # NetworkManager currently controls device
d9c3f4e
         #
d9c3f4e
-        # @param comm [Communicator]
d9c3f4e
+        # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @param device_name [String]
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def nm_controlled?(comm, device_name)
d9c3f4e
d9c3f4e
From ff021fcab404c95e52566bfca4207da9c0101e01 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Thu, 20 Sep 2018 16:44:08 -0700
d9c3f4e
Subject: [PATCH 08/10] Update redhat change host name capability to support
d9c3f4e
 systemd
d9c3f4e
d9c3f4e
Update capability to use guest inspection module for determining
d9c3f4e
correct actions to execute. When systemd is in use restart the
d9c3f4e
correct active service, either NetworkManager or networkd. Default
d9c3f4e
to using the original service restart when systemd service is not
d9c3f4e
found.
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 41 ++++++++++---------
d9c3f4e
 1 file changed, 21 insertions(+), 20 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index ae02460156..5da660df05 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -2,6 +2,9 @@ module VagrantPlugins
d9c3f4e
   module GuestRedHat
d9c3f4e
     module Cap
d9c3f4e
       class ChangeHostName
d9c3f4e
+
d9c3f4e
+        extend Vagrant::Util::GuestInspection
d9c3f4e
+
d9c3f4e
         def self.change_host_name(machine, name)
d9c3f4e
           comm = machine.communicate
d9c3f4e
 
d9c3f4e
@@ -10,34 +13,32 @@ def self.change_host_name(machine, name)
d9c3f4e
             comm.sudo <<-EOH.gsub(/^ {14}/, '')
d9c3f4e
               # Update sysconfig
d9c3f4e
               sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
d9c3f4e
-
d9c3f4e
               # Update DNS
d9c3f4e
               sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-*
d9c3f4e
-
d9c3f4e
               # Set the hostname - use hostnamectl if available
d9c3f4e
               echo '#{name}' > /etc/hostname
d9c3f4e
-              if command -v hostnamectl; then
d9c3f4e
-                hostnamectl set-hostname --static '#{name}'
d9c3f4e
-                hostnamectl set-hostname --transient '#{name}'
d9c3f4e
-              else
d9c3f4e
-                hostname -F /etc/hostname
d9c3f4e
-              fi
d9c3f4e
-
d9c3f4e
-              # Prepend ourselves to /etc/hosts
d9c3f4e
               grep -w '#{name}' /etc/hosts || {
d9c3f4e
                 sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
d9c3f4e
               }
d9c3f4e
-
d9c3f4e
-              # Restart network
d9c3f4e
-              if (test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service); then
d9c3f4e
-                systemctl restart NetworkManager.service
d9c3f4e
-              elif test -f /etc/init.d/network; then
d9c3f4e
-                service network restart
d9c3f4e
-              else
d9c3f4e
-                printf "Could not restart the network to set the new hostname!\n"
d9c3f4e
-                exit 1
d9c3f4e
-              fi
d9c3f4e
             EOH
d9c3f4e
+
d9c3f4e
+            if hostnamectl?(comm)
d9c3f4e
+              comm.sudo("hostnamectl set-hostname --static '#{name}' ; " \
d9c3f4e
+                "hostnamectl set-hostname --transient '#{name}'")
d9c3f4e
+            else
d9c3f4e
+              comm.sudo("hostname -F /etc/hostname")
d9c3f4e
+            end
d9c3f4e
+
d9c3f4e
+            restart_command = "service network restart"
d9c3f4e
+
d9c3f4e
+            if systemd?
d9c3f4e
+              if systemd_networkd?(comm)
d9c3f4e
+                restart_command = "systemctl restart systemd-networkd.service"
d9c3f4e
+              elsif systemd_controlled?(comm, "NetworkManager.service")
d9c3f4e
+                restart_command = "systemctl restart NetworkManager.service"
d9c3f4e
+              end
d9c3f4e
+            end
d9c3f4e
+            comm.sudo(restart_command)
d9c3f4e
           end
d9c3f4e
         end
d9c3f4e
       end
d9c3f4e
d9c3f4e
From bc217d5e577457df5ac4ecdfffa17fd0a8c85b18 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Thu, 20 Sep 2018 16:46:45 -0700
d9c3f4e
Subject: [PATCH 09/10] Update redhat change host name capability tests for
d9c3f4e
 systemd/NetworkManger updates
d9c3f4e
d9c3f4e
---
d9c3f4e
 .../redhat/cap/change_host_name_test.rb       | 95 +++++++++++++------
d9c3f4e
 1 file changed, 68 insertions(+), 27 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
index b85802e947..8d0c9ebd4b 100644
d9c3f4e
--- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
+++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
d9c3f4e
@@ -20,50 +20,91 @@
d9c3f4e
 
d9c3f4e
   describe ".change_host_name" do
d9c3f4e
     let(:cap) { caps.get(:change_host_name) }
d9c3f4e
-
d9c3f4e
     let(:name) { "banana-rama.example.com" }
d9c3f4e
+    let(:hostname_changed) { true }
d9c3f4e
+    let(:systemd) { true }
d9c3f4e
+    let(:hostnamectl) { true }
d9c3f4e
+    let(:networkd) { true }
d9c3f4e
+    let(:network_manager) { false }
d9c3f4e
 
d9c3f4e
-    it "sets the hostname" do
d9c3f4e
-      comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
+    before do
d9c3f4e
+      comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: hostname_changed ? 1 : 0)
d9c3f4e
+      allow(cap).to receive(:systemd?).and_return(systemd)
d9c3f4e
+      allow(cap).to receive(:hostnamectl?).and_return(hostnamectl)
d9c3f4e
+      allow(cap).to receive(:systemd_networkd?).and_return(networkd)
d9c3f4e
+      allow(cap).to receive(:systemd_controlled?).with(anything, /NetworkManager/).and_return(network_manager)
d9c3f4e
+    end
d9c3f4e
 
d9c3f4e
+    it "sets the hostname" do
d9c3f4e
       cap.change_host_name(machine, name)
d9c3f4e
       expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
d9c3f4e
       expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/)
d9c3f4e
-      expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/)
d9c3f4e
-      expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/)
d9c3f4e
-      expect(comm.received_commands[1]).to match(/service network restart|systemctl restart NetworkManager.service/)
d9c3f4e
     end
d9c3f4e
 
d9c3f4e
-    it "does not change the hostname if already set" do
d9c3f4e
-      comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
d9c3f4e
-      cap.change_host_name(machine, name)
d9c3f4e
-      expect(comm.received_commands.size).to eq(1)
d9c3f4e
-    end
d9c3f4e
+    context "when hostnamectl is in use" do
d9c3f4e
+      let(:hostnamectl) { true }
d9c3f4e
 
d9c3f4e
-    context "restarts the network" do
d9c3f4e
-      it "uses systemctl and NetworkManager.service" do
d9c3f4e
-        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
-        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 0)
d9c3f4e
+      it "sets hostname with hostnamectl" do
d9c3f4e
         cap.change_host_name(machine, name)
d9c3f4e
-        expect(comm.received_commands[1]).to match(/systemctl restart NetworkManager.service/)
d9c3f4e
+        expect(comm.received_commands[2]).to match(/hostnamectl/)
d9c3f4e
       end
d9c3f4e
+    end
d9c3f4e
+
d9c3f4e
+    context "when hostnamectl is not in use" do
d9c3f4e
+      let(:hostnamectl) { false }
d9c3f4e
 
d9c3f4e
-      it "uses the service command" do
d9c3f4e
-        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
-        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1)
d9c3f4e
-        comm.stub_command("test -f /etc/init.d/network", exit_code: 0)
d9c3f4e
+      it "sets hostname with hostname command" do
d9c3f4e
         cap.change_host_name(machine, name)
d9c3f4e
-        expect(comm.received_commands[1]).to match(/service network restart/)
d9c3f4e
+        expect(comm.received_commands[2]).to match(/hostname -F/)
d9c3f4e
       end
d9c3f4e
     end
d9c3f4e
 
d9c3f4e
-    context "cannot restart the network" do
d9c3f4e
-      it "prints cannot restart message" do
d9c3f4e
-        comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
d9c3f4e
-        comm.stub_command("test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service", exit_code: 1)
d9c3f4e
-        comm.stub_command("test -f /etc/init.d/network", exit_code: 1)
d9c3f4e
+    context "when host name is already set" do
d9c3f4e
+      let(:hostname_changed) { false }
d9c3f4e
+
d9c3f4e
+      it "does not change the hostname" do
d9c3f4e
         cap.change_host_name(machine, name)
d9c3f4e
-        expect(comm.received_commands[1]).to match(/printf "Could not restart the network to set the new hostname!/)
d9c3f4e
+        expect(comm.received_commands.size).to eq(1)
d9c3f4e
+      end
d9c3f4e
+    end
d9c3f4e
+
d9c3f4e
+    context "restarts the network" do
d9c3f4e
+      context "when networkd is in use" do
d9c3f4e
+        let(:networkd) { true }
d9c3f4e
+
d9c3f4e
+        it "restarts networkd with systemctl" do
d9c3f4e
+          cap.change_host_name(machine, name)
d9c3f4e
+          expect(comm.received_commands[3]).to match(/systemctl restart systemd-networkd/)
d9c3f4e
+        end
d9c3f4e
+      end
d9c3f4e
+
d9c3f4e
+      context "when NetworkManager is in use" do
d9c3f4e
+        let(:networkd) { false }
d9c3f4e
+        let(:network_manager) { true }
d9c3f4e
+
d9c3f4e
+        it "restarts NetworkManager with systemctl" do
d9c3f4e
+          cap.change_host_name(machine, name)
d9c3f4e
+          expect(comm.received_commands[3]).to match(/systemctl restart NetworkManager/)
d9c3f4e
+        end
d9c3f4e
+      end
d9c3f4e
+
d9c3f4e
+      context "when networkd and NetworkManager are not in use" do
d9c3f4e
+        let(:networkd) { false }
d9c3f4e
+        let(:network_manager) { false }
d9c3f4e
+
d9c3f4e
+        it "restarts the network using service" do
d9c3f4e
+          cap.change_host_name(machine, name)
d9c3f4e
+          expect(comm.received_commands[3]).to match(/service network restart/)
d9c3f4e
+        end
d9c3f4e
+      end
d9c3f4e
+
d9c3f4e
+      context "when systemd is not in use" do
d9c3f4e
+        let(:systemd) { false }
d9c3f4e
+
d9c3f4e
+        it "restarts the network using service" do
d9c3f4e
+          cap.change_host_name(machine, name)
d9c3f4e
+          expect(comm.received_commands[3]).to match(/service network restart/)
d9c3f4e
+        end
d9c3f4e
       end
d9c3f4e
     end
d9c3f4e
   end
d9c3f4e
d9c3f4e
From 8fd05fe3c1b773777f08ca50dd651cbaf33838d3 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Fri, 21 Sep 2018 09:19:40 -0700
d9c3f4e
Subject: [PATCH 10/10] Use `command -v` for checks in all inspection helpers.
d9c3f4e
 Fix stubs in tests.
d9c3f4e
d9c3f4e
---
d9c3f4e
 lib/vagrant/util/guest_inspection.rb               |  4 ++--
d9c3f4e
 .../guests/debian/cap/configure_networks_test.rb   | 14 +++++++-------
d9c3f4e
 2 files changed, 9 insertions(+), 9 deletions(-)
d9c3f4e
d9c3f4e
diff --git a/lib/vagrant/util/guest_inspection.rb b/lib/vagrant/util/guest_inspection.rb
d9c3f4e
index 5a1902d8da..cd0a96d3ef 100644
d9c3f4e
--- a/lib/vagrant/util/guest_inspection.rb
d9c3f4e
+++ b/lib/vagrant/util/guest_inspection.rb
d9c3f4e
@@ -47,7 +47,7 @@ def hostnamectl?(comm)
d9c3f4e
         # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def netplan?(comm)
d9c3f4e
-          comm.test("netplan -h")
d9c3f4e
+          comm.test("command -v netplan")
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         ## nmcli helpers
d9c3f4e
--- a/lib/vagrant/util/guest_inspection.rb
d9c3f4e
+++ b/lib/vagrant/util/guest_inspection.rb
d9c3f4e
@@ -57,7 +57,7 @@ def netplan?(comm)
d9c3f4e
         # @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
d9c3f4e
         # @return [Boolean]
d9c3f4e
         def nmcli?(comm)
d9c3f4e
-          comm.test("nmcli")
d9c3f4e
+          comm.test("command -v nmcli")
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         # NetworkManager currently controls device
d9c3f4e
diff --git a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb
d9c3f4e
index b4691d0fd4..d3a523c95b 100644
d9c3f4e
--- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb
d9c3f4e
+++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb
d9c3f4e
@@ -66,8 +66,8 @@
d9c3f4e
 
d9c3f4e
     before do
d9c3f4e
       allow(comm).to receive(:test).with("systemctl | grep '^-.mount'").and_return(false)
d9c3f4e
-      allow(comm).to receive(:test).with("sudo systemctl status systemd-networkd.service").and_return(false)
d9c3f4e
-      allow(comm).to receive(:test).with("netplan -h").and_return(false)
d9c3f4e
+      allow(comm).to receive(:test).with("systemctl -q is-active systemd-networkd.service", anything).and_return(false)
d9c3f4e
+      allow(comm).to receive(:test).with("command -v netplan").and_return(false)
d9c3f4e
     end
d9c3f4e
 
d9c3f4e
     it "creates and starts the networks using net-tools" do
d9c3f4e
@@ -83,7 +83,7 @@
d9c3f4e
     context "with systemd" do
d9c3f4e
       before do
d9c3f4e
         expect(comm).to receive(:test).with("systemctl | grep '^-.mount'").and_return(true)
d9c3f4e
-        allow(comm).to receive(:test).with("netplan -h").and_return(false)
d9c3f4e
+        allow(comm).to receive(:test).with("command -v netplan").and_return(false)
d9c3f4e
       end
d9c3f4e
 
d9c3f4e
       it "creates and starts the networks using net-tools" do
d9c3f4e
@@ -100,7 +100,7 @@
d9c3f4e
 
d9c3f4e
       context "with systemd-networkd" do
d9c3f4e
         before do
d9c3f4e
-          expect(comm).to receive(:test).with("sudo systemctl status systemd-networkd.service").and_return(true)
d9c3f4e
+          expect(comm).to receive(:test).with("systemctl -q is-active systemd-networkd.service", anything).and_return(true)
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         it "creates and starts the networks using systemd-networkd" do
d9c3f4e
@@ -115,7 +115,7 @@
d9c3f4e
 
d9c3f4e
       context "with netplan" do
d9c3f4e
         before do
d9c3f4e
-          expect(comm).to receive(:test).with("netplan -h").and_return(true)
d9c3f4e
+          expect(comm).to receive(:test).with("command -v netplan").and_return(true)
d9c3f4e
         end
d9c3f4e
 
d9c3f4e
         it "creates and starts the networks for systemd with netplan" do
d9c3f4e
d9c3f4e
From 1797798760eb72d6b02724f75bc54dd83815e986 Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Fri, 28 Sep 2018 07:59:39 -0700
d9c3f4e
Subject: [PATCH] Fix module name
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 2 +-
d9c3f4e
 1 file changed, 1 insertion(+), 1 deletion(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index 5da660df05..37c8912a80 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -3,7 +3,7 @@ module GuestRedHat
d9c3f4e
     module Cap
d9c3f4e
       class ChangeHostName
d9c3f4e
 
d9c3f4e
-        extend Vagrant::Util::GuestInspection
d9c3f4e
+        extend Vagrant::Util::GuestInspection::Linux
d9c3f4e
 
d9c3f4e
         def self.change_host_name(machine, name)
d9c3f4e
           comm = machine.communicate
d9c3f4e
d9c3f4e
From 11b0d58fa081cd9a27c272244a0d119acc81f32e Mon Sep 17 00:00:00 2001
d9c3f4e
From: Chris Roberts <croberts@hashicorp.com>
d9c3f4e
Date: Mon, 1 Oct 2018 08:43:49 -0700
d9c3f4e
Subject: [PATCH] Include communicator on call
d9c3f4e
d9c3f4e
---
d9c3f4e
 plugins/guests/redhat/cap/change_host_name.rb | 2 +-
d9c3f4e
 1 file changed, 1 insertion(+), 1 deletion(-)
d9c3f4e
d9c3f4e
diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
index 37c8912a80..5ceb63665a 100644
d9c3f4e
--- a/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
+++ b/plugins/guests/redhat/cap/change_host_name.rb
d9c3f4e
@@ -31,7 +31,7 @@ def self.change_host_name(machine, name)
d9c3f4e
 
d9c3f4e
             restart_command = "service network restart"
d9c3f4e
 
d9c3f4e
-            if systemd?
d9c3f4e
+            if systemd?(comm)
d9c3f4e
               if systemd_networkd?(comm)
d9c3f4e
                 restart_command = "systemctl restart systemd-networkd.service"
d9c3f4e
               elsif systemd_controlled?(comm, "NetworkManager.service")