Skip to content

Commit

Permalink
fixes saltstack#61946 sync_after_install immutabledict error
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes authored and garethgreenaway committed May 23, 2022
1 parent e848b9f commit 048bfd5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog/61946.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix salt-cloud sync_after_install functionality
35 changes: 27 additions & 8 deletions doc/topics/cloud/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ be configured to use SCP instead.
Sync After Install
==================
Salt allows users to create custom modules, grains, and states which can be
synchronised to minions to extend Salt with further functionality.
Salt allows users to create custom plugins such as execution, grains, and state
modules which can be synchronised to minions to extend Salt with further
functionality.

This option will inform Salt Cloud to synchronise your custom modules, grains,
states or all these to the minion just after it has been created. For this to
happen, the following line needs to be added to the main cloud
configuration file:
This option will inform Salt Cloud to synchronise your custom modules to the
minion just after it has been created. For this to happen, the following line
needs to be added to the main cloud configuration file:

.. code-block:: yaml
Expand All @@ -60,10 +60,29 @@ The available options for this setting are:

.. code-block:: yaml
modules
all
beacons
clouds
engines
executors
grains
log
matchers
modules
output
pillar
proxymodules
renderers
returners
sdb
serializers
states
all
thorium
utils
A present and non-falsy value that doesn't match one of these list items will
assume `all`, so `sync_after_install: True` and `sync_after_install: all` are
equivalent (though the former will produce a warning).


Setting Up New Salt Masters
Expand Down
29 changes: 24 additions & 5 deletions salt/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,22 +1225,41 @@ def create(self, vm_, local_master=True):
):
output = self.clouds[func](vm_)
if output is not False and "sync_after_install" in self.opts:
if self.opts["sync_after_install"] not in (
if self.opts["sync_after_install"] and self.opts[
"sync_after_install"
] not in (
"all",
"beacons",
"clouds",
"engines",
"executors",
"grains",
"log",
"matchers",
"modules",
"output",
"pillar",
"proxymodules",
"renderers",
"returners",
"sdb",
"serializers",
"states",
"grains",
"thorium",
"utils",
):
log.error("Bad option for sync_after_install")
return output
log.warning(
"Bad option for sync_after_install. Defaulting to 'all'"
)
self.opts["sync_after_install"] = "all"

# A small pause helps the sync work more reliably
time.sleep(3)

start = int(time.time())
while int(time.time()) < start + 60:
# We'll try every <timeout> seconds, up to a minute
mopts_ = salt.config.DEFAULT_MASTER_OPTS
mopts_ = copy.deepcopy(salt.config.DEFAULT_MASTER_OPTS)
conf_path = "/".join(self.opts["conf_file"].split("/")[:-1])
mopts_.update(
salt.config.master_config(os.path.join(conf_path, "master"))
Expand Down
38 changes: 38 additions & 0 deletions tests/pytests/unit/cloud/test_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import copy

import pytest
import salt.config
from salt.cloud import Cloud
from tests.support.mock import MagicMock, patch


@pytest.fixture
def master_config():
opts = copy.deepcopy(salt.config.DEFAULT_MASTER_OPTS)
opts["parallel"] = False
opts["providers"] = {
"test": {},
}
return opts


@pytest.fixture
def vm_config():
return {
"driver": "test",
"name": "test",
"provider": "test:test",
}


def test_cloud_create_attempt_sync_after_install(master_config, vm_config):
master_config["sync_after_install"] = "all"
cloud = Cloud(master_config)
cloud.clouds["test.create"] = lambda x: True

with patch(
"salt.client.get_local_client",
MagicMock(return_value=MagicMock(return_value=True)),
):
ret = cloud.create(vm_config)
assert ret

0 comments on commit 048bfd5

Please sign in to comment.