Skip to content

Commit

Permalink
[collect] Node and Inherit config files
Browse files Browse the repository at this point in the history
Signed-off-by: Trevor Benson <[email protected]>
  • Loading branch information
TrevorBenson committed Nov 26, 2024
1 parent 4f1b3a9 commit 30909a4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sos/collector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class SoSCollector(SoSComponent):
'encrypt_pass': '',
'group': None,
'image': '',
'inherit_config_file': None,
'force_pull_image': True,
'skip_cleaning_files': [],
'jobs': 4,
Expand All @@ -102,6 +103,7 @@ class SoSCollector(SoSComponent):
'map_file': '/etc/sos/cleaner/default_mapping',
'primary': '',
'namespaces': None,
'node_config_file': None,
'nodes': [],
'no_env_vars': False,
'no_local': False,
Expand Down Expand Up @@ -463,6 +465,15 @@ def add_parser_options(cls, parser):
choices=['auto', 'https', 'ftp', 'sftp',
's3'],
help="Manually specify the upload protocol")
collect_grp.add_argument('--inherit-config-file', type=str,
default=None,
help='Path to a local config file to'
' copy to the remote node and use with'
' sos report')
collect_grp.add_argument('--node-config-file', type=str,
default=None,
help='Path to a config file on the'
' remote node to use with sos report')

# Group the cleaner options together
cleaner_grp = parser.add_argument_group(
Expand Down
13 changes: 13 additions & 0 deletions sos/collector/sosnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __init__(self, address, commons, password=None, local_sudo=None,
self.hostlen = commons['hostlen']
self.need_sudo = commons['need_sudo']
self.sos_options = commons['sos_options']
self.node_config_file = self.opts.node_config_file
self.inherit_config_file = self.opts.inherit_config_file
self.local = False
self.host = None
self.cluster = None
Expand Down Expand Up @@ -762,6 +764,17 @@ def execute_sos_command(self):
try:
path = False
checksum = False
config_file_arg = ''
if self.opts.node_config_file:
config_file_arg = f'--config-file={self.opts.node_config_file}'
elif self.opts.inherit_config_file:
if not self.local:
remote_config_path = os.path.join('/var/tmp/', 'remote_sos.conf')
self._transport.copy_file_to_remote(self.opts.inherit_config_file, remote_config_path)
config_file_arg = f'--config-file={remote_config_path}'
else:
config_file_arg = f'--config-file={self.opts.inherit_config_file}'
self.sos_cmd = f"{self.sos_cmd} {config_file_arg}" if config_file_arg else self.sos_cmd
res = self.run_command(self.sos_cmd,
timeout=self.opts.timeout,
use_shell=True,
Expand Down
32 changes: 32 additions & 0 deletions sos/collector/transports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,38 @@ def _get_hostname(self):
self.log_info(f"Hostname set to {self._hostname}")
return self._hostname

def copy_file_to_remote(self, fname, dest):
"""Copy a local file, fname, to dest on the remote node
:param fname: The name of the file to copy
:type fname: ``str``
:param dest: Where to save the file to remotely
:type dest: ``str``
:returns: True if file was successfully copied to remote, or False
:rtype: ``bool``
"""
print(f"[RemoteTransport.copy_file_to_remote line 367]: Copying {fname} to {self.address}:{dest}")
attempts = 0
try:
while attempts < 5:
attempts += 1
ret = self._copy_file_to_remote(fname, dest)
if ret:
return True
self.log_info(f"File copy attempt {attempts} failed")
self.log_info("File copy failed after 5 attempts")
return False
except Exception as err:
self.log_error("Exception encountered during config copy attempt "
f"{attempts} for {fname}: {err}")
raise err

def _copy_file_to_remote(self, fname, dest):
raise NotImplementedError(
f"Transport {self.name} does not support file copying")

def retrieve_file(self, fname, dest):
"""Copy a remote file, fname, to dest on the local node
Expand Down
6 changes: 6 additions & 0 deletions sos/collector/transports/control_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ def remote_exec(self):
f"{self.opts.ssh_user}@{self.address}")
return self.ssh_cmd

def _copy_file_to_remote(self, fname, dest):
cmd = (f"/usr/bin/scp -oControlPath={self.control_path} "
f"{fname} {self.opts.ssh_user}@{self.address}:{dest}")
res = sos_get_command_output(cmd)
return res['status'] == 0

def _retrieve_file(self, fname, dest):
cmd = (f"/usr/bin/scp -oControlPath={self.control_path} "
f"{self.opts.ssh_user}@{self.address}:{fname} {dest}")
Expand Down

0 comments on commit 30909a4

Please sign in to comment.