mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
geoIPURL = "https://mmdbcdn.insights.net/"
|
|
shareDir = "./share"
|
|
mmdbFile = "./share/GeoLite2-City.mmdb"
|
|
jsonFile = "./share/GeoLite2-City.json"
|
|
)
|
|
|
|
func DownloadGeoIP() error {
|
|
logger := GetLogger()
|
|
logger.Debug("DownloadGeoIP: shareDir=%s, mmdbFile=%s", shareDir, mmdbFile)
|
|
|
|
if err := os.MkdirAll(shareDir, 0755); err != nil {
|
|
logger.Debug("Failed to create share directory: %v", err)
|
|
return fmt.Errorf("failed to create share directory: %w", err)
|
|
}
|
|
|
|
if FileExists(mmdbFile) {
|
|
logger.Debug("GeoIP database already exists at %s", mmdbFile)
|
|
logger.WriteString("GeoIP database already exists\n")
|
|
return nil
|
|
}
|
|
|
|
if err := installGeoIPDeps(); err != nil {
|
|
logger.Debug("Failed to install GeoIP deps: %v", err)
|
|
return err
|
|
}
|
|
|
|
logger.WriteString("Downloading GeoIP database...\n")
|
|
logger.Debug("Downloading from %s", geoIPURL)
|
|
cmd := exec.Command("sh", "-c",
|
|
fmt.Sprintf("curl -L '%s' --http1.1 | brotli --decompress --output=%s", geoIPURL, mmdbFile))
|
|
if err := cmd.Run(); err != nil {
|
|
logger.Debug("GeoIP download failed: %v", err)
|
|
return fmt.Errorf("failed to download GeoIP database: %w", err)
|
|
}
|
|
logger.WriteString("GeoIP database downloaded\n")
|
|
|
|
jsonContent := fmt.Sprintf(`{"date": "%s"}`, time.Now().Format("2006-01-02"))
|
|
if err := os.WriteFile(jsonFile, []byte(jsonContent), 0644); err != nil {
|
|
logger.Debug("Failed to write GeoIP metadata: %v", err)
|
|
return fmt.Errorf("failed to write GeoIP metadata: %w", err)
|
|
}
|
|
|
|
if err := os.Chmod(mmdbFile, 0644); err != nil {
|
|
return fmt.Errorf("failed to set permissions on GeoIP database: %w", err)
|
|
}
|
|
if err := os.Chmod(jsonFile, 0644); err != nil {
|
|
return fmt.Errorf("failed to set permissions on GeoIP metadata: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func installGeoIPDeps() error {
|
|
logger := GetLogger()
|
|
if _, err := exec.LookPath("brotli"); err == nil {
|
|
logger.Debug("brotli already installed")
|
|
return nil
|
|
}
|
|
if err := RequireRoot("install GeoIP dependencies"); err != nil {
|
|
return err
|
|
}
|
|
if err := exec.Command("apt-get", "update").Run(); err != nil {
|
|
return fmt.Errorf("failed to update apt cache: %w", err)
|
|
}
|
|
logger.Debug("Installing brotli")
|
|
cmd := exec.Command("apt-get", "install", "-y", "--no-install-recommends", "brotli")
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("failed to install brotli: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GeoIPExists() bool {
|
|
return FileExists(mmdbFile)
|
|
}
|