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 { func (c *commandVolumeConfigureReplication) Help() string {
return `change volume replication value 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 package shell
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
@ -21,7 +22,7 @@ func (c *commandVolumeCopy) Name() string {
func (c *commandVolumeCopy) Help() string { func (c *commandVolumeCopy) Help() string {
return `copy a volume from one volume server to another volume server 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. This command copies a volume from one volume server to another volume server.
Usually you will want to unmount the volume first before copying. 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 return
} }
if len(args) != 3 { volCopyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
fmt.Fprintf(writer, "received args: %+v\n", args) volumeIdInt := volCopyCommand.Int("volumeId", 0, "the volume id")
return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <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) sourceVolumeServer, targetVolumeServer := *sourceNodeStr, *targetNodeStr
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err) volumeId := needle.VolumeId(*volumeIdInt)
}
if sourceVolumeServer == targetVolumeServer { if sourceVolumeServer == targetVolumeServer {
return fmt.Errorf("source and target volume servers are the same!") return fmt.Errorf("source and target volume servers are the same!")

View file

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

View file

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

View file

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

View file

@ -2,6 +2,7 @@ package shell
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -27,7 +28,7 @@ func (c *commandVolumeMove) Name() string {
func (c *commandVolumeMove) Help() string { func (c *commandVolumeMove) Help() string {
return `move a live volume from one volume server to another volume server 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: 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 return
} }
if len(args) != 3 { volMoveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
fmt.Fprintf(writer, "received args: %+v\n", args) volumeIdInt := volMoveCommand.Int("volumeId", 0, "the volume id")
return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <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) sourceVolumeServer, targetVolumeServer := *sourceNodeStr, *targetNodeStr
if err != nil {
return fmt.Errorf("wrong volume id format %s: %v", volumeId, err) volumeId := needle.VolumeId(*volumeIdInt)
}
if sourceVolumeServer == targetVolumeServer { if sourceVolumeServer == targetVolumeServer {
return fmt.Errorf("source and target volume servers are the same!") return fmt.Errorf("source and target volume servers are the same!")

View file

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