summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schweikert <rjschwei@suse.com>2022-03-01 03:39:14 -0500
committerGitHub <noreply@github.com>2022-03-01 09:39:14 +0100
commit3642ed7c63fbd2fb67630b21efdb321c7e99b913 (patch)
tree994f2c0922ed2b08edad210d81d4bd3d27eb2a7d
parentf106b2f78e36c464158672b18b64f89ca63b709a (diff)
downloadcloud-init-git-3642ed7c63fbd2fb67630b21efdb321c7e99b913.tar.gz
Stop hardcoding systemctl location (#1278)
Expect that "systemctl" is found in the PATH. Using the '/bin' prefix is very distribution specific. A number of distributions are moving all executables from '/' to '/usr'.
-rw-r--r--cloudinit/analyze/show.py2
-rw-r--r--cloudinit/config/cc_puppet.py6
-rwxr-xr-xcloudinit/sources/helpers/azure.py4
-rw-r--r--tests/unittests/config/test_cc_puppet.py24
4 files changed, 19 insertions, 17 deletions
diff --git a/cloudinit/analyze/show.py b/cloudinit/analyze/show.py
index 5fd9cdfd..e1e340ce 100644
--- a/cloudinit/analyze/show.py
+++ b/cloudinit/analyze/show.py
@@ -139,7 +139,7 @@ class SystemctlReader(object):
def __init__(self, property, parameter=None):
self.epoch = None
- self.args = ["/bin/systemctl", "show"]
+ self.args = [subp.which("systemctl"), "show"]
if parameter:
self.args.append(parameter)
self.args.extend(["-p", property])
diff --git a/cloudinit/config/cc_puppet.py b/cloudinit/config/cc_puppet.py
index f51f49bc..ad6acafe 100644
--- a/cloudinit/config/cc_puppet.py
+++ b/cloudinit/config/cc_puppet.py
@@ -142,10 +142,8 @@ def _autostart_puppet(log):
],
capture=False,
)
- elif os.path.exists("/bin/systemctl"):
- subp.subp(
- ["/bin/systemctl", "enable", "puppet.service"], capture=False
- )
+ elif subp.which("systemctl"):
+ subp.subp(["systemctl", "enable", "puppet.service"], capture=False)
elif os.path.exists("/sbin/chkconfig"):
subp.subp(["/sbin/chkconfig", "puppet", "on"], capture=False)
else:
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
index d07dc3c0..0a9f1f45 100755
--- a/cloudinit/sources/helpers/azure.py
+++ b/cloudinit/sources/helpers/azure.py
@@ -115,7 +115,7 @@ def get_boot_telemetry():
try:
out, _ = subp.subp(
- ["/bin/systemctl", "show", "-p", "UserspaceTimestampMonotonic"],
+ ["systemctl", "show", "-p", "UserspaceTimestampMonotonic"],
capture=True,
)
tsm = None
@@ -140,7 +140,7 @@ def get_boot_telemetry():
try:
out, _ = subp.subp(
[
- "/bin/systemctl",
+ "systemctl",
"show",
"cloud-init-local",
"-p",
diff --git a/tests/unittests/config/test_cc_puppet.py b/tests/unittests/config/test_cc_puppet.py
index 2c4481da..d070ab41 100644
--- a/tests/unittests/config/test_cc_puppet.py
+++ b/tests/unittests/config/test_cc_puppet.py
@@ -10,10 +10,13 @@ from tests.unittests.util import get_cloud
LOG = logging.getLogger(__name__)
+@mock.patch("cloudinit.config.cc_puppet.subp.which")
@mock.patch("cloudinit.config.cc_puppet.subp.subp")
@mock.patch("cloudinit.config.cc_puppet.os")
class TestAutostartPuppet(CiTestCase):
- def test_wb_autostart_puppet_updates_puppet_default(self, m_os, m_subp):
+ def test_wb_autostart_puppet_updates_puppet_default(
+ self, m_os, m_subp, m_subpw
+ ):
"""Update /etc/default/puppet to autostart if it exists."""
def _fake_exists(path):
@@ -37,27 +40,28 @@ class TestAutostartPuppet(CiTestCase):
m_subp.call_args_list,
)
- def test_wb_autostart_pupppet_enables_puppet_systemctl(self, m_os, m_subp):
+ def test_wb_autostart_pupppet_enables_puppet_systemctl(
+ self, m_os, m_subp, m_subpw
+ ):
"""If systemctl is present, enable puppet via systemctl."""
- def _fake_exists(path):
- return path == "/bin/systemctl"
-
- m_os.path.exists.side_effect = _fake_exists
+ m_os.path.exists.return_value = False
+ m_subpw.return_value = "/usr/bin/systemctl"
cc_puppet._autostart_puppet(LOG)
expected_calls = [
- mock.call(
- ["/bin/systemctl", "enable", "puppet.service"], capture=False
- )
+ mock.call(["systemctl", "enable", "puppet.service"], capture=False)
]
self.assertEqual(expected_calls, m_subp.call_args_list)
- def test_wb_autostart_pupppet_enables_puppet_chkconfig(self, m_os, m_subp):
+ def test_wb_autostart_pupppet_enables_puppet_chkconfig(
+ self, m_os, m_subp, m_subpw
+ ):
"""If chkconfig is present, enable puppet via checkcfg."""
def _fake_exists(path):
return path == "/sbin/chkconfig"
+ m_subpw.return_value = None
m_os.path.exists.side_effect = _fake_exists
cc_puppet._autostart_puppet(LOG)
expected_calls = [