Mini Shell
"""bakmgr update cron"""
import os
import sys
import subprocess
from argparse import ArgumentParser
from pathlib import Path
import distro
try:
from .setup import download_restic
DEPS_READY = True
except ImportError:
DEPS_READY = False
def main():
"""Entry point for /opt/bakmgr/bin/bakmgr-update"""
if os.getuid() != 0:
sys.exit('This tool must run as root')
args = parse_args()
# get the */site-packages/bakmgr folder
module_root = Path(__file__).resolve().parent.parent
if DEPS_READY:
download_restic()
if args.deps:
update_dependencies(module_root, args.force)
return
changed = update_bakmgr(args.force)
if changed or args.force:
update_dependencies(module_root, args.force)
print('running /opt/bakmgr/bin/bakmgr-setup')
subprocess.call(['/opt/bakmgr/bin/bakmgr-setup'])
def parse_args():
"""Parse CLI args to /opt/bakmgr/bin/bakmgr-update"""
parser = ArgumentParser()
parser.add_argument(
'--force', action='store_true', help='Force reinstallation'
)
parser.add_argument(
'--deps', action='store_true', help='Only update dependencies'
)
return parser.parse_args()
def get_bakauth_version() -> str:
"""Get version of bakauth module"""
ret = subprocess.run(
['/opt/bakmgr/bin/pip', 'freeze'],
stdout=subprocess.PIPE,
encoding='utf-8',
check=False,
)
for line in ret.stdout.splitlines():
if line.startswith('bakmgr=='):
return line.split('==', 1)[1].strip()
return ''
def update_bakmgr(force: bool) -> bool:
"""Update the bakmgr module"""
repo = 'https://repo-ded.inmotionhosting.com/pypi'
# get the version of bakmgr prior to upgrade
start_ver = get_bakauth_version()
# try to upgrade bakmgr from the IMH ded pypi repo
if force:
pip_install('--force-reinstall', '-i', repo, 'bakmgr')
else:
pip_install('-U', '-i', repo, 'bakmgr')
# return whether the version changed
return get_bakauth_version() != start_ver
def update_dependencies(module_root: Path, force: bool):
"""re-check and install dependencies"""
dist_id = distro.id()
print(distro.info())
name = 'requirements.txt'
if dist_id in ('ubuntu', 'debian'):
name = f"{dist_id}_{distro.codename()}.txt"
elif dist_id in ('centos', 'almalinux'):
name = f"{dist_id}_{distro.major_version()}.txt"
elif dist_id == 'opensuse':
name = f"{dist_id}_{distro.major_version()}.txt"
require_file = module_root / 'deps' / name
if not require_file.is_file():
require_file = module_root / 'deps' / 'requirements.txt'
if force:
pip_install('--force-reinstall', '-r', str(require_file))
else:
pip_install('-U', '-r', str(require_file))
def pip_install(*args):
"""Executes pip install with the given args"""
cmd = ['/opt/bakmgr/bin/pip', 'install']
cmd.extend(args)
print('running', *cmd)
subprocess.call(cmd)
if __name__ == '__main__':
main()
Zerion Mini Shell 1.0