Skip to content

Commit

Permalink
fix: handle db connection string host for nitric run (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws authored Dec 9, 2024
1 parent fff2739 commit a03f47b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var runCmd = &cobra.Command{
LogWriter: logWriter,
LocalConfig: proj.LocalConfig,
MigrationRunner: project.BuildAndRunMigrations,
LocalCloudMode: cloud.LocalCloudModeRun,
})
tui.CheckErr(err)
runView.Send(local.LocalCloudStartStatusMsg{Status: local.Done})
Expand Down
1 change: 1 addition & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ var startCmd = &cobra.Command{
LogWriter: logWriter,
LocalConfig: proj.LocalConfig,
MigrationRunner: project.BuildAndRunMigrations,
LocalCloudMode: cloud.LocalCloudModeStart,
})
tui.CheckErr(err)
runView.Send(local.LocalCloudStartStatusMsg{Status: local.Done})
Expand Down
21 changes: 20 additions & 1 deletion pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/nitrictech/cli/pkg/cloud/websockets"
"github.com/nitrictech/cli/pkg/grpcx"
"github.com/nitrictech/cli/pkg/netx"
"github.com/nitrictech/cli/pkg/project/dockerhost"
"github.com/nitrictech/cli/pkg/project/localconfig"
"github.com/nitrictech/nitric/core/pkg/logger"
"github.com/nitrictech/nitric/core/pkg/server"
Expand Down Expand Up @@ -229,11 +230,22 @@ func (lc *LocalCloud) AddService(serviceName string) (int, error) {
return ports[0], nil
}

// LocalCloudMode type run or start
type LocalCloudMode string

const (
// LocalCloudModeRun - run mode
LocalCloudModeRun LocalCloudMode = "run"
// LocalCloudModeStart - start mode
LocalCloudModeStart LocalCloudMode = "start"
)

type LocalCloudOptions struct {
TLSCredentials *gateway.TLSCredentials
LogWriter io.Writer
LocalConfig localconfig.LocalConfiguration
MigrationRunner sql.MigrationRunner
LocalCloudMode LocalCloudMode
}

func New(projectName string, opts LocalCloudOptions) (*LocalCloud, error) {
Expand Down Expand Up @@ -291,7 +303,14 @@ func New(projectName string, opts LocalCloudOptions) (*LocalCloud, error) {
return nil, err
}

localDatabaseService, err := sql.NewLocalSqlServer(projectName, localResources, opts.MigrationRunner)
connectionStringHost := "localhost"

// Use the host.docker.internal address for connection strings with local cloud run mode
if opts.LocalCloudMode == LocalCloudModeRun {
connectionStringHost = dockerhost.GetInternalDockerHost()
}

localDatabaseService, err := sql.NewLocalSqlServer(projectName, localResources, opts.MigrationRunner, connectionStringHost)
if err != nil {
return nil, err
}
Expand Down
27 changes: 17 additions & 10 deletions pkg/cloud/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ type (
)

type LocalSqlServer struct {
projectName string
containerId string
port int
State State
projectName string
containerId string
connectionStringHost string
port int
State State
sqlpb.UnimplementedSqlServer

migrationRunner MigrationRunner
Expand Down Expand Up @@ -120,7 +121,7 @@ func (l *LocalSqlServer) ensureDatabaseExists(databaseName string) (string, erro
}

// Return the connection string of the new database
return fmt.Sprintf("postgresql://postgres:localsecret@localhost:%d/%s?sslmode=disable", l.port, databaseName), nil
return fmt.Sprintf("postgresql://postgres:localsecret@%s:%d/%s?sslmode=disable", l.connectionStringHost, l.port, databaseName), nil
}

func (l *LocalSqlServer) start() error {
Expand Down Expand Up @@ -338,12 +339,18 @@ func (l *LocalSqlServer) RegisterDatabases(lrs resources.LocalResourcesState) {
l.Publish(l.State)
}

func NewLocalSqlServer(projectName string, localResources *resources.LocalResourcesService, migrationRunner MigrationRunner) (*LocalSqlServer, error) {
func NewLocalSqlServer(projectName string, localResources *resources.LocalResourcesService, migrationRunner MigrationRunner, connectionStringHost string) (*LocalSqlServer, error) {
if connectionStringHost == "" {
// default to localhost
connectionStringHost = "localhost"
}

localSql := &LocalSqlServer{
projectName: projectName,
State: make(State),
bus: EventBus.New(),
migrationRunner: migrationRunner,
projectName: projectName,
State: make(State),
bus: EventBus.New(),
migrationRunner: migrationRunner,
connectionStringHost: connectionStringHost,
}

err := localSql.start()
Expand Down
7 changes: 6 additions & 1 deletion pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/nitrictech/cli/pkg/cloud"
"github.com/nitrictech/cli/pkg/collector"
"github.com/nitrictech/cli/pkg/netx"
"github.com/nitrictech/cli/pkg/project/dockerhost"
resourcespb "github.com/nitrictech/nitric/core/pkg/proto/resources/v1"
websocketspb "github.com/nitrictech/nitric/core/pkg/proto/websockets/v1"

Expand Down Expand Up @@ -603,12 +604,16 @@ func (d *Dashboard) updateSqlDatabases(state sql.State) {
sqlDatabases := []*SQLDatabaseSpec{}

for dbName, db := range state {
// connection strings should always use localhost for dashboard
strToReplace := dockerhost.GetInternalDockerHost()
connectionString := strings.Replace(db.ConnectionString, strToReplace, "localhost", 1)

sqlDatabases = append(sqlDatabases, &SQLDatabaseSpec{
BaseResourceSpec: &BaseResourceSpec{
Name: dbName,
RequestingServices: db.ResourceRegister.RequestingServices,
},
ConnectionString: db.ConnectionString,
ConnectionString: connectionString,
Status: db.Status,
MigrationsPath: db.ResourceRegister.Resource.Migrations.GetMigrationsPath(),
})
Expand Down
35 changes: 35 additions & 0 deletions pkg/project/dockerhost/dockerhost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dockerhost

import (
goruntime "runtime"

"github.com/nitrictech/nitric/core/pkg/env"
)

func GetInternalDockerHost() string {
dockerHost := "host.docker.internal"

if goruntime.GOOS == "linux" {
host := env.GetEnv("NITRIC_DOCKER_HOST", "172.17.0.1")

return host.String()
}

return dockerHost
}
10 changes: 2 additions & 8 deletions pkg/project/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"github.com/nitrictech/cli/pkg/cloud/sql"
"github.com/nitrictech/cli/pkg/collector"
"github.com/nitrictech/cli/pkg/docker"
"github.com/nitrictech/cli/pkg/project/dockerhost"
"github.com/nitrictech/cli/pkg/project/runtime"
"github.com/nitrictech/nitric/core/pkg/env"
"github.com/nitrictech/nitric/core/pkg/logger"
resourcespb "github.com/nitrictech/nitric/core/pkg/proto/resources/v1"
)
Expand Down Expand Up @@ -196,13 +196,7 @@ func RunMigration(databaseName string, connectionString string) error {
imageName := migrationImageName(databaseName)

// Update connection string for docker host...
dockerHost := "host.docker.internal"

if goruntime.GOOS == "linux" {
host := env.GetEnv("NITRIC_DOCKER_HOST", "172.17.0.1")

dockerHost = host.String()
}
dockerHost := dockerhost.GetInternalDockerHost()

dockerConnectionString := strings.Replace(connectionString, "localhost", dockerHost, 1)

Expand Down

0 comments on commit a03f47b

Please sign in to comment.