Skip to content

Commit a0e5585

Browse files
tjholmdavemooreuws
andcommitted
feat: WIP nitric run command
Co-authored-by: davemooreuws <[email protected]>
1 parent 048d30c commit a0e5585

File tree

12 files changed

+449
-18
lines changed

12 files changed

+449
-18
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ require (
2222
github.com/jhoonb/archivex v0.0.0-20201016144719-6a343cdae81d
2323
github.com/mattn/go-runewidth v0.0.13 // indirect
2424
github.com/mitchellh/mapstructure v1.4.2
25-
github.com/nitrictech/apis v0.13.0-rc.4
2625
github.com/nitrictech/boxygen v0.0.1-rc.7.0.20211212231606-62c668408f91
26+
github.com/nitrictech/nitric v0.13.0-rc.7
2727
github.com/pkg/errors v0.9.1
2828
github.com/spf13/cobra v1.2.1
2929
github.com/spf13/viper v1.9.0
3030
golang.org/x/net v0.0.0-20211105192438-b53810dc28af // indirect
3131
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
3232
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
33-
google.golang.org/genproto v0.0.0-20211005153810-c76a74d43a8e // indirect
33+
golang.org/x/tools v0.1.8 // indirect
3434
google.golang.org/grpc v1.41.0
3535
gopkg.in/yaml.v2 v2.4.0
3636
)

go.sum

Lines changed: 110 additions & 13 deletions
Large diffs are not rendered by default.

pkg/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/nitrictech/newcli/pkg/cmd/build"
3131
"github.com/nitrictech/newcli/pkg/cmd/deployment"
3232
"github.com/nitrictech/newcli/pkg/cmd/provider"
33+
"github.com/nitrictech/newcli/pkg/cmd/run"
3334
"github.com/nitrictech/newcli/pkg/cmd/stack"
3435
"github.com/nitrictech/newcli/pkg/cmd/target"
3536
"github.com/nitrictech/newcli/pkg/output"
@@ -89,6 +90,7 @@ func init() {
8990
rootCmd.AddCommand(provider.RootCommand())
9091
rootCmd.AddCommand(stack.RootCommand())
9192
rootCmd.AddCommand(target.RootCommand())
93+
rootCmd.AddCommand(run.RootCommand())
9294
rootCmd.AddCommand(versionCmd)
9395
rootCmd.AddCommand(configHelpTopic)
9496
addAliases()

pkg/cmd/run/root.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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 run
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"os/signal"
23+
"path/filepath"
24+
"runtime"
25+
"syscall"
26+
"time"
27+
28+
"github.com/docker/docker/api/types/container"
29+
"github.com/docker/docker/api/types/mount"
30+
"github.com/docker/docker/api/types/strslice"
31+
"github.com/nitrictech/newcli/pkg/build"
32+
"github.com/nitrictech/newcli/pkg/containerengine"
33+
"github.com/nitrictech/newcli/pkg/provider/run"
34+
"github.com/nitrictech/nitric/pkg/membrane"
35+
boltdb_service "github.com/nitrictech/nitric/pkg/plugins/document/boltdb"
36+
gateway_plugin "github.com/nitrictech/nitric/pkg/plugins/gateway/dev"
37+
minio "github.com/nitrictech/nitric/pkg/plugins/storage/minio"
38+
"github.com/nitrictech/nitric/pkg/worker"
39+
"github.com/spf13/cobra"
40+
)
41+
42+
var runCmd = &cobra.Command{
43+
Use: "run [entrypointsGlob]",
44+
Short: "run a nitric stack",
45+
Long: `Run a nitric stack locally for
46+
development/testing
47+
`,
48+
Run: func(cmd *cobra.Command, args []string) {
49+
term := make(chan os.Signal, 1)
50+
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
51+
signal.Notify(term, os.Interrupt, syscall.SIGINT)
52+
53+
files, err := filepath.Glob(args[0])
54+
55+
if err != nil {
56+
cobra.CheckErr(err)
57+
}
58+
59+
ce, err := containerengine.Discover()
60+
61+
// Prepare development images
62+
ctx, _ := filepath.Abs(".")
63+
if err := build.CreateBaseDev(ctx, map[string]string{
64+
"ts": "nitric-ts-dev",
65+
}); err != nil {
66+
cobra.CheckErr(err)
67+
}
68+
69+
mio, err := run.NewMinio("./.nitric/run", "test-run")
70+
71+
if err != nil {
72+
cobra.CheckErr(err)
73+
}
74+
75+
// start minio
76+
if err := mio.Start(); err != nil {
77+
cobra.CheckErr(err)
78+
}
79+
80+
if err := mio.Stop(); err != nil {
81+
cobra.CheckErr(err)
82+
}
83+
// Connect dev storage
84+
os.Setenv(minio.MINIO_ENDPOINT_ENV, fmt.Sprintf("localhost:%d", mio.GetApiPort()))
85+
os.Setenv(minio.MINIO_ACCESS_KEY_ENV, "minioadmin")
86+
os.Setenv(minio.MINIO_SECRET_KEY_ENV, "minioadmin")
87+
sp, err := minio.New()
88+
if err != nil {
89+
cobra.CheckErr(err)
90+
}
91+
92+
// Connect dev documents
93+
dp, err := boltdb_service.New()
94+
if err != nil {
95+
cobra.CheckErr(err)
96+
}
97+
98+
// Create a new Worker Pool
99+
// TODO: We may want to override GetWorker on the default ProcessPool
100+
// For now we'll use the default and expand from there
101+
pool := worker.NewProcessPool(&worker.ProcessPoolOptions{
102+
MinWorkers: 0,
103+
MaxWorkers: 100,
104+
})
105+
106+
// Start a new gateway plugin
107+
gw, err := gateway_plugin.New()
108+
109+
if err != nil {
110+
cobra.CheckErr(err)
111+
}
112+
113+
// Prepare development membrane to start
114+
// This will start a single membrane that all
115+
// running functions will connect to
116+
mem, err := membrane.New(&membrane.MembraneOptions{
117+
ServiceAddress: "0.0.0.0:50051",
118+
ChildCommand: []string{"echo", "running membrane 🚀"},
119+
StoragePlugin: sp,
120+
DocumentPlugin: dp,
121+
GatewayPlugin: gw,
122+
Pool: pool,
123+
TolerateMissingServices: true,
124+
})
125+
126+
if err != nil {
127+
cobra.CheckErr(err)
128+
}
129+
130+
memerr := make(chan error)
131+
go func(errch chan error) {
132+
errch <- mem.Start()
133+
}(memerr)
134+
135+
time.Sleep(time.Second * time.Duration(2))
136+
137+
// Start the functions
138+
// cids := make([]string, 0)
139+
fmt.Println("found files:", files)
140+
for i, f := range files {
141+
hostConfig := &container.HostConfig{
142+
AutoRemove: true,
143+
Mounts: []mount.Mount{
144+
{
145+
Type: "bind",
146+
Source: ctx,
147+
Target: "/app",
148+
},
149+
},
150+
}
151+
if runtime.GOOS == "linux" {
152+
// setup host.docker.internal to route to host gateway
153+
// to access rpc server hosted by local CLI run
154+
hostConfig.ExtraHosts = []string{"host.docker.internal:172.17.0.1"}
155+
}
156+
157+
cID, err := ce.ContainerCreate(&container.Config{
158+
Image: "nitric-ts-dev", // Select an image to use based on the handler
159+
// Set the address to the bound port
160+
Env: []string{fmt.Sprintf("SERVICE_ADDRESS=host.docker.internal:%d", 50051)},
161+
Entrypoint: strslice.StrSlice{"nodemon"},
162+
Cmd: strslice.StrSlice{"--watch", "/app/**", "--ext", "ts,json", "--exec", "ts-node -T " + "/app/" + f},
163+
}, hostConfig, nil, fmt.Sprintf("test-container-%d", i))
164+
165+
if err != nil {
166+
cobra.CheckErr(err)
167+
}
168+
169+
// cids = append(cids, cID)
170+
171+
err = ce.Start(cID)
172+
}
173+
174+
fmt.Println("Local running, use ctrl-C to stop")
175+
176+
select {
177+
case membraneError := <-memerr:
178+
fmt.Println(fmt.Sprintf("Membrane Error: %v, exiting", membraneError))
179+
case sigTerm := <-term:
180+
fmt.Println(fmt.Sprintf("Received %v, exiting", sigTerm))
181+
}
182+
183+
// Stop the membrane
184+
mem.Stop()
185+
// Stop the minio server
186+
mio.Stop()
187+
},
188+
Args: cobra.MaximumNArgs(1),
189+
}
190+
191+
func RootCommand() *cobra.Command {
192+
return runCmd
193+
}

pkg/codeconfig/codeconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333
"github.com/pkg/errors"
3434
"google.golang.org/grpc"
3535

36-
v1 "github.com/nitrictech/apis/go/nitric/v1"
3736
"github.com/nitrictech/newcli/pkg/containerengine"
3837
"github.com/nitrictech/newcli/pkg/stack"
3938
"github.com/nitrictech/newcli/pkg/utils"
39+
v1 "github.com/nitrictech/nitric/pkg/api/nitric/v1"
4040
)
4141

4242
// CodeConfig - represents a collection of related functions and their shared dependencies.

pkg/codeconfig/function.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"github.com/nitrictech/newcli/pkg/utils"
2525

26-
pb "github.com/nitrictech/apis/go/nitric/v1"
26+
pb "github.com/nitrictech/nitric/pkg/api/nitric/v1"
2727
)
2828

2929
type Api struct {

pkg/codeconfig/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"google.golang.org/grpc/codes"
2323
"google.golang.org/grpc/status"
2424

25-
pb "github.com/nitrictech/apis/go/nitric/v1"
25+
pb "github.com/nitrictech/nitric/pkg/api/nitric/v1"
2626
)
2727

2828
type Server struct {

pkg/containerengine/docker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ func (d *docker) Start(nameOrID string) error {
203203
return d.cli.ContainerStart(context.Background(), nameOrID, types.ContainerStartOptions{})
204204
}
205205

206+
func (d *docker) Stop(nameOrID string, timeout *time.Duration) error {
207+
return d.cli.ContainerStop(context.Background(), nameOrID, timeout)
208+
}
209+
206210
func (d *docker) CopyFromArchive(nameOrID string, path string, reader io.Reader) error {
207211
return d.cli.CopyToContainer(context.Background(), nameOrID, path, reader, types.CopyToContainerOptions{})
208212
}

pkg/containerengine/podman.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os/exec"
2525
"strings"
26+
"time"
2627

2728
"github.com/docker/docker/api/types"
2829
"github.com/docker/docker/api/types/container"
@@ -98,6 +99,10 @@ func (p *podman) Start(nameOrID string) error {
9899
return p.docker.Start(nameOrID)
99100
}
100101

102+
func (p *podman) Stop(nameOrID string, timeout *time.Duration) error {
103+
return p.docker.Stop(nameOrID, timeout)
104+
}
105+
101106
func (p *podman) ContainerWait(containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
102107
return p.docker.ContainerWait(containerID, condition)
103108
}

pkg/containerengine/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type ContainerEngine interface {
4343
NetworkCreate(name string) error
4444
ContainerCreate(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, name string) (string, error)
4545
Start(nameOrID string) error
46+
Stop(nameOrID string, timeout *time.Duration) error
4647
ContainerWait(containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error)
4748
CopyFromArchive(nameOrID string, path string, reader io.Reader) error
4849
ContainersListByLabel(match map[string]string) ([]types.Container, error)

pkg/provider/run/gateway.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package run

0 commit comments

Comments
 (0)