Skip to content

Commit 3949e2e

Browse files
authored
Refactor: Post-PMG-Script (#7212)
1 parent ab4560b commit 3949e2e

File tree

1 file changed

+210
-65
lines changed

1 file changed

+210
-65
lines changed

tools/pve/post-pmg-install.sh

Lines changed: 210 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,113 +43,258 @@ msg_error() {
4343
echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
4444
}
4545

46+
if ! grep -q "Proxmox Mail Gateway" /etc/issue 2>/dev/null; then
47+
msg_error "This script is only intended for Proxmox Mail Gateway"
48+
exit 1
49+
fi
50+
51+
repo_state() {
52+
# $1 = repo name (e.g. pmg-enterprise, pmg-no-subscription, pmgtest)
53+
local repo="$1"
54+
local file=""
55+
local state="missing"
56+
for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
57+
[[ -f "$f" ]] || continue
58+
if grep -q "$repo" "$f"; then
59+
file="$f"
60+
if grep -qE "^[^#].*${repo}" "$f"; then
61+
state="active"
62+
elif grep -qE "^#.*${repo}" "$f"; then
63+
state="disabled"
64+
fi
65+
break
66+
fi
67+
done
68+
echo "$state $file"
69+
}
70+
4671
start_routines() {
4772
header_info
4873
VERSION="$(awk -F'=' '/^VERSION_CODENAME=/{ print $NF }' /etc/os-release)"
49-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG SOURCES" --menu "This will set the correct sources to update and install Proxmox Mail Gateway.\n \nChange to Proxmox Mail Gateway sources?" 14 58 2 \
74+
75+
# ---- SOURCES ----
76+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG SOURCES" --menu \
77+
"This will set the correct Debian sources for Proxmox Mail Gateway.\n\nCorrect sources?" 14 58 2 \
5078
"yes" " " \
5179
"no" " " 3>&2 2>&1 1>&3)
5280
case $CHOICE in
5381
yes)
54-
msg_info "Changing to Proxmox Mail Gateway Sources"
82+
msg_info "Correcting Debian Sources"
5583
cat <<EOF >/etc/apt/sources.list
5684
deb http://deb.debian.org/debian ${VERSION} main contrib
5785
deb http://deb.debian.org/debian ${VERSION}-updates main contrib
5886
deb http://security.debian.org/debian-security ${VERSION}-security main contrib
5987
EOF
60-
msg_ok "Changed to Proxmox Mail Gateway Sources"
61-
;;
62-
no)
63-
msg_error "Selected no to Correcting Proxmox Mail Gateway Sources"
88+
msg_ok "Corrected Debian Sources"
6489
;;
90+
no) msg_error "Selected no to Correcting Debian Sources" ;;
6591
esac
6692

67-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-ENTERPRISE" --menu "The 'pmg-enterprise' repository is only available to users who have purchased a Proxmox Mail Gateway subscription.\n \nDisable 'pmg-enterprise' repository?" 14 58 2 \
68-
"yes" " " \
69-
"no" " " 3>&2 2>&1 1>&3)
70-
case $CHOICE in
71-
yes)
72-
msg_info "Disabling 'pmg-enterprise' repository"
73-
cat <<EOF >/etc/apt/sources.list.d/pmg-enterprise.list
74-
# deb https://enterprise.proxmox.com/debian/pmg ${VERSION} pmg-enterprise
75-
EOF
76-
msg_ok "Disabled 'pmg-enterprise' repository"
93+
# ---- PMG-ENTERPRISE ----
94+
read -r state file <<<"$(repo_state pmg-enterprise)"
95+
case $state in
96+
active)
97+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-ENTERPRISE" \
98+
--menu "'pmg-enterprise' repository is currently ENABLED.\n\nWhat do you want to do?" 14 58 3 \
99+
"keep" "Keep as is" \
100+
"disable" "Comment out (disable)" \
101+
"delete" "Delete repo file" \
102+
3>&2 2>&1 1>&3)
103+
case $CHOICE in
104+
keep) msg_ok "Kept 'pmg-enterprise' repository" ;;
105+
disable)
106+
msg_info "Disabling 'pmg-enterprise' repository"
107+
sed -i "s/^[^#].*pmg-enterprise/# &/" "$file"
108+
msg_ok "Disabled 'pmg-enterprise' repository"
109+
;;
110+
delete)
111+
msg_info "Deleting 'pmg-enterprise' repository file"
112+
rm -f "$file"
113+
msg_ok "Deleted 'pmg-enterprise' repository file"
114+
;;
115+
esac
77116
;;
78-
no)
79-
msg_error "Selected no to disabling 'pmg-enterprise' repository"
117+
disabled)
118+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-ENTERPRISE" \
119+
--menu "'pmg-enterprise' repository is currently DISABLED.\n\nWhat do you want to do?" 14 58 3 \
120+
"enable" "Uncomment (enable)" \
121+
"keep" "Keep disabled" \
122+
"delete" "Delete repo file" \
123+
3>&2 2>&1 1>&3)
124+
case $CHOICE in
125+
enable)
126+
msg_info "Enabling 'pmg-enterprise' repository"
127+
sed -i "s/^#.*pmg-enterprise/deb/" "$file"
128+
msg_ok "Enabled 'pmg-enterprise' repository"
129+
;;
130+
keep) msg_ok "Kept 'pmg-enterprise' repository disabled" ;;
131+
delete)
132+
msg_info "Deleting 'pmg-enterprise' repository file"
133+
rm -f "$file"
134+
msg_ok "Deleted 'pmg-enterprise' repository file"
135+
;;
136+
esac
137+
;;
138+
missing)
139+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-ENTERPRISE" \
140+
--menu "Add 'pmg-enterprise' repository?\n\nOnly for subscription customers." 14 58 2 \
141+
"no" " " \
142+
"yes" " " \
143+
--default-item "no" \
144+
3>&2 2>&1 1>&3)
145+
case $CHOICE in
146+
yes)
147+
msg_info "Adding 'pmg-enterprise' repository"
148+
cat >/etc/apt/sources.list.d/pmg-enterprise.list <<EOF
149+
deb https://enterprise.proxmox.com/debian/pmg ${VERSION} pmg-enterprise
150+
EOF
151+
msg_ok "Added 'pmg-enterprise' repository"
152+
;;
153+
no) msg_error "Selected no to Adding 'pmg-enterprise' repository" ;;
154+
esac
80155
;;
81156
esac
82157

83-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-NO-SUBSCRIPTION" --menu "The 'pmg-no-subscription' repository provides access to all of the open-source components of Proxmox Mail Gateway.\n \nEnable 'pmg-no-subscription' repository?" 14 58 2 \
84-
"yes" " " \
85-
"no" " " 3>&2 2>&1 1>&3)
86-
case $CHOICE in
87-
yes)
88-
msg_info "Enabling 'pmg-no-subscription' repository"
89-
cat <<EOF >/etc/apt/sources.list.d/pmg-install-repo.list
158+
# ---- PMG-NO-SUBSCRIPTION ----
159+
read -r state file <<<"$(repo_state pmg-no-subscription)"
160+
case $state in
161+
active)
162+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-NO-SUBSCRIPTION" \
163+
--menu "'pmg-no-subscription' repository is currently ENABLED.\n\nWhat do you want to do?" 14 58 3 \
164+
"keep" "Keep as is" \
165+
"disable" "Comment out (disable)" \
166+
"delete" "Delete repo file" \
167+
3>&2 2>&1 1>&3)
168+
case $CHOICE in
169+
keep) msg_ok "Kept 'pmg-no-subscription' repository" ;;
170+
disable)
171+
msg_info "Disabling 'pmg-no-subscription' repository"
172+
sed -i "s/^[^#].*pmg-no-subscription/# &/" "$file"
173+
msg_ok "Disabled 'pmg-no-subscription' repository"
174+
;;
175+
delete)
176+
msg_info "Deleting 'pmg-no-subscription' repository file"
177+
rm -f "$file"
178+
msg_ok "Deleted 'pmg-no-subscription' repository file"
179+
;;
180+
esac
181+
;;
182+
disabled)
183+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-NO-SUBSCRIPTION" \
184+
--menu "'pmg-no-subscription' repository is currently DISABLED.\n\nWhat do you want to do?" 14 58 3 \
185+
"enable" "Uncomment (enable)" \
186+
"keep" "Keep disabled" \
187+
"delete" "Delete repo file" \
188+
3>&2 2>&1 1>&3)
189+
case $CHOICE in
190+
enable)
191+
msg_info "Enabling 'pmg-no-subscription' repository"
192+
sed -i "s/^#.*pmg-no-subscription/deb/" "$file"
193+
msg_ok "Enabled 'pmg-no-subscription' repository"
194+
;;
195+
keep) msg_ok "Kept 'pmg-no-subscription' repository disabled" ;;
196+
delete)
197+
msg_info "Deleting 'pmg-no-subscription' repository file"
198+
rm -f "$file"
199+
msg_ok "Deleted 'pmg-no-subscription' repository file"
200+
;;
201+
esac
202+
;;
203+
missing)
204+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG-NO-SUBSCRIPTION" \
205+
--menu "Add 'pmg-no-subscription' repository?" 14 58 2 \
206+
"yes" " " \
207+
"no" " " \
208+
3>&2 2>&1 1>&3)
209+
case $CHOICE in
210+
yes)
211+
msg_info "Adding 'pmg-no-subscription' repository"
212+
cat >/etc/apt/sources.list.d/pmg-install-repo.list <<EOF
90213
deb http://download.proxmox.com/debian/pmg ${VERSION} pmg-no-subscription
91214
EOF
92-
msg_ok "Enabled 'pmg-no-subscription' repository"
93-
;;
94-
no)
95-
msg_error "Selected no to enabling 'pmg-no-subscription' repository"
215+
msg_ok "Added 'pmg-no-subscription' repository"
216+
;;
217+
no) msg_error "Selected no to Adding 'pmg-no-subscription' repository" ;;
218+
esac
96219
;;
97220
esac
98221

99-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG TEST" --menu "The 'pmgtest' repository can give advanced users access to new features and updates before they are officially released.\n \nAdd (Disabled) 'pmgtest' repository?" 14 58 2 \
100-
"yes" " " \
101-
"no" " " 3>&2 2>&1 1>&3)
102-
case $CHOICE in
103-
yes)
104-
msg_info "Adding 'pmgtest' repository and set disabled"
105-
cat <<EOF >/etc/apt/sources.list.d/pmgtest-for-beta.list
222+
# ---- PMG-TEST ----
223+
read -r state file <<<"$(repo_state pmgtest)"
224+
case $state in
225+
active) msg_ok "'pmgtest' repository already active (skipped)" ;;
226+
disabled) msg_ok "'pmgtest' repository already disabled (skipped)" ;;
227+
missing)
228+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "PMG TEST" \
229+
--menu "The 'pmgtest' repository can give advanced users access to new features early.\n\nAdd (disabled) 'pmgtest' repository?" 14 58 2 \
230+
"yes" " " \
231+
"no" " " 3>&2 2>&1 1>&3)
232+
case $CHOICE in
233+
yes)
234+
msg_info "Adding 'pmgtest' repository (disabled)"
235+
cat >/etc/apt/sources.list.d/pmgtest-for-beta.list <<EOF
106236
# deb http://download.proxmox.com/debian/pmg ${VERSION} pmgtest
107237
EOF
108-
msg_ok "Added 'pmgtest' repository"
109-
;;
110-
no)
111-
msg_error "Selected no to adding 'pmgtest' repository"
238+
msg_ok "Added 'pmgtest' repository"
239+
;;
240+
no) msg_error "Selected no to Adding 'pmgtest' repository" ;;
241+
esac
112242
;;
113243
esac
114244

115-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "SUBSCRIPTION NAG" --menu "This will disable the nag message reminding you to purchase a subscription every time you log in to the web interface.\n \nDisable subscription nag?" 14 58 2 \
116-
"yes" " " \
117-
"no" " " 3>&2 2>&1 1>&3)
245+
# ---- SUBSCRIPTION NAG ----
246+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "SUBSCRIPTION NAG" --menu \
247+
"Disable subscription nag in PMG UI?" 14 58 2 "yes" " " "no" " " 3>&2 2>&1 1>&3)
118248
case $CHOICE in
119249
yes)
120-
whiptail --backtitle "Proxmox VE Helper Scripts" --msgbox --title "Support Subscriptions" "Supporting the software's development team is essential. Check their official website's Support Subscriptions for pricing. Without their dedicated work, we wouldn't have this exceptional software." 10 58
250+
whiptail --backtitle "Proxmox VE Helper Scripts" --msgbox --title "Support Subscriptions" \
251+
"Supporting the software's development team is essential.\nPlease consider buying a subscription." 10 58
121252
msg_info "Disabling subscription nag"
122-
# Normal GUI (proxmox-widget-toolkit)
123-
echo "DPkg::Post-Invoke { \"if [ -s /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js ] && ! grep -q -F 'NoMoreNagging' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js; then echo 'Removing subscription nag from UI...'; sed -i '/data\.status/{s/\!//;s/active/NoMoreNagging/}' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js; fi\" };" >/etc/apt/apt.conf.d/no-nag-script
124-
# JS library used when accessing via mobile device browser
125-
echo "DPkg::Post-Invoke { \"if [ -s /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js ] && ! grep -q -F 'NoMoreNagging' /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js; then echo 'Removing subscription nag from mobile UI...'; sed -i '/data\.status/{s/\!//;s/active/NoMoreNagging/}' /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js; fi\" };" >/etc/apt/apt.conf.d/no-nag-script-pmgmanagerlib-mobile
126-
msg_ok "Disabled subscription nag (Delete browser cache)"
253+
cat <<'EOF' >/etc/apt/apt.conf.d/no-nag-script
254+
DPkg::Post-Invoke {
255+
"if [ -s /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js ] && ! grep -q -F 'NoMoreNagging' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js; then
256+
sed -i '/data\.status/{s/\\!//;s/active/NoMoreNagging/}' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js;
257+
fi";
258+
"if [ -s /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js ] && ! grep -q -F 'NoMoreNagging' /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js; then
259+
sed -i '/data\.status/{s/\\!//;s/active/NoMoreNagging/}' /usr/share/javascript/pmg-gui/js/pmgmanagerlib-mobile.js;
260+
fi";
261+
};
262+
EOF
263+
msg_ok "Disabled subscription nag (clear browser cache!)"
127264
;;
128265
no)
129-
whiptail --backtitle "Proxmox VE Helper Scripts" --msgbox --title "Support Subscriptions" "Supporting the software's development team is essential. Check their official website's Support Subscriptions for pricing. Without their dedicated work, we wouldn't have this exceptional software." 10 58
130-
msg_error "Selected no to disabling subscription nag"
266+
msg_error "Selected no to Disabling subscription nag"
267+
rm -f /etc/apt/apt.conf.d/no-nag-script 2>/dev/null
131268
;;
132269
esac
133-
apt --reinstall install proxmox-widget-toolkit pmg-gui &>/dev/null
270+
apt --reinstall install proxmox-widget-toolkit pmg-gui &>/dev/null || msg_error "Widget toolkit reinstall failed"
134271

135-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "UPDATE" --menu "\nUpdate Proxmox Mail Gateway now?" 11 58 2 \
136-
"yes" " " \
137-
"no" " " 3>&2 2>&1 1>&3)
272+
# ---- UPDATE ----
273+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "UPDATE" --menu \
274+
"Update Proxmox Mail Gateway now?" 11 58 2 "yes" " " "no" " " 3>&2 2>&1 1>&3)
138275
case $CHOICE in
139276
yes)
140277
msg_info "Updating Proxmox Mail Gateway (Patience)"
141-
apt-get update &>/dev/null
142-
apt-get -y dist-upgrade &>/dev/null
278+
apt update &>/dev/null || msg_error "apt update failed"
279+
apt -y dist-upgrade &>/dev/null || msg_error "apt dist-upgrade failed"
143280
msg_ok "Updated Proxmox Mail Gateway"
144281
;;
145-
no)
146-
msg_error "Selected no to updating Proxmox Mail Gateway"
147-
;;
282+
no) msg_error "Selected no to updating Proxmox Mail Gateway" ;;
148283
esac
149284

150-
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "REBOOT" --menu "\nReboot Proxmox Mail Gateway now? (recommended)" 11 58 2 \
151-
"yes" " " \
152-
"no" " " 3>&2 2>&1 1>&3)
285+
# ---- REMINDER ----
286+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Post-Install Reminder" --msgbox \
287+
"IMPORTANT:
288+
289+
Please run this script on every PMG node individually if you have multiple nodes.
290+
291+
After completing these steps, it is strongly recommended to REBOOT your node.
292+
293+
After the upgrade or post-install routines, always clear your browser cache or perform a hard reload (Ctrl+Shift+R) before using the PMG Web UI to avoid UI display issues." 20 80
294+
295+
# ---- REBOOT ----
296+
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "REBOOT" --menu \
297+
"Reboot Proxmox Mail Gateway now? (recommended)" 11 58 2 "yes" " " "no" " " 3>&2 2>&1 1>&3)
153298
case $CHOICE in
154299
yes)
155300
msg_info "Rebooting Proxmox Mail Gateway"
@@ -158,7 +303,7 @@ EOF
158303
reboot
159304
;;
160305
no)
161-
msg_error "Selected no to reboot Proxmox Mail Gateway (Reboot recommended)"
306+
msg_error "Selected no to reboot (Reboot recommended)"
162307
msg_ok "Completed Post Install Routines"
163308
;;
164309
esac
@@ -167,7 +312,7 @@ EOF
167312
header_info
168313
echo -e "\nThis script will Perform Post Install Routines.\n"
169314
while true; do
170-
read -p "Start the Proxmox Mail Gateway Post Install Script (y/n)?" yn
315+
read -rp "Start the Proxmox Mail Gateway Post Install Script (y/n)? " yn
171316
case $yn in
172317
[Yy]*) break ;;
173318
[Nn]*)

0 commit comments

Comments
 (0)