Skip to content

Commit a03f47b

Browse files
authored
fix: handle db connection string host for nitric run (#827)
1 parent fff2739 commit a03f47b

File tree

7 files changed

+82
-20
lines changed

7 files changed

+82
-20
lines changed

cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ var runCmd = &cobra.Command{
102102
LogWriter: logWriter,
103103
LocalConfig: proj.LocalConfig,
104104
MigrationRunner: project.BuildAndRunMigrations,
105+
LocalCloudMode: cloud.LocalCloudModeRun,
105106
})
106107
tui.CheckErr(err)
107108
runView.Send(local.LocalCloudStartStatusMsg{Status: local.Done})

cmd/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ var startCmd = &cobra.Command{
182182
LogWriter: logWriter,
183183
LocalConfig: proj.LocalConfig,
184184
MigrationRunner: project.BuildAndRunMigrations,
185+
LocalCloudMode: cloud.LocalCloudModeStart,
185186
})
186187
tui.CheckErr(err)
187188
runView.Send(local.LocalCloudStartStatusMsg{Status: local.Done})

pkg/cloud/cloud.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/nitrictech/cli/pkg/cloud/websockets"
4040
"github.com/nitrictech/cli/pkg/grpcx"
4141
"github.com/nitrictech/cli/pkg/netx"
42+
"github.com/nitrictech/cli/pkg/project/dockerhost"
4243
"github.com/nitrictech/cli/pkg/project/localconfig"
4344
"github.com/nitrictech/nitric/core/pkg/logger"
4445
"github.com/nitrictech/nitric/core/pkg/server"
@@ -229,11 +230,22 @@ func (lc *LocalCloud) AddService(serviceName string) (int, error) {
229230
return ports[0], nil
230231
}
231232

233+
// LocalCloudMode type run or start
234+
type LocalCloudMode string
235+
236+
const (
237+
// LocalCloudModeRun - run mode
238+
LocalCloudModeRun LocalCloudMode = "run"
239+
// LocalCloudModeStart - start mode
240+
LocalCloudModeStart LocalCloudMode = "start"
241+
)
242+
232243
type LocalCloudOptions struct {
233244
TLSCredentials *gateway.TLSCredentials
234245
LogWriter io.Writer
235246
LocalConfig localconfig.LocalConfiguration
236247
MigrationRunner sql.MigrationRunner
248+
LocalCloudMode LocalCloudMode
237249
}
238250

239251
func New(projectName string, opts LocalCloudOptions) (*LocalCloud, error) {
@@ -291,7 +303,14 @@ func New(projectName string, opts LocalCloudOptions) (*LocalCloud, error) {
291303
return nil, err
292304
}
293305

294-
localDatabaseService, err := sql.NewLocalSqlServer(projectName, localResources, opts.MigrationRunner)
306+
connectionStringHost := "localhost"
307+
308+
// Use the host.docker.internal address for connection strings with local cloud run mode
309+
if opts.LocalCloudMode == LocalCloudModeRun {
310+
connectionStringHost = dockerhost.GetInternalDockerHost()
311+
}
312+
313+
localDatabaseService, err := sql.NewLocalSqlServer(projectName, localResources, opts.MigrationRunner, connectionStringHost)
295314
if err != nil {
296315
return nil, err
297316
}

pkg/cloud/sql/sql.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ type (
6969
)
7070

7171
type LocalSqlServer struct {
72-
projectName string
73-
containerId string
74-
port int
75-
State State
72+
projectName string
73+
containerId string
74+
connectionStringHost string
75+
port int
76+
State State
7677
sqlpb.UnimplementedSqlServer
7778

7879
migrationRunner MigrationRunner
@@ -120,7 +121,7 @@ func (l *LocalSqlServer) ensureDatabaseExists(databaseName string) (string, erro
120121
}
121122

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

126127
func (l *LocalSqlServer) start() error {
@@ -338,12 +339,18 @@ func (l *LocalSqlServer) RegisterDatabases(lrs resources.LocalResourcesState) {
338339
l.Publish(l.State)
339340
}
340341

341-
func NewLocalSqlServer(projectName string, localResources *resources.LocalResourcesService, migrationRunner MigrationRunner) (*LocalSqlServer, error) {
342+
func NewLocalSqlServer(projectName string, localResources *resources.LocalResourcesService, migrationRunner MigrationRunner, connectionStringHost string) (*LocalSqlServer, error) {
343+
if connectionStringHost == "" {
344+
// default to localhost
345+
connectionStringHost = "localhost"
346+
}
347+
342348
localSql := &LocalSqlServer{
343-
projectName: projectName,
344-
State: make(State),
345-
bus: EventBus.New(),
346-
migrationRunner: migrationRunner,
349+
projectName: projectName,
350+
State: make(State),
351+
bus: EventBus.New(),
352+
migrationRunner: migrationRunner,
353+
connectionStringHost: connectionStringHost,
347354
}
348355

349356
err := localSql.start()

pkg/dashboard/dashboard.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/nitrictech/cli/pkg/cloud"
4040
"github.com/nitrictech/cli/pkg/collector"
4141
"github.com/nitrictech/cli/pkg/netx"
42+
"github.com/nitrictech/cli/pkg/project/dockerhost"
4243
resourcespb "github.com/nitrictech/nitric/core/pkg/proto/resources/v1"
4344
websocketspb "github.com/nitrictech/nitric/core/pkg/proto/websockets/v1"
4445

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

605606
for dbName, db := range state {
607+
// connection strings should always use localhost for dashboard
608+
strToReplace := dockerhost.GetInternalDockerHost()
609+
connectionString := strings.Replace(db.ConnectionString, strToReplace, "localhost", 1)
610+
606611
sqlDatabases = append(sqlDatabases, &SQLDatabaseSpec{
607612
BaseResourceSpec: &BaseResourceSpec{
608613
Name: dbName,
609614
RequestingServices: db.ResourceRegister.RequestingServices,
610615
},
611-
ConnectionString: db.ConnectionString,
616+
ConnectionString: connectionString,
612617
Status: db.Status,
613618
MigrationsPath: db.ResourceRegister.Resource.Migrations.GetMigrationsPath(),
614619
})

pkg/project/dockerhost/dockerhost.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Nitric Pty Ltd.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package dockerhost
18+
19+
import (
20+
goruntime "runtime"
21+
22+
"github.com/nitrictech/nitric/core/pkg/env"
23+
)
24+
25+
func GetInternalDockerHost() string {
26+
dockerHost := "host.docker.internal"
27+
28+
if goruntime.GOOS == "linux" {
29+
host := env.GetEnv("NITRIC_DOCKER_HOST", "172.17.0.1")
30+
31+
return host.String()
32+
}
33+
34+
return dockerHost
35+
}

pkg/project/migrations.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
"github.com/nitrictech/cli/pkg/cloud/sql"
3232
"github.com/nitrictech/cli/pkg/collector"
3333
"github.com/nitrictech/cli/pkg/docker"
34+
"github.com/nitrictech/cli/pkg/project/dockerhost"
3435
"github.com/nitrictech/cli/pkg/project/runtime"
35-
"github.com/nitrictech/nitric/core/pkg/env"
3636
"github.com/nitrictech/nitric/core/pkg/logger"
3737
resourcespb "github.com/nitrictech/nitric/core/pkg/proto/resources/v1"
3838
)
@@ -196,13 +196,7 @@ func RunMigration(databaseName string, connectionString string) error {
196196
imageName := migrationImageName(databaseName)
197197

198198
// Update connection string for docker host...
199-
dockerHost := "host.docker.internal"
200-
201-
if goruntime.GOOS == "linux" {
202-
host := env.GetEnv("NITRIC_DOCKER_HOST", "172.17.0.1")
203-
204-
dockerHost = host.String()
205-
}
199+
dockerHost := dockerhost.GetInternalDockerHost()
206200

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

0 commit comments

Comments
 (0)