-
Notifications
You must be signed in to change notification settings - Fork 0
/
periodicDelete.sh
179 lines (124 loc) · 4.5 KB
/
periodicDelete.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
#!/bin/bash
########################################################################################
# This script is designed to be called with no arguments as a periodic cron job. It #
# will check for instances that have been shut down, and delete them after a set time #
# period elapses (currently, 4 weeks). They would have to be manually/automatically #
# recreated to be spun up again. #
########################################################################################
#####################
# Exclusivity Check #
#####################
function finish {
# Remove lock files
rm -f ~/deployTestInstance.lock
}
trap finish EXIT
###############
# Basic Setup #
###############
# Echo out what we're doing
echo "Checking for any instances to delete (current time: $(date))."
# Enter the folder of instances
cd ~/CICD_TestInstances || { echo "Test Instances Folder Missing, Aborting."; exit 1; }
# Save current time
NOW=$(date +"%s")
# Calculate time offset. seconds * minutes * days * 4 weeks
(( TIMEOUT = 60*60*24*28 ))
##################
# Instance Check #
##################
# Loop through each folder and check if it's time to delete it
for entry in "."/*
do
entry=${entry:2}
echo "$entry"
# Check the modified time
INSTANCETIME=$(stat -c %Y "$entry") # Checks the modified time of the instance folder
# Compare
(( DAYSOLD = ( ( NOW - INSTANCETIME ) / ( 60*60*24 ) ) ))
echo "Instance is $DAYSOLD days old ($(stat -c %y "$entry"))."
# Check if too old
if (( ( NOW - INSTANCETIME ) > TIMEOUT )); then
echo "Shutting down and deleting instance."
# Enter the folder
cd "$entry/PollBuddy" || { echo "Failed to cd into instance, aborting."; exit 1; }
docker-compose -p "$entry" down -v
docker-compose -p "$entry" rm -s -f -v
cd ../../ || { echo "Failed to cd out of instance, aborting."; exit 1; }
echo "Instance has been shut down and containers removed."
# Delete the folder
rm -rf "$entry"
echo "Instance folder has been deleted."
else
echo "Instance does not need to be deleted."
fi
echo "---"
done
echo "Instance check complete on $(date)."
#######################
# Docker Data Cleanup #
#######################
echo "Cleaning up Docker Data..."
# Filter makes sure images are at least 2h old to prune (in case any builds are running)
docker image prune -f -a --filter "until=2h"
docker volume prune -f # Doesn't support the filter for some reason, shouldn't really matter though
docker network prune -f --filter "until=2h"
echo "Docker Data cleanup complete on $(date)."
###########################
# Port Allocation Cleanup #
###########################
echo "Cleaning up old port allocations..."
# Enter the folder of instances ports
cd ~/dev-site-ports || { echo "Test Instances Ports Folder Missing, Aborting."; exit 1; }
# Loop over each file in the directory
for file in ./*
do
ID=$(cat "$file")
# Check if a folder exists that matches the ID
if [ ! -d "$HOME/CICD_TestInstances/$ID" ]; then
echo "Folder with ID of $ID DOES NOT exist, port should be deallocated."
rm "$file"
else
echo "Folder with ID of $ID exists, port should be kept."
fi
done
echo "Old port allocations cleanup complete on $(date)."
###########################
# Config File Cleanup #
###########################
echo "Cleaning up old config files..."
# Enter the folder of instances configs
cd ~/dev-site-configs || { echo "Test Instances Configs Folder Missing, Aborting."; exit 1; }
# Loop over each file in the directory
for file in ./*
do
# Split the filename into just the ID by removing the first 2 chars (./) and last 5 (.conf)
ID=${file:2:-5}
# Check if a folder exists that matches the ID
if [ ! -d "$HOME/CICD_TestInstances/$ID" ]; then
echo "Folder with ID of $ID DOES NOT exist, config should be deleted."
rm "$file"
else
echo "Folder with ID of $ID exists, config should be kept."
fi
done
echo "Old config files cleanup complete on $(date)."
####################
# Restart Dev Site #
####################
# Talk about it
echo "Restarting dev site"
# Acquire the lock
lockfile -5 ~/deployTestInstance.lock
# Move over to the website folder
cd ~/PollBuddy.app/ || { echo "CD to PollBuddy.app Folder Failed, Aborting."; exit 1; }
# Restart dev site (instance configs are bind mounted, so we just need to restart nginx)
docker-compose restart
# We're done!
echo "Dev site restarted"
##########
# Finish #
##########
echo "Done!"
# Exit
exit 0