-
Notifications
You must be signed in to change notification settings - Fork 10
/
settings.run
executable file
·255 lines (219 loc) · 10.2 KB
/
settings.run
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
#!/bin/bash
source config.ini
folder="$(pwd)"
if [[ ! -f $folder/config.ini ]] ;then
echo " $folder/config.ini not found, exiting script"
exit
fi
if ! command -v mysql &> /dev/null
then
echo "mysql could not be found"
exit 1
fi
if ! command -v jq &> /dev/null
then
echo "jq could not be found"
exit 1
fi
# Logging
mkdir -p $folder/logs
touch $folder/logs/log_$(date '+%Y%m').log
echo "[`date '+%Y%m%d %H:%M:%S'`] Setting.run executed" >> $folder/logs/log_$(date '+%Y%m').log
# Create tables
echo "Creating required tables"
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb < $folder/default_files/table.sql
echo ""
# Check for table updates
echo "Checking for table updates"
version=$(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select version from version where version.key='blissey'")
for file in $folder/default_files/sql_updates/* ;do
number=$(echo $file | grep -o -P '(?<=sql_updates/).*(?=.sql)')
if (( $number > $version )) ;then
echo "Apply updates from $file"
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb < $file
fi
done
echo ""
# Create procedures
echo "Creating required procedures"
if [[ -z $golbat_host ]] ;then
cp $folder/default_files/procedures.sql.default $folder/procedures.sql
else
cp $folder/default_files/proceduresGolbatExternal.sql.default $folder/procedures.sql
fi
sed -i "s/blissey/$blisseydb/g" $folder/procedures.sql
sed -i "s/golbat/$scannerdb/g" $folder/procedures.sql
if [[ $use_koji == "true" ]] ;then
sed -i "s/project_controller/$project_controller/g" $folder/procedures.sql
sed -i "s/project_golbat/$project_golbat/g" $folder/procedures.sql
sed -i "s/koji/$kojidb/g" $folder/procedures.sql
sed -i "s/-- useKoji //g" $folder/procedures.sql
if [[ -z $fort_area_name ]] ;then
sed -i 's/and a.name not in (fortarray)//g' $folder/procedures.sql
else
fortarray=$(echo "'$fort_area_name'" | sed "s/ /','/g")
sed -i "s/fortarray/$fortarray/g" $folder/procedures.sql
fi
else
sed -i "s/-- nonKoji //g" $folder/procedures.sql
fi
# Load procedure
if [[ -z $golbat_host ]] ;then
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb < $folder/procedures.sql
else
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$golbat_host -P$dbport $scannerdb < $folder/procedures.sql
fi
echo ""
# copy sql to cron_files
echo "Creating sql files"
echo ""
rm -f $folder/cron_files/*.sql
cp $folder/default_files/5_worker.sql.default $folder/cron_files/5_worker.sql
cp $folder/default_files/15_drago_account.sql.default $folder/cron_files/15_drago_account.sql
if [[ -z $golbat_host ]] ;then
cp $folder/default_files/5_mon_area.sql.default $folder/cron_files/5_mon_area.sql
else
cp $folder/default_files/5_mon_area_external.sql.default $folder/cron_files/5_mon_area_external.sql
fi
sed -i "s/blissey\./$blisseydb\./g" $folder/cron_files/*.sql
sed -i "s/golbat\./$scannerdb\./g" $folder/cron_files/*.sql
sed -i "s/dragonite\./$controllerdb\./g" $folder/cron_files/*.sql
# Create crontab file
echo "Creating crontab.txt in $folder"
cp $folder/default_files/crontab.txt.default $folder/crontab.txt
sed -i "s,blisseypath,$folder,g" $folder/crontab.txt
echo ""
# Remove old grafana folder
rm -rf $folder/grafana
if [[ -z $use_koji || $use_koji == "false" ]] ;then
# Add/update st data to table geofences
echo "Update geometry data for mon fences"
echo ""
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
coords=$(echo $line | awk 'BEGIN { FS = "|" } ; {$1=$2=""}1')
reverse=$(echo $coords | sed 's/,/\n/g' | awk '{ print $2,$1}' | tr '\n' ',' | sed 's/,*$//g')
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "update geofences set st=st_geomfromtext('POLYGON(($coords))'), st_lonlat=st_geomfromtext('POLYGON(($reverse))') where area = '$area' and fence = '$fence' and type = 'mon' ;"
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,coords from geofences where type = 'mon';" | sed 's/\t/|/g')
echo "Update geometry data for quest fences"
echo ""
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
coords=$(echo $line | awk 'BEGIN { FS = "|" } ; {$1=$2=""}1')
reverse=$(echo $coords | sed 's/,/\n/g' | awk '{ print $2,$1}' | tr '\n' ',' | sed 's/,*$//g')
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "update geofences set st=st_geomfromtext('POLYGON(($coords))'), st_lonlat=st_geomfromtext('POLYGON(($reverse))') where area = '$area' and fence = '$fence' and type = 'quest' ;"
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,coords from geofences where type = 'quest';" | sed 's/\t/|/g')
echo "Update geometry data for both fences"
echo ""
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
coords=$(echo $line | awk 'BEGIN { FS = "|" } ; {$1=$2=""}1')
reverse=$(echo $coords | sed 's/,/\n/g' | awk '{ print $2,$1}' | tr '\n' ',' | sed 's/,*$//g')
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "update geofences set st=st_geomfromtext('POLYGON(($coords))'), st_lonlat=st_geomfromtext('POLYGON(($reverse))') where area = '$area' and fence = '$fence' and type = 'both' ;"
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,coords from geofences where type = 'both';" | sed 's/\t/|/g')
# Append/update km2
echo "Update km2 on table geofences"
echo ""
if [[ $(dpkg -s geographiclib-tools | grep 'Status: install ok installed' | wc -l) == 1 ]] ;then
mkdir -p $folder/tmp/
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
type=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $3}')
polygon=$(echo $line |awk 'BEGIN { FS = "|" } ; $1=$2=$3=""; {print $0}' | xargs)
echo $polygon | sed 's/, /,/g' | tr ',' '\n' > $folder/tmp/coord.txt
km2=$(Planimeter --input-file $folder/tmp/coord.txt | awk '{ print $3/1000000 }' | sed 's/-//g')
# echo "Updating $area, $fence, $type to $km2 km2"
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -e "update geofences set km2='$km2' where area='$area' and fence='$fence' and type ='$type'"
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,type,coords from geofences;" | sed 's/\t/|/g')
else
echo "make sure to install geographiclib-tools to add km2 to geofences table"
echo ""
fi
# Append/update utcoffset+country
echo "Update utcoffset and country on table geofences"
echo ""
get_tz(){
curl -s -k -L --fail --show-error https://api.wheretheiss.at/v1/coordinates/$1
}
# get tz data based of quest fences first to limit the api calls
counter=0
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
type=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $3}')
coord=$(echo $line | awk 'BEGIN { FS = "|" } ; $1=$2=$3=""; {print $0}' | awk '{ sub(/^[ \t]+/, ""); print }' | sed 's/ /,/g')
if (( $counter >= 300 )) ;then
echo ""
echo "We called the api 300 times, sleeping 60s"
sleep 60s
counter=0
fi
# call api for tz data
result=$(get_tz $coord)
# get offset
if [[ -z $result ]] ;then
echo "whoops, no result returned, exiting script"
exit 1
else
offset=$(echo $result | grep -oP '(?<=offset":).*(?=,"country)')
country=$(echo $result | grep -oP '(?<=country_code":).*(?=,"map)')
fi
# add offset to db
if [[ -z $offset ]] ;then
echo "No offset found, exit script"
exit 1
else
area=$(echo $area | awk -F'_' '{ print $1 }')
# echo "Appending utc offset and country to all fences of $area, $offset hours, $country"
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -e "update geofences set utcoffset = $offset, country = $country where area like concat('$area','%')"
fi
# echo $area, $fence, $type, $coord, $offset, $country
counter=$((counter+1))
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,type,st_astext(st_centroid(st)) as coord from geofences where type = 'quest'" | sed 's/POINT(//g' | sed 's/)//g' | sed 's/\t/|/g')
#echo "Check if we missed any offsets"
#echo ""
# get tz data based of missed fences
counter=0
while read -r line ;do
area=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $1}')
fence=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $2}')
type=$(echo $line | awk 'BEGIN { FS = "|" } ; {print $3}')
coord=$(echo $line | awk 'BEGIN { FS = "|" } ; $1=$2=$3=""; {print $0}' | awk '{ sub(/^[ \t]+/, ""); print }' | sed 's/ /,/g')
if (( $counter >= 300 )) ;then
echo ""
echo "We called the api 300 times, sleeping 60s"
sleep 60s
counter=0
fi
# call api for tz data
result=$(get_tz $coord)
# get offset+country
if [[ -z $result ]] ;then
echo "whoops, no result returned, exiting script"
exit 1
else
offset=$(echo $result | grep -oP '(?<=offset":).*(?=,"country)')
country=$(echo $result | grep -oP '(?<=country_code":).*(?=,"map)')
fi
# add offset+country to db
if [[ -z $offset ]] ;then
echo "No offset found, exit script"
exit 1
else
# echo "Appending utc offset and country for $area, $fence, $type to db, $offset hours, $country"
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -e "update geofences set utcoffset = $offset, country = $country where area='$area' and fence='$fence' and type='$type'"
fi
# echo $area, $fence, $type, $coord, $offset, $country
counter=$((counter+1))
done < <(MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "select area,fence,type,st_astext(st_centroid(st)) as coord from geofences where utcoffset is NULL" | sed 's/POINT(//g' | sed 's/)//g' | sed 's/\t/|/g')
fi
#create procedure input file, if needed
if [[ ! -z $golbat_host ]] && [[ $use_koji == "false" ]] && [[ $questareastats == "true" ]] ;then
MYSQL_PWD=$sqlpass mysql -u$sqluser -h$dbip -P$dbport $blisseydb -NB -e "SET @row_number = 0; select concat((@row_number:=@row_number + 1),'|',area,'|',fence,'|',astext(st_lonlat)) from geofences where type='quest' or type='both';" > $folder/cron_files/questfences.txt
fi
echo ""
echo "All done!"