Skip to content

Commit

Permalink
fix(db): make local hostname configurable for docker-in-docker (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Jan 16, 2024
1 parent 4548c83 commit 272027d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 22 deletions.
8 changes: 7 additions & 1 deletion internal/db/diff/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w
return "", err
}
fmt.Fprintln(w, "Diffing schemas:", strings.Join(schema, ","))
source := fmt.Sprintf("postgresql://postgres:[email protected]:%d/postgres", utils.Config.Db.ShadowPort)
source := utils.ToPostgresURL(pgconn.Config{
Host: utils.Config.Hostname,
Port: uint16(utils.Config.Db.ShadowPort),
User: "postgres",
Password: utils.Config.Db.Password,
Database: "postgres",
})
target := utils.ToPostgresURL(config)
return DiffSchemaMigra(ctx, source, target, schema)
}
4 changes: 2 additions & 2 deletions internal/db/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func WithSyslogConfig(hostConfig container.HostConfig) container.HostConfig {
if utils.Config.Analytics.Enabled {
hostConfig.LogConfig.Type = "syslog"
hostConfig.LogConfig.Config = map[string]string{
"syslog-address": fmt.Sprintf("tcp://127.0.0.1:%d", utils.Config.Analytics.VectorPort),
"syslog-address": fmt.Sprintf("tcp://%s:%d", utils.Config.Hostname, utils.Config.Analytics.VectorPort),
"tag": "{{.Name}}",
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func initSchema15(ctx context.Context, host string) error {
return err
}
return utils.DockerRunOnceWithStream(ctx, utils.Config.Auth.Image, []string{
fmt.Sprintf("API_EXTERNAL_URL=http://127.0.0.1:%v", utils.Config.Api.Port),
fmt.Sprintf("API_EXTERNAL_URL=http://%s:%d", host, utils.Config.Api.Port),
"GOTRUE_LOG_LEVEL=error",
"GOTRUE_DB_DRIVER=postgres",
fmt.Sprintf("GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres", utils.Config.Db.Password, host),
Expand Down
3 changes: 2 additions & 1 deletion internal/migration/squash/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ func squashMigrations(ctx context.Context, migrations []string, fsys afero.Fs, o
}
defer f.Close()
config := pgconn.Config{
Host: "127.0.0.1",
Host: utils.Config.Hostname,
Port: uint16(utils.Config.Db.ShadowPort),
User: "postgres",
Password: utils.Config.Db.Password,
Database: "postgres",
}
return dump.DumpSchema(ctx, config, nil, false, false, f)
}
Expand Down
16 changes: 9 additions & 7 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type kongConfig struct {
PgmetaId string
EdgeRuntimeId string
LogflareId string
ApiHost string
ApiPort uint
}

Expand Down Expand Up @@ -305,6 +306,7 @@ EOF
PgmetaId: utils.PgmetaId,
EdgeRuntimeId: utils.EdgeRuntimeId,
LogflareId: utils.LogflareId,
ApiHost: utils.Config.Hostname,
ApiPort: utils.Config.Api.Port,
}); err != nil {
return errors.Errorf("failed to exec template: %w", err)
Expand Down Expand Up @@ -377,7 +379,7 @@ EOF
}

env := []string{
fmt.Sprintf("API_EXTERNAL_URL=http://127.0.0.1:%v", utils.Config.Api.Port),
fmt.Sprintf("API_EXTERNAL_URL=http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port),

"GOTRUE_API_HOST=0.0.0.0",
"GOTRUE_API_PORT=9999",
Expand All @@ -394,7 +396,7 @@ EOF
"GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated",
fmt.Sprintf("GOTRUE_JWT_EXP=%v", utils.Config.Auth.JwtExpiry),
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
fmt.Sprintf("GOTRUE_JWT_ISSUER=http://127.0.0.1:%v/auth/v1", utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_JWT_ISSUER=http://%s:%d/auth/v1", utils.Config.Hostname, utils.Config.Api.Port),

fmt.Sprintf("GOTRUE_EXTERNAL_EMAIL_ENABLED=%v", utils.Config.Auth.Email.EnableSignup),
fmt.Sprintf("GOTRUE_MAILER_SECURE_EMAIL_CHANGE_ENABLED=%v", utils.Config.Auth.Email.DoubleConfirmChanges),
Expand All @@ -407,10 +409,10 @@ EOF
// TODO: To be reverted to `/auth/v1/verify` once
// https://github.com/supabase/supabase/issues/16100
// is fixed on upstream GoTrue.
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_INVITE=http://127.0.0.1:%v/auth/v1/verify", utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://127.0.0.1:%v/auth/v1/verify", utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_RECOVERY=http://127.0.0.1:%v/auth/v1/verify", utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE=http://127.0.0.1:%v/auth/v1/verify", utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_INVITE=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_RECOVERY=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
"GOTRUE_RATE_LIMIT_EMAIL_SENT=360000",

fmt.Sprintf("GOTRUE_EXTERNAL_PHONE_ENABLED=%v", utils.Config.Auth.Sms.EnableSignup),
Expand Down Expand Up @@ -501,7 +503,7 @@ EOF
)
} else {
env = append(env,
fmt.Sprintf("GOTRUE_EXTERNAL_%s_REDIRECT_URI=http://127.0.0.1:%v/auth/v1/callback", strings.ToUpper(name), utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_EXTERNAL_%s_REDIRECT_URI=http://%s:%d/auth/v1/callback", strings.ToUpper(name), utils.Config.Hostname, utils.Config.Api.Port),
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/start/templates/kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ services:
config:
add:
headers:
- "Forwarded: host=127.0.0.1:{{ .ApiPort }}/storage/v1;proto=http"
- "Forwarded: host={{ .ApiHost }}:{{ .ApiPort }}/storage/v1;proto=http"
- name: pg-meta
_comment: "pg-meta: /pg/* -> http://pg-meta:8080/*"
url: http://{{ .PgmetaId }}:8080/
Expand Down
16 changes: 8 additions & 8 deletions internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"

Expand All @@ -27,22 +28,22 @@ type CustomName struct {

func (c *CustomName) toValues(exclude ...string) map[string]string {
values := map[string]string{
c.DbURL: fmt.Sprintf("postgresql://postgres:%s@127.0.0.1:%d/postgres", utils.Config.Db.Password, utils.Config.Db.Port),
c.DbURL: fmt.Sprintf("postgresql://%s@%s:%d/postgres", url.UserPassword("postgres", utils.Config.Db.Password), utils.Config.Hostname, utils.Config.Db.Port),
}
if utils.Config.Api.Enabled && !utils.SliceContains(exclude, utils.RestId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Api.Image)) {
values[c.ApiURL] = fmt.Sprintf("http://127.0.0.1:%d", utils.Config.Api.Port)
values[c.GraphqlURL] = fmt.Sprintf("http://127.0.0.1:%d/graphql/v1", utils.Config.Api.Port)
values[c.ApiURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port)
values[c.GraphqlURL] = fmt.Sprintf("http://%s:%d/graphql/v1", utils.Config.Hostname, utils.Config.Api.Port)
}
if utils.Config.Studio.Enabled && !utils.SliceContains(exclude, utils.StudioId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.StudioImage)) {
values[c.StudioURL] = fmt.Sprintf("http://127.0.0.1:%d", utils.Config.Studio.Port)
values[c.StudioURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Studio.Port)
}
if !utils.SliceContains(exclude, utils.GotrueId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Auth.Image)) {
values[c.JWTSecret] = utils.Config.Auth.JwtSecret
values[c.AnonKey] = utils.Config.Auth.AnonKey
values[c.ServiceRoleKey] = utils.Config.Auth.ServiceRoleKey
}
if utils.Config.Inbucket.Enabled && !utils.SliceContains(exclude, utils.InbucketId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.InbucketImage)) {
values[c.InbucketURL] = fmt.Sprintf("http://127.0.0.1:%d", utils.Config.Inbucket.Port)
values[c.InbucketURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Inbucket.Port)
}
return values
}
Expand Down Expand Up @@ -119,13 +120,13 @@ func IsServiceReady(ctx context.Context, container string) bool {

func isPostgRESTHealthy(ctx context.Context) bool {
// PostgREST does not support native health checks
restUrl := fmt.Sprintf("http://127.0.0.1:%d/rest-admin/v1/ready", utils.Config.Api.Port)
restUrl := fmt.Sprintf("http://%s:%d/rest-admin/v1/ready", utils.Config.Hostname, utils.Config.Api.Port)
return checkHTTPHead(ctx, restUrl)
}

func isEdgeRuntimeHealthy(ctx context.Context) bool {
// Native health check logs too much hyper::Error(IncompleteMessage)
restUrl := fmt.Sprintf("http://127.0.0.1:%d/functions/v1/_internal/health", utils.Config.Api.Port)
restUrl := fmt.Sprintf("http://%s:%d/functions/v1/_internal/health", utils.Config.Hostname, utils.Config.Api.Port)
return checkHTTPHead(ctx, restUrl)
}

Expand All @@ -141,7 +142,6 @@ func checkHTTPHead(ctx context.Context, url string) bool {

func printStatus(names CustomName, format string, w io.Writer, exclude ...string) (err error) {
values := names.toValues(exclude...)

return utils.EncodeOutput(format, w, values)
}

Expand Down
2 changes: 2 additions & 0 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var Config = config{
type (
config struct {
ProjectId string `toml:"project_id"`
Hostname string `toml:"-"`
Api api `toml:"api"`
Db db `toml:"db" mapstructure:"db"`
Realtime realtime `toml:"realtime"`
Expand Down Expand Up @@ -463,6 +464,7 @@ func LoadConfigFS(fsys afero.Fs) error {
if Config.ProjectId == "" {
return errors.New("Missing required field in config: project_id")
}
Config.Hostname = GetHostname()
UpdateDockerIds()
// Validate api config
if Config.Api.Port == 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getPoolerConfig(dbConfig pgconn.Config) *pgconn.Config {
// Connnect to local Postgres with optimised settings. The caller is responsible for closing the connection returned.
func ConnectLocalPostgres(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) {
if len(config.Host) == 0 {
config.Host = "127.0.0.1"
config.Host = Config.Hostname
}
if config.Port == 0 {
config.Port = uint16(Config.Db.Port)
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/flags/db_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error {
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
DbConfig.Host = "127.0.0.1"
DbConfig.Host = utils.Config.Hostname
DbConfig.Port = uint16(utils.Config.Db.Port)
DbConfig.User = "postgres"
DbConfig.Password = utils.Config.Db.Password
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"context"
_ "embed"
"net"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -300,3 +301,13 @@ func ValidateFunctionSlug(slug string) error {
func Ptr[T any](v T) *T {
return &v
}

func GetHostname() string {
host := Docker.DaemonHost()
if parsed, err := client.ParseHostURL(host); err == nil && parsed.Scheme == "tcp" {
if host, _, err := net.SplitHostPort(parsed.Host); err == nil {
return host
}
}
return "127.0.0.1"
}

0 comments on commit 272027d

Please sign in to comment.