2021-01-08 13:41:50 +01:00
|
|
|
From 9fba801c1e1e6136808dca80ccd7524ed483250e Mon Sep 17 00:00:00 2001
|
2019-01-17 10:18:02 +01:00
|
|
|
From: Bo Maryniuk <bo@suse.de>
|
|
|
|
Date: Fri, 19 Oct 2018 15:44:47 +0200
|
|
|
|
Subject: [PATCH] Add supportconfig module for remote calls and SaltSSH
|
|
|
|
|
|
|
|
Add log collector for remote purposes
|
|
|
|
|
|
|
|
Implement default archive name
|
|
|
|
|
|
|
|
Fix imports
|
|
|
|
|
|
|
|
Implement runner function
|
|
|
|
|
|
|
|
Remove targets data collector function as it is now called by a module instead
|
|
|
|
|
|
|
|
Add external method decorator marker
|
|
|
|
|
|
|
|
Add utility class for detecting exportable methods
|
|
|
|
|
|
|
|
Mark run method as an external function
|
|
|
|
|
|
|
|
Implement function setter
|
|
|
|
|
|
|
|
Fix imports
|
|
|
|
|
|
|
|
Setup config from __opts__
|
|
|
|
|
|
|
|
Use utility class
|
|
|
|
|
|
|
|
Remove utils class
|
|
|
|
|
|
|
|
Allow specify profile from the API parameter directly
|
|
|
|
|
|
|
|
Rename module by virtual name
|
|
|
|
|
|
|
|
Bypass parent subclass
|
|
|
|
|
|
|
|
Implement profiles listing (local only for now)
|
|
|
|
|
|
|
|
Specify profile from the state/call
|
|
|
|
|
|
|
|
Set default or personalised archive name
|
|
|
|
|
|
|
|
Add archives lister
|
|
|
|
|
|
|
|
Add personalised name element to the archive name
|
|
|
|
|
|
|
|
Use proper args/kwargs to the exported function
|
|
|
|
|
|
|
|
Add archives deletion function
|
|
|
|
|
|
|
|
Change log level when debugging rendered profiles
|
|
|
|
|
|
|
|
Add ability to directly pass profile source when taking local data
|
|
|
|
|
|
|
|
Add pillar profile support
|
|
|
|
|
|
|
|
Remove extra-line
|
|
|
|
|
|
|
|
Fix header
|
|
|
|
|
|
|
|
Change output format for deleting archives
|
|
|
|
|
|
|
|
Refactor logger output format
|
|
|
|
|
|
|
|
Add time/milliseconds to each log notification
|
|
|
|
|
|
|
|
Fix imports
|
|
|
|
|
|
|
|
Switch output destination by context
|
|
|
|
|
|
|
|
Add last archive function
|
|
|
|
|
|
|
|
Lintfix
|
|
|
|
|
|
|
|
Return consistent type
|
|
|
|
|
|
|
|
Change output format for deleted archives report
|
|
|
|
|
|
|
|
Implement report archive syncing to the reporting node
|
|
|
|
|
|
|
|
Send multiple files at once via rsync, instead of send one after another
|
|
|
|
|
|
|
|
Add sync stats formatter
|
|
|
|
|
|
|
|
Change signature: cleanup -> move. Update docstring.
|
|
|
|
|
|
|
|
Flush empty data from the output format
|
|
|
|
|
|
|
|
Report archfiles activity
|
|
|
|
|
|
|
|
Refactor imports
|
|
|
|
|
|
|
|
Do not remove retcode if it is EX_OK
|
|
|
|
|
|
|
|
Do not raise rsync error for undefined archives.
|
|
|
|
|
|
|
|
Update header
|
|
|
|
|
|
|
|
Add salt-support state module
|
|
|
|
|
|
|
|
Move all functions into a callable class object
|
|
|
|
|
|
|
|
Support __call__ function in state and command modules as default entrance that does not need to be specified in SLS state syntax
|
|
|
|
|
|
|
|
Access from the outside only allowed class methods
|
|
|
|
|
|
|
|
Pre-create destination of the archive, preventing single archive copied as a group name
|
|
|
|
|
|
|
|
Handle functions exceptions
|
|
|
|
|
|
|
|
Add unit test scaffold
|
|
|
|
|
|
|
|
Add LogCollector UT for testing regular message
|
|
|
|
|
|
|
|
Add LogCollector UT for testing INFO message
|
|
|
|
|
|
|
|
Add LogCollector UT for testing WARNING message
|
|
|
|
|
|
|
|
Replace hardcoded variables with defined constants
|
|
|
|
|
|
|
|
Add LogCollector UT for testing ERROR message
|
|
|
|
|
|
|
|
Test title attribute in msg method of LogCollector
|
|
|
|
|
|
|
|
Add UT for LogCollector on highlighter method
|
|
|
|
|
|
|
|
Add UT for LogCollector on put method
|
|
|
|
|
|
|
|
Fix docstrings
|
|
|
|
|
|
|
|
Add UT for archive name generator
|
|
|
|
|
|
|
|
Add UT for custom archive name
|
|
|
|
|
|
|
|
Fix docstring for the UT
|
|
|
|
|
|
|
|
Add UT for checking profiles list format
|
|
|
|
|
|
|
|
Add Unit Test for existing archives listing
|
|
|
|
|
|
|
|
Add UT for the last archive function
|
|
|
|
|
|
|
|
Create instance of the support class
|
|
|
|
|
|
|
|
Add UT for successfully deleting all archives
|
|
|
|
|
|
|
|
Add UT for deleting archives with failures
|
|
|
|
|
|
|
|
Add UI for formatting sync stats and order preservation
|
|
|
|
|
|
|
|
Add UT for testing sync failure when no archives has been specified
|
|
|
|
|
|
|
|
Add UT for last picked archive has not found
|
|
|
|
|
|
|
|
Add UT for last specified archive was not found
|
|
|
|
|
|
|
|
Bugfix: do not create an array with None element in it
|
|
|
|
|
|
|
|
Fix UT for found bugfix
|
|
|
|
|
|
|
|
Add UT for syncing no archives failure
|
|
|
|
|
|
|
|
Add UT for sync function
|
|
|
|
|
|
|
|
Add UT for run support function
|
|
|
|
|
|
|
|
Fix docstring for function "run"
|
|
|
|
|
|
|
|
lintfix: use 'salt.support.mock' and 'patch()'
|
|
|
|
|
|
|
|
Rewrite subdirectory creation and do not rely on Python3-only code
|
|
|
|
|
|
|
|
Lintfix: remove unused imports
|
|
|
|
|
|
|
|
Lintfix: regexp strings
|
|
|
|
|
|
|
|
Break-down oneliner if/else clause
|
|
|
|
|
|
|
|
Use ordered dictionary to preserve order of the state.
|
|
|
|
|
|
|
|
This has transparent effect to the current process: OrderedDict is the
|
|
|
|
same as just Python dict, except it is preserving order of the state
|
|
|
|
chunks.
|
|
|
|
|
|
|
|
Refactor state processing class.
|
|
|
|
|
|
|
|
Add __call__ function to process single-id syntax
|
|
|
|
|
|
|
|
Add backward-compatibility with default SLS syntax (id-per-call)
|
|
|
|
|
|
|
|
Lintfix: E1120 no value in argument 'name' for class constructor
|
|
|
|
|
|
|
|
Remove unused import
|
|
|
|
|
|
|
|
Check last function by full name
|
|
|
|
---
|
2021-01-08 13:41:50 +01:00
|
|
|
doc/ref/modules/all/index.rst | 1 +
|
|
|
|
doc/ref/states/all/index.rst | 1 +
|
2019-01-17 10:18:02 +01:00
|
|
|
salt/cli/support/__init__.py | 2 +-
|
2021-01-08 13:41:50 +01:00
|
|
|
salt/cli/support/collector.py | 14 +-
|
2019-01-17 10:18:02 +01:00
|
|
|
salt/loader.py | 6 +-
|
2021-01-08 13:41:50 +01:00
|
|
|
salt/modules/saltsupport.py | 405 ++++++++++++++++++++
|
|
|
|
salt/state.py | 38 +-
|
|
|
|
salt/states/saltsupport.py | 225 +++++++++++
|
|
|
|
salt/utils/args.py | 23 +-
|
|
|
|
salt/utils/decorators/__init__.py | 68 ++--
|
|
|
|
tests/unit/modules/test_saltsupport.py | 496 +++++++++++++++++++++++++
|
|
|
|
11 files changed, 1220 insertions(+), 59 deletions(-)
|
2019-01-17 10:18:02 +01:00
|
|
|
create mode 100644 salt/modules/saltsupport.py
|
|
|
|
create mode 100644 salt/states/saltsupport.py
|
|
|
|
create mode 100644 tests/unit/modules/test_saltsupport.py
|
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
diff --git a/doc/ref/modules/all/index.rst b/doc/ref/modules/all/index.rst
|
|
|
|
index 4c93972276..9fea7af07f 100644
|
|
|
|
--- a/doc/ref/modules/all/index.rst
|
|
|
|
+++ b/doc/ref/modules/all/index.rst
|
|
|
|
@@ -415,6 +415,7 @@ execution modules
|
|
|
|
salt_version
|
|
|
|
saltcheck
|
|
|
|
saltcloudmod
|
|
|
|
+ saltsupport
|
|
|
|
saltutil
|
|
|
|
schedule
|
|
|
|
scp_mod
|
|
|
|
diff --git a/doc/ref/states/all/index.rst b/doc/ref/states/all/index.rst
|
|
|
|
index 2664b4ce45..052efe4582 100644
|
|
|
|
--- a/doc/ref/states/all/index.rst
|
|
|
|
+++ b/doc/ref/states/all/index.rst
|
|
|
|
@@ -281,6 +281,7 @@ state modules
|
|
|
|
rvm
|
|
|
|
salt_proxy
|
|
|
|
saltmod
|
|
|
|
+ saltsupport
|
|
|
|
saltutil
|
|
|
|
schedule
|
|
|
|
selinux
|
2019-01-17 10:18:02 +01:00
|
|
|
diff --git a/salt/cli/support/__init__.py b/salt/cli/support/__init__.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index 4fdf44186f..59c2609e07 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/cli/support/__init__.py
|
|
|
|
+++ b/salt/cli/support/__init__.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -47,7 +47,7 @@ def get_profile(profile, caller, runner):
|
2019-01-17 10:18:02 +01:00
|
|
|
if os.path.exists(profile_path):
|
|
|
|
try:
|
|
|
|
rendered_template = _render_profile(profile_path, caller, runner)
|
2021-01-08 13:41:50 +01:00
|
|
|
- log.trace("\n{d}\n{t}\n{d}\n".format(d="-" * 80, t=rendered_template))
|
|
|
|
+ log.debug("\n{d}\n{t}\n{d}\n".format(d="-" * 80, t=rendered_template))
|
2019-01-17 10:18:02 +01:00
|
|
|
data.update(yaml.load(rendered_template))
|
|
|
|
except Exception as ex:
|
|
|
|
log.debug(ex, exc_info=True)
|
|
|
|
diff --git a/salt/cli/support/collector.py b/salt/cli/support/collector.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index a08a0b8c6e..1879cc5220 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/cli/support/collector.py
|
|
|
|
+++ b/salt/cli/support/collector.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -362,7 +362,7 @@ class SaltSupport(salt.utils.parsers.SaltSupportOptionParser):
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
- def collect_local_data(self):
|
|
|
|
+ def collect_local_data(self, profile=None, profile_source=None):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
Collects master system data.
|
|
|
|
:return:
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -388,8 +388,8 @@ class SaltSupport(salt.utils.parsers.SaltSupportOptionParser):
|
|
|
|
self._local_run({"fun": func, "arg": args, "kwarg": kwargs})
|
|
|
|
)
|
2019-01-17 10:18:02 +01:00
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
- scenario = salt.cli.support.get_profile(
|
|
|
|
- self.config["support_profile"], call, run
|
|
|
|
+ scenario = profile_source or salt.cli.support.get_profile(
|
|
|
|
+ profile or self.config["support_profile"], call, run
|
|
|
|
)
|
2019-01-17 10:18:02 +01:00
|
|
|
for category_name in scenario:
|
|
|
|
self.out.put(category_name)
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -441,13 +441,6 @@ class SaltSupport(salt.utils.parsers.SaltSupportOptionParser):
|
2019-01-17 10:18:02 +01:00
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
return action_name.split(":")[0] or None
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
- def collect_targets_data(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
- """
|
2019-01-17 10:18:02 +01:00
|
|
|
- Collects minion targets data
|
|
|
|
- :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
- """
|
2019-01-17 10:18:02 +01:00
|
|
|
- # TODO: remote collector?
|
|
|
|
-
|
|
|
|
def _cleanup(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
Cleanup if crash/exception
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -551,7 +544,6 @@ class SaltSupport(salt.utils.parsers.SaltSupportOptionParser):
|
2019-01-17 10:18:02 +01:00
|
|
|
self.collector.open()
|
|
|
|
self.collect_local_data()
|
|
|
|
self.collect_internal_data()
|
|
|
|
- self.collect_targets_data()
|
|
|
|
self.collector.close()
|
|
|
|
|
|
|
|
archive_path = self.collector.archive_path
|
|
|
|
diff --git a/salt/loader.py b/salt/loader.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index 8232ed632e..1ee40712e5 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/loader.py
|
|
|
|
+++ b/salt/loader.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -1843,8 +1843,10 @@ class LazyLoader(salt.utils.lazy.LazyDict):
|
|
|
|
}
|
2019-01-17 10:18:02 +01:00
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
for attr in getattr(mod, "__load__", dir(mod)):
|
|
|
|
- if attr.startswith("_"):
|
2019-01-17 10:18:02 +01:00
|
|
|
- # private functions are skipped
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if attr.startswith("_") and attr != "__call__":
|
2019-01-17 10:18:02 +01:00
|
|
|
+ # private functions are skipped,
|
|
|
|
+ # except __call__ which is default entrance
|
|
|
|
+ # for multi-function batch-like state syntax
|
|
|
|
continue
|
|
|
|
func = getattr(mod, attr)
|
|
|
|
if not inspect.isfunction(func) and not isinstance(func, functools.partial):
|
|
|
|
diff --git a/salt/modules/saltsupport.py b/salt/modules/saltsupport.py
|
|
|
|
new file mode 100644
|
2021-01-08 13:41:50 +01:00
|
|
|
index 0000000000..e800e3bf1f
|
2019-01-17 10:18:02 +01:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/salt/modules/saltsupport.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -0,0 +1,405 @@
|
2019-01-17 10:18:02 +01:00
|
|
|
+#
|
|
|
|
+# Author: Bo Maryniuk <bo@suse.de>
|
|
|
|
+#
|
|
|
|
+# Copyright 2018 SUSE LLC
|
|
|
|
+# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
+# you may not use this file except in compliance with the License.
|
|
|
|
+# You may obtain a copy of the License at
|
|
|
|
+#
|
|
|
|
+# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
+#
|
|
|
|
+# Unless required by applicable law or agreed to in writing, software
|
|
|
|
+# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
+# See the License for the specific language governing permissions and
|
|
|
|
+# limitations under the License.
|
2021-01-08 13:41:50 +01:00
|
|
|
+"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+:codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
|
|
|
|
+
|
|
|
|
+Module to run salt-support within Salt.
|
2021-01-08 13:41:50 +01:00
|
|
|
+"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+# pylint: disable=W0231,W0221
|
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+import datetime
|
|
|
|
+import logging
|
2019-01-17 10:18:02 +01:00
|
|
|
+import os
|
2021-01-08 13:41:50 +01:00
|
|
|
+import re
|
2019-01-17 10:18:02 +01:00
|
|
|
+import sys
|
2021-01-08 13:41:50 +01:00
|
|
|
+import tempfile
|
2019-01-17 10:18:02 +01:00
|
|
|
+import time
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+import salt.cli.support
|
2019-01-17 10:18:02 +01:00
|
|
|
+import salt.cli.support.intfunc
|
2021-01-08 13:41:50 +01:00
|
|
|
+import salt.defaults.exitcodes
|
|
|
|
+import salt.exceptions
|
2019-01-17 10:18:02 +01:00
|
|
|
+import salt.utils.decorators
|
2021-01-08 13:41:50 +01:00
|
|
|
+import salt.utils.dictupdate
|
|
|
|
+import salt.utils.odict
|
2019-01-17 10:18:02 +01:00
|
|
|
+import salt.utils.path
|
|
|
|
+import salt.utils.stringutils
|
|
|
|
+from salt.cli.support.collector import SaltSupport, SupportDataCollector
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+__virtualname__ = "support"
|
2019-01-17 10:18:02 +01:00
|
|
|
+log = logging.getLogger(__name__)
|
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+class LogCollector:
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Output collector.
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ INFO = "info"
|
|
|
|
+ WARNING = "warning"
|
|
|
|
+ ERROR = "error"
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ class MessagesList(list):
|
|
|
|
+ def append(self, obj):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ list.append(
|
|
|
|
+ self,
|
|
|
|
+ "{} - {}".format(
|
|
|
|
+ datetime.datetime.utcnow().strftime("%T.%f")[:-3], obj
|
|
|
|
+ ),
|
|
|
|
+ )
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ __call__ = append
|
|
|
|
+
|
|
|
|
+ def __init__(self):
|
|
|
|
+ self.messages = {
|
|
|
|
+ self.INFO: self.MessagesList(),
|
|
|
|
+ self.WARNING: self.MessagesList(),
|
|
|
|
+ self.ERROR: self.MessagesList(),
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def msg(self, message, *args, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ title = kwargs.get("title")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if title:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ message = "{}: {}".format(title, message)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ self.messages[self.INFO](message)
|
|
|
|
+
|
|
|
|
+ def info(self, message, *args, **kwargs):
|
|
|
|
+ self.msg(message)
|
|
|
|
+
|
|
|
|
+ def warning(self, message, *args, **kwargs):
|
|
|
|
+ self.messages[self.WARNING](message)
|
|
|
|
+
|
|
|
|
+ def error(self, message, *args, **kwargs):
|
|
|
|
+ self.messages[self.ERROR](message)
|
|
|
|
+
|
|
|
|
+ def put(self, message, *args, **kwargs):
|
|
|
|
+ self.messages[self.INFO](message)
|
|
|
|
+
|
|
|
|
+ def highlight(self, message, *values, **kwargs):
|
|
|
|
+ self.msg(message.format(*values))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class SaltSupportModule(SaltSupport):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Salt Support module class.
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def __init__(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Constructor
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ self.config = self.setup_config()
|
|
|
|
+
|
|
|
|
+ def setup_config(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Return current configuration
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ return __opts__
|
|
|
|
+
|
|
|
|
+ def _get_archive_name(self, archname=None):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Create default archive name.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+ archname = re.sub("[^a-z0-9]", "", (archname or "").lower()) or "support"
|
|
|
|
+ for grain in ["fqdn", "host", "localhost", "nodename"]:
|
2019-01-17 10:18:02 +01:00
|
|
|
+ host = __grains__.get(grain)
|
|
|
|
+ if host:
|
|
|
|
+ break
|
|
|
|
+ if not host:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ host = "localhost"
|
|
|
|
+
|
|
|
|
+ return os.path.join(
|
|
|
|
+ tempfile.gettempdir(),
|
|
|
|
+ "{hostname}-{archname}-{date}-{time}.bz2".format(
|
|
|
|
+ archname=archname,
|
|
|
|
+ hostname=host,
|
|
|
|
+ date=time.strftime("%Y%m%d"),
|
|
|
|
+ time=time.strftime("%H%M%S"),
|
|
|
|
+ ),
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ @salt.utils.decorators.external
|
|
|
|
+ def profiles(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get list of profiles.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ return {
|
2021-01-08 13:41:50 +01:00
|
|
|
+ "standard": salt.cli.support.get_profiles(self.config),
|
|
|
|
+ "custom": [],
|
2019-01-17 10:18:02 +01:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @salt.utils.decorators.external
|
|
|
|
+ def archives(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get list of existing archives.
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ arc_files = []
|
|
|
|
+ tmpdir = tempfile.gettempdir()
|
|
|
|
+ for filename in os.listdir(tmpdir):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ mtc = re.match(r"\w+-\w+-\d+-\d+\.bz2", filename)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if mtc and len(filename) == mtc.span()[-1]:
|
|
|
|
+ arc_files.append(os.path.join(tmpdir, filename))
|
|
|
|
+
|
|
|
|
+ return arc_files
|
|
|
|
+
|
|
|
|
+ @salt.utils.decorators.external
|
|
|
|
+ def last_archive(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get the last available archive
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ archives = {}
|
|
|
|
+ for archive in self.archives():
|
2021-01-08 13:41:50 +01:00
|
|
|
+ archives[int(archive.split(".")[0].split("-")[-1])] = archive
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return archives and archives[max(archives)] or None
|
|
|
|
+
|
|
|
|
+ @salt.utils.decorators.external
|
|
|
|
+ def delete_archives(self, *archives):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Delete archives
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ # Remove paths
|
|
|
|
+ _archives = []
|
|
|
|
+ for archive in archives:
|
|
|
|
+ _archives.append(os.path.basename(archive))
|
|
|
|
+ archives = _archives[:]
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret = {"files": {}, "errors": {}}
|
2019-01-17 10:18:02 +01:00
|
|
|
+ for archive in self.archives():
|
|
|
|
+ arc_dir = os.path.dirname(archive)
|
|
|
|
+ archive = os.path.basename(archive)
|
|
|
|
+ if archives and archive in archives or not archives:
|
|
|
|
+ archive = os.path.join(arc_dir, archive)
|
|
|
|
+ try:
|
|
|
|
+ os.unlink(archive)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["files"][archive] = "removed"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ except Exception as err:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["errors"][archive] = str(err)
|
|
|
|
+ ret["files"][archive] = "left"
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return ret
|
|
|
|
+
|
|
|
|
+ def format_sync_stats(self, cnt):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Format stats of the sync output.
|
|
|
|
+
|
|
|
|
+ :param cnt:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ stats = salt.utils.odict.OrderedDict()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if cnt.get("retcode") == salt.defaults.exitcodes.EX_OK:
|
|
|
|
+ for line in cnt.get("stdout", "").split(os.linesep):
|
|
|
|
+ line = line.split(": ")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if len(line) == 2:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ stats[line[0].lower().replace(" ", "_")] = line[1]
|
|
|
|
+ cnt["transfer"] = stats
|
|
|
|
+ del cnt["stdout"]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ # Remove empty
|
|
|
|
+ empty_sections = []
|
|
|
|
+ for section in cnt:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if not cnt[section] and section != "retcode":
|
2019-01-17 10:18:02 +01:00
|
|
|
+ empty_sections.append(section)
|
|
|
|
+ for section in empty_sections:
|
|
|
|
+ del cnt[section]
|
|
|
|
+
|
|
|
|
+ return cnt
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ @salt.utils.decorators.depends("rsync")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ @salt.utils.decorators.external
|
|
|
|
+ def sync(self, group, name=None, host=None, location=None, move=False, all=False):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Sync the latest archive to the host on given location.
|
|
|
|
+
|
|
|
|
+ CLI Example:
|
|
|
|
+
|
|
|
|
+ .. code-block:: bash
|
|
|
|
+
|
|
|
|
+ salt '*' support.sync group=test
|
|
|
|
+ salt '*' support.sync group=test name=/tmp/myspecial-12345-67890.bz2
|
|
|
|
+ salt '*' support.sync group=test name=/tmp/myspecial-12345-67890.bz2 host=allmystuff.lan
|
|
|
|
+ salt '*' support.sync group=test name=/tmp/myspecial-12345-67890.bz2 host=allmystuff.lan location=/opt/
|
|
|
|
+
|
|
|
|
+ :param group: name of the local directory to which sync is going to put the result files
|
|
|
|
+ :param name: name of the archive. Latest, if not specified.
|
|
|
|
+ :param host: name of the destination host for rsync. Default is master, if not specified.
|
|
|
|
+ :param location: local destination directory, default temporary if not specified
|
|
|
|
+ :param move: move archive file[s]. Default is False.
|
|
|
|
+ :param all: work with all available archives. Default is False (i.e. latest available)
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ tfh, tfn = tempfile.mkstemp()
|
|
|
|
+ processed_archives = []
|
|
|
|
+ src_uri = uri = None
|
|
|
|
+
|
|
|
|
+ last_arc = self.last_archive()
|
|
|
|
+ if name:
|
|
|
|
+ archives = [name]
|
|
|
|
+ elif all:
|
|
|
|
+ archives = self.archives()
|
|
|
|
+ elif last_arc:
|
|
|
|
+ archives = [last_arc]
|
|
|
|
+ else:
|
|
|
|
+ archives = []
|
|
|
|
+
|
|
|
|
+ for name in archives:
|
|
|
|
+ err = None
|
|
|
|
+ if not name:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ err = "No support archive has been defined."
|
2019-01-17 10:18:02 +01:00
|
|
|
+ elif not os.path.exists(name):
|
|
|
|
+ err = 'Support archive "{}" was not found'.format(name)
|
|
|
|
+ if err is not None:
|
|
|
|
+ log.error(err)
|
|
|
|
+ raise salt.exceptions.SaltInvocationError(err)
|
|
|
|
+
|
|
|
|
+ if not uri:
|
|
|
|
+ src_uri = os.path.dirname(name)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ uri = "{host}:{loc}".format(
|
|
|
|
+ host=host or __opts__["master"],
|
|
|
|
+ loc=os.path.join(location or tempfile.gettempdir(), group),
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ os.write(tfh, salt.utils.stringutils.to_bytes(os.path.basename(name)))
|
|
|
|
+ os.write(tfh, salt.utils.stringutils.to_bytes(os.linesep))
|
|
|
|
+ processed_archives.append(name)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ log.debug("Syncing {filename} to {uri}".format(filename=name, uri=uri))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ os.close(tfh)
|
|
|
|
+
|
|
|
|
+ if not processed_archives:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ raise salt.exceptions.SaltInvocationError("No archives found to transfer.")
|
|
|
|
+
|
|
|
|
+ ret = __salt__["rsync.rsync"](
|
|
|
|
+ src=src_uri,
|
|
|
|
+ dst=uri,
|
|
|
|
+ additional_opts=["--stats", "--files-from={}".format(tfn)],
|
|
|
|
+ )
|
|
|
|
+ ret["files"] = {}
|
2019-01-17 10:18:02 +01:00
|
|
|
+ for name in processed_archives:
|
|
|
|
+ if move:
|
|
|
|
+ salt.utils.dictupdate.update(ret, self.delete_archives(name))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ log.debug("Deleting {filename}".format(filename=name))
|
|
|
|
+ ret["files"][name] = "moved"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ else:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["files"][name] = "copied"
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ os.unlink(tfn)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ except OSError as err:
|
|
|
|
+ log.error(
|
|
|
|
+ "Cannot remove temporary rsync file {fn}: {err}".format(fn=tfn, err=err)
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return self.format_sync_stats(ret)
|
|
|
|
+
|
|
|
|
+ @salt.utils.decorators.external
|
2021-01-08 13:41:50 +01:00
|
|
|
+ def run(self, profile="default", pillar=None, archive=None, output="nested"):
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Run Salt Support on the minion.
|
|
|
|
+
|
|
|
|
+ profile
|
|
|
|
+ Set available profile name. Default is "default".
|
|
|
|
+
|
|
|
|
+ pillar
|
|
|
|
+ Set available profile from the pillars.
|
|
|
|
+
|
|
|
|
+ archive
|
|
|
|
+ Override archive name. Default is "support". This results to "hostname-support-YYYYMMDD-hhmmss.bz2".
|
|
|
|
+
|
|
|
|
+ output
|
|
|
|
+ Change the default outputter. Default is "nested".
|
|
|
|
+
|
|
|
|
+ CLI Example:
|
|
|
|
+
|
|
|
|
+ .. code-block:: bash
|
|
|
|
+
|
|
|
|
+ salt '*' support.run
|
|
|
|
+ salt '*' support.run profile=network
|
|
|
|
+ salt '*' support.run pillar=something_special
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ class outputswitch:
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Output switcher on context
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def __init__(self, output_device):
|
|
|
|
+ self._tmp_out = output_device
|
|
|
|
+ self._orig_out = None
|
|
|
|
+
|
|
|
|
+ def __enter__(self):
|
|
|
|
+ self._orig_out = salt.cli.support.intfunc.out
|
|
|
|
+ salt.cli.support.intfunc.out = self._tmp_out
|
|
|
|
+
|
|
|
|
+ def __exit__(self, *args):
|
|
|
|
+ salt.cli.support.intfunc.out = self._orig_out
|
|
|
|
+
|
|
|
|
+ self.out = LogCollector()
|
|
|
|
+ with outputswitch(self.out):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ self.collector = SupportDataCollector(
|
|
|
|
+ archive or self._get_archive_name(archname=archive), output
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ self.collector.out = self.out
|
|
|
|
+ self.collector.open()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ self.collect_local_data(
|
|
|
|
+ profile=profile, profile_source=__pillar__.get(pillar)
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ self.collect_internal_data()
|
|
|
|
+ self.collector.close()
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ return {"archive": self.collector.archive_path, "messages": self.out.messages}
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+def __virtual__():
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Set method references as module functions aliases
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = SaltSupportModule()
|
|
|
|
+
|
|
|
|
+ def _set_function(obj):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Create a Salt function for the SaltSupport class.
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def _cmd(*args, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Call support method as a function from the Salt.
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ _kwargs = {}
|
|
|
|
+ for kw in kwargs:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if not kw.startswith("__"):
|
2019-01-17 10:18:02 +01:00
|
|
|
+ _kwargs[kw] = kwargs[kw]
|
|
|
|
+ return obj(*args, **_kwargs)
|
2021-01-08 13:41:50 +01:00
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ _cmd.__doc__ = obj.__doc__
|
|
|
|
+ return _cmd
|
|
|
|
+
|
|
|
|
+ for m_name in dir(support):
|
|
|
|
+ obj = getattr(support, m_name)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if getattr(obj, "external", False):
|
2019-01-17 10:18:02 +01:00
|
|
|
+ setattr(sys.modules[__name__], m_name, _set_function(obj))
|
|
|
|
+
|
|
|
|
+ return __virtualname__
|
|
|
|
diff --git a/salt/state.py b/salt/state.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index beab2cb16c..b1bce4e0cd 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/state.py
|
|
|
|
+++ b/salt/state.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -1547,7 +1547,9 @@ class State:
|
2019-01-17 10:18:02 +01:00
|
|
|
names = []
|
2021-01-08 13:41:50 +01:00
|
|
|
if state.startswith("__"):
|
2019-01-17 10:18:02 +01:00
|
|
|
continue
|
2021-01-08 13:41:50 +01:00
|
|
|
- chunk = {"state": state, "name": name}
|
2019-01-17 10:18:02 +01:00
|
|
|
+ chunk = OrderedDict()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ chunk["state"] = state
|
|
|
|
+ chunk["name"] = name
|
2019-01-17 10:18:02 +01:00
|
|
|
if orchestration_jid is not None:
|
2021-01-08 13:41:50 +01:00
|
|
|
chunk["__orchestration_jid__"] = orchestration_jid
|
|
|
|
if "__sls__" in body:
|
|
|
|
@@ -2150,9 +2152,16 @@ class State:
|
2019-01-17 10:18:02 +01:00
|
|
|
ret = self.call_parallel(cdata, low)
|
|
|
|
else:
|
|
|
|
self.format_slots(cdata)
|
2021-01-08 13:41:50 +01:00
|
|
|
- ret = self.states[cdata["full"]](
|
|
|
|
- *cdata["args"], **cdata["kwargs"]
|
|
|
|
- )
|
|
|
|
+ if cdata["full"].split(".")[-1] == "__call__":
|
2019-01-17 10:18:02 +01:00
|
|
|
+ # __call__ requires OrderedDict to preserve state order
|
|
|
|
+ # kwargs are also invalid overall
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret = self.states[cdata["full"]](
|
|
|
|
+ cdata["args"], module=None, state=cdata["kwargs"]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ else:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret = self.states[cdata["full"]](
|
|
|
|
+ *cdata["args"], **cdata["kwargs"]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
self.states.inject_globals = {}
|
2021-01-08 13:41:50 +01:00
|
|
|
if (
|
|
|
|
"check_cmd" in low
|
|
|
|
@@ -3188,10 +3197,31 @@ class State:
|
2019-01-17 10:18:02 +01:00
|
|
|
running.update(errors)
|
|
|
|
return running
|
|
|
|
|
|
|
|
+ def inject_default_call(self, high):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Sets .call function to a state, if not there.
|
|
|
|
+
|
|
|
|
+ :param high:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ for chunk in high:
|
|
|
|
+ state = high[chunk]
|
|
|
|
+ for state_ref in state:
|
|
|
|
+ needs_default = True
|
|
|
|
+ for argset in state[state_ref]:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if isinstance(argset, str):
|
2019-01-17 10:18:02 +01:00
|
|
|
+ needs_default = False
|
|
|
|
+ break
|
|
|
|
+ if needs_default:
|
|
|
|
+ order = state[state_ref].pop(-1)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ state[state_ref].append("__call__")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ state[state_ref].append(order)
|
|
|
|
+
|
|
|
|
def call_high(self, high, orchestration_jid=None):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
Process a high data call and ensure the defined states.
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+ self.inject_default_call(high)
|
|
|
|
errors = []
|
|
|
|
# If there is extension data reconcile it
|
|
|
|
high, ext_errors = self.reconcile_extend(high)
|
|
|
|
diff --git a/salt/states/saltsupport.py b/salt/states/saltsupport.py
|
|
|
|
new file mode 100644
|
2021-01-08 13:41:50 +01:00
|
|
|
index 0000000000..fb0c9e0372
|
2019-01-17 10:18:02 +01:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/salt/states/saltsupport.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -0,0 +1,225 @@
|
2019-01-17 10:18:02 +01:00
|
|
|
+#
|
|
|
|
+# Author: Bo Maryniuk <bo@suse.de>
|
|
|
|
+#
|
|
|
|
+# Copyright 2018 SUSE LLC
|
|
|
|
+# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
+# you may not use this file except in compliance with the License.
|
|
|
|
+# You may obtain a copy of the License at
|
|
|
|
+#
|
|
|
|
+# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
+#
|
|
|
|
+# Unless required by applicable law or agreed to in writing, software
|
|
|
|
+# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
+# See the License for the specific language governing permissions and
|
|
|
|
+# limitations under the License.
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+r"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+:codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
|
|
|
|
+
|
|
|
|
+Execution of Salt Support from within states
|
|
|
|
+============================================
|
|
|
|
+
|
|
|
|
+State to collect support data from the systems:
|
|
|
|
+
|
|
|
|
+.. code-block:: yaml
|
|
|
|
+
|
|
|
|
+ examine_my_systems:
|
|
|
|
+ support.taken:
|
|
|
|
+ - profile: default
|
|
|
|
+
|
|
|
|
+ support.collected:
|
|
|
|
+ - group: somewhere
|
|
|
|
+ - move: true
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+import logging
|
|
|
|
+import os
|
|
|
|
+import tempfile
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+import salt.exceptions
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+# Import salt modules
|
|
|
|
+import salt.fileclient
|
|
|
|
+import salt.utils.decorators.path
|
|
|
|
+import salt.utils.odict
|
|
|
|
+
|
|
|
|
+log = logging.getLogger(__name__)
|
2021-01-08 13:41:50 +01:00
|
|
|
+__virtualname__ = "support"
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+class SaltSupportState:
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Salt-support.
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ EXPORTED = ["collected", "taken"]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def get_kwargs(self, data):
|
|
|
|
+ kwargs = {}
|
|
|
|
+ for keyset in data:
|
|
|
|
+ kwargs.update(keyset)
|
|
|
|
+
|
|
|
|
+ return kwargs
|
|
|
|
+
|
|
|
|
+ def __call__(self, state):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Call support.
|
|
|
|
+
|
|
|
|
+ :param args:
|
|
|
|
+ :param kwargs:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ret = {
|
2021-01-08 13:41:50 +01:00
|
|
|
+ "name": state.pop("name"),
|
|
|
|
+ "changes": {},
|
|
|
|
+ "result": True,
|
|
|
|
+ "comment": "",
|
2019-01-17 10:18:02 +01:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ out = {}
|
2021-01-08 13:41:50 +01:00
|
|
|
+ functions = ["Functions:"]
|
2019-01-17 10:18:02 +01:00
|
|
|
+ try:
|
|
|
|
+ for ref_func, ref_kwargs in state.items():
|
|
|
|
+ if ref_func not in self.EXPORTED:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ raise salt.exceptions.SaltInvocationError(
|
|
|
|
+ "Function {} is not found".format(ref_func)
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out[ref_func] = getattr(self, ref_func)(**self.get_kwargs(ref_kwargs))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ functions.append(" - {}".format(ref_func))
|
|
|
|
+ ret["comment"] = "\n".join(functions)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ except Exception as ex:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["comment"] = str(ex)
|
|
|
|
+ ret["result"] = False
|
|
|
|
+ ret["changes"] = out
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return ret
|
|
|
|
+
|
|
|
|
+ def check_destination(self, location, group):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Check destination for the archives.
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ # Pre-create destination, since rsync will
|
|
|
|
+ # put one file named as group
|
|
|
|
+ try:
|
|
|
|
+ destination = os.path.join(location, group)
|
|
|
|
+ if os.path.exists(destination) and not os.path.isdir(destination):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ raise salt.exceptions.SaltException(
|
|
|
|
+ 'Destination "{}" should be directory!'.format(destination)
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if not os.path.exists(destination):
|
|
|
|
+ os.makedirs(destination)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ log.debug("Created destination directory for archives: %s", destination)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ else:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ log.debug(
|
|
|
|
+ "Archives destination directory %s already exists", destination
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ except OSError as err:
|
|
|
|
+ log.error(err)
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ def collected(
|
|
|
|
+ self, group, filename=None, host=None, location=None, move=True, all=True
|
|
|
|
+ ):
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Sync archives to a central place.
|
|
|
|
+
|
|
|
|
+ :param name:
|
|
|
|
+ :param group:
|
|
|
|
+ :param filename:
|
|
|
|
+ :param host:
|
|
|
|
+ :param location:
|
|
|
|
+ :param move:
|
|
|
|
+ :param all:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ret = {
|
2021-01-08 13:41:50 +01:00
|
|
|
+ "name": "support.collected",
|
|
|
|
+ "changes": {},
|
|
|
|
+ "result": True,
|
|
|
|
+ "comment": "",
|
2019-01-17 10:18:02 +01:00
|
|
|
+ }
|
|
|
|
+ location = location or tempfile.gettempdir()
|
|
|
|
+ self.check_destination(location, group)
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["changes"] = __salt__["support.sync"](
|
|
|
|
+ group, name=filename, host=host, location=location, move=move, all=all
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return ret
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ def taken(self, profile="default", pillar=None, archive=None, output="nested"):
|
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Takes minion support config data.
|
|
|
|
+
|
|
|
|
+ :param profile:
|
|
|
|
+ :param pillar:
|
|
|
|
+ :param archive:
|
|
|
|
+ :param output:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ret = {
|
2021-01-08 13:41:50 +01:00
|
|
|
+ "name": "support.taken",
|
|
|
|
+ "changes": {},
|
|
|
|
+ "result": True,
|
2019-01-17 10:18:02 +01:00
|
|
|
+ }
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ result = __salt__["support.run"](
|
|
|
|
+ profile=profile, pillar=pillar, archive=archive, output=output
|
|
|
|
+ )
|
|
|
|
+ if result.get("archive"):
|
|
|
|
+ ret[
|
|
|
|
+ "comment"
|
|
|
|
+ ] = "Information about this system has been saved to {} file.".format(
|
|
|
|
+ result["archive"]
|
|
|
|
+ )
|
|
|
|
+ ret["changes"]["archive"] = result["archive"]
|
|
|
|
+ ret["changes"]["messages"] = {}
|
|
|
|
+ for key in ["info", "error", "warning"]:
|
|
|
|
+ if result.get("messages", {}).get(key):
|
|
|
|
+ ret["changes"]["messages"][key] = result["messages"][key]
|
2019-01-17 10:18:02 +01:00
|
|
|
+ else:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ ret["comment"] = ""
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ return ret
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+_support_state = SaltSupportState()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def __call__(*args, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ SLS single-ID syntax processing.
|
|
|
|
+
|
|
|
|
+ module:
|
|
|
|
+ This module reference, equals to sys.modules[__name__]
|
|
|
|
+
|
|
|
|
+ state:
|
|
|
|
+ Compiled state in preserved order. The function supposed to look
|
|
|
|
+ at first level array of functions.
|
|
|
|
+
|
|
|
|
+ :param cdata:
|
|
|
|
+ :param kwargs:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+ return _support_state(kwargs.get("state", {}))
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+def taken(name, profile="default", pillar=None, archive=None, output="nested"):
|
|
|
|
+ return _support_state.taken(
|
|
|
|
+ profile=profile, pillar=pillar, archive=archive, output=output
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+def collected(
|
|
|
|
+ name, group, filename=None, host=None, location=None, move=True, all=True
|
|
|
|
+):
|
|
|
|
+ return _support_state.collected(
|
|
|
|
+ group=group, filename=filename, host=host, location=location, move=move, all=all
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+def __virtual__():
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Salt Support state
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ return __virtualname__
|
|
|
|
diff --git a/salt/utils/args.py b/salt/utils/args.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index 87afdd3597..102402500c 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/utils/args.py
|
|
|
|
+++ b/salt/utils/args.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -1,8 +1,6 @@
|
|
|
|
-# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Functions used for CLI argument handling
|
|
|
|
"""
|
|
|
|
-from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import copy
|
|
|
|
import fnmatch
|
|
|
|
@@ -17,6 +15,7 @@ import salt.utils.jid
|
2019-01-17 10:18:02 +01:00
|
|
|
import salt.utils.versions
|
|
|
|
import salt.utils.yaml
|
2021-01-08 13:41:50 +01:00
|
|
|
from salt.exceptions import SaltInvocationError
|
2019-01-17 10:18:02 +01:00
|
|
|
+from salt.utils.odict import OrderedDict
|
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
@@ -70,9 +69,9 @@ def invalid_kwargs(invalid_kwargs, raise_exc=True):
|
|
|
|
"""
|
|
|
|
if invalid_kwargs:
|
|
|
|
if isinstance(invalid_kwargs, dict):
|
|
|
|
- new_invalid = ["{0}={1}".format(x, y) for x, y in invalid_kwargs.items()]
|
|
|
|
+ new_invalid = ["{}={}".format(x, y) for x, y in invalid_kwargs.items()]
|
|
|
|
invalid_kwargs = new_invalid
|
|
|
|
- msg = "The following keyword arguments are not valid: {0}".format(
|
|
|
|
+ msg = "The following keyword arguments are not valid: {}".format(
|
|
|
|
", ".join(invalid_kwargs)
|
|
|
|
)
|
|
|
|
if raise_exc:
|
|
|
|
@@ -259,7 +258,7 @@ def get_function_argspec(func, is_class_method=None):
|
|
|
|
and this is not always wanted.
|
|
|
|
"""
|
|
|
|
if not callable(func):
|
|
|
|
- raise TypeError("{0} is not a callable".format(func))
|
|
|
|
+ raise TypeError("{} is not a callable".format(func))
|
|
|
|
|
|
|
|
if hasattr(func, "__wrapped__"):
|
|
|
|
func = func.__wrapped__
|
|
|
|
@@ -279,7 +278,7 @@ def get_function_argspec(func, is_class_method=None):
|
|
|
|
try:
|
|
|
|
sig = inspect.signature(func)
|
|
|
|
except TypeError:
|
|
|
|
- raise TypeError("Cannot inspect argument list for '{0}'".format(func))
|
|
|
|
+ raise TypeError("Cannot inspect argument list for '{}'".format(func))
|
|
|
|
else:
|
|
|
|
# argspec-related functions are deprecated in Python 3 in favor of
|
|
|
|
# the new inspect.Signature class, and will be removed at some
|
|
|
|
@@ -439,7 +438,7 @@ def format_call(
|
2019-01-17 10:18:02 +01:00
|
|
|
ret = initial_ret is not None and initial_ret or {}
|
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
ret["args"] = []
|
|
|
|
- ret["kwargs"] = {}
|
|
|
|
+ ret["kwargs"] = OrderedDict()
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
aspec = get_function_argspec(fun, is_class_method=is_class_method)
|
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -470,7 +469,7 @@ def format_call(
|
|
|
|
used_args_count = len(ret["args"]) + len(args)
|
|
|
|
args_count = used_args_count + len(missing_args)
|
|
|
|
raise SaltInvocationError(
|
|
|
|
- "{0} takes at least {1} argument{2} ({3} given)".format(
|
|
|
|
+ "{} takes at least {} argument{} ({} given)".format(
|
|
|
|
fun.__name__, args_count, args_count > 1 and "s" or "", used_args_count
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@@ -506,18 +505,18 @@ def format_call(
|
|
|
|
# In case this is being called for a state module
|
|
|
|
"full",
|
|
|
|
# Not a state module, build the name
|
|
|
|
- "{0}.{1}".format(fun.__module__, fun.__name__),
|
|
|
|
+ "{}.{}".format(fun.__module__, fun.__name__),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
- msg = "{0} and '{1}' are invalid keyword arguments for '{2}'".format(
|
|
|
|
- ", ".join(["'{0}'".format(e) for e in extra][:-1]),
|
|
|
|
+ msg = "{} and '{}' are invalid keyword arguments for '{}'".format(
|
|
|
|
+ ", ".join(["'{}'".format(e) for e in extra][:-1]),
|
|
|
|
list(extra.keys())[-1],
|
|
|
|
ret.get(
|
|
|
|
# In case this is being called for a state module
|
|
|
|
"full",
|
|
|
|
# Not a state module, build the name
|
|
|
|
- "{0}.{1}".format(fun.__module__, fun.__name__),
|
|
|
|
+ "{}.{}".format(fun.__module__, fun.__name__),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-01-17 10:18:02 +01:00
|
|
|
diff --git a/salt/utils/decorators/__init__.py b/salt/utils/decorators/__init__.py
|
2021-01-08 13:41:50 +01:00
|
|
|
index 940d0a90f2..b06cf0abc8 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/utils/decorators/__init__.py
|
|
|
|
+++ b/salt/utils/decorators/__init__.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -1,10 +1,7 @@
|
|
|
|
-# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Helpful decorators for module writing
|
|
|
|
"""
|
|
|
|
|
|
|
|
-# Import python libs
|
|
|
|
-from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import inspect
|
|
|
|
@@ -15,13 +12,10 @@ import time
|
|
|
|
from collections import defaultdict
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
-# Import salt libs
|
|
|
|
import salt.utils.args
|
|
|
|
import salt.utils.data
|
|
|
|
import salt.utils.versions
|
|
|
|
from salt.exceptions import CommandExecutionError, SaltConfigurationError
|
|
|
|
-
|
|
|
|
-# Import 3rd-party libs
|
|
|
|
from salt.ext import six
|
|
|
|
from salt.log import LOG_LEVELS
|
|
|
|
|
|
|
|
@@ -32,7 +26,7 @@ if getattr(sys, "getwindowsversion", False):
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
-class Depends(object):
|
|
|
|
+class Depends:
|
|
|
|
"""
|
|
|
|
This decorator will check the module when it is loaded and check that the
|
|
|
|
dependencies passed in are in the globals of the module. If not, it will
|
|
|
|
@@ -121,7 +115,7 @@ class Depends(object):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run_command(dependency, mod_name, func_name):
|
|
|
|
- full_name = "{0}.{1}".format(mod_name, func_name)
|
|
|
|
+ full_name = "{}.{}".format(mod_name, func_name)
|
|
|
|
log.trace("Running '%s' for '%s'", dependency, full_name)
|
|
|
|
if IS_WINDOWS:
|
|
|
|
args = salt.utils.args.shlex_split(dependency, posix=False)
|
|
|
|
@@ -145,8 +139,8 @@ class Depends(object):
|
|
|
|
It will modify the "functions" dict and remove/replace modules that
|
|
|
|
are missing dependencies.
|
|
|
|
"""
|
|
|
|
- for dependency, dependent_dict in six.iteritems(cls.dependency_dict[kind]):
|
|
|
|
- for (mod_name, func_name), (frame, params) in six.iteritems(dependent_dict):
|
|
|
|
+ for dependency, dependent_dict in cls.dependency_dict[kind].items():
|
|
|
|
+ for (mod_name, func_name), (frame, params) in dependent_dict.items():
|
|
|
|
if mod_name != tgt_mod:
|
|
|
|
continue
|
|
|
|
# Imports from local context take presedence over those from the global context.
|
|
|
|
@@ -232,7 +226,7 @@ class Depends(object):
|
|
|
|
except (AttributeError, KeyError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
- mod_key = "{0}.{1}".format(mod_name, func_name)
|
|
|
|
+ mod_key = "{}.{}".format(mod_name, func_name)
|
|
|
|
|
|
|
|
# if we don't have this module loaded, skip it!
|
|
|
|
if mod_key not in functions:
|
|
|
|
@@ -267,9 +261,7 @@ def timing(function):
|
|
|
|
mod_name = function.__module__[16:]
|
|
|
|
else:
|
|
|
|
mod_name = function.__module__
|
|
|
|
- fstr = "Function %s.%s took %.{0}f seconds to execute".format(
|
|
|
|
- sys.float_info.dig
|
|
|
|
- )
|
|
|
|
+ fstr = "Function %s.%s took %.{}f seconds to execute".format(sys.float_info.dig)
|
|
|
|
log.profile(fstr, mod_name, function.__name__, end_time - start_time)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@@ -291,13 +283,13 @@ def memoize(func):
|
|
|
|
def _memoize(*args, **kwargs):
|
|
|
|
str_args = []
|
|
|
|
for arg in args:
|
|
|
|
- if not isinstance(arg, six.string_types):
|
|
|
|
- str_args.append(six.text_type(arg))
|
|
|
|
+ if not isinstance(arg, str):
|
|
|
|
+ str_args.append(str(arg))
|
|
|
|
else:
|
|
|
|
str_args.append(arg)
|
|
|
|
|
|
|
|
args_ = ",".join(
|
|
|
|
- list(str_args) + ["{0}={1}".format(k, kwargs[k]) for k in sorted(kwargs)]
|
|
|
|
+ list(str_args) + ["{}={}".format(k, kwargs[k]) for k in sorted(kwargs)]
|
|
|
|
)
|
|
|
|
if args_ not in cache:
|
|
|
|
cache[args_] = func(*args, **kwargs)
|
|
|
|
@@ -306,7 +298,7 @@ def memoize(func):
|
|
|
|
return _memoize
|
|
|
|
|
|
|
|
|
|
|
|
-class _DeprecationDecorator(object):
|
|
|
|
+class _DeprecationDecorator:
|
|
|
|
"""
|
|
|
|
Base mix-in class for the deprecation decorator.
|
|
|
|
Takes care of a common functionality, used in its derivatives.
|
|
|
|
@@ -359,7 +351,7 @@ class _DeprecationDecorator(object):
|
|
|
|
try:
|
|
|
|
return self._function(*args, **kwargs)
|
|
|
|
except TypeError as error:
|
|
|
|
- error = six.text_type(error).replace(
|
|
|
|
+ error = str(error).replace(
|
|
|
|
self._function, self._orig_f_name
|
|
|
|
) # Hide hidden functions
|
|
|
|
log.error(
|
|
|
|
@@ -374,7 +366,7 @@ class _DeprecationDecorator(object):
|
|
|
|
self._function.__name__,
|
|
|
|
error,
|
|
|
|
)
|
|
|
|
- six.reraise(*sys.exc_info())
|
|
|
|
+ raise
|
2019-01-17 10:18:02 +01:00
|
|
|
else:
|
2021-01-08 13:41:50 +01:00
|
|
|
raise CommandExecutionError(
|
|
|
|
"Function is deprecated, but the successor function was not found."
|
|
|
|
@@ -626,11 +618,11 @@ class _WithDeprecated(_DeprecationDecorator):
|
|
|
|
|
|
|
|
if use_deprecated and use_superseded:
|
|
|
|
raise SaltConfigurationError(
|
|
|
|
- "Function '{0}' is mentioned both in deprecated "
|
|
|
|
+ "Function '{}' is mentioned both in deprecated "
|
|
|
|
"and superseded sections. Please remove any of that.".format(full_name)
|
|
|
|
)
|
|
|
|
old_function = self._globals.get(
|
|
|
|
- self._with_name or "_{0}".format(function.__name__)
|
|
|
|
+ self._with_name or "_{}".format(function.__name__)
|
|
|
|
)
|
|
|
|
if self._policy == self.OPT_IN:
|
|
|
|
self._function = function if use_superseded else old_function
|
|
|
|
@@ -782,12 +774,30 @@ def ensure_unicode_args(function):
|
|
|
|
|
|
|
|
@wraps(function)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
- if six.PY2:
|
|
|
|
- return function(
|
|
|
|
- *salt.utils.data.decode_list(args),
|
|
|
|
- **salt.utils.data.decode_dict(kwargs)
|
|
|
|
- )
|
|
|
|
- else:
|
|
|
|
- return function(*args, **kwargs)
|
|
|
|
+ return function(*args, **kwargs)
|
|
|
|
|
2019-01-17 10:18:02 +01:00
|
|
|
return wrapped
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def external(func):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Mark function as external.
|
|
|
|
+
|
|
|
|
+ :param func:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def f(*args, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Stub.
|
|
|
|
+
|
|
|
|
+ :param args:
|
|
|
|
+ :param kwargs:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ return func(*args, **kwargs)
|
|
|
|
+
|
|
|
|
+ f.external = True
|
|
|
|
+ f.__doc__ = func.__doc__
|
|
|
|
+
|
|
|
|
+ return f
|
|
|
|
diff --git a/tests/unit/modules/test_saltsupport.py b/tests/unit/modules/test_saltsupport.py
|
|
|
|
new file mode 100644
|
2021-01-08 13:41:50 +01:00
|
|
|
index 0000000000..f9ce7be29a
|
2019-01-17 10:18:02 +01:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/tests/unit/modules/test_saltsupport.py
|
2021-01-08 13:41:50 +01:00
|
|
|
@@ -0,0 +1,496 @@
|
|
|
|
+"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+ :codeauthor: Bo Maryniuk <bo@suse.de>
|
2021-01-08 13:41:50 +01:00
|
|
|
+"""
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+import datetime
|
|
|
|
+
|
|
|
|
+import salt.exceptions
|
|
|
|
+from salt.modules import saltsupport
|
2019-01-17 10:18:02 +01:00
|
|
|
+from tests.support.mixins import LoaderModuleMockMixin
|
2021-01-08 13:41:50 +01:00
|
|
|
+from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2019-01-17 10:18:02 +01:00
|
|
|
+from tests.support.unit import TestCase, skipIf
|
|
|
|
+
|
|
|
|
+try:
|
|
|
|
+ import pytest
|
|
|
|
+except ImportError:
|
|
|
|
+ pytest = None
|
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+@skipIf(not bool(pytest), "Pytest required")
|
2019-01-17 10:18:02 +01:00
|
|
|
+@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
+class SaltSupportModuleTestCase(TestCase, LoaderModuleMockMixin):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test cases for salt.modules.support::SaltSupportModule
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def setup_loader_modules(self):
|
|
|
|
+ return {saltsupport: {}}
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ @patch("tempfile.gettempdir", MagicMock(return_value="/mnt/storage"))
|
|
|
|
+ @patch("salt.modules.saltsupport.__grains__", {"fqdn": "c-3po"})
|
|
|
|
+ @patch("time.strftime", MagicMock(return_value="000"))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_get_archive_name(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test archive name construction.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert support._get_archive_name() == "/mnt/storage/c-3po-support-000-000.bz2"
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ @patch("tempfile.gettempdir", MagicMock(return_value="/mnt/storage"))
|
|
|
|
+ @patch("salt.modules.saltsupport.__grains__", {"fqdn": "c-3po"})
|
|
|
|
+ @patch("time.strftime", MagicMock(return_value="000"))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_get_custom_archive_name(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test get custom archive name.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ temp_name = support._get_archive_name(archname="Darth Wader")
|
|
|
|
+ assert temp_name == "/mnt/storage/c-3po-darthwader-000-000.bz2"
|
|
|
|
+ temp_name = support._get_archive_name(archname="Яйця з сіллю")
|
|
|
|
+ assert temp_name == "/mnt/storage/c-3po-support-000-000.bz2"
|
|
|
|
+ temp_name = support._get_archive_name(archname="!@#$%^&*()Fillip J. Fry")
|
|
|
|
+ assert temp_name == "/mnt/storage/c-3po-fillipjfry-000-000.bz2"
|
|
|
|
+
|
|
|
|
+ @patch(
|
|
|
|
+ "salt.cli.support.get_profiles",
|
|
|
|
+ MagicMock(return_value={"message": "Feature was not beta tested"}),
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_profiles_format(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test profiles format.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
|
|
|
+ profiles = support.profiles()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert "custom" in profiles
|
|
|
|
+ assert "standard" in profiles
|
|
|
|
+ assert "message" in profiles["standard"]
|
|
|
|
+ assert profiles["custom"] == []
|
|
|
|
+ assert profiles["standard"]["message"] == "Feature was not beta tested"
|
|
|
|
+
|
|
|
|
+ @patch("tempfile.gettempdir", MagicMock(return_value="/mnt/storage"))
|
|
|
|
+ @patch(
|
|
|
|
+ "os.listdir",
|
|
|
|
+ MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "one-support-000-000.bz2",
|
|
|
|
+ "two-support-111-111.bz2",
|
|
|
|
+ "trash.bz2",
|
|
|
|
+ "hostname-000-000.bz2",
|
|
|
|
+ "three-support-wrong222-222.bz2",
|
|
|
|
+ "000-support-000-000.bz2",
|
|
|
|
+ ]
|
|
|
|
+ ),
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_get_existing_archives(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get list of existing archives.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
|
|
|
+ out = support.archives()
|
|
|
|
+ assert len(out) == 3
|
2021-01-08 13:41:50 +01:00
|
|
|
+ for name in [
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/000-support-000-000.bz2",
|
|
|
|
+ ]:
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert name in out
|
|
|
|
+
|
|
|
|
+ def test_last_archive(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get last archive name
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
|
|
|
+ assert support.last_archive() == "/mnt/storage/three-support-222-222.bz2"
|
|
|
|
+
|
|
|
|
+ @patch("os.unlink", MagicMock(return_value=True))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_delete_all_archives_success(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test delete archives
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ret = support.delete_archives()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert "files" in ret
|
|
|
|
+ assert "errors" in ret
|
|
|
|
+ assert not bool(ret["errors"])
|
|
|
|
+ assert bool(ret["files"])
|
|
|
|
+ assert isinstance(ret["errors"], dict)
|
|
|
|
+ assert isinstance(ret["files"], dict)
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ for arc in support.archives():
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert ret["files"][arc] == "removed"
|
|
|
|
+
|
|
|
|
+ @patch(
|
|
|
|
+ "os.unlink",
|
|
|
|
+ MagicMock(
|
|
|
|
+ return_value=False,
|
|
|
|
+ side_effect=[
|
|
|
|
+ OSError("Decreasing electron flux"),
|
|
|
|
+ OSError("Solar flares interference"),
|
|
|
|
+ None,
|
|
|
|
+ ],
|
|
|
|
+ ),
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_delete_all_archives_failure(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test delete archives failure
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ret = support.delete_archives()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert "files" in ret
|
|
|
|
+ assert "errors" in ret
|
|
|
|
+ assert bool(ret["errors"])
|
|
|
|
+ assert bool(ret["files"])
|
|
|
|
+ assert isinstance(ret["errors"], dict)
|
|
|
|
+ assert isinstance(ret["files"], dict)
|
|
|
|
+
|
|
|
|
+ assert ret["files"]["/mnt/storage/three-support-222-222.bz2"] == "removed"
|
|
|
|
+ assert ret["files"]["/mnt/storage/one-support-000-000.bz2"] == "left"
|
|
|
|
+ assert ret["files"]["/mnt/storage/two-support-111-111.bz2"] == "left"
|
|
|
|
+
|
|
|
|
+ assert len(ret["errors"]) == 2
|
|
|
|
+ assert (
|
|
|
|
+ ret["errors"]["/mnt/storage/one-support-000-000.bz2"]
|
|
|
|
+ == "Decreasing electron flux"
|
|
|
|
+ )
|
|
|
|
+ assert (
|
|
|
|
+ ret["errors"]["/mnt/storage/two-support-111-111.bz2"]
|
|
|
|
+ == "Solar flares interference"
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_format_sync_stats(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test format rsync stats for preserving ordering of the keys
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ stats = """
|
2019-01-17 10:18:02 +01:00
|
|
|
+robot: Bender
|
|
|
|
+cute: Leela
|
|
|
|
+weird: Zoidberg
|
|
|
|
+professor: Farnsworth
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+ f_stats = support.format_sync_stats({"retcode": 0, "stdout": stats})
|
|
|
|
+ assert list(f_stats["transfer"].keys()) == [
|
|
|
|
+ "robot",
|
|
|
|
+ "cute",
|
|
|
|
+ "weird",
|
|
|
|
+ "professor",
|
|
|
|
+ ]
|
|
|
|
+ assert list(f_stats["transfer"].values()) == [
|
|
|
|
+ "Bender",
|
|
|
|
+ "Leela",
|
|
|
|
+ "Zoidberg",
|
|
|
|
+ "Farnsworth",
|
|
|
|
+ ]
|
|
|
|
+
|
|
|
|
+ @patch("tempfile.mkstemp", MagicMock(return_value=(0, "dummy")))
|
|
|
|
+ @patch("os.close", MagicMock())
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_sync_no_archives_failure(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test sync failed when no archives specified.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
|
|
|
+ support.archives = MagicMock(return_value=[])
|
|
|
|
+
|
|
|
|
+ with pytest.raises(salt.exceptions.SaltInvocationError) as err:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.sync("group-name")
|
|
|
|
+ assert "No archives found to transfer" in str(err)
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ @patch("tempfile.mkstemp", MagicMock(return_value=(0, "dummy")))
|
|
|
|
+ @patch("os.path.exists", MagicMock(return_value=False))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_sync_last_picked_archive_not_found_failure(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test sync failed when archive was not found (last picked)
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ with pytest.raises(salt.exceptions.SaltInvocationError) as err:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.sync("group-name")
|
|
|
|
+ assert (
|
|
|
|
+ ' Support archive "/mnt/storage/three-support-222-222.bz2" was not found'
|
|
|
|
+ in str(err)
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ @patch("tempfile.mkstemp", MagicMock(return_value=(0, "dummy")))
|
|
|
|
+ @patch("os.path.exists", MagicMock(return_value=False))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_sync_specified_archive_not_found_failure(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test sync failed when archive was not found (last picked)
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ with pytest.raises(salt.exceptions.SaltInvocationError) as err:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.sync("group-name", name="lost.bz2")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert ' Support archive "lost.bz2" was not found' in str(err)
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ @patch("tempfile.mkstemp", MagicMock(return_value=(0, "dummy")))
|
|
|
|
+ @patch("os.path.exists", MagicMock(return_value=False))
|
|
|
|
+ @patch("os.close", MagicMock())
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_sync_no_archive_to_transfer_failure(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test sync failed when no archive was found to transfer
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
|
|
|
+ support.archives = MagicMock(return_value=[])
|
|
|
|
+ with pytest.raises(salt.exceptions.SaltInvocationError) as err:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.sync("group-name", all=True)
|
|
|
|
+ assert "No archives found to transfer" in str(err)
|
|
|
|
+
|
|
|
|
+ @patch("tempfile.mkstemp", MagicMock(return_value=(0, "dummy")))
|
|
|
|
+ @patch("os.path.exists", MagicMock(return_value=True))
|
|
|
|
+ @patch("os.close", MagicMock())
|
|
|
|
+ @patch("os.write", MagicMock())
|
|
|
|
+ @patch("os.unlink", MagicMock())
|
|
|
|
+ @patch(
|
|
|
|
+ "salt.modules.saltsupport.__salt__", {"rsync.rsync": MagicMock(return_value={})}
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_sync_archives(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test sync archives
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ support.archives = MagicMock(
|
|
|
|
+ return_value=[
|
|
|
|
+ "/mnt/storage/one-support-000-000.bz2",
|
|
|
|
+ "/mnt/storage/two-support-111-111.bz2",
|
|
|
|
+ "/mnt/storage/three-support-222-222.bz2",
|
|
|
|
+ ]
|
|
|
|
+ )
|
|
|
|
+ out = support.sync("group-name", host="buzz", all=True, move=False)
|
|
|
|
+ assert "files" in out
|
|
|
|
+ for arc_name in out["files"]:
|
|
|
|
+ assert out["files"][arc_name] == "copied"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert saltsupport.os.unlink.call_count == 1
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert saltsupport.os.unlink.call_args_list[0][0][0] == "dummy"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ calls = []
|
|
|
|
+ for call in saltsupport.os.write.call_args_list:
|
|
|
|
+ assert len(call) == 2
|
|
|
|
+ calls.append(call[0])
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert calls == [
|
|
|
|
+ (0, b"one-support-000-000.bz2"),
|
|
|
|
+ (0, b"\n"),
|
|
|
|
+ (0, b"two-support-111-111.bz2"),
|
|
|
|
+ (0, b"\n"),
|
|
|
|
+ (0, b"three-support-222-222.bz2"),
|
|
|
|
+ (0, b"\n"),
|
|
|
|
+ ]
|
|
|
|
+
|
|
|
|
+ @patch("salt.modules.saltsupport.__pillar__", {})
|
|
|
|
+ @patch("salt.modules.saltsupport.SupportDataCollector", MagicMock())
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def test_run_support(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test run support
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+ saltsupport.SupportDataCollector(None, None).archive_path = "dummy"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ support = saltsupport.SaltSupportModule()
|
|
|
|
+ support.collect_internal_data = MagicMock()
|
|
|
|
+ support.collect_local_data = MagicMock()
|
|
|
|
+ out = support.run()
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+ for section in ["messages", "archive"]:
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert section in out
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert out["archive"] == "dummy"
|
|
|
|
+ for section in ["warning", "error", "info"]:
|
|
|
|
+ assert section in out["messages"]
|
2019-01-17 10:18:02 +01:00
|
|
|
+ ld_call = support.collect_local_data.call_args_list[0][1]
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert "profile" in ld_call
|
|
|
|
+ assert ld_call["profile"] == "default"
|
|
|
|
+ assert "profile_source" in ld_call
|
|
|
|
+ assert ld_call["profile_source"] is None
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert support.collector.open.call_count == 1
|
|
|
|
+ assert support.collector.close.call_count == 1
|
|
|
|
+ assert support.collect_internal_data.call_count == 1
|
|
|
|
+
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
+@skipIf(not bool(pytest), "Pytest required")
|
2019-01-17 10:18:02 +01:00
|
|
|
+@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
+class LogCollectorTestCase(TestCase, LoaderModuleMockMixin):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test cases for salt.modules.support::LogCollector
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ def setup_loader_modules(self):
|
|
|
|
+ return {saltsupport: {}}
|
|
|
|
+
|
|
|
|
+ def test_msg(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
|
|
|
+ msg = "Upgrading /dev/null device"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ out.msg(msg, title="Here")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert saltsupport.LogCollector.INFO in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.INFO])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.INFO] == [
|
|
|
|
+ "00:00:00.000 - {}: {}".format("Here", msg)
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_info_message(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test info message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
|
|
|
+ msg = "SIMM crosstalk during tectonic stress"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
|
|
|
+ out.info(msg)
|
|
|
|
+ assert saltsupport.LogCollector.INFO in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.INFO])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.INFO] == [
|
|
|
|
+ "00:00:00.000 - {}".format(msg)
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_put_message(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test put message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
|
|
|
+ msg = "Webmaster kidnapped by evil cult"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
|
|
|
+ out.put(msg)
|
|
|
|
+ assert saltsupport.LogCollector.INFO in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.INFO])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.INFO] == [
|
|
|
|
+ "00:00:00.000 - {}".format(msg)
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_warning_message(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test warning message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
|
|
|
+ msg = "Your e-mail is now being delivered by USPS"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
|
|
|
+ out.warning(msg)
|
|
|
|
+ assert saltsupport.LogCollector.WARNING in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.WARNING])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.WARNING] == [
|
|
|
|
+ "00:00:00.000 - {}".format(msg)
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_error_message(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test error message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
|
|
|
+ msg = "Learning curve appears to be fractal"
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
|
|
|
+ out.error(msg)
|
|
|
|
+ assert saltsupport.LogCollector.ERROR in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.ERROR])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.ERROR] == [
|
|
|
|
+ "00:00:00.000 - {}".format(msg)
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ def test_hl_message(self):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Test highlighter message to the log collector.
|
|
|
|
+
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ utcmock = MagicMock()
|
|
|
|
+ utcmock.utcnow = MagicMock(return_value=datetime.datetime.utcfromtimestamp(0))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ with patch("datetime.datetime", utcmock):
|
2019-01-17 10:18:02 +01:00
|
|
|
+ out = saltsupport.LogCollector()
|
2021-01-08 13:41:50 +01:00
|
|
|
+ out.highlight("The {} TTYs became {} TTYs and vice versa", "real", "pseudo")
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert saltsupport.LogCollector.INFO in out.messages
|
2021-01-08 13:41:50 +01:00
|
|
|
+ assert (
|
|
|
|
+ type(out.messages[saltsupport.LogCollector.INFO])
|
|
|
|
+ == saltsupport.LogCollector.MessagesList
|
|
|
|
+ )
|
|
|
|
+ assert out.messages[saltsupport.LogCollector.INFO] == [
|
|
|
|
+ "00:00:00.000 - The real TTYs became " "pseudo TTYs and vice versa"
|
|
|
|
+ ]
|
2019-01-17 10:18:02 +01:00
|
|
|
--
|
2021-01-08 13:41:50 +01:00
|
|
|
2.29.2
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
|