Files
zeekay 10e6fec4c0 debrand: the brand token is INSIGHTS_, and a wrong one stops the container
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO
split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live
pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the
container entrypoint, so the misspelling does not degrade a feature -- it runs
migrate --check against a database whose history this tree does not share and
the container never starts.

Both replacements are measured against the shipped tree rather than chosen:
every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL ->
INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12
determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_
tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of
the bad rule and cannot collide with an upstream name.

The rule is fixed for the next convergence, and the tree it already produced is
repaired in the same commit -- the codemod is idempotent against its own output,
so it cannot undo this itself.
2026-08-01 17:42:28 -07:00

229 lines
5.6 KiB
Go

package core
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
const insightsRepoURL = "https://github.com/Insights/insights.git"
// Function variables for testability - tests swap these to mock command execution
var (
runCmd = RunCommand
runCmdDir = RunCommandWithDir
)
func CloneInsights() error {
logger := GetLogger()
if DirExists("insights") {
logger.WriteString("Insights repository already exists\n")
return nil
}
logger.WriteString("Cloning Insights repository...\n")
_, err := runCmd("git", "clone", "--filter=blob:none", insightsRepoURL)
if err == nil {
logger.WriteString("Repository cloned successfully\n")
}
return err
}
func UpdateInsights() error {
logger := GetLogger()
if !DirExists("insights") {
return CloneInsights()
}
logger.WriteString("Fetching latest changes...\n")
_, err := runCmdDir("insights", "git", "fetch", "--prune")
if err != nil {
return err
}
// Check if on a branch before pulling - detached HEAD can't pull
// This happens when CI checks out a specific commit SHA
branch, _ := runCmdDir("insights", "git", "branch", "--show-current")
if strings.TrimSpace(branch) == "" {
logger.WriteString("On detached HEAD, skipping pull (CheckoutVersion will handle it)\n")
return nil
}
logger.WriteString("Pulling updates...\n")
_, err = runCmdDir("insights", "git", "pull")
return err
}
func CheckoutVersion(version string) error {
logger := GetLogger()
logger.Debug("CheckoutVersion called with version=%q", version)
if !DirExists("insights") {
logger.Debug("insights directory not found")
return fmt.Errorf("insights directory not found")
}
switch version {
case "latest":
logger.Debug("Checking out latest (HEAD of current branch)")
return checkoutLatest()
case "latest-release":
logger.Debug("Checking out latest release tag")
return checkoutLatestRelease()
default:
logger.Debug("Checking out specific version: %s", version)
return checkoutSpecific(version)
}
}
func checkoutLatest() error {
if _, err := runCmdDir("insights", "git", "fetch", "origin"); err != nil {
return err
}
branch, err := runCmdDir("insights", "git", "branch", "--show-current")
if err != nil {
return err
}
branch = strings.TrimSpace(branch)
if branch != "" {
_, err = runCmdDir("insights", "git", "reset", "--hard", "origin/"+branch)
}
return err
}
func checkoutLatestRelease() error {
if _, err := runCmdDir("insights", "git", "fetch", "--tags"); err != nil {
return err
}
out, err := runCmdDir("insights", "git", "describe", "--tags", "--abbrev=0")
if err != nil {
out, err = runCmdDir("insights", "sh", "-c", "git describe --tags $(git rev-list --tags --max-count=1)")
if err != nil {
return fmt.Errorf("no release tags found")
}
}
tag := strings.TrimSpace(out)
_, err = runCmdDir("insights", "git", "checkout", tag)
return err
}
func checkoutSpecific(version string) error {
isCommit := regexp.MustCompile(`^[0-9a-f]{40}$`).MatchString(version)
if isCommit {
_, err := runCmdDir("insights", "git", "checkout", version)
return err
}
if _, err := runCmdDir("insights", "git", "fetch", "--tags"); err != nil {
return err
}
releaseTag := strings.TrimPrefix(version, "release-")
_, err := runCmdDir("insights", "git", "checkout", releaseTag)
return err
}
func GetCurrentCommit() (string, error) {
out, err := runCmdDir("insights", "git", "rev-parse", "--short", "HEAD")
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
func CopyComposeFiles() error {
logger := GetLogger()
logger.Debug("CopyComposeFiles")
_ = os.Remove("docker-compose.yml") // Ignore error if file doesn't exist
if err := copyFile("insights/docker-compose.base.yml", "docker-compose.base.yml"); err != nil {
logger.Debug("Failed to copy docker-compose.base.yml: %v", err)
return err
}
// Copy the shared service-connection env file that docker-compose.base.yml references via env_file.
if err := copyFile("insights/.env.services", ".env.services"); err != nil {
logger.Debug("Failed to copy .env.services: %v", err)
return err
}
// Copy as-is — Docker Compose reads REGISTRY_URL, INSIGHTS_APP_TAG, and
// INSIGHTS_NODE_TAG from .env at runtime, so no substitution needed here.
return copyFile("insights/docker-compose.hobby.yml", "docker-compose.yml")
}
func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0644)
}
func CreateComposeScripts() error {
if err := os.MkdirAll("compose", 0755); err != nil {
return err
}
startScript := `#!/bin/bash
./compose/wait
./bin/migrate
./bin/docker-server
`
if err := os.WriteFile("compose/start", []byte(startScript), 0755); err != nil {
return err
}
temporalScript := `#!/bin/bash
./bin/temporal-django-worker
`
if err := os.WriteFile("compose/temporal-django-worker", []byte(temporalScript), 0755); err != nil {
return err
}
waitScript := `#!/usr/bin/env python3
import socket
import time
def loop():
print("Waiting for Datastore and Postgres to be ready")
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('datastore', 9000))
print("Datastore is ready")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('db', 5432))
print("Postgres is ready")
except ConnectionRefusedError as e:
time.sleep(5)
loop()
loop()
`
return os.WriteFile("compose/wait", []byte(waitScript), 0755)
}
func SetupGit() error {
logger := GetLogger()
if _, err := exec.LookPath("git"); err == nil {
logger.Debug("git already installed")
return nil
}
logger.Debug("Installing git")
cmd := exec.Command("apt", "install", "-y", "git")
return cmd.Run()
}