force leave for comms net

This commit is contained in:
afeiszli 2022-02-20 18:09:32 -05:00
parent d0b940fab7
commit 5e8eab0a1e
5 changed files with 16 additions and 7 deletions

View file

@ -43,7 +43,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
if err != nil {
return err
}
err = command.Leave(cfg)
err = command.Leave(cfg, c.String("force") == "yes")
return err
},
},

View file

@ -198,5 +198,11 @@ func GetFlags(hostname string) []cli.Flag {
Value: "yes",
Usage: "Checks for IP changes if 'yes'. Ignores if 'no'. Yes by default.",
},
&cli.StringFlag{
Name: "force",
EnvVars: []string{"NETCLIENT_FORCE"},
Value: "no",
Usage: "Allows to run the command with force, if otherwise prevented.",
},
}
}

View file

@ -49,7 +49,7 @@ func Join(cfg config.ClientConfig, privateKey string) error {
if err != nil && !cfg.DebugOn {
if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
ncutils.PrintLog("error installing: "+err.Error(), 1)
err = functions.LeaveNetwork(cfg.Network)
err = functions.LeaveNetwork(cfg.Network, true)
if err != nil {
err = functions.WipeLocal(cfg.Network)
if err != nil {
@ -88,8 +88,8 @@ func Join(cfg config.ClientConfig, privateKey string) error {
}
// Leave - runs the leave command from cli
func Leave(cfg config.ClientConfig) error {
err := functions.LeaveNetwork(cfg.Network)
func Leave(cfg config.ClientConfig, force bool) error {
err := functions.LeaveNetwork(cfg.Network, force)
if err != nil {
ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
} else {

View file

@ -124,7 +124,7 @@ func Uninstall() error {
ncutils.PrintLog("continuing uninstall without leaving networks", 1)
} else {
for _, network := range networks {
err = LeaveNetwork(network)
err = LeaveNetwork(network, true)
if err != nil {
ncutils.PrintLog("Encounter issue leaving network "+network+": "+err.Error(), 1)
}
@ -147,13 +147,16 @@ func Uninstall() error {
}
// LeaveNetwork - client exits a network
func LeaveNetwork(network string) error {
func LeaveNetwork(network string, force bool) error {
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
servercfg := cfg.Server
node := cfg.Node
if node.NetworkSettings.IsComms == "yes" && !force {
return errors.New("COMMS_NET - You are trying to leave the comms network. This will break network updates. Unless you re-join. If you really want to leave, run with --force=yes.")
}
if node.IsServer != "yes" {
var wcclient nodepb.NodeServiceClient

View file

@ -61,7 +61,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
case models.NODE_DELETE:
ncutils.PrintLog(fmt.Sprintf("received delete request for %s", nodeCfg.Node.Name), 0)
unsubscribeNode(client, &nodeCfg)
if err = LeaveNetwork(nodeCfg.Node.Network); err != nil {
if err = LeaveNetwork(nodeCfg.Node.Network, true); err != nil {
if !strings.Contains("rpc error", err.Error()) {
ncutils.PrintLog(fmt.Sprintf("failed to leave, please check that local files for network %s were removed", nodeCfg.Node.Network), 0)
return