Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Republish example #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions cmd/nbe/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"

Expand Down Expand Up @@ -259,34 +260,40 @@ func (r *ComposeRunner) Run(imageTag string) error {
return err
}

err = copyFile(composeFile, filepath.Join(buildDir, "docker-compose.yaml"))
if err != nil {
return err
}

err = createFile(filepath.Join(buildDir, ".env"), []byte(fmt.Sprintf("IMAGE_TAG=%s", imageTag)))
if err != nil {
return fmt.Errorf("create .env: %w", err)
}

// Best effort to bring containers down..
defer exec.Command(
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"down",
"--remove-orphans",
"--timeout", "3",
).Run()
defer func() {
cmd := exec.Command(
"docker",
"compose",
"--project-name", uid,
"down",
"--remove-orphans",
"--timeout", "3",
)
cmd.Dir = buildDir
cmd.Run()
}()

cmd := exec.Command(
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"pull",
"--include-deps",
"--quiet",
"--ignore-pull-failures",
)
cmd.Dir = buildDir

stderrb := bytes.NewBuffer(nil)
cmd.Stderr = stderrb
Expand All @@ -300,8 +307,6 @@ func (r *ComposeRunner) Run(imageTag string) error {
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"up",
)
} else {
Expand All @@ -316,18 +321,33 @@ func (r *ComposeRunner) Run(imageTag string) error {
"compose",
"--ansi", ansi,
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"run",
"--no-TTY",
"--rm",
"app",
)
}

cmd.Dir = buildDir
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = stdin

return cmd.Run()
done := make(chan error, 1)
sigch := make(chan os.Signal, 1)

signal.Notify(sigch, os.Interrupt)

go func() {
done <- cmd.Run()
}()

// Wait for interrupt or once the command finishes.
// This ensures the `docker compose down` runs.
select {
case err := <-done:
return err
case <-sigch:
return nil
}
}
36 changes: 36 additions & 0 deletions examples/jetstream/republish/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: '3.9'
services:
hub:
image: docker.io/synadia/nats-server:nightly
command:
- "--debug"
- "--http_port=8222"
- "--config=/nats.conf"
ports:
- "14222:4222"
- "18222:8222"
volumes:
- "./hub.conf:/nats.conf"
- "./accounts.conf:/accounts.conf"

leaf:
image: docker.io/synadia/nats-server:nightly
command:
- "--debug"
- "--http_port=8222"
- "--config=/nats.conf"
ports:
- "14223:4222"
- "18223:8222"
volumes:
- "./leaf.conf:/nats.conf"
- "./accounts.conf:/accounts.conf"

app:
image: ${IMAGE_TAG}
environment:
- NATS_URL=nats://app:app@leaf:4222
depends_on:
- leaf
- hub

11 changes: 11 additions & 0 deletions examples/jetstream/republish/go/accounts.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
accounts {
SYS: {
users: [{user: sys, password: sys}]
},
APP: {
users: [{user: app, password: app}],
jetstream: enabled
}
}

system_account: SYS
14 changes: 14 additions & 0 deletions examples/jetstream/republish/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/bruth/nats-by-example/examples/jetstream/republish/go

go 1.18

require github.com/nats-io/nats.go v1.16.1-0.20220803221958-cc189da40f83

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/nats-io/nats-server/v2 v2.8.4 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
38 changes: 38 additions & 0 deletions examples/jetstream/republish/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.14.4 h1:eijASRJcobkVtSt81Olfh7JX43osYLwy5krOJo6YEu4=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a h1:lem6QCvxR0Y28gth9P+wV2K/zYUUAkJ+55U8cpS0p5I=
github.com/nats-io/nats-server/v2 v2.8.4 h1:0jQzze1T9mECg8YZEl8+WYUXb9JKluJfCBriPUtluB4=
github.com/nats-io/nats-server/v2 v2.8.4/go.mod h1:8zZa+Al3WsESfmgSs98Fi06dRWLH5Bnq90m5bKD/eT4=
github.com/nats-io/nats.go v1.16.0 h1:zvLE7fGBQYW6MWaFaRdsgm9qT39PJDQoju+DS8KsO1g=
github.com/nats-io/nats.go v1.16.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nats.go v1.16.1-0.20220803074641-59d7d9ccc73e h1:E9cb15fk4R0K4I9Jxebk5lypSvn2zHfKdhM8cUA3+IE=
github.com/nats-io/nats.go v1.16.1-0.20220803074641-59d7d9ccc73e/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nats.go v1.16.1-0.20220803180602-ec49000ee1e2 h1:SfnWrSDnTRFeLtpPfGpctfb6Ke/Npgmi9n/nLzeYfKo=
github.com/nats-io/nats.go v1.16.1-0.20220803180602-ec49000ee1e2/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nats.go v1.16.1-0.20220803221958-cc189da40f83 h1:EAK6rdyLzKzw3Cklsth//yfG2rZn0jdvCVuUdpFbpzQ=
github.com/nats-io/nats.go v1.16.1-0.20220803221958-cc189da40f83/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 h1:GZokNIeuVkl3aZHJchRrr13WCsols02MLUcz1U9is6M=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
12 changes: 12 additions & 0 deletions examples/jetstream/republish/go/hub.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
port: 4222
server_name: hub

jetstream {
domain: hub
}

leafnodes {
port: 7422
}

include ./accounts.conf
21 changes: 21 additions & 0 deletions examples/jetstream/republish/go/leaf.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
port: 4222
server_name: leaf

jetstream {
domain: leaf
}

leafnodes {
remotes = [
{
urls: ["nats-leaf://sys:sys@hub:7422"]
account: SYS
},
{
urls: ["nats-leaf://app:app@hub:7422"]
account: APP
}
]
}

include ./accounts.conf
Loading