Browse Source

force leave for comms net

afeiszli 3 years ago
parent
commit
5e8eab0a1e

+ 1 - 1
netclient/cli_options/cmds.go

@@ -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
 			},
 		},

+ 6 - 0
netclient/cli_options/flags.go

@@ -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.",
+		},
 	}
 }

+ 3 - 3
netclient/command/commands.go

@@ -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 {

+ 5 - 2
netclient/functions/common.go

@@ -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

+ 1 - 1
netclient/functions/mqhandlers.go

@@ -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