ipa-backup/restore: remove remaining chdir calls

Closes: https://pagure.io/freeipa/issue/7416
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
This commit is contained in:
Peter Keresztes Schmidt 2020-06-01 22:33:15 +02:00 committed by Rob Crittenden
parent 61c71e4a62
commit cf8ef6fd2d
2 changed files with 7 additions and 30 deletions

View File

@ -306,7 +306,6 @@ class Backup(admintool.AdminTool):
self.header = os.path.join(self.top_dir, 'header')
cwd = os.getcwd()
try:
dirsrv = services.knownservices.dirsrv
@ -359,10 +358,6 @@ class Backup(admintool.AdminTool):
options.gpg_keyring)
finally:
try:
os.chdir(cwd)
except Exception as e:
logger.error('Cannot change directory to %s: %s', cwd, e)
shutil.rmtree(self.top_dir)
def check_roles(self, raiseonerr=True):
@ -761,11 +756,10 @@ class Backup(admintool.AdminTool):
'Unexpected error: %s' % e
)
os.chdir(self.dir)
args = [
'tar', '--xattrs', '--selinux', '-czf', filename, '.'
]
result = run(args, raiseonerr=False)
result = run(args, raiseonerr=False, cwd=self.dir)
if result.returncode != 0:
raise admintool.ScriptError(
'tar returned non-zero code %s: %s' %

View File

@ -357,8 +357,6 @@ class Restore(admintool.AdminTool):
os.chmod(self.dir, 0o750)
os.chown(self.dir, pent.pw_uid, pent.pw_gid)
cwd = os.getcwd()
logger.info("Temporary setting umask to 022")
old_umask = os.umask(0o022)
try:
@ -483,10 +481,6 @@ class Restore(admintool.AdminTool):
if result.returncode != 0:
logger.error('Restarting IPA failed: %s', result.error_log)
finally:
try:
os.chdir(cwd)
except Exception as e:
logger.error('Cannot change directory to %s: %s', cwd, e)
shutil.rmtree(self.top_dir)
logger.info("Restoring umask to %s", old_umask)
os.umask(old_umask)
@ -708,7 +702,6 @@ class Restore(admintool.AdminTool):
if result.returncode != 0:
logger.critical("bak2db failed: %s", result.error_log)
def restore_default_conf(self):
'''
Restore paths.IPA_DEFAULT_CONF to temporary directory.
@ -716,8 +709,6 @@ class Restore(admintool.AdminTool):
Primary purpose of this method is to get configuration for api
finalization when restoring ipa after uninstall.
'''
cwd = os.getcwd()
os.chdir(self.dir)
args = ['tar',
'--xattrs',
'--selinux',
@ -725,12 +716,11 @@ class Restore(admintool.AdminTool):
os.path.join(self.dir, 'files.tar'),
paths.IPA_DEFAULT_CONF[1:],
]
result = run(args, raiseonerr=False, cwd=self.dir)
result = run(args, raiseonerr=False)
if result.returncode != 0:
logger.critical('Restoring %s failed: %s',
paths.IPA_DEFAULT_CONF, result.error_log)
os.chdir(cwd)
def remove_old_files(self):
"""
@ -770,8 +760,6 @@ class Restore(admintool.AdminTool):
databases.
'''
logger.info("Restoring files")
cwd = os.getcwd()
os.chdir('/')
args = ['tar',
'--xattrs',
'--selinux',
@ -782,13 +770,10 @@ class Restore(admintool.AdminTool):
args.append('--exclude')
args.append('var/log')
result = run(args, raiseonerr=False)
result = run(args, cwd='/', raiseonerr=False)
if result.returncode != 0:
logger.critical('Restoring files failed: %s', result.error_log)
os.chdir(cwd)
def read_header(self):
'''
Read the backup file header that contains the meta data about
@ -831,8 +816,6 @@ class Restore(admintool.AdminTool):
logger.info('Decrypting %s', filename)
filename = decrypt_file(self.dir, filename)
os.chdir(self.dir)
args = ['tar',
'--xattrs',
'--selinux',
@ -840,7 +823,7 @@ class Restore(admintool.AdminTool):
filename,
'.'
]
run(args)
run(args, cwd=self.dir)
pent = pwd.getpwnam(constants.DS_USER)
os.chown(self.top_dir, pent.pw_uid, pent.pw_gid)