This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
gravity-sync.sh
executable file
·454 lines (381 loc) · 13 KB
/
gravity-sync.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env bash
# GRAVITY SYNC BY VMSTAN #####################
# GS 3.x to 4.0 Upgrade Utility ##############
# Run only to upgrade your existing Gravity Sync 3.x installation to 4.0 format
PROGRAM='Gravity Sync'
GS_FILEPATH=$(realpath $0)
LOCAL_FOLDR=$(dirname $GS_FILEPATH)
GS_ETC_PATH="/etc/gravity-sync"
GS_GRAVITY_FI_MD5_LOG='gs-gravity.md5'
GS_CUSTOM_DNS_MD5_LOG='gs-clist.md5'
GS_CNAME_CONF_MD5_LOG='05-pihole-custom-cname.conf.md5'
OS_DAEMON_PATH='/etc/systemd/system'
## Script Colors
RED='\033[0;91m'
GREEN='\033[0;92m'
CYAN='\033[0;96m'
YELLOW='\033[0;93m'
PURPLE='\033[0;95m'
BLUE='\033[0;94m'
BOLD='\033[1m'
NC='\033[0m'
## Message Codes
FAIL="${RED}✗${NC}"
WARN="${PURPLE}!${NC}"
GOOD="${GREEN}✓${NC}"
STAT="${CYAN}∞${NC}"
INFO="${YELLOW}»${NC}"
INF1="${CYAN}›${NC}"
NEED="${BLUE}?${NC}"
LOGO="${PURPLE}∞${NC}"
## Echo Stack
### Informative
function echo_info {
echo -e "${INFO} ${YELLOW}${MESSAGE}${NC}"
}
function echo_prompt {
echo -e "${INF1} ${CYAN}${MESSAGE}${NC}"
}
### Warning
function echo_warn {
echo -e "${WARN} ${PURPLE}${MESSAGE}${NC}"
}
### Executing
function echo_stat {
echo -en "${STAT} ${MESSAGE}"
}
### Success
function echo_good {
echo -e "\r${GOOD} ${MESSAGE}"
}
### Success
function echo_good_clean {
echo -e "\r${GOOD} ${MESSAGE}"
}
### Failure
function echo_fail {
echo -e "\r${FAIL} ${MESSAGE}"
}
### Request
function echo_need {
echo -en "${NEED} ${BOLD}${MESSAGE}:${NC} "
}
### Indent
function echo_over {
echo -e " ${MESSAGE}"
}
### Gravity Sync Logo
function echo_grav {
echo -e "${LOGO} ${BOLD}${MESSAGE}${NC}"
}
### Lines
function echo_blank {
echo -e ""
}
## Error Validation
function error_validate {
if [ "$?" != "0" ]; then
echo_fail
exit 1
else
echo_good
fi
}
function start_gs_no_config {
MESSAGE="Gravity Sync 3.x to 4.0 Migration Utility"
echo_grav
}
function check_old_version {
MESSAGE="Checking for 3.x Configuration File"
echo_stat
if [ -f settings/gravity-sync.conf ]; then
echo_good
else
echo_fail
exit 1
fi
}
function install_new_gravity {
MESSAGE="Installing Gravity Sync 4.0"
echo_info
if [ -d /etc/gravity-sync/.gs ]; then
MESSAGE="Removing existing GitHub cache"
echo_stat
sudo rm -fr /etc/gravity-sync/.gs
error_validate
fi
if [ ! -d /etc/gravity-sync ]; then
MESSAGE="Creating new configuration directory"
echo_stat
sudo mkdir /etc/gravity-sync
error_validate
fi
MESSAGE="Validating configuration directory permissions"
echo_stat
sudo chmod 775 /etc/gravity-sync
error_validate
if [ -f /usr/local/bin/gravity-sync ]; then
MESSAGE="Removing old Gravity Sync binary"
echo_stat
sudo rm -f /usr/local/bin/gravity-sync
error_validate
fi
MESSAGE="Creating new GitHub cache"
echo_prompt
sudo git clone https://github.com/vmstan/gravity-sync.git /etc/gravity-sync/.gs
# MESSAGE="Enabling beta updates"
# echo_stat
# sudo touch /etc/gravity-sync/.gs/dev
# echo -e "BRANCH='origin/4.0.0'" | sudo tee /etc/gravity-sync/.gs/dev 1> /dev/null
# error_validate
sudo cp /etc/gravity-sync/.gs/gravity-sync /usr/local/bin
}
function upgrade_to_4 {
MESSAGE="Migrating Previous Configuration"
echo_info
REMOTE_HOST=''
REMOTE_USER=''
PIHOLE_DIR=''
RIHOLE_DIR=''
DNSMAQ_DIR=''
RNSMAQ_DIR=''
FILE_OWNER=''
RILE_OWNER=''
DOCKER_CON=''
ROCKER_CON=''
SSH_PORT=''
SSH_PKIF=''
MESSAGE="Reviewing old configuration file settings"
echo_stat
source settings/gravity-sync.conf
error_validate
MESSAGE="Creating new configuration file from template"
echo_stat
sudo cp /etc/gravity-sync/.gs/templates/gravity-sync.conf.example /etc/gravity-sync/gravity-sync.conf
error_validate
LOCAL_PIHOLE_DIRECTORY=${PIHOLE_DIR}
REMOTE_PIHOLE_DIRECTORY=${RIHOLE_DIR}
LOCAL_DNSMASQ_DIRECTORY=${DNSMAQ_DIR}
REMOTE_DNSMASQ_DIRECTORY=${RNSMAQ_DIR}
LOCAL_FILE_OWNER=${FILE_OWNER}
REMOTE_FILE_OWNER=${RILE_OWNER}
LOCAL_DOCKER_CONTAINER=${DOCKER_CON}
REMOTE_DOCKER_CONTAINER=${ROCKER_CON}
CURRENTUSER=$(whoami)
MESSAGE="Transferring SSH keys"
echo_stat
if [ "${SSH_PKIF}" == "" ]; then
sudo cp $HOME/.ssh/id_rsa /etc/gravity-sync/gravity-sync.rsa
sudo cp $HOME/.ssh/id_rsa.pub /etc/gravity-sync/gravity-sync.rsa.pub
else
sudo cp ${SSH_PKIF} /etc/gravity-sync/gravity-sync.rsa
sudo cp ${SSH_PKIF}.pub /etc/gravity-sync/gravity-sync.rsa.pub
fi
error_validate
MESSAGE="Setting SSH key owner"
echo_stat
sudo chown ${CURRENTUSER}:${CURRENTUSER} /etc/gravity-sync/gravity-sync.rsa
sudo chown ${CURRENTUSER}:${CURRENTUSER} /etc/gravity-sync/gravity-sync.rsa.pub
error_validate
MESSAGE="Migrating remote host settings"
echo_stat
sudo sed -i "/REMOTE_HOST=''/c\REMOTE_HOST='${REMOTE_HOST}'" /etc/gravity-sync/gravity-sync.conf
error_validate
MESSAGE="Migrating remote user settings"
echo_stat
sudo sed -i "/REMOTE_USER=''/c\REMOTE_USER='${REMOTE_USER}'" /etc/gravity-sync/gravity-sync.conf
error_validate
if [ "${SSH_PORT}" != "" ]; then
GS_SSH_PORT=${SSH_PORT}
MESSAGE="Migrating target host SSH settings"
echo_stat
echo -e "GS_SSH_PORT='${GS_SSH_PORT}" | sudo tee -a /etc/gravity-sync/gravity-sync.conf 1> /dev/null
error_validate
fi
if [ "${LOCAL_PIHOLE_DIRECTORY}" == '' ] || [ "${LOCAL_PIHOLE_DIRECTORY}" == '/etc/pihole' ]; then
MESSAGE="Defaulting local Pi-hole directory setting"
echo_good_clean
else
MESSAGE="Migrating local Pi-hole directory setting"
echo_stat
sudo sed -i "/LOCAL_PIHOLE_DIRECTORY=''/c\LOCAL_PIHOLE_DIRECTORY='${LOCAL_PIHOLE_DIRECTORY}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${REMOTE_PIHOLE_DIRECTORY}" == '' ] || [ "${REMOTE_PIHOLE_DIRECTORY}" == '/etc/pihole' ]; then
MESSAGE="Defaulting remote Pi-hole directory setting"
echo_good_clean
else
MESSAGE="Migrating remote Pi-hole directory setting"
echo_stat
sudo sed -i "/REMOTE_PIHOLE_DIRECTORY=''/c\REMOTE_PIHOLE_DIRECTORY='${REMOTE_PIHOLE_DIRECTORY}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${LOCAL_DNSMASQ_DIRECTORY}" == '' ] || [ "${LOCAL_DNSMASQ_DIRECTORY}" == '/etc/dnsmasq.d' ]; then
MESSAGE="Defaulting local DNSMASQ directory setting"
echo_good_clean
else
MESSAGE="Migrating local DNSMASQ directory setting"
echo_stat
sudo sed -i "/LOCAL_DNSMASQ_DIRECTORY=''/c\LOCAL_DNSMASQ_DIRECTORY='${LOCAL_DNSMASQ_DIRECTORY}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${REMOTE_DNSMASQ_DIRECTORY}" == '' ] || [ "${REMOTE_DNSMASQ_DIRECTORY}" == '/etc/dnsmasq.d' ]; then
MESSAGE="Defaulting remote DNSMASQ directory setting"
echo_good_clean
else
MESSAGE="Migrating remote DNSMASQ directory setting"
echo_stat
sudo sed -i "/REMOTE_DNSMASQ_DIRECTORY=''/c\REMOTE_DNSMASQ_DIRECTORY='${REMOTE_DNSMASQ_DIRECTORY}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${LOCAL_FILE_OWNER}" == '' ]; then
MESSAGE="Defaulting local file owner setting"
echo_good_clean
else
MESSAGE="Migrating local file owner setting"
echo_stat
sudo sed -i "/LOCAL_FILE_OWNER=''/c\LOCAL_FILE_OWNER='${LOCAL_FILE_OWNER}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${REMOTE_FILE_OWNER}" == '' ]; then
MESSAGE="Defaulting remote file owner setting"
echo_good_clean
else
MESSAGE="Migrating remote file owner setting"
echo_stat
sudo sed -i "/REMOTE_FILE_OWNER=''/c\REMOTE_FILE_OWNER='${REMOTE_FILE_OWNER}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${LOCAL_DOCKER_CONTAINER}" == '' ] || [ "${LOCAL_DOCKER_CONTAINER}" == 'pihole' ]; then
MESSAGE="Defaulting local Pi-hole container setting"
echo_good_clean
else
MESSAGE="Migrating local Pi-hole container setting"
echo_stat
sudo sed -i "/LOCAL_DOCKER_CONTAINER=''/c\LOCAL_DOCKER_CONTAINER='${LOCAL_DOCKER_CONTAINER}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
if [ "${REMOTE_DOCKER_CONTAINER}" == '' ] || [ "${REMOTE_DOCKER_CONTAINER}" == 'pihole' ]; then
MESSAGE="Defaulting remote Pi-hole container setting"
echo_good_clean
else
MESSAGE="Migrating local Pi-hole container setting"
echo_stat
sudo sed -i "/REMOTE_DOCKER_CONTAINER=''/c\REMOTE_DOCKER_CONTAINER='${REMOTE_DOCKER_CONTAINER}'" /etc/gravity-sync/gravity-sync.conf
error_validate
fi
MESSAGE="Migrating task history"
echo_stat
sudo cp logs/gravity-sync.log /etc/gravity-sync/gs-sync.log
error_validate
MESSAGE="Migrating hashing tables"
echo_stat
if [ -f "logs/gravity-sync.md5" ]; then
REMOTE_DB_MD5=$(sed "1q;d" logs/gravity-sync.md5)
LOCAL_DB_MD5=$(sed "2q;d" logs/gravity-sync.md5)
REMOTE_CL_MD5=$(sed "3q;d" logs/gravity-sync.md5)
LOCAL_CL_MD5=$(sed "4q;d" logs/gravity-sync.md5)
REMOTE_CN_MD5=$(sed "5q;d" logs/gravity-sync.md5)
LOCAL_CN_MD5=$(sed "6q;d" logs/gravity-sync.md5)
echo -e ${REMOTE_DB_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_GRAVITY_FI_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_DB_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_GRAVITY_FI_MD5_LOG} 1> /dev/null
echo -e ${REMOTE_CL_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CUSTOM_DNS_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_CL_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CUSTOM_DNS_MD5_LOG} 1> /dev/null
echo -e ${REMOTE_CN_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CNAME_CONF_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_CN_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CNAME_CONF_MD5_LOG} 1> /dev/null
else
REMOTE_DB_MD5="0"
LOCAL_DB_MD5="0"
REMOTE_CL_MD5="0"
LOCAL_CL_MD5="0"
REMOTE_CN_MD5="0"
LOCAL_CN_MD5="0"
echo -e ${REMOTE_DB_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_GRAVITY_FI_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_DB_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_GRAVITY_FI_MD5_LOG} 1> /dev/null
echo -e ${REMOTE_CL_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CUSTOM_DNS_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_CL_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CUSTOM_DNS_MD5_LOG} 1> /dev/null
echo -e ${REMOTE_CN_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CNAME_CONF_MD5_LOG} 1> /dev/null
echo -e ${LOCAL_CN_MD5} | sudo tee -a ${GS_ETC_PATH}/${GS_CNAME_CONF_MD5_LOG} 1> /dev/null
fi
error_validate
}
function remove_old_version {
MESSAGE="Removing Old Version & Settings"
echo_info
if hash crontab 2>/dev/null; then
MESSAGE="Clearing automation from crontab"
echo_stat
crontab -l > cronjob-old.tmp
sed "/gravity-sync.sh/d" cronjob-old.tmp > cronjob-new.tmp
crontab cronjob-new.tmp
error_validate
fi
kill_automation_service
if [ -f /etc/bash.bashrc ]; then
MESSAGE="Cleaning up bash.bashrc"
echo_info
sudo sed -i "/gravity-sync.sh/d" /etc/bash.bashrc
error_validate
fi
MESSAGE="Removing old Gravity Sync folder"
echo_stat
sudo rm -fr ${LOCAL_FOLDR}
error_validate
}
function kill_automation_service {
if systemctl is-active --quiet gravity-sync.timer; then
MESSAGE="Stopping ${PROGRAM} timer"
echo_stat
sudo systemctl stop gravity-sync
error_validate
MESSAGE="Disabling ${PROGRAM} automation service"
echo_stat
sudo systemctl disable gravity-sync --quiet
error_validate
MESSAGE="Removing systemd timer"
echo_stat
sudo rm -f ${OS_DAEMON_PATH}/gravity-sync.timer
error_validate
MESSAGE="Removing systemd service"
echo_stat
sudo rm -f ${OS_DAEMON_PATH}/gravity-sync.service
error_validate
MESSAGE="Reloading systemd daemon"
echo_stat
sudo systemctl daemon-reload --quiet
error_validate
fi
}
function end_migration {
MESSAGE="Migration Complete"
echo_info
}
# SCRIPT EXECUTION ###########################
case $# in
0)
start_gs_no_config
check_old_version
install_new_gravity
upgrade_to_4
remove_old_version
end_migration ;;
1)
case $1 in
*)
start_gs_no_config
check_old_version
install_new_gravity
upgrade_to_4
remove_old_version
end_migration ;;
esac
;;
*)
start_gs_no_config
check_old_version
install_new_gravity
upgrade_to_4
remove_old_version
end_migration ;;
esac
# END OF SCRIPT ##############################