From 5313123fe5321ed7d21ab9597e86eac399e60632 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Fri, 28 Jan 2022 10:02:04 +1000 Subject: [PATCH 1/2] fix: Remove the timeout in mio.Stop() as it causes errors when running again --- pkg/run/minio.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/run/minio.go b/pkg/run/minio.go index c59e1c17c..8b53acb35 100644 --- a/pkg/run/minio.go +++ b/pkg/run/minio.go @@ -19,7 +19,6 @@ import ( "fmt" "os" "path/filepath" - "time" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" @@ -127,8 +126,7 @@ func (m *MinioServer) GetApiPort() int { } func (m *MinioServer) Stop() error { - defaultTimeout := time.Duration(5) * time.Second - return m.ce.Stop(m.cid, &defaultTimeout) + return m.ce.Stop(m.cid, nil) } func NewMinio(dir string, name string) (*MinioServer, error) { From 06bd8f0eb1ea1e56125a2a41027e2afb27755996 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Fri, 28 Jan 2022 10:03:40 +1000 Subject: [PATCH 2/2] feat(run): Print a Status when the localServices have started ``` SUCCESS Started Local Services! +------------------+------------------------------------------------------------------+ | RUNNING | true | | RUNDIR | /run/user/1000/nitric/rest-api | | GATEWAYADDRESS | :9001 | | MEMBRANEADDRESS | localhost:50051 | | MINIOCONTAINERID | 196a3cf88054b22ae740a51ade2e9aab864b3d0fc2bf54989beb3e7fd7831011 | | MINIOENDPOINT | localhost:9000 | +------------------+------------------------------------------------------------------+ ``` --- pkg/cmd/run/root.go | 3 +++ pkg/run/gateway.go | 4 +--- pkg/run/run.go | 48 ++++++++++++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/run/root.go b/pkg/cmd/run/root.go index 65befa883..7479e41d5 100644 --- a/pkg/cmd/run/root.go +++ b/pkg/cmd/run/root.go @@ -28,6 +28,7 @@ import ( "github.com/nitrictech/newcli/pkg/build" "github.com/nitrictech/newcli/pkg/containerengine" + "github.com/nitrictech/newcli/pkg/output" "github.com/nitrictech/newcli/pkg/run" "github.com/nitrictech/newcli/pkg/stack" "github.com/nitrictech/newcli/pkg/tasklet" @@ -96,6 +97,8 @@ var runCmd = &cobra.Command{ } tasklet.MustRun(startLocalServices, tasklet.Opts{Signal: term}) + output.Print(*ls.Status()) + var functions []*run.Function startFunctions := tasklet.Runner{ diff --git a/pkg/run/gateway.go b/pkg/run/gateway.go index 6f9105eae..e4702affe 100644 --- a/pkg/run/gateway.go +++ b/pkg/run/gateway.go @@ -148,9 +148,7 @@ func (s *BaseHttpGateway) Stop() error { // Create new HTTP gateway // XXX: No External Args for function atm (currently the plugin loader does not pass any argument information) -func NewGateway() (gateway.GatewayService, error) { - address := nitric_utils.GetEnv("GATEWAY_ADDRESS", ":9001") - +func NewGateway(address string) (gateway.GatewayService, error) { return &BaseHttpGateway{ address: address, }, nil diff --git a/pkg/run/run.go b/pkg/run/run.go index 8fcfdd3e6..d637ba785 100644 --- a/pkg/run/run.go +++ b/pkg/run/run.go @@ -29,6 +29,7 @@ import ( queue_service "github.com/nitrictech/nitric/pkg/plugins/queue/dev" secret_service "github.com/nitrictech/nitric/pkg/plugins/secret/dev" minio "github.com/nitrictech/nitric/pkg/plugins/storage/minio" + nitric_utils "github.com/nitrictech/nitric/pkg/utils" "github.com/nitrictech/nitric/pkg/worker" ) @@ -36,6 +37,16 @@ type LocalServices interface { Start() error Stop() error Running() bool + Status() *LocalServicesStatus +} + +type LocalServicesStatus struct { + Running bool `yaml:"running"` + RunDir string `yaml:"runDir"` + GatewayAddress string `yaml:"gatewayAddress"` + MembraneAddress string `yaml:"membraneAddress"` + MinioContainerID string `yaml:"minioContainerID"` + MinioEndpoint string `yaml:"minioEndpoint"` } type localServices struct { @@ -43,12 +54,19 @@ type localServices struct { stackPath string mio *MinioServer mem *membrane.Membrane + status *LocalServicesStatus } func NewLocalServices(stackName, stackPath string) LocalServices { return &localServices{ stackName: stackName, stackPath: stackPath, + status: &LocalServicesStatus{ + Running: false, + RunDir: path.Join(utils.NitricRunDir(), stackName), + GatewayAddress: nitric_utils.GetEnv("GATEWAY_ADDRESS", ":9001"), + MembraneAddress: net.JoinHostPort("localhost", "50051"), + }, } } @@ -58,22 +76,22 @@ func (l *localServices) Stop() error { } func (l *localServices) Running() bool { + l.status.Running = false conn, err := net.DialTimeout("tcp", net.JoinHostPort("0.0.0.0", "50051"), time.Second) - if err != nil { - return false - } - if conn != nil { + if err == nil && conn != nil { defer conn.Close() - return true + l.status.Running = true } - return false + return l.status.Running } -func (l *localServices) Start() error { - runDir := path.Join(utils.NitricRunDir(), l.stackName) +func (l *localServices) Status() *LocalServicesStatus { + return l.status +} +func (l *localServices) Start() error { var err error - l.mio, err = NewMinio(runDir, "test-run") + l.mio, err = NewMinio(l.status.RunDir, "minio") if err != nil { return err } @@ -83,9 +101,11 @@ func (l *localServices) Start() error { if err != nil { return err } + l.status.MinioContainerID = l.mio.cid + l.status.MinioEndpoint = fmt.Sprintf("localhost:%d", l.mio.GetApiPort()) // Connect dev storage - os.Setenv(minio.MINIO_ENDPOINT_ENV, fmt.Sprintf("localhost:%d", l.mio.GetApiPort())) + os.Setenv(minio.MINIO_ENDPOINT_ENV, l.status.MinioEndpoint) os.Setenv(minio.MINIO_ACCESS_KEY_ENV, "minioadmin") os.Setenv(minio.MINIO_SECRET_KEY_ENV, "minioadmin") sp, err := minio.New() @@ -94,21 +114,21 @@ func (l *localServices) Start() error { } // Connect dev documents - os.Setenv("LOCAL_DB_DIR", runDir) + os.Setenv("LOCAL_DB_DIR", l.status.RunDir) dp, err := boltdb_service.New() if err != nil { return err } // Connect secrets plugin - os.Setenv("LOCAL_SEC_DIR", runDir) + os.Setenv("LOCAL_SEC_DIR", l.status.RunDir) secp, err := secret_service.New() if err != nil { return err } // Connect queue plugin - os.Setenv("LOCAL_QUEUE_DIR", runDir) + os.Setenv("LOCAL_QUEUE_DIR", l.status.RunDir) qp, err := queue_service.New() if err != nil { return err @@ -128,7 +148,7 @@ func (l *localServices) Start() error { } // Start a new gateway plugin - gw, err := NewGateway() + gw, err := NewGateway(l.status.GatewayAddress) if err != nil { return err }