summarylogtreecommitdiffstats
path: root/maze.install
blob: a5ee1ed34b00defb1386377d54063b18ea5bb7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# Maze Network post-install hooks
# Handles venv creation, Python package install/upgrade, and the helper daemon.

_INSTALL_DIR="/opt/maze"
_VENV="$_INSTALL_DIR/venv"
_PIP="$_VENV/bin/pip"

_PACKAGES=(
    "scapy>=2.5.0"
    "httpx>=0.27.0"
    "PyQt6>=6.6.0"
    "qasync>=0.27.0"
)

# ── helpers ───────────────────────────────────────────────────────────────────

_info()  { echo "  [maze] $*"; }
_warn()  { echo "  [maze] WARNING: $*"; }
_ok()    { echo "  [maze] ✓ $*"; }

_setup_venv() {
    _info "Creating Python virtual environment at $_VENV ..."
    python3 -m venv "$_VENV"

    _info "Upgrading pip ..."
    "$_PIP" install --quiet --upgrade pip
}

_install_packages() {
    _info "Installing Python packages into venv ..."
    "$_PIP" install --quiet "${_PACKAGES[@]}"
    _ok "Core packages installed"
}

_install_dbus_python() {
    # Try pip first; fall back to linking system python-dbus into the venv
    if "$_PIP" install --quiet "dbus-python>=1.3.2" 2>/dev/null; then
        _ok "dbus-python installed via pip"
        return
    fi

    _warn "dbus-python pip build failed — linking system python-dbus ..."

    local sys_site venv_site
    sys_site=$(python3 -c \
        "import sysconfig; print(sysconfig.get_path('purelib'))" 2>/dev/null)
    venv_site=$("$_VENV/bin/python3" -c \
        "import sysconfig; print(sysconfig.get_path('purelib'))" 2>/dev/null)

    if [[ -z "$sys_site" || -z "$venv_site" ]]; then
        _warn "Could not determine site-packages paths — dbus integration may be unavailable"
        return
    fi

    local linked=0
    for item in dbus _dbus_bindings.so _dbus_glib_bindings.so; do
        if [[ -e "$sys_site/$item" ]]; then
            ln -sf "$sys_site/$item" "$venv_site/"
            (( linked++ )) || true
        fi
    done

    if (( linked > 0 )); then
        _ok "Linked system python-dbus into venv ($linked item(s))"
    else
        _warn "System python-dbus not found — NetworkManager integration unavailable"
    fi
}

_fix_permissions() {
    chown -R root:root "$_INSTALL_DIR"
    find "$_INSTALL_DIR" -type d  -exec chmod 755 {} +
    find "$_INSTALL_DIR" -type f  -exec chmod 644 {} +
    find "$_VENV/bin"   -type f  -exec chmod 755 {} + 2>/dev/null || true
    chmod 755 "$_INSTALL_DIR/main.py"
    chmod 755 "$_INSTALL_DIR/maze/helper.py"
}

_refresh_desktop() {
    command -v gtk-update-icon-cache &>/dev/null && \
        gtk-update-icon-cache -f -t /usr/share/icons/hicolor 2>/dev/null || true
    command -v update-desktop-database &>/dev/null && \
        update-desktop-database /usr/share/applications 2>/dev/null || true
}

_setup_group() {
    # systemd-sysusers normally creates 'maze' from /usr/lib/sysusers.d/maze.conf;
    # fall back to groupadd in case the hook did not run.
    getent group maze >/dev/null 2>&1 || groupadd -r maze 2>/dev/null || true
}

_enable_daemon() {
    systemctl daemon-reload 2>/dev/null || true
    systemctl enable --now maze.service 2>/dev/null \
        && _ok "maze.service enabled and started" \
        || _warn "Could not enable maze.service automatically — run: sudo systemctl enable --now maze.service"
}

_print_summary() {
    echo ""
    echo "  ┌──────────────────────────────────────────────────────────┐"
    echo "  │  Maze Network installed — run: maze                       │"
    echo "  │                                                          │"
    echo "  │  The privileged helper now runs as a daemon — NO password │"
    echo "  │  prompt. To use it, join the 'maze' group and re-login:    │"
    echo "  │                                                          │"
    echo "  │    sudo usermod -aG maze \$USER                            │"
    echo "  │    # then log out and back in (or run: newgrp maze)       │"
    echo "  │                                                          │"
    echo "  │  Autostart launches Maze Network hidden in the tray.      │"
    echo "  └──────────────────────────────────────────────────────────┘"
    echo ""
}

# ── hooks ─────────────────────────────────────────────────────────────────────

post_install() {
    _setup_venv
    _install_packages
    _install_dbus_python
    _fix_permissions
    _refresh_desktop
    _setup_group
    _enable_daemon
    _print_summary
}

post_upgrade() {
    if [[ ! -x "$_VENV/bin/python3" ]]; then
        # venv missing or broken — recreate fully
        rm -rf "$_VENV"
        post_install
        return
    fi

    _info "Upgrading Python packages in venv ..."
    "$_PIP" install --quiet --upgrade "${_PACKAGES[@]}"
    "$_PIP" install --quiet --upgrade "dbus-python>=1.3.2" 2>/dev/null || true
    _fix_permissions
    _refresh_desktop
    _setup_group
    # Pick up unit changes and restart the running helper.
    systemctl daemon-reload 2>/dev/null || true
    systemctl try-restart maze.service 2>/dev/null || true
    _ok "Maze Network upgraded"
}

pre_remove() {
    # Stop and disable the daemon before its files disappear.
    systemctl disable --now maze.service 2>/dev/null || true
}

post_remove() {
    systemctl daemon-reload 2>/dev/null || true
    rm -rf /run/maze 2>/dev/null || true
    # venv is inside /opt/maze which pacman already removed; just refresh caches
    _refresh_desktop
}