Skip to content

Commit db72c84

Browse files
authored
puppet config: add the start_agent option (canonical#1002)
The current code starts the puppet agent and also sets autostart in all cases. This conflicts with a common pattern where puppet itself manages the agent and autostart state. For example, in my deploy puppet disables the puppet agent and replaces it with a cron. This causes various races both within this cloud-init unit and within puppet itself while cloud-init and puppet fight over whether or not to enable the service.
1 parent 58c2de4 commit db72c84

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

cloudinit/config/cc_puppet.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@
5959
attributes and certificate extension requests.
6060
See https://puppet.com/docs/puppet/latest/config_file_csr_attributes.html
6161
62-
The puppet service will be automatically enabled after installation. A manual
63-
run can also be triggered by setting ``exec`` to ``true``, and additional
64-
arguments can be passed to ``puppet agent`` via the ``exec_args`` key (by
65-
default the agent will execute with the ``--test`` flag).
62+
By default, the puppet service will be automatically enabled after installation
63+
and set to automatically start on boot. To override this in favor of manual
64+
puppet execution set ``start_service`` to ``false``.
65+
66+
A single manual run can be triggered by setting ``exec`` to ``true``, and
67+
additional arguments can be passed to ``puppet agent`` via the ``exec_args``
68+
key (by default the agent will execute with the ``--test`` flag).
6669
6770
**Internal name:** ``cc_puppet``
6871
@@ -85,6 +88,7 @@
8588
package_name: 'puppet'
8689
exec: <true/false>
8790
exec_args: ['--test']
91+
start_service: <true/false>
8892
conf:
8993
agent:
9094
server: "puppetserver.example.org"
@@ -197,6 +201,9 @@ def handle(name, cfg, cloud, log, _args):
197201
puppet_cfg, 'install_type', 'packages')
198202
cleanup = util.get_cfg_option_bool(puppet_cfg, 'cleanup', True)
199203
run = util.get_cfg_option_bool(puppet_cfg, 'exec', default=False)
204+
start_puppetd = util.get_cfg_option_bool(puppet_cfg,
205+
'start_service',
206+
default=True)
200207
aio_install_url = util.get_cfg_option_str(
201208
puppet_cfg, 'aio_install_url', default=AIO_INSTALL_URL)
202209

@@ -291,7 +298,8 @@ def handle(name, cfg, cloud, log, _args):
291298
default_flow_style=False))
292299

293300
# Set it up so it autostarts
294-
_autostart_puppet(log)
301+
if start_puppetd:
302+
_autostart_puppet(log)
295303

296304
# Run the agent if needed
297305
if run:
@@ -312,7 +320,8 @@ def handle(name, cfg, cloud, log, _args):
312320
cmd.extend(PUPPET_AGENT_DEFAULT_ARGS)
313321
subp.subp(cmd, capture=False)
314322

315-
# Start puppetd
316-
subp.subp(['service', 'puppet', 'start'], capture=False)
323+
if start_puppetd:
324+
# Start puppetd
325+
subp.subp(['service', 'puppet', 'start'], capture=False)
317326

318327
# vi: ts=4 expandtab

tests/unittests/test_handler/test_handler_puppet.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ def test_puppet_runs_puppet_if_requested(self, m_subp, m_auto):
277277
[mock.call(['puppet', 'agent', '--test'], capture=False)],
278278
m_subp.call_args_list)
279279

280+
@mock.patch('cloudinit.config.cc_puppet.subp.subp', return_value=("", ""))
281+
def test_puppet_starts_puppetd(self, m_subp, m_auto):
282+
"""Run puppet with default args if 'exec' is set to True."""
283+
mycloud = self._get_cloud('ubuntu')
284+
cfg = {'puppet': {}}
285+
cc_puppet.handle('notimportant', cfg, mycloud, LOG, None)
286+
self.assertEqual(1, m_auto.call_count)
287+
self.assertIn(
288+
[mock.call(['service', 'puppet', 'start'], capture=False)],
289+
m_subp.call_args_list)
290+
291+
@mock.patch('cloudinit.config.cc_puppet.subp.subp', return_value=("", ""))
292+
def test_puppet_skips_puppetd(self, m_subp, m_auto):
293+
"""Run puppet with default args if 'exec' is set to True."""
294+
mycloud = self._get_cloud('ubuntu')
295+
cfg = {'puppet': {'start_service': False}}
296+
cc_puppet.handle('notimportant', cfg, mycloud, LOG, None)
297+
self.assertEqual(0, m_auto.call_count)
298+
self.assertNotIn(
299+
[mock.call(['service', 'puppet', 'start'], capture=False)],
300+
m_subp.call_args_list)
301+
280302
@mock.patch('cloudinit.config.cc_puppet.subp.subp', return_value=("", ""))
281303
def test_puppet_runs_puppet_with_args_list_if_requested(self,
282304
m_subp, m_auto):

0 commit comments

Comments
 (0)