| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | RED='\033[0;31m' |
| 6 | YELLOW='\033[1;33m' |
| 7 | GRAY='\033[0;90m' |
| 8 | NC='\033[0m' |
| 9 | TOPOLOGY_IP_INTEL_STOCK_TMPDIR="" |
| 10 | |
| 11 | run() { |
| 12 | printf >&2 "${GRAY}%s >${NC} " "$(pwd)" |
| 13 | printf >&2 "${YELLOW}" |
| 14 | printf >&2 "%q " "$@" |
| 15 | printf >&2 "${NC}\n" |
| 16 | |
| 17 | if "$@"; then |
| 18 | return 0 |
| 19 | else |
| 20 | local exit_code=$? |
| 21 | echo -e >&2 "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" |
| 22 | echo -e >&2 "${RED}[ERROR]${NC} Command failed with exit code ${exit_code}: ${YELLOW}$1${NC}" |
| 23 | echo -e >&2 "${RED} Full command:${NC} $*" |
| 24 | echo -e >&2 "${RED} Working dir:${NC} $(pwd)" |
| 25 | echo -e >&2 "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" |
| 26 | return "${exit_code}" |
| 27 | fi |
| 28 | } |
| 29 | |
| 30 | cleanup_topology_ip_intel_stock_tmpdir() { |
| 31 | if [ -n "${TOPOLOGY_IP_INTEL_STOCK_TMPDIR:-}" ]; then |
| 32 | rm -rf -- "${TOPOLOGY_IP_INTEL_STOCK_TMPDIR}" |
| 33 | fi |
| 34 | } |
| 35 | |
| 36 | trap cleanup_topology_ip_intel_stock_tmpdir EXIT |
| 37 | |
| 38 | usage() { |
| 39 | cat <<'EOF' |
| 40 | Usage: prepare-topology-ip-intel-stock.sh --mode synthetic|release --output-dir DIR |
| 41 | |
| 42 | Modes: |
| 43 | synthetic Create a tiny deterministic ASN/GEO stock payload for PR package checks. |
| 44 | release Build the real DB-IP ASN + GEO stock payload used for package/release builds. |
| 45 | EOF |
| 46 | } |
| 47 | |
| 48 | MODE="" |
| 49 | OUTPUT_DIR="" |
| 50 | |
| 51 | while [ "$#" -gt 0 ]; do |
| 52 | case "$1" in |
| 53 | --mode) |
| 54 | MODE="${2:-}" |
| 55 | shift 2 |
| 56 | ;; |
| 57 | --output-dir) |
| 58 | OUTPUT_DIR="${2:-}" |
| 59 | shift 2 |
| 60 | ;; |
| 61 | -h|--help) |
| 62 | usage |
| 63 | exit 0 |
| 64 | ;; |
| 65 | *) |
| 66 | printf >&2 "Unknown argument: %s\n" "$1" |
| 67 | usage >&2 |
| 68 | exit 1 |
| 69 | ;; |
| 70 | esac |
| 71 | done |
| 72 | |
| 73 | if [ -z "${MODE}" ] || [ -z "${OUTPUT_DIR}" ]; then |
| 74 | usage >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | case "${OUTPUT_DIR}" in |
| 79 | /*) ;; |
| 80 | *) |
| 81 | OUTPUT_DIR="$(pwd)/${OUTPUT_DIR}" |
| 82 | ;; |
| 83 | esac |
| 84 | |
| 85 | SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" |
| 86 | REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." && pwd)" |
| 87 | DOWNLOADER_DIR="${REPO_ROOT}/src/go" |
| 88 | DOWNLOADER_CONFIG="${REPO_ROOT}/src/go/tools/topology-ip-intel-downloader/configs/topology-ip-intel.yaml" |
| 89 | STOCK_README="${REPO_ROOT}/src/go/tools/topology-ip-intel-downloader/stock/README.md" |
| 90 | |
| 91 | case "${MODE}" in |
| 92 | synthetic|release) ;; |
| 93 | *) |
| 94 | printf >&2 "Unsupported mode: %s\n" "${MODE}" |
| 95 | usage >&2 |
| 96 | exit 1 |
| 97 | ;; |
| 98 | esac |
| 99 | |
| 100 | run mkdir -p "${OUTPUT_DIR}" |
| 101 | run rm -f \ |
| 102 | "${OUTPUT_DIR}/README.md" \ |
| 103 | "${OUTPUT_DIR}/topology-ip-asn.mmdb" \ |
| 104 | "${OUTPUT_DIR}/topology-ip-geo.mmdb" \ |
| 105 | "${OUTPUT_DIR}/topology-ip-intel.json" |
| 106 | |
| 107 | prepare_synthetic() { |
| 108 | local tmpdir |
| 109 | tmpdir="$(mktemp -d)" |
| 110 | TOPOLOGY_IP_INTEL_STOCK_TMPDIR="${tmpdir}" |
| 111 | |
| 112 | run tee "${tmpdir}/asn.csv" >/dev/null <<'EOF' |
| 113 | 1.1.1.0,1.1.1.255,13335,"Cloudflare, Inc." |
| 114 | 8.8.8.0,8.8.8.255,15169,Google LLC |
| 115 | 203.0.113.0,203.0.113.255,64500,Example Transit |
| 116 | EOF |
| 117 | |
| 118 | run tee "${tmpdir}/geo.csv" >/dev/null <<'EOF' |
| 119 | 1.1.1.0,1.1.1.255,,AU,Queensland,South Brisbane,-27.4748,153.0170 |
| 120 | 8.8.8.0,8.8.8.255,,US,California,Mountain View,37.4220,-122.0850 |
| 121 | 203.0.113.0,203.0.113.255,,US,Virginia,Ashburn,39.0438,-77.4874 |
| 122 | EOF |
| 123 | |
| 124 | run tee "${tmpdir}/topology-ip-intel.yaml" >/dev/null <<EOF |
| 125 | sources: |
| 126 | - name: pr-synthetic-asn |
| 127 | family: asn |
| 128 | provider: dbip |
| 129 | artifact: asn-lite |
| 130 | format: csv |
| 131 | path: ${tmpdir}/asn.csv |
| 132 | - name: pr-synthetic-geo |
| 133 | family: geo |
| 134 | provider: dbip |
| 135 | artifact: city-lite |
| 136 | format: csv |
| 137 | path: ${tmpdir}/geo.csv |
| 138 | EOF |
| 139 | |
| 140 | cd "${DOWNLOADER_DIR}" |
| 141 | run go run ./tools/topology-ip-intel-downloader --config "${tmpdir}/topology-ip-intel.yaml" --output-dir "${OUTPUT_DIR}" |
| 142 | } |
| 143 | |
| 144 | prepare_release() { |
| 145 | cd "${DOWNLOADER_DIR}" |
| 146 | run go run ./tools/topology-ip-intel-downloader --config "${DOWNLOADER_CONFIG}" --output-dir "${OUTPUT_DIR}" |
| 147 | } |
| 148 | |
| 149 | case "${MODE}" in |
| 150 | synthetic) |
| 151 | prepare_synthetic |
| 152 | ;; |
| 153 | release) |
| 154 | prepare_release |
| 155 | ;; |
| 156 | esac |
| 157 | |
| 158 | run cp "${STOCK_README}" "${OUTPUT_DIR}/README.md" |