Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(hobby): backfill CADDY_HOST on upgrade for older installs (#114228)

posthog[bot]1141f8c3aaa7
diff --git a/bin/hobby-installer/core/env.go b/bin/hobby-installer/core/env.go
index 9cb8bdbc144e..071a05dae666 100644
--- a/bin/hobby-installer/core/env.go
+++ b/bin/hobby-installer/core/env.go
@@ -107,6 +107,8 @@ func LoadExistingEnv() map[string]string {
"ENCRYPTION_SALT_KEYS",
"DOMAIN",
"TLS_BLOCK",
+ "CADDY_HOST",
+ "CADDY_TLS_BLOCK",
"REGISTRY_URL",
"POSTHOG_APP_TAG",
"POSTHOG_NODE_TAG",
@@ -138,13 +140,19 @@ func UpdateEnvValue(key, value string) error {
if strings.HasPrefix(line, prefix) {
lines[i] = prefix + value
found = true
- break
}
}
if !found {
+ if len(lines) > 0 && lines[len(lines)-1] == "" {
+ lines = lines[:len(lines)-1]
+ }
lines = append(lines, prefix+value)
}
- return os.WriteFile(".env", []byte(strings.Join(lines, "\n")), 0600)
+ updated := strings.Join(lines, "\n")
+ if !strings.HasSuffix(updated, "\n") {
+ updated += "\n"
+ }
+ return os.WriteFile(".env", []byte(updated), 0600)
}
func UpdateEnvForUpgrade(version string) error {
@@ -179,6 +187,19 @@ func UpdateEnvForUpgrade(version string) error {
}
}
+ // Compose fills the proxy Caddyfile from .env, so older installs without these keys serve http://localhost:8000.
+ if existing["CADDY_HOST"] == "" && existing["DOMAIN"] != "" {
+ if err := UpdateEnvValue("CADDY_HOST", fmt.Sprintf(`"%s, http://, https://"`, existing["DOMAIN"])); err != nil {
+ return err
+ }
+ }
+
+ if existing["CADDY_TLS_BLOCK"] == "" && existing["TLS_BLOCK"] != "" {
+ if err := UpdateEnvValue("CADDY_TLS_BLOCK", fmt.Sprintf(`"%s"`, existing["TLS_BLOCK"])); err != nil {
+ return err
+ }
+ }
+
if version != "" {
if err := UpdateEnvValue("POSTHOG_APP_TAG", version); err != nil {
return err
diff --git a/bin/hobby-installer/core/env_test.go b/bin/hobby-installer/core/env_test.go
index c98cfc4c5b83..86fe09f49eab 100644
--- a/bin/hobby-installer/core/env_test.go
+++ b/bin/hobby-installer/core/env_test.go
@@ -2,6 +2,7 @@ package core
import (
"os"
+ "strings"
"testing"
)
@@ -53,3 +54,99 @@ func TestDedicatedSecretsAreGeneratedAndKept(t *testing.T) {
}
}
}
+
+func TestUpdateEnvForUpgradeCaddyKeys(t *testing.T) {
+ tests := []struct {
+ name string
+ env string
+ want []string
+ notWant []string
+ }{
+ {
+ name: "backfills caddy keys from domain and tls block",
+ env: "DOMAIN=posthog.example.com\nTLS_BLOCK=\"acme_ca https://acme.example.com/directory\"\n",
+ want: []string{
+ `CADDY_HOST="posthog.example.com, http://, https://"`,
+ `CADDY_TLS_BLOCK="acme_ca https://acme.example.com/directory"`,
+ },
+ },
+ {
+ name: "keeps existing caddy host",
+ env: "DOMAIN=posthog.example.com\nCADDY_HOST=\"custom.example.com\"\n",
+ want: []string{`CADDY_HOST="custom.example.com"`},
+ notWant: []string{"posthog.example.com, http://"},
+ },
+ {
+ name: "replaces blank caddy keys",
+ env: "DOMAIN=posthog.example.com\nTLS_BLOCK=internal\nCADDY_HOST=\nCADDY_TLS_BLOCK=\"\"\n",
+ want: []string{
+ `CADDY_HOST="posthog.example.com, http://, https://"`,
+ `CADDY_TLS_BLOCK="internal"`,
+ },
+ notWant: []string{"CADDY_HOST=\n", "CADDY_TLS_BLOCK=\"\"\n"},
+ },
+ {
+ name: "replaces quoted empty caddy host without trailing newline",
+ env: "DOMAIN=posthog.example.com\nCADDY_HOST=''",
+ want: []string{`CADDY_HOST="posthog.example.com, http://, https://"`},
+ },
+ {
+ name: "replaces the effective blank duplicate",
+ env: "DOMAIN=posthog.example.com\nCADDY_HOST=custom.example.com\nCADDY_HOST=\n",
+ want: []string{`CADDY_HOST="posthog.example.com, http://, https://"`},
+ },
+ {
+ name: "keeps the effective custom duplicate",
+ env: "DOMAIN=posthog.example.com\nCADDY_HOST=\nCADDY_HOST=custom.example.com\n",
+ want: []string{`CADDY_HOST=custom.example.com`},
+ },
+ {
+ name: "skips caddy keys without domain or tls block",
+ env: "TLS_BLOCK=\n",
+ notWant: []string{"CADDY_HOST=", "CADDY_TLS_BLOCK="},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Chdir(t.TempDir())
+ if err := os.WriteFile(".env", []byte(tt.env), 0600); err != nil {
+ t.Fatal(err)
+ }
+
+ if err := UpdateEnvForUpgrade(""); err != nil {
+ t.Fatal(err)
+ }
+ firstData, err := os.ReadFile(".env")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := UpdateEnvForUpgrade(""); err != nil {
+ t.Fatal(err)
+ }
+
+ data, err := os.ReadFile(".env")
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := string(data)
+ if got != string(firstData) {
+ t.Errorf("expected second upgrade to keep .env unchanged, got:\n%s", got)
+ }
+ for _, w := range tt.want {
+ if !strings.Contains(got, w+"\n") {
+ t.Errorf("expected .env to contain %q, got:\n%s", w, got)
+ }
+ parts := strings.SplitN(w, "=", 2)
+ if actual := ReadEnvValue(parts[0]); actual != strings.Trim(parts[1], "\"'") {
+ t.Errorf("expected effective %s value %q, got %q", parts[0], parts[1], actual)
+ }
+ }
+ for _, nw := range tt.notWant {
+ if strings.Contains(got, nw) {
+ t.Errorf("expected .env not to contain %q, got:\n%s", nw, got)
+ }
+ }
+ })
+ }
+}
diff --git a/bin/hobby-installer/core/system.go b/bin/hobby-installer/core/system.go
index e5543b108cd2..0d291441b9a7 100644
--- a/bin/hobby-installer/core/system.go
+++ b/bin/hobby-installer/core/system.go
@@ -93,23 +93,31 @@ func ReadEnvValue(key string) string {
return ""
}
lines := strings.Split(string(data), "\n")
+ value := ""
for _, line := range lines {
if strings.HasPrefix(line, key+"=") {
- value := strings.TrimPrefix(line, key+"=")
- value = strings.Trim(value, "\"'")
- return value
+ value = strings.Trim(strings.TrimPrefix(line, key+"="), "\"'")
}
}
- return ""
+ return value
}
func AppendToEnv(key, value string) error {
+ data, err := os.ReadFile(".env")
+ if err != nil {
+ return err
+ }
f, err := os.OpenFile(".env", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
+ if len(data) > 0 && data[len(data)-1] != '\n' {
+ if _, err := f.WriteString("\n"); err != nil {
+ return err
+ }
+ }
_, err = fmt.Fprintf(f, "%s=%s\n", key, value)
return err
}
diff --git a/bin/upgrade-hobby b/bin/upgrade-hobby
index c4fd0d68f577..d018d6704572 100755
--- a/bin/upgrade-hobby
+++ b/bin/upgrade-hobby
@@ -110,6 +110,26 @@ awk '
}
' .env > "$tmp" && mv "$tmp" .env
+# Compose fills the proxy Caddyfile from .env, not from the service environment.
+# Installs from before July 2025 have no CADDY_HOST, so Caddy serves http://localhost:8000 instead of DOMAIN.
+read_env_value() {
+ grep "^$1=" .env | tail -n 1 | cut -d '=' -f2- | sed -e 's/^["'\'']//' -e 's/["'\'']$//'
+}
+if [[ -f ".env" ]] && [ -z "$(read_env_value CADDY_HOST)" ]; then
+ ENV_DOMAIN=$(read_env_value DOMAIN)
+ if [ -n "$ENV_DOMAIN" ]; then
+ echo "CADDY_HOST=\"$ENV_DOMAIN, http://, https://\"" >> .env
+ echo "Added missing CADDY_HOST to .env file"
+ fi
+fi
+if [[ -f ".env" ]] && [ -z "$(read_env_value CADDY_TLS_BLOCK)" ]; then
+ ENV_TLS_BLOCK=$(read_env_value TLS_BLOCK)
+ if [ -n "$ENV_TLS_BLOCK" ]; then
+ echo "CADDY_TLS_BLOCK=\"$ENV_TLS_BLOCK\"" >> .env
+ echo "Added missing CADDY_TLS_BLOCK to .env file"
+ fi
+fi
+
if [[ -f ".env" ]]; then
set -a # auto-export all variables
. ./.env # or: source .env