-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupgrade-php.sh
250 lines (234 loc) · 6.48 KB
/
upgrade-php.sh
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash
# bash <(curl -s https://raw.githubusercontent.com/KnoAll/cacti-template/dev/upgrade-php.sh)
#ingest options
if [[ "$#" > 0 ]]; then
for var in "$@"; do
case $var in
debug|-debug|--debug)
trap 'echo cmd: "$BASH_COMMAND" on line $LINENO exited with code: $?' DEBUG
;;
dev|-dev|--dev)
branch="dev"
;;
esac
done
fi
# error handling
#set -eE
exit_trap() {
local lc="$BASH_COMMAND" rc=$?
if [ $rc -ne 0 ]; then
printerror "Command [$lc] on $LINENO exited with code [$rc]"
fi
}
trap exit_trap EXIT
green=$(tput setaf 2)
red=$(tput setaf 1)
tan=$(tput setaf 3)
reset=$(tput sgr0)
printinfo() {
if [ -z "$1" ]; then
printf "${tan}::: ${green}%s${reset}\n" "$@"
else
printf "${tan}::: ${green}%s${reset}\n" "$(date +%a_%R): $@"
fi
}
printwarn() {
printf "${tan}*** WARNING: %s${reset}\n" "$(date +%a_%R): $@"
}
printerror() {
printf "${red}!!! ERROR: %s${reset}\n" "$(date +%a_%R): $@"
}
#installed cacti version
cactiver=$( cat /var/www/html/cacti/include/cacti_version )
#minimum version for php
upgrade_version=1.2.18
#installed php version
php_ver=v$( php -r 'echo PHP_VERSION;' )
smphp_ver=$(echo $php_ver | cut -c-4)
php_minimum=7.2
printinfo "Checking for PHP upgrade..."
printinfo
if [[ `whoami` == "root" ]]; then
printerror "You ran me as root! Do not run me as root!"
exit
elif grep -q "Raspbian GNU/Linux 9" /etc/os-release; then
printerror "Sorry, Raspbian not supported for PHP upgrade yet, cannot proceed..."
printinfo
exit
if [[ `whoami` != "pi" ]]; then
printerror "Uh-oh. You are not logged in as the default pi user. Exiting..."
exit 1
else
os_dist=raspbian
os_name=Raspbian
webserver=apache2
pkg_mgr=apt
verphp="$(php -v | grep -Po '(?<=PHP )([0-7.]+)' | cut -c-3)"
fi
elif grep -q "Raspbian GNU/Linux 10" /etc/os-release; then
printerror "Sorry, Raspbian not supported for PHP upgrade yet, cannot proceed..."
printinfo
exit
if [[ `whoami` != "pi" ]]; then
printerror "Uh-oh. You are not logged in as the default pi user. Exiting..."
printinfo
exit 1
else
os_dist=raspbian
os_name=Raspbian
webserver=apache2
pkg_mgr=apt
verphp="$(php -v | grep -Po '(?<=PHP )([0-7.]+)' | cut -c-3)"
fi
elif grep -q "AlmaLinux 9" /etc/os-release; then
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default cacti user. Exiting..."
printinfo
exit
else
os_dist=almalinux
os_name=AlmaLinux
webserver=httpd
pkg_mgr=dnf
remi=remi-release-9.rpm
fi
elif grep -q "Rocky Linux 9" /etc/os-release; then
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default cacti user. Exiting..."
printinfo
exit
else
os_dist=rockylinux
os_name=RockyLinux
webserver=httpd
pkg_mgr=dnf
remi=remi-release-9.rpm
fi
elif grep -q "CentOS Linux 7" /etc/os-release; then
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default cacti user. Exiting..."
printinfo
exit
else
os_dist=centos
os_name=CentOS7
webserver=httpd
pkg_mgr=yum
remi=remi-release-7.rpm
fi
elif grep -q "CentOS Linux 8" /etc/os-release; then
#printerror "Sorry, CentOS8 not supported for PHP upgrade yet, cannot proceed..."
printinfo
# exit
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default cacti user. Exiting..."
printinfo
exit
else
os_dist=centos
os_name=CentOS8
webserver=httpd
pkg_mgr=yum
remi=remi-release-8.rpm
php_version=remi-7.4
fi
else
printerror "We don't appear to be on a supported OS. Exiting..."
printinfo
exit
fi
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
if version_ge $cactiver $upgrade_version; then
# cacti must be at least v1.2.18 to go to php7.4
#set upgrade version
php_version=php74
php_description="v7.4.x"
php_num=7.4
else
# php7.3 for =< v1.2.17
#set upgrade version
php_version=php73
php_description="v7.3.x"
php_num=7.3
fi
phpMinimum() {
if version_ge $smphp_ver $php_minimum; then
printwarn "To automatically upgrade Cacti you must now be at minimum PHP v7.2. Cacti can still be upgraded, but not by this script."
read -p "Are you sure want to cancel upgrading PHP? y/N: " minAsk
case "$minAsk" in
y | Y | yes | YES| Yes )
printinfo "Ok, if you change your mind, come back and try again."
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=decline-minimum-php&write=0 )
exit 167
;;
* )
upgradeAsk
;;
esac
fi
}
upgradeAsk () {
#check version of PHP installed
php -r 'exit((int)version_compare(PHP_VERSION, '$php_num', "<"));'
if [ $? -ne 0 ];then
printinfo
read -p "Do you want to upgrade your PHP install to $php_description? y/N: " upAsk
case "$upAsk" in
y | Y | yes | YES| Yes ) printinfo "Ok, let's go!"
if [[ $param1 == "dev" ]]; then
printwarn $param1
else
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-php_$smphp_ver-$php_version&write=0 )
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-php&write=0 )
fi
upgradePHP
;;
* )
#printwarn "OK, please consider upgrading, old versions of PHP are not updated and may contain known security and stability issues."
if [[ $param1 == "dev" ]]; then
printwarn $param1
else
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=decline-upgrade-php&write=0 )
fi
phpMinimum
;;
esac
else
printinfo "Installed PHP $php_ver >= current stable $php_description."
exit 0
fi
}
upgradePHP() {
printinfo "Setting up repo"
sudo $pkg_mgr install -y -q http://rpms.remirepo.net/enterprise/$remi
sudo $pkg_mgr install -y -q yum-utils php72-php-xml php-gmp php-xml php-simplexml
printinfo "Enabling new $php_description"
case "$os_name" in
CentOS8|AlmaLinux|RockyLinux )
sudo $pkg_mgr -y -q module reset php
sudo $pkg_mgr module -y -q enable php:$php_version
sudo dnf -y -q install php
if [ $? -ne 0 ];then
printerror "ERROR upgrading PHP version."
else
php_ver=v$( php -r 'echo PHP_VERSION;' )
printinfo "PHP upgraded to $php_ver"
fi
;;
* )
sudo $pkg_mgr-config-manager --enable remi-$php_version
sudo $pkg_mgr -y -q update
if [ $? -ne 0 ];then
printerror "ERROR upgrading PHP version."
else
printwarn "Restarting webserver..."
sudo systemctl restart $webserver
php_ver=v$( php -r 'echo PHP_VERSION;' )
printinfo "PHP upgraded to $php_ver"
fi
;;
esac
}
upgradeAsk
printinfo