Mini Shell

Direktori : /opt/bakmgr/lib64/python3.6/site-packages/bakmgr/inst/
Upload File :
Current File : //opt/bakmgr/lib64/python3.6/site-packages/bakmgr/inst/uninstall.py

"""bakmgr uninstaller"""
import os
import sys
import shutil
from argparse import ArgumentParser
from pathlib import Path
from subprocess import call, check_call, CalledProcessError


def main():
    """Entry point for /opt/bakmgr/bin/bakmgr-uninstall"""
    if os.getuid() != 0:
        sys.exit('This tool must run as root')
    parser = ArgumentParser(description='bakmgr uninstall tool')
    parser.add_argument(
        '-y', '--yes', action='store_true', help='auto-confirm uninstallation'
    )
    args = parser.parse_args()
    msg = 'This will completely remove bakmgr from the system. Confirm? [y/N] '
    if not args.yes and input(msg).strip().lower() not in ('y', 'yes'):
        sys.exit('Canceled on user input')
    # These are left behind:
    # * /etc/bakmgr/auth.json (bakauth registration)
    # * /etc/bakmgr/bakmgr.yaml (custom user configuration)
    # * /usr/bin/restic (just in case)
    remove = [
        '/etc/bakmgr/example.yaml',
        '/opt/bakmgr',
        '/etc/cron.d/bakmgr',
        '/etc/logrotate.d/bakmgr',
        '/usr/bin/bakmgr',
        '/etc/bash_completion.d/bakmgr',
    ]
    for path in remove:
        print('Removing', path, '...')
        path = Path(path)
        if path.is_file():
            try:
                path.unlink()
            except Exception as exc:
                print(exc, file=sys.stderr)
        elif path.is_dir():
            shutil.rmtree(path, onerror=_rmtree_err)
        elif path.exists():
            print('Unknown Error', file=sys.stderr)
    print('Restarting crond ...')
    try:
        check_call(['killall', '-HUP', '-r', '^crond?$'])
    except (FileNotFoundError, CalledProcessError):
        print('Failed to restart crond', file=sys.stderr)
    service_remove()


def service_remove():
    init = Path('/sbin/init').resolve().name
    if init == 'systemd' and Path('/usr/lib/systemd/system').is_dir():
        print("Removing systemd service")
        try:
            call(['systemctl', 'stop', 'bakmgr.service'])
            call(['systemctl', 'disable', 'bakmgr.service'])
        except OSError:
            pass
        try:
            os.remove('/usr/lib/systemd/system/bakmgr.service')
        except OSError:
            pass
        try:
            call(['systemctl', 'daemon-reload'])
        except OSError:
            pass
    elif init == 'upstart' and Path('/etc/init').is_dir():
        print("Removing upstart servce")
        try:
            call(['stop', 'bakmgr'])
        except OSError:
            pass
        try:
            os.remove('/etc/init/bakmgr.conf')
        except OSError:
            pass
    elif Path('/etc/init.d').is_dir():
        print("Removing sysv service")
        try:
            call(['chkconfig', '--del', 'bakmgr'])
        except OSError:
            pass
        try:
            call(['service', 'bakmgr', 'stop'])
        except OSError:
            pass
        try:
            os.remove('/etc/init.d/bakmgr')
        except OSError:
            pass


def _rmtree_err(_func, _path, excinfo):
    print(excinfo[1], file=sys.stderr)


if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0