Skip to content

Commit

Permalink
Merge pull request #1060 from ripienaar/online_peer_remove
Browse files Browse the repository at this point in the history
Allow online peer removal
  • Loading branch information
ripienaar committed May 23, 2024
2 parents bf4d8ef + 3ac79f5 commit 6264f3e
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 81 deletions.
7 changes: 4 additions & 3 deletions cli/auth_account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package cli
import (
"encoding/json"
"fmt"
au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/util"
"io"
"net/url"
"os"
"strconv"
"time"

au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/util"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -331,7 +332,7 @@ func (c *authAccountCommand) queryAction(_ *fisk.ParseContext) error {
}

if c.output != "" {
err = os.WriteFile(c.output, []byte(token), 0700)
err = os.WriteFile(c.output, []byte(token), 0600)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions cli/auth_operator_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/choria-io/scaffold/forms"
"github.com/ghodss/yaml"
au "github.com/nats-io/natscli/internal/auth"
iu "github.com/nats-io/natscli/internal/util"
"io"
"net/url"
"os"
"sort"
"text/template"

"github.com/choria-io/scaffold/forms"
"github.com/ghodss/yaml"
au "github.com/nats-io/natscli/internal/auth"
iu "github.com/nats-io/natscli/internal/util"

"github.com/nats-io/nkeys"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (c *authOperatorCommand) generateAction(_ *fisk.ParseContext) error {
return nil
}

err = os.WriteFile(c.outputFile, buff.Bytes(), 0644)
err = os.WriteFile(c.outputFile, buff.Bytes(), 0600)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/auth_user_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,5 @@ func (c *authUserCommand) writeCred(user ab.User, credFile string, force bool) e
return err
}

return os.WriteFile(c.credFile, cred, 0700)
return os.WriteFile(c.credFile, cred, 0600)
}
2 changes: 1 addition & 1 deletion cli/bench_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (c *benchCmd) bench(_ *fisk.ParseContext) error {

if c.csvFile != "" {
csv := bm.CSV()
err := os.WriteFile(c.csvFile, []byte(csv), 0644)
err := os.WriteFile(c.csvFile, []byte(csv), 0600)
if err != nil {
log.Printf("error writing file %s: %v", c.csvFile, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"encoding/json"
"fmt"
iu "github.com/nats-io/natscli/internal/util"
"io"
"math"
"math/rand"
Expand All @@ -28,6 +27,8 @@ import (
"strings"
"time"

iu "github.com/nats-io/natscli/internal/util"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -1498,7 +1499,7 @@ func (c *consumerCmd) createAction(pc *fisk.ParseContext) (err error) {
fisk.Fatalf("Validation Failed: %s", strings.Join(errs, "\n\t"))
}

return os.WriteFile(c.outFile, j, 0644)
return os.WriteFile(c.outFile, j, 0600)
}

c.connectAndSetup(true, false)
Expand Down
2 changes: 1 addition & 1 deletion cli/errors_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *errCmd) editAction(pc *fisk.ParseContext) error {
return err
}

err = os.WriteFile(c.file, ej, 0644)
err = os.WriteFile(c.file, ej, 0600)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions cli/server_account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cli
import (
"encoding/json"
"fmt"
iu "github.com/nats-io/natscli/internal/util"
"os"
"strings"
"time"

iu "github.com/nats-io/natscli/internal/util"

"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/natscli/columns"
Expand Down Expand Up @@ -152,23 +153,23 @@ func (c *srvAccountCommand) infoAction(_ *fisk.ParseContext) error {
cols.AddSectionTitle("Imports")

for _, imp := range nfo.Imports {
c.renderImport(cols, &imp)
c.renderImport(cols, imp)
}
}

if len(nfo.Exports) > 0 {
cols.AddSectionTitle("Exports")

for _, exp := range nfo.Exports {
c.renderExport(cols, &exp)
c.renderExport(cols, exp)
}
}

if len(nfo.Responses) > 0 {
cols.AddSectionTitle("Responses")

for _, imp := range nfo.Responses {
c.renderImport(cols, &imp)
c.renderImport(cols, imp)
}
}

Expand Down Expand Up @@ -206,7 +207,7 @@ func (c *srvAccountCommand) renderMappings(cols *columns.Writer, subj string, ma
cols.Println()
}

func (c *srvAccountCommand) renderExport(cols *columns.Writer, exp *server.ExtExport) {
func (c *srvAccountCommand) renderExport(cols *columns.Writer, exp server.ExtExport) {
cols.AddRow("Subject", exp.Subject)
cols.AddRow("Type", exp.Type.String())
cols.AddRow("Tokens Required", exp.TokenReq)
Expand Down Expand Up @@ -254,7 +255,7 @@ func (c *srvAccountCommand) renderExport(cols *columns.Writer, exp *server.ExtEx
cols.Println()
}

func (c *srvAccountCommand) renderImport(cols *columns.Writer, imp *server.ExtImport) {
func (c *srvAccountCommand) renderImport(cols *columns.Writer, imp server.ExtImport) {
local := string(imp.LocalSubject)
subj := string(imp.Subject)
if local == "" {
Expand Down
21 changes: 17 additions & 4 deletions cli/server_cluster_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (c *SrvClusterCmd) metaPeerRemoveAction(_ *fisk.ParseContext) error {
found := false
foundName := ""
foundID := ""
state := "offline"

srv := &jszr{}
err = json.Unmarshal(res[0], srv)
Expand All @@ -144,7 +145,7 @@ func (c *SrvClusterCmd) metaPeerRemoveAction(_ *fisk.ParseContext) error {
for _, r := range srv.Data.Meta.Replicas {
if r.Name == c.peer || r.Peer == c.peer {
if !r.Offline {
return fmt.Errorf("can only remove offline nodes")
state = "online"
}
foundID = r.Peer
foundName = r.Name
Expand All @@ -157,13 +158,25 @@ func (c *SrvClusterCmd) metaPeerRemoveAction(_ *fisk.ParseContext) error {
}

if !c.force {
fmt.Printf("Removing %s can not be reversed, data on this node will be inaccessible and the server name can not be used again. You should only remove nodes that will not return in future.\n\n", c.peer)
fmt.Println(`Removing peers is intended for peers that will not come back, generally for peers that will remain
in the cluster removing the peer is not needed - simply do your maintenance with the peer shut down.
To return a removed peer to the cluster you have to restart the entire cluster or super-cluster first.
If the peer was online when it was removed you need to restart the removed peer last.
R1 streams on this peer will migrate to other peers but data will be lost, even if the
peer returns later.
R1 consumers on this peer will lose their state and revert to starting configuring
which may lead to duplicate deliveries.`)

fmt.Println()

var remove bool
if c.peer == foundName || strings.Contains(foundName, foundID) {
remove, err = askConfirmation(fmt.Sprintf("Really remove peer %s", foundName), false)
remove, err = askConfirmation(fmt.Sprintf("Really remove %s peer %s", state, foundName), false)
} else {
remove, err = askConfirmation(fmt.Sprintf("Really remove peer %s with id %s", foundName, foundID), false)
remove, err = askConfirmation(fmt.Sprintf("Really remove %s peer %s with id %s", state, foundName, foundID), false)
}
fisk.FatalIfError(err, "Could not prompt for confirmation")
if !remove {
Expand Down
11 changes: 9 additions & 2 deletions cli/server_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ package cli

import (
"fmt"
"net/url"
"strings"

"github.com/choria-io/fisk"
au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/scaffold"
iu "github.com/nats-io/natscli/internal/util"
"net/url"
"strings"
)

type serverGenerateCmd struct {
Expand Down Expand Up @@ -48,14 +49,20 @@ func (c *serverGenerateCmd) generateAction(_ *fisk.ParseContext) error {
case strings.Contains(c.source, "://"):
var uri *url.URL
uri, err = url.Parse(c.source)

if err != nil {
return err
}

if uri.Scheme == "" {
return fmt.Errorf("invalid URL %q", c.source)
}

b, err = scaffold.FromUrl(uri)

case iu.IsDirectory(c.source):
b, err = scaffold.FromDir(c.source)

default:
b, err = scaffold.FromFile(c.source)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/server_request_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (c *SrvRequestCmd) profilez(_ *fisk.ParseContext) error {
}

func (c *SrvRequestCmd) profilezWrite(filename string, resp *profilezResponse) error {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0o644)
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ func (c *streamCmd) reportAction(_ *fisk.ParseContext) error {
c.renderReplication(stats)

if c.outFile != "" {
os.WriteFile(c.outFile, []byte(dg.String()), 0644)
os.WriteFile(c.outFile, []byte(dg.String()), 0600)
}
}

Expand Down Expand Up @@ -2822,7 +2822,7 @@ func (c *streamCmd) addAction(pc *fisk.ParseContext) (err error) {
fisk.Fatalf("Validation Failed: %s", strings.Join(errs, "\n\t"))
}

return os.WriteFile(c.outFile, j, 0644)
return os.WriteFile(c.outFile, j, 0600)
}

str, err := mgr.NewStreamFromDefault(c.stream, cfg)
Expand Down
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/emicklei/dot v1.6.2
github.com/expr-lang/expr v1.16.7
github.com/fatih/color v1.16.0
github.com/fatih/color v1.17.0
github.com/ghodss/yaml v1.0.0
github.com/google/go-cmp v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gosuri/uiprogress v0.0.1
github.com/guptarohit/asciigraph v0.7.1
github.com/jedib0t/go-pretty/v6 v6.5.8
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.17.8
github.com/mattn/go-isatty v0.0.20
github.com/nats-io/jsm.go v0.1.1-0.20240416131620-2690708e8318
github.com/nats-io/jwt/v2 v2.5.6
github.com/nats-io/jsm.go v0.1.1-0.20240517153523-ca97f76bed5c
github.com/nats-io/jwt/v2 v2.5.7
github.com/nats-io/nats-server/v2 v2.11.0-preview.2
github.com/nats-io/nats.go v1.34.1
github.com/nats-io/nats.go v1.35.0
github.com/nats-io/nkeys v0.4.7
github.com/nats-io/nuid v1.0.1
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.53.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/synadia-io/jwt-auth-builder.go v0.0.0-20240501200458-e2594dc0b29f
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f
golang.org/x/crypto v0.23.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/term v0.20.0
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -58,14 +58,14 @@ require (
github.com/nats-io/nsc/v2 v2.8.6 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 6264f3e

Please sign in to comment.