Load the modules into the state
(self, data=None, proxy=None)
| 1365 | ) |
| 1366 | |
| 1367 | def load_modules(self, data=None, proxy=None): |
| 1368 | """ |
| 1369 | Load the modules into the state |
| 1370 | """ |
| 1371 | log.info("Loading fresh modules for state activity") |
| 1372 | self.utils = salt.loader.utils(self.opts, file_client=self.file_client) |
| 1373 | resource_type = self.opts.get("resource_type") |
| 1374 | # ``self.minion_functions`` is the managing minion's standard module |
| 1375 | # set, exposed to resource-context state modules and execution |
| 1376 | # overrides as ``__minion__`` so they can escape-hatch back to the |
| 1377 | # underlying minion when needed (e.g. a resource override calling |
| 1378 | # ``ssh-keygen`` locally before pushing the key over SSH). In the |
| 1379 | # non-resource case it is None. |
| 1380 | self.minion_functions = None |
| 1381 | if resource_type: |
| 1382 | # Resource context: load execution modules through the per-resource |
| 1383 | # loader so __salt__ in state modules dispatches to the resource's |
| 1384 | # own modules (e.g. dummyresource_test) instead of the managing |
| 1385 | # minion's. State modules under salt/states/ then work unchanged |
| 1386 | # in a resource context. |
| 1387 | self.resource_funcs = salt.loader.resource( |
| 1388 | self.opts, utils=self.utils, context=self.state_con |
| 1389 | ) |
| 1390 | # Call init() so __context__ is populated before any state or |
| 1391 | # execution module function tries to read connection data from it. |
| 1392 | # The minion calls init() at startup on its own resource_funcs |
| 1393 | # loader, but State.load_modules creates a fresh loader with a |
| 1394 | # new context (self.state_con), so init() must be called again here. |
| 1395 | init_fn = f"{resource_type}.init" |
| 1396 | if init_fn in self.resource_funcs: |
| 1397 | try: |
| 1398 | self.resource_funcs[init_fn](self.opts) |
| 1399 | except Exception as exc: # pylint: disable=broad-except |
| 1400 | log.error( |
| 1401 | "Failed to initialize resource type '%s' in state loader: %s", |
| 1402 | resource_type, |
| 1403 | exc, |
| 1404 | ) |
| 1405 | # Build the managing minion's loader as the ``__minion__`` |
| 1406 | # escape hatch. Built without ``resource_type`` so its |
| 1407 | # __virtual__ checks behave normally (not the resource gate). |
| 1408 | minion_opts = dict(self.opts) |
| 1409 | minion_opts.pop("resource_type", None) |
| 1410 | self.minion_functions = salt.loader.minion_mods( |
| 1411 | minion_opts, |
| 1412 | self.state_con, |
| 1413 | utils=self.utils, |
| 1414 | proxy=self.proxy, |
| 1415 | file_client=salt.fileclient.ContextlessFileClient(self.file_client), |
| 1416 | ) |
| 1417 | self.functions = salt.loader.resource_modules( |
| 1418 | self.opts, |
| 1419 | resource_type, |
| 1420 | resource_funcs=self.resource_funcs, |
| 1421 | utils=self.utils, |
| 1422 | context=self.state_con, |
| 1423 | minion_mods=self.minion_functions, |
| 1424 | ) |