Newer Older
f16725e 3 months ago History
83 lines | 2.364kb
Bogdan Timofte authored 3 months ago
1
#!/bin/bash
2

            
3
set -euo pipefail
4

            
5
PROJECT_ID="thunderbolts"
6
ORG_ID="xdev"
7
INSTALL_DIR="/usr/local/lib/${ORG_ID}/${PROJECT_ID}"
8
DOC_DIR="/usr/local/share/doc/${ORG_ID}/${PROJECT_ID}"
9
RECOVER_WRAPPER="/usr/local/sbin/tb-recover.sh"
10
UNINSTALL_WRAPPER="/usr/local/sbin/${ORG_ID}-${PROJECT_ID}-uninstall"
11
UDEV_RULE_PATH="/etc/udev/rules.d/90-thunderbolt-net-systemd.rules"
12
TB_BRIDGE_UNIT="/etc/systemd/system/tb-bridge.service"
13
TB_ENLIST_UNIT="/etc/systemd/system/tb-enlist@.service"
14
TB_RECOVER_UNIT="/etc/systemd/system/tb-recover.service"
15
TB_RECOVER_TIMER="/etc/systemd/system/tb-recover.timer"
16

            
17
FORCE_MODE=0
18

            
19
log() {
20
    if [[ "${FORCE_MODE}" -eq 0 ]]; then
21
        echo "$@"
22
    fi
23
}
24

            
25
require_root() {
26
    if [[ "${EUID}" -ne 0 ]]; then
27
        echo "ERROR: this script must be run as root" >&2
28
        exit 1
29
    fi
30
}
31

            
32
stop_enlist_instances() {
33
    local units
34
    units="$(systemctl list-units --all 'tb-enlist@*.service' --no-legend --no-pager 2>/dev/null | awk '{print $1}')"
35
    if [[ -n "${units}" ]]; then
36
        # shellcheck disable=SC2086
37
        systemctl stop ${units} >/dev/null 2>&1 || true
38
    fi
39
}
40

            
41
main() {
42
    while [[ $# -gt 0 ]]; do
43
        case "$1" in
44
            --force)
45
                FORCE_MODE=1
46
                shift
47
                ;;
48
            -h|--help)
49
                echo "Usage: $0 [--force]"
50
                exit 0
51
                ;;
52
            *)
53
                echo "ERROR: unknown option: $1" >&2
54
                exit 1
55
                ;;
56
        esac
57
    done
58

            
59
    require_root
60

            
61
    log "=== Uninstalling ${PROJECT_ID} shared runtime ==="
62

            
63
    stop_enlist_instances
64
    systemctl disable --now tb-recover.timer >/dev/null 2>&1 || true
65
    systemctl stop tb-recover.service >/dev/null 2>&1 || true
66
    systemctl disable tb-bridge.service >/dev/null 2>&1 || true
67
    systemctl stop tb-bridge.service >/dev/null 2>&1 || true
68

            
69
    rm -f "${TB_RECOVER_TIMER}" "${TB_RECOVER_UNIT}" "${TB_ENLIST_UNIT}" "${TB_BRIDGE_UNIT}" "${UDEV_RULE_PATH}"
70
    rm -f "${UNINSTALL_WRAPPER}" "${RECOVER_WRAPPER}"
71
    rm -rf "${DOC_DIR}" "${INSTALL_DIR}"
72

            
73
    systemctl daemon-reload
74
    udevadm control --reload-rules
75

            
76
    rmdir /usr/local/lib/${ORG_ID} 2>/dev/null || true
77
    rmdir /usr/local/share/doc/${ORG_ID} 2>/dev/null || true
78

            
79
    log "Shared runtime removed."
80
    log "Network interface configuration was left untouched."
81
}
82

            
83
main "$@"