shell: move volume operations to use flag parsing arguments

This commit is contained in:
Chris Lu 2020-09-20 09:27:34 -07:00
parent fcbc520373
commit f498c71199
7 changed files with 60 additions and 52 deletions

View file

@ -28,7 +28,7 @@ func (c *commandVolumeConfigureReplication) Name() string {
func (c *commandVolumeConfigureReplication) Help() string {
return `change volume replication value
This command changes a volume replication value. It should be followed by volume.fix.replication.
This command changes a volume replication value. It should be followed by "volume.fix.replication".
`
}

View file

@ -1,6 +1,7 @@
package shell
import (
"flag"
"fmt"
"io"
@ -21,7 +22,7 @@ func (c *commandVolumeCopy) Name() string {
func (c *commandVolumeCopy) Help() string {
return `copy a volume from one volume server to another volume server
volume.copy <source volume server host:port> <target volume server host:port> <volume id>
volume.copy -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id>
This command copies a volume from one volume server to another volume server.
Usually you will want to unmount the volume first before copying.
@ -35,16 +36,17 @@ func (c *commandVolumeCopy) Do(args []string, commandEnv *CommandEnv, writer io.
return
}
if len(args) != 3 {
fmt.Fprintf(writer, "received args: %+v\n", args)
return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <volume id>")
volCopyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := volCopyCommand.Int("volumeId", 0, "the volume id")
sourceNodeStr := volCopyCommand.String("source", "", "the source volume server <host>:<port>")
targetNodeStr := volCopyCommand.String("target", "", "the target volume server <host>:<port>")
if err = volCopyCommand.Parse(args); err != nil {
return nil
}
sourceVolumeServer, targetVolumeServer, volumeIdString := args[0], args[1], args[2]
volumeId, err := needle.NewVolumeId(volumeIdString)
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
}
sourceVolumeServer, targetVolumeServer := *sourceNodeStr, *targetNodeStr
volumeId := needle.VolumeId(*volumeIdInt)
if sourceVolumeServer == targetVolumeServer {
return fmt.Errorf("source and target volume servers are the same!")

View file

@ -1,7 +1,7 @@
package shell
import (
"fmt"
"flag"
"io"
"github.com/chrislusf/seaweedfs/weed/storage/needle"
@ -21,7 +21,7 @@ func (c *commandVolumeDelete) Name() string {
func (c *commandVolumeDelete) Help() string {
return `delete a live volume from one volume server
volume.delete <volume server host:port> <volume id>
volume.delete -node <volume server host:port> -volumeId <volume id>
This command deletes a volume from one volume server.
@ -34,16 +34,16 @@ func (c *commandVolumeDelete) Do(args []string, commandEnv *CommandEnv, writer i
return
}
if len(args) != 2 {
fmt.Fprintf(writer, "received args: %+v\n", args)
return fmt.Errorf("need 2 args of <volume server host:port> <volume id>")
volDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := volDeleteCommand.Int("volumeId", 0, "the volume id")
nodeStr := volDeleteCommand.String("node", "", "the volume server <host>:<port>")
if err = volDeleteCommand.Parse(args); err != nil {
return nil
}
sourceVolumeServer, volumeIdString := args[0], args[1]
volumeId, err := needle.NewVolumeId(volumeIdString)
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
}
sourceVolumeServer := *nodeStr
volumeId := needle.VolumeId(*volumeIdInt)
return deleteVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)

View file

@ -2,6 +2,7 @@ package shell
import (
"context"
"flag"
"fmt"
"github.com/chrislusf/seaweedfs/weed/storage/needle"
"io"
@ -50,11 +51,14 @@ func (c *commandVolumeFixReplication) Do(args []string, commandEnv *CommandEnv,
return
}
takeAction := true
if len(args) > 0 && args[0] == "-n" {
takeAction = false
volFixReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
skipChange := volFixReplicationCommand.Bool("n", false, "skip the changes")
if err = volFixReplicationCommand.Parse(args); err != nil {
return nil
}
takeAction := !*skipChange
var resp *master_pb.VolumeListResponse
err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})

View file

@ -2,7 +2,7 @@ package shell
import (
"context"
"fmt"
"flag"
"io"
"github.com/chrislusf/seaweedfs/weed/operation"
@ -25,7 +25,7 @@ func (c *commandVolumeMount) Name() string {
func (c *commandVolumeMount) Help() string {
return `mount a volume from one volume server
volume.mount <volume server host:port> <volume id>
volume.mount -node <volume server host:port> -volumeId <volume id>
This command mounts a volume from one volume server.
@ -38,16 +38,16 @@ func (c *commandVolumeMount) Do(args []string, commandEnv *CommandEnv, writer io
return
}
if len(args) != 2 {
fmt.Fprintf(writer, "received args: %+v\n", args)
return fmt.Errorf("need 2 args of <volume server host:port> <volume id>")
volMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := volMountCommand.Int("volumeId", 0, "the volume id")
nodeStr := volMountCommand.String("node", "", "the volume server <host>:<port>")
if err = volMountCommand.Parse(args); err != nil {
return nil
}
sourceVolumeServer, volumeIdString := args[0], args[1]
volumeId, err := needle.NewVolumeId(volumeIdString)
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
}
sourceVolumeServer := *nodeStr
volumeId := needle.VolumeId(*volumeIdInt)
return mountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)

View file

@ -2,6 +2,7 @@ package shell
import (
"context"
"flag"
"fmt"
"io"
"log"
@ -27,7 +28,7 @@ func (c *commandVolumeMove) Name() string {
func (c *commandVolumeMove) Help() string {
return `move a live volume from one volume server to another volume server
volume.move <source volume server host:port> <target volume server host:port> <volume id>
volume.move -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id>
This command move a live volume from one volume server to another volume server. Here are the steps:
@ -48,16 +49,17 @@ func (c *commandVolumeMove) Do(args []string, commandEnv *CommandEnv, writer io.
return
}
if len(args) != 3 {
fmt.Fprintf(writer, "received args: %+v\n", args)
return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <volume id>")
volMoveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := volMoveCommand.Int("volumeId", 0, "the volume id")
sourceNodeStr := volMoveCommand.String("source", "", "the source volume server <host>:<port>")
targetNodeStr := volMoveCommand.String("target", "", "the target volume server <host>:<port>")
if err = volMoveCommand.Parse(args); err != nil {
return nil
}
sourceVolumeServer, targetVolumeServer, volumeIdString := args[0], args[1], args[2]
volumeId, err := needle.NewVolumeId(volumeIdString)
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
}
sourceVolumeServer, targetVolumeServer := *sourceNodeStr, *targetNodeStr
volumeId := needle.VolumeId(*volumeIdInt)
if sourceVolumeServer == targetVolumeServer {
return fmt.Errorf("source and target volume servers are the same!")

View file

@ -2,7 +2,7 @@ package shell
import (
"context"
"fmt"
"flag"
"io"
"github.com/chrislusf/seaweedfs/weed/operation"
@ -25,7 +25,7 @@ func (c *commandVolumeUnmount) Name() string {
func (c *commandVolumeUnmount) Help() string {
return `unmount a volume from one volume server
volume.unmount <volume server host:port> <volume id>
volume.unmount -node <volume server host:port> -volumeId <volume id>
This command unmounts a volume from one volume server.
@ -38,16 +38,16 @@ func (c *commandVolumeUnmount) Do(args []string, commandEnv *CommandEnv, writer
return
}
if len(args) != 2 {
fmt.Fprintf(writer, "received args: %+v\n", args)
return fmt.Errorf("need 2 args of <volume server host:port> <volume id>")
volUnmountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := volUnmountCommand.Int("volumeId", 0, "the volume id")
nodeStr := volUnmountCommand.String("node", "", "the volume server <host>:<port>")
if err = volUnmountCommand.Parse(args); err != nil {
return nil
}
sourceVolumeServer, volumeIdString := args[0], args[1]
volumeId, err := needle.NewVolumeId(volumeIdString)
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
}
sourceVolumeServer := *nodeStr
volumeId := needle.VolumeId(*volumeIdInt)
return unmountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)