-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo.sh
More file actions
250 lines (228 loc) · 8.1 KB
/
Copy pathgeo.sh
File metadata and controls
250 lines (228 loc) · 8.1 KB
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
set -e
# GeoForge
#
# Standard Go layout:
# cmd/builder/ -- entrypoint
# internal/ -- consensus, geozip, output, refdata, strnorm
# data/ -- input databases (DB-IP, MaxMind, IP2Location, Sypex)
# release/ -- output (geo.mmdb)
#
# Run from the project root.
RESET='\033[0m'
BOLD='\033[1m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CYAN='\033[36m'
GRAY='\033[90m'
RED='\033[31m'
print_header() {
echo ""
echo -e "${BOLD}${BLUE}+--------------------------------------------------------------+${RESET}"
echo -e "${BOLD}${BLUE}| GeoForge |${RESET}"
echo -e "${BOLD}${BLUE}| Consensus GeoIP Compiler | Offline | Multi-Source |${RESET}"
echo -e "${BOLD}${BLUE}+--------------------------------------------------------------+${RESET}"
echo ""
}
print_section() { echo -e "${BOLD}${CYAN}>> $1${RESET}"; }
print_ok() { echo -e "${GREEN} +${RESET} $1"; }
print_warn() { echo -e "${YELLOW} !${RESET} $1"; }
print_error() { echo -e "${RED} x${RESET} $1"; }
print_info() { echo -e "${GRAY} i $1${RESET}"; }
print_divider() { echo -e "${GRAY} ------------------------------------------------------------${RESET}"; }
acquire_lock() {
mkdir -p release
LOCK_FILE="release/builder.lock"
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
print_error "Another builder is already running."
print_info "Lock: ${LOCK_FILE}"
print_info "Wait for the current build to finish, or stop the stale process before retrying."
exit 1
fi
print_ok "Build lock acquired: ${LOCK_FILE}"
}
check_go() {
print_section "Environment Check"
if ! command -v go &> /dev/null; then
print_error "Go not installed!"
print_info "Install: https://go.dev/doc/install"
exit 1
fi
GO_VERSION=$(go version | awk '{print $3}')
print_ok "Go installed: ${BOLD}${GO_VERSION}${RESET}"
}
check_files() {
print_section "File Check"
# Required source files (all under cmd/ and internal/)
SCRIPTS=(
"cmd/builder/main.go"
"internal/consensus/consensus.go"
"internal/consensus/matching.go"
"internal/consensus/validators.go"
"internal/geozip/resolver.go"
"internal/output/normalize.go"
"internal/refdata/countries.go"
"internal/refdata/priorities.go"
"internal/strnorm/strnorm.go"
"go.mod"
)
DATA_FILES=("dbip-city-lite.csv" "GeoLite2-City.mmdb" "IP2LOCATION-LITE-DB5.BIN" "SxGeoCity.dat")
local missing_scripts=0
local missing_data=0
echo ""
echo -e " ${BOLD}Source code:${RESET}"
for file in "${SCRIPTS[@]}"; do
if [ -f "$file" ]; then
local size=$(du -h "$file" 2>/dev/null | cut -f1)
print_ok "${file} ${GRAY}(${size})${RESET}"
else
print_error "${file} ${RED}(NOT FOUND)${RESET}"
missing_scripts=1
fi
done
mkdir -p data release
echo ""
echo -e " ${BOLD}Databases (./data/):${RESET}"
for file in "${DATA_FILES[@]}"; do
if [ -f "data/${file}" ]; then
local size=$(du -h "data/${file}" 2>/dev/null | cut -f1)
print_ok "data/${file} ${GRAY}(${size})${RESET}"
else
print_warn "data/${file} ${YELLOW}(will be skipped)${RESET}"
missing_data=1
fi
done
if [ $missing_scripts -eq 1 ]; then
echo ""
print_error "Missing required source files!"
exit 1
fi
if [ $missing_data -eq 1 ]; then
echo ""
print_warn "Some databases missing -- continuing with available"
fi
print_divider
}
download_data() {
DATA_CHANGED=1
if [ "${AUTO_DOWNLOAD:-1}" != "1" ]; then
print_info "AUTO_DOWNLOAD disabled"
return
fi
print_section "Data Download"
if [ -x "scripts/download_data.sh" ]; then
scripts/download_data.sh
else
bash scripts/download_data.sh
fi
if [ -f "data/download-changed.txt" ]; then
DATA_CHANGED=$(cat data/download-changed.txt)
fi
print_divider
}
install_deps() {
print_section "Installing Dependencies"
echo -ne " ${GRAY}-> go mod download...${RESET}"
go mod download &>/dev/null
echo -e "\r ${GREEN}+${RESET} go mod download ${GRAY}OK${RESET} "
echo -ne " ${GRAY}-> go mod tidy...${RESET}"
go mod tidy &>/dev/null
echo -e "\r ${GREEN}+${RESET} go mod tidy ${GRAY}OK${RESET} "
print_ok "Dependencies ready"
print_divider
}
run_tests() {
print_section "Tests"
echo -ne " ${GRAY}Running unit tests...${RESET}"
if go test ./... &>/tmp/test.log; then
echo -e "\r ${GREEN}+${RESET} All tests passed "
else
echo -e "\r ${RED}x Tests failed!${RESET} "
cat /tmp/test.log
exit 1
fi
print_divider
}
compile() {
print_section "Compilation"
echo -ne " ${GRAY}Building ./builder...${RESET}"
if go build -o builder ./cmd/builder 2>&1; then
local size=$(du -h builder 2>/dev/null | cut -f1)
echo -e "\r ${GREEN}+${RESET} Compiled: ${BOLD}builder${RESET} ${GRAY}(${size})${RESET} "
else
echo -e "\r ${RED}x Compilation error!${RESET} "
exit 1
fi
print_divider
}
snapshot_previous_release() {
if [ -s "release/geo.csv" ]; then
cp release/geo.csv release/geo.previous.csv
print_ok "Previous CSV snapshot saved: release/geo.previous.csv"
else
print_info "No previous release/geo.csv snapshot found"
fi
}
run_builder() {
print_section "Database Generation"
echo ""
echo -e "${BOLD}${CYAN} ------------------------------------------------------------${RESET}"
./builder
echo -e "${BOLD}${CYAN} ------------------------------------------------------------${RESET}"
echo ""
}
run_quality_check() {
print_section "Quality Check"
local strict_flag=""
if [ "${QUALITY_STRICT:-0}" = "1" ]; then
strict_flag="-strict"
fi
echo -ne " ${GRAY}Checking release quality...${RESET}"
if go run ./cmd/qualitycheck ${strict_flag} > /tmp/geo-quality.log 2>&1; then
echo -e "\r ${GREEN}+${RESET} Quality report written: release/geo-quality-report.txt "
else
echo -e "\r ${RED}x Quality check failed${RESET} "
cat /tmp/geo-quality.log
exit 1
fi
if grep -q "previous release CSV not found" /tmp/geo-quality.log; then
print_info "First quality baseline; next build will compare against this release"
fi
print_divider
}
print_footer() {
local output_file="release/geo.mmdb"
if [ -f "$output_file" ]; then
local size=$(du -h "$output_file" 2>/dev/null | cut -f1)
echo -e "${BOLD}${GREEN}+--------------------------------------------------------------+${RESET}"
echo -e "${BOLD}${GREEN}| DONE! File: ${output_file} ${GRAY}(${size})${GREEN} |${RESET}"
echo -e "${BOLD}${GREEN}+--------------------------------------------------------------+${RESET}"
else
echo -e "${BOLD}${YELLOW}+--------------------------------------------------------------+${RESET}"
echo -e "${BOLD}${YELLOW}| Warning: output file not found |${RESET}"
echo -e "${BOLD}${YELLOW}+--------------------------------------------------------------+${RESET}"
fi
echo ""
}
print_header
acquire_lock
export GOCACHE="${GOCACHE:-/private/tmp/geo-go-build}"
check_go
download_data
check_files
if [ "${DATA_CHANGED:-1}" = "0" ] && [ -s "release/geo.mmdb" ] && [ "${FORCE_BUILD:-0}" != "1" ]; then
print_section "Build"
print_ok "No source databases changed; keeping existing release/geo.mmdb"
print_info "Set FORCE_BUILD=1 to rebuild anyway"
print_footer
exit 0
fi
install_deps
run_tests
compile
snapshot_previous_release
run_builder
run_quality_check
print_footer