-
Notifications
You must be signed in to change notification settings - Fork 254
/
update.sh
executable file
·353 lines (322 loc) · 9.62 KB
/
update.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash
### Colors for output
RESTORE='\033[0m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
### Global variables
OS=""
OS_VERSION=""
VERBOSE=false
BACKUP_LOCATION="/tmp/fiercephish_db_backup.sql"
GITHUB_BRANCH="master"
## Main function
main()
{
if [ $EUID != 0 ]
then
error "You must run the installation script as root"
exit 1
fi
if [[ ! $1 = '-r' ]]
then
show_header
validate_vars_general
check_install
check_new_version
self_update
exit 0
else
check_os
run_update
exit 0
fi
}
## Action functions
show_header()
{
echo -e ""
echo -e "${CYAN}#################################################################"
echo -e "#################### ${LYELLOW}FiercePhish Updater${CYAN} ####################"
echo -e "#################### By ${GREEN}Chris King${CYAN} ####################"
echo -e "#################### ${GREEN}@raikiasec${CYAN} ####################"
echo -e "#################################################################${RESTORE}"
echo -e ""
echo -e ""
notice "This updater automatically updates your FiercePhish instance to the latest version."
notice "It is designed to work with Ubuntu and currently only works for Ubuntu 16.04 and Ubuntu 16.10"
notice "This updater expects you to have installed FiercePhish using the automated installer or following"
notice "the manual install method"
echo -e ""
}
check_new_version()
{
info "Checking for new FiercePhish version"
local current_version=$(cat VERSION)
local latest_version=$(curl -s https://raw.githubusercontent.com/Raikia/FiercePhish/${GITHUB_BRANCH}/VERSION?${RANDOM})
if [[ $current_version == $latest_version ]]
then
info "You are already running the latest version of FiercePhish (v${current_version})!"
exit 0
fi
notice "Update available!"
notice "You are running v${LYELLOW}${current_version}${WHITE} and the latest version is v${LYELLOW}${latest_version}${WHITE}!"
prompt "Do you want to update? [y/N]"
INPUT=$(get_input "n")
if [[ $INPUT = "" ]]
then
INPUT="n"
fi
if [[ ! $INPUT =~ ^[y|Y]$ ]]
then
error "Exiting updater"
exit 1
fi
}
check_install()
{
info "Looking for FiercePhish instance..."
if [[ ! -f /etc/apache2/sites-enabled/fiercephish.conf || ! -d /var/www/fiercephish/ ]]
then
error "Unable to find a FiercePhish instance. Did you install it with the automated installer or with the correct settings?"
exit 1
fi
}
self_update()
{
info "Pulling latest version from GitHub"
sys_cmd "pushd /var/www/fiercephish/"
sys_cmd "wget --no-cache -O update.sh https://raw.githubusercontent.com/Raikia/FiercePhish/${GITHUB_BRANCH}/update.sh?${RANDOM}"
info "Successfully pulled the latest updater!"
info "Beginning update process"
/usr/bin/env bash ./update.sh -r
}
validate_vars_general()
{
mem=$(free -m | awk '/^Mem:/{print $2}')
swap=$(free -m | awk '/^Swap:/{print $2}')
total=$(($mem+$swap))
CREATE_SWAPSPACE="false"
if [[ $total -lt 600 ]]
then
error "System memory + swap is less than 600 MB (it has ${total} MB)!"
echo -e ""
notice "FiercePhish requires at least 600 MB of RAM. Creating swap space can fix a low RAM issue."
prompt "Do you want to create a 2GB swap space? [Y/n]"
CREATE_SWAPSPACE_INPUT=$(get_input "n")
if [[ $CREATE_SWAPSPACE_INPUT =~ ^[n|N]$ ]]
then
CREATE_SWAPSPACE="false"
else
CREATE_SWAPSPACE="true"
fi
fi
if [[ $CREATE_SWAPSPACE = "true" ]]
then
notice "Creating a 2GB swapspace at /swapfile"
sys_cmd "fallocate -l 2G /swapfile"
sys_cmd "chmod 600 /swapfile"
sys_cmd "mkswap /swapfile"
sys_cmd "swapon /swapfile"
echo "/swapfile none swap sw 0 0" >> /etc/fstab
grep -q -F 'swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
notice "Done creating swapspace. Swap enabled"
fi
}
check_os()
{
info "Detecting OS distribution..."
if [[ -f /etc/lsb-release ]]
then
. /etc/lsb-release
OS=${DISTRIB_ID}
OS_VERSION=${DISTRIB_RELEASE}
if [[ $OS = 'Ubuntu' ]]
then
if [[ $OS_VERSION = "16.04" || $OS_VERSION = "16.10" ]]
then
notice "Found that you are running ${LYELLOW}${OS} ${OS_VERSION}${WHITE}! This is a supported operating system!"
else
error "You are running ${LYELLOW}${OS} ${OS_VERSION}${LRED}. This is not supported by this update. There's really no reason to not update Ubuntu"
exit 1
fi
else
error "You are running ${LYELLOW}${OS} ${OS_VERSION}${RESTORE}. This isn't supported by the updater yet"
exit 1;
fi
else
error "Could not identify what OS is running! This updater works on Ubuntu only right now"
exit 1
fi
echo -e ""
}
run_update()
{
info "Ensuring dependencies are properly installed"
sys_cmd "apt-get update"
sys_cmd "apt-get install -y git curl unzip"
sys_cmd "pushd /var/www/fiercephish/"
info "Updating Bower"
sys_cmd "/usr/bin/env npm cache clean"
sys_cmd "/usr/bin/env npm update -g bower"
info "Updating Composer"
sys_cmd "composer self-update"
info "Putting FiercePhish into maintenance mode"
sys_cmd "/usr/bin/env php artisan down"
backup_database
info "Pulling the latest version of FiercePhish"
sys_cmd "git fetch --all"
sys_cmd "git reset --hard origin/${GITHUB_BRANCH}"
info "Updating Composer"
sys_cmd "composer install"
info "Updating Bower"
sys_cmd "bower install --allow-root"
info "Running migrations"
sys_cmd "/usr/bin/env php artisan migrate"
update_env
info "Updating cron job"
cron_command="/usr/bin/env php /var/www/fiercephish/artisan schedule:run >> /dev/null 2>&1"
cron_job="* * * * * $cron_command"
cat <(grep -i -F -v "$cron_command" <(crontab -u www-data -l 2>/dev/null)) <(echo "$cron_job") | crontab -u www-data -
info "Updating Supervisor for job processing"
if [[ $OS = "Ubuntu" ]]
then
cat > /etc/supervisor/conf.d/fiercephish.conf <<- EOM
[program:fiercephish]
command=/usr/bin/php /var/www/fiercephish/artisan queue:work --queue=operation,email,campaign_email,default --tries 1 --timeout=86100
process_name = %(program_name)s-80%(process_num)02d
stdout_logfile = /var/log/fiercephish-80%(process_num)02d.log
stdout_logfile_maxbytes=100MB
stdout_logfile_backups=10
autostart=true
autorestart=true
numprocs=10
directory=/var/www/fiercephish
user=www-data
redirect_stderr=true
EOM
sys_cmd "supervisorctl reread"
sleep 2
sys_cmd "supervisorctl update"
sleep 2
sys_cmd "service supervisor restart"
sleep 5
sys_cmd "supervisorctl reload"
sleep 10
sys_cmd "service supervisor restart"
sleep 5
fi
info "Restarting queue workers"
sys_cmd "/usr/bin/env php artisan queue:restart"
info "Clearing cache"
sys_cmd "/usr/bin/env php artisan cache:clear"
sys_cmd "/usr/bin/env php artisan clear-compiled"
info "Setting proper permissions"
if [[ $OS = "Ubuntu" ]]
then
sys_cmd "chown -R www-data:www-data /var/www/fiercephish/"
fi
info "Turning off maintenance mode"
sys_cmd "/usr/bin/env php artisan up"
notice "Update complete!"
cleanup_backup
notice "Process complete! Enjoy the new FiercePhish"
}
update_env()
{
info "Creating new .env file"
local envVars=("APP_ENV" "APP_DEBUG" "APP_LOG_LEVEL" "APP_TIMEZONE" "APP_KEY" "APP_URL" "APP_NAME" "PROXY_URL" "PROXY_SCHEMA" "DB_CONNECTION" "DB_HOST" "DB_PORT" "DB_USERNAME" "DB_PASSWORD" "DB_DATABASE" "CACHE_DRIVER" "SESSION_DRIVER" "BROADCAST_DRIVER" "QUEUE_DRIVER" "REDIS_HOST" "REDIS_PASSWORD" "REDIS_PORT" "PUSHER_APP_ID" "PUSHER_APP_KEY" "PUSHER_APP_SECRET" "MAIL_DRIVER" "MAIL_HOST" "MAIL_PORT" "MAIL_USERNAME" "MAIL_PASSWORD" "MAIL_ENCRYPTION" "MAILGUN_DOMAIN" "MAILGUN_SECRET" "URI_PREFIX" "TEST_EMAIL_JOB" "IMAP_HOST" "IMAP_PORT" "IMAP_USERNAME" "IMAP_PASSWORD" "MAIL_BCC_ALL")
sys_cmd "mv .env .env_old"
sys_cmd "cp .env.example .env"
source .env_old
for i in "${!envVars[@]}"
do
eval tempVar=\$${envVars[$i]}
tempVar=${tempVar//\//\\/}
sys_cmd "sed -i 's/${envVars[$i]}=.*$/${envVars[$i]}=${tempVar}/' .env"
done
sys_cmd "rm .env_old"
info "Caching new configuration"
sys_cmd "/usr/bin/env php artisan config:cache"
}
backup_database()
{
source .env
info "Backing up the FiercePhish database"
mysqldump -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_DATABASE" > $BACKUP_LOCATION 2> /dev/null
info "Done backing up database"
}
cleanup_backup()
{
notice "Do you want to clean up the database backup file (${BACKUP_LOCATION})?"
prompt "Delete backups? [y/N]"
INPUT=$(get_input "n")
if [[ $INPUT =~ ^[y|Y]$ ]]
then
info "Removing backups"
sys_cmd "rm ${BACKUP_LOCATION}"
info "Backups removed"
else
info "Keeping backup database dump, located: ${LYELLOW}${BACKUP_LOCATION}${RESET}"
fi
}
## Helper functions
get_input()
{
local default=$1
if [[ $0 = "bash" ]]
then
echo $default
else
local input_answer=""
read -e input_answer
echo $input_answer
fi
}
prompt()
{
local prompt=$1
if [[ $0 != "bash" ]]
then
echo -ne " ${LYELLOW}[>] ${prompt} > ${RESTORE}"
fi
}
sys_cmd()
{
com=$1
if [[ $VERBOSE = true ]]
then
notice "Running ${com}..."
eval "${com}"
else
#notice "Running ${com} > /dev/null 2>&1"
eval "${com} > /dev/null 2>&1"
fi
}
info()
{
local prompt=$1
echo -e " ${YELLOW}[~] ${GREEN}${prompt}${RESTORE}"
}
error()
{
local prompt=$1
echo -e " ${LRED}[!] ${prompt}${RESTORE}"
}
notice()
{
local prompt=$1
echo -e " ${YELLOW}[~] ${WHITE}${prompt}${RESTORE}"
}
main "$@"