[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ssh arguments in config file #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions btrfs_sxbackup/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class Configuration:
__KEY_DEST_RETENTION = 'destination-retention'
__KEY_LOG_IDENT = 'log-ident'
__key_EMAIL_RECIPIENT = 'email-recipient'
__KEY_SSH_OPTIONS = 'ssh-options'

def __init__(self):
self.__source_retention = None
self.__destination_retention = None
self.__log_ident = None
self.__email_recipient = None
self.__ssh_options = None

@staticmethod
def instance():
Expand Down Expand Up @@ -80,6 +82,10 @@ def log_ident(self):
def email_recipient(self):
return self.__email_recipient

@property
def ssh_options(self):
return self.__ssh_options

def read(self):
cparser = ConfigParser()

Expand All @@ -93,6 +99,7 @@ def read(self):
self.__destination_retention = RetentionExpression(dest_retention_str) if dest_retention_str else None
self.__log_ident = cparser.get(self.__SECTION_NAME, self.__KEY_LOG_IDENT, fallback=None)
self.__email_recipient = cparser.get(self.__SECTION_NAME, self.__key_EMAIL_RECIPIENT, fallback=None)
self.__ssh_options = cparser.get(self.__SECTION_NAME, self.__KEY_SSH_OPTIONS, fallback=None)


class Location:
Expand All @@ -108,8 +115,9 @@ def __init__(self, url: parse.SplitResult):
self.__url = None

self.url = url

self.filesystem = Filesystem(self.build_path(), self.url)
self.ssh_options = Configuration.instance().ssh_options

self.filesystem = Filesystem(self.build_path(), self.url, self.ssh_options)

@property
def url(self) -> parse.SplitResult:
Expand Down Expand Up @@ -157,23 +165,23 @@ def exec_check_output(self, cmd) -> bytes:
:param cmd: Command to execute
:return: output
"""
return shell.exec_check_output(cmd, self.url)
return shell.exec_check_output(cmd, self.url, ssh_options=self.ssh_options)

def exec_call(self, cmd) -> int:
"""
Wrapper for shell.exec_call
:param cmd: Command to execute
:return: returncode
"""
return shell.exec_call(cmd, self.url)
return shell.exec_call(cmd, self.url, ssh_options=self.ssh_options)

def build_subprocess_args(self, cmd) -> list:
"""
Wrapper for shell.create_subprocess_args, autmoatically passing location url
Wrapper for shell.create_subprocess_args, automatically passing location url
:param cmd: Command to execute
:return: subprocess args
"""
return shell.build_subprocess_args(cmd, self.url)
return shell.build_subprocess_args(cmd, self.url, ssh_options=self.ssh_options)

def build_path(self, path: str = '') -> str:
"""
Expand Down
5 changes: 3 additions & 2 deletions btrfs_sxbackup/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ class Filesystem:

__regex = re.compile('uuid: .*\\n')

def __init__(self, path, url=None):
def __init__(self, path, url=None, ssh_options=None):
self.__path = os.path.abspath(path)
self.url = url
self.ssh_options = ssh_options

@property
def path(self):
Expand All @@ -177,7 +178,7 @@ def uuid(self):
currentpath = self.__path
for x in range(0, len(currentpath.split(os.path.sep))):
try:
ret = shell.exec_check_output('btrfs fi show %s' % currentpath, self.url)
ret = shell.exec_check_output('btrfs fi show %s' % currentpath, self.url, self.ssh_options)
except Exception as e:
pass
else:
Expand Down
24 changes: 16 additions & 8 deletions btrfs_sxbackup/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@

import subprocess
import logging
import shlex


_logger = logging.getLogger(__name__)


def build_subprocess_args(cmd, url=None):
def build_subprocess_args(cmd, url=None, ssh_options=None):
"""
Create subprocess arguments for shell command/args to be executed
Internally Wraps command into ssh call if url host name is not None
:param cmd: Shell command string or argument list
:param url: url of remote host
:param ssh_options: list of options for ssh
:return: Subprocess arguments
"""
# in case cmd is a regular value, convert to list
cmd = cmd if isinstance(cmd, list) else [cmd]
# wrap into bash or ssh command respectively
# depending if command is executed locally (host==None) or remotely
url_string = None
ssh_args = ['ssh', '-o', 'ServerAliveInterval=5', '-o', 'ServerAliveCountMax=3']
if ssh_options:
ssh_options = shlex.split(ssh_options)
ssh_options = ssh_options or ['-o', 'ServerAliveInterval=5', '-o', 'ServerAliveCountMax=3']
ssh_args = ['ssh'] + ssh_options

if url is not None and url.hostname is not None:
url_string = url.hostname
Expand All @@ -44,34 +49,37 @@ def build_subprocess_args(cmd, url=None):
return subprocess_args


def exec_check_output(cmd, url=None) -> bytes:
def exec_check_output(cmd, url=None, ssh_options=None) -> bytes:
"""
Wrapper for subprocess.check_output
:param cmd: Command text
:param url: URL
:param ssh_options: list of options for ssh
:return: output
"""
return subprocess.check_output(build_subprocess_args(cmd, url), stderr=subprocess.STDOUT)
return subprocess.check_output(build_subprocess_args(cmd, url, ssh_options), stderr=subprocess.STDOUT)


def exec_call(cmd, url=None) -> int:
def exec_call(cmd, url=None, ssh_options=None) -> int:
"""
Wrapper for subprocess.call
:param cmd: Command text
:param url: URL
:param ssh_options: list of options for ssh
:return:
"""
return subprocess.call(build_subprocess_args(cmd, url), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return subprocess.call(build_subprocess_args(cmd, url, ssh_options), stderr=subprocess.PIPE, stdout=subprocess.PIPE)


def exists(command, url=None):
def exists(command, url=None, ssh_options=None):
"""
Check if shell command exists
:param command: Command to verify
:param url: url of remote host
:param ssh_options: list of options for ssh
:return: True if location exists, otherwise False
"""
type_prc = subprocess.Popen(build_subprocess_args(['type ' + command], url),
type_prc = subprocess.Popen(build_subprocess_args(['type ' + command], url, ssh_options),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
Expand Down
1 change: 1 addition & 0 deletions etc/btrfs-sxbackup.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ destination-retention = 10
source-retention = 10
email-recipient =
log-ident =
ssh-options = -o ServerAliveInterval=5 -o ServerAliveCountMax=3