-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-11334] Adds a 'peer node unjoin' CLI entrypoint to unjoin a peer…
… from a channel (#2769) This change adds a Viper cobra.Command to unjoin the peer from a specified channel. Signed-off-by: Josh Kneubuhl <[email protected]>
- Loading branch information
Showing
7 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ The `peer node` command has the following subcommands: | |
* resume | ||
* rollback | ||
* start | ||
* unjoin | ||
* upgrade-dbs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package node | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
coreconfig "github.com/hyperledger/fabric/core/config" | ||
"github.com/hyperledger/fabric/core/ledger/kvledger" | ||
"github.com/hyperledger/fabric/core/transientstore" | ||
"github.com/hyperledger/fabric/internal/peer/common" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func unjoinCmd() *cobra.Command { | ||
var channelID string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "unjoin", | ||
Short: "Unjoin the peer from a channel.", | ||
Long: "Unjoin the peer from a channel. When the command is executed, the peer must be offline.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if channelID == common.UndefinedParamValue { | ||
return errors.New("Must supply channel ID") | ||
} | ||
|
||
if err := unjoinChannel(channelID); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
flags := cmd.Flags() | ||
flags.StringVarP(&channelID, "channelID", "c", common.UndefinedParamValue, "Channel to unjoin.") | ||
|
||
return cmd | ||
} | ||
|
||
// unjoin the peer from a channel. | ||
func unjoinChannel(channelID string) error { | ||
// transient storage must be scrubbed prior to removing the kvledger for the channel. Once the | ||
// kvledger storage has been removed, a subsequent ledger removal will return a "no such ledger" error. | ||
// By removing the transient storage prior to deleting the ledger, a crash may be recovered by re-running | ||
// the peer unjoin. | ||
transientStoragePath := filepath.Join(coreconfig.GetPath("peer.fileSystemPath"), "transientstore") | ||
if err := transientstore.Drop(transientStoragePath, channelID); err != nil { | ||
return err | ||
} | ||
|
||
config := ledgerConfig() | ||
if err := kvledger.UnjoinChannel(config, channelID); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package node | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnjoinCmd(t *testing.T) { | ||
t.Run("when the channelID is not specified", func(t *testing.T) { | ||
cmd := unjoinCmd() | ||
args := []string{} | ||
cmd.SetArgs(args) | ||
err := cmd.Execute() | ||
require.EqualError(t, err, "Must supply channel ID") | ||
}) | ||
|
||
t.Run("when the channel does not exist", func(t *testing.T) { | ||
testPath := "/tmp/hyperledger/test" | ||
os.RemoveAll(testPath) | ||
viper.Set("peer.fileSystemPath", testPath) | ||
defer os.RemoveAll(testPath) | ||
|
||
cmd := unjoinCmd() | ||
cmd.SetArgs([]string{"-c", "invalid_channel_does_not_exist"}) | ||
err := cmd.Execute() | ||
require.EqualError(t, err, "unjoin channel [invalid_channel_does_not_exist]: cannot update ledger status, ledger [invalid_channel_does_not_exist] does not exist") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters