-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YLShiJustFly <youseeicanfly@126.com>
- Loading branch information
1 parent
3b6b0a1
commit 39aaf68
Showing
21 changed files
with
1,442 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (c) 2021 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2022-02-08 | ||
* Author: Jingli Chen (Wine93) | ||
*/ | ||
|
||
package spdk_target | ||
|
||
import ( | ||
"github.com/fatih/color" | ||
"github.com/opencurve/curveadm/cli/cli" | ||
"github.com/opencurve/curveadm/cli/command/client" | ||
comm "github.com/opencurve/curveadm/internal/common" | ||
"github.com/opencurve/curveadm/internal/configure" | ||
"github.com/opencurve/curveadm/internal/configure/topology" | ||
"github.com/opencurve/curveadm/internal/errno" | ||
"github.com/opencurve/curveadm/internal/playbook" | ||
"github.com/opencurve/curveadm/internal/task/task/bs" | ||
cliutil "github.com/opencurve/curveadm/internal/utils" | ||
utils "github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
ADD_PLAYBOOK_STEPS = []int{ | ||
//playbook.CREATE_VOLUME, | ||
playbook.ADD_TARGET, | ||
} | ||
) | ||
|
||
type addOptions struct { | ||
image string | ||
host string | ||
size string | ||
create bool | ||
filename string | ||
blocksize string | ||
} | ||
|
||
func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error { | ||
if _, _, err := client.ParseImage(options.image); err != nil { | ||
return err | ||
} else if _, err = client.ParseSize(options.size); err != nil { | ||
return err | ||
} else if _, err = client.ParseBlockSize(options.blocksize); err != nil { | ||
return err | ||
} else if !utils.PathExist(options.filename) { | ||
return errno.ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST. | ||
F("file path: %s", utils.AbsPath(options.filename)) | ||
} | ||
return nil | ||
} | ||
|
||
func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
var options addOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "add USER:VOLUME [OPTIONS]", | ||
Short: "Add a spdk target of CurveBS", | ||
Args: cliutil.ExactArgs(1), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
options.image = args[0] | ||
return checkAddOptions(curveadm, options) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
options.image = args[0] | ||
return runAdd(curveadm, options) | ||
}, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host") | ||
flags.BoolVar(&options.create, "create", false, "Create volume if not exist") | ||
flags.StringVar(&options.size, "size", "10GiB", "Specify volume size") | ||
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file") | ||
flags.StringVar(&options.blocksize, "blocksize", "4096B", "Specify volume blocksize") | ||
return cmd | ||
} | ||
|
||
func genAddPlaybook(curveadm *cli.CurveAdm, | ||
ccs []*configure.ClientConfig, | ||
options addOptions) (*playbook.Playbook, error) { | ||
user, name, _ := client.ParseImage(options.image) | ||
size, _ := client.ParseSize(options.size) | ||
blocksize, _ := client.ParseBlockSize(options.blocksize) | ||
steps := ADD_PLAYBOOK_STEPS | ||
pb := playbook.NewPlaybook(curveadm) | ||
for _, step := range steps { | ||
pb.AddStep(&playbook.PlaybookStep{ | ||
Type: step, | ||
Configs: ccs, | ||
Options: map[string]interface{}{ | ||
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{ | ||
Host: options.host, | ||
User: user, | ||
Volume: name, | ||
Size: uint64(size), | ||
Blocksize: blocksize, | ||
Create: options.create, | ||
}, | ||
}, | ||
}) | ||
} | ||
return pb, nil | ||
} | ||
|
||
func runAdd(curveadm *cli.CurveAdm, options addOptions) error { | ||
// 1) parse client configure | ||
cc, err := configure.ParseClientConfig(options.filename) | ||
if err != nil { | ||
return err | ||
} else if cc.GetKind() != topology.KIND_CURVEBS { | ||
return errno.ERR_REQUIRE_CURVEBS_KIND_CLIENT_CONFIGURE_FILE. | ||
F("kind: %s", cc.GetKind()) | ||
} | ||
|
||
// 2) generate map playbook | ||
pb, err := genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 3) run playground | ||
err = pb.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 4) print success prompt | ||
curveadm.WriteOutln("") | ||
curveadm.WriteOutln(color.GreenString("Add target (%s) to %s success ^_^"), | ||
options.image, options.host) | ||
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,47 @@ | ||
/* | ||
* Copyright (c) 2021 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2022-02-08 | ||
* Author: Jingli Chen (Wine93) | ||
*/ | ||
|
||
package spdk_target | ||
|
||
import ( | ||
"github.com/opencurve/curveadm/cli/cli" | ||
cliutil "github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewSpdkTargetCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "spdktarget", | ||
Short: "Manage spdk target of CurveBS", | ||
Args: cliutil.NoArgs, | ||
RunE: cliutil.ShowHelp(curveadm.Err()), | ||
} | ||
|
||
cmd.AddCommand( | ||
NewStartCommand(curveadm), | ||
NewStopCommand(curveadm), | ||
NewAddCommand(curveadm), | ||
NewDeleteCommand(curveadm), | ||
NewListCommand(curveadm), | ||
) | ||
return cmd | ||
} |
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,109 @@ | ||
/* | ||
* Copyright (c) 2021 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2022-02-09 | ||
* Author: Jingli Chen (Wine93) | ||
*/ | ||
|
||
package spdk_target | ||
|
||
import ( | ||
"github.com/fatih/color" | ||
"github.com/opencurve/curveadm/cli/cli" | ||
comm "github.com/opencurve/curveadm/internal/common" | ||
"github.com/opencurve/curveadm/internal/playbook" | ||
"github.com/opencurve/curveadm/internal/task/task/bs" | ||
cliutil "github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
DELETE_PLAYBOOK_STEPS = []int{ | ||
playbook.DELETE_TARGET, | ||
} | ||
) | ||
|
||
type deleteOptions struct { | ||
host string | ||
sockname string | ||
rpcpath string | ||
tid string | ||
} | ||
|
||
func NewDeleteCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
var options deleteOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "rm TID [OPTIONS]", | ||
Aliases: []string{"delete"}, | ||
Short: "Delete a spdk target of CurveBS", | ||
Args: cliutil.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
options.sockname = args[0] | ||
options.rpcpath = args[1] | ||
options.tid = args[2] | ||
return runDelete(curveadm, options) | ||
}, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host") | ||
|
||
return cmd | ||
} | ||
|
||
func genDeletePlaybook(curveadm *cli.CurveAdm, options deleteOptions) (*playbook.Playbook, error) { | ||
steps := DELETE_PLAYBOOK_STEPS | ||
pb := playbook.NewPlaybook(curveadm) | ||
for _, step := range steps { | ||
pb.AddStep(&playbook.PlaybookStep{ | ||
Type: step, | ||
Configs: nil, | ||
Options: map[string]interface{}{ | ||
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{ | ||
Host: options.host, | ||
Sockname: options.sockname, | ||
RpcPath: options.rpcpath, | ||
Tid: options.tid, | ||
}, | ||
}, | ||
}) | ||
} | ||
return pb, nil | ||
} | ||
|
||
func runDelete(curveadm *cli.CurveAdm, options deleteOptions) error { | ||
// 1) generate list playbook | ||
pb, err := genDeletePlaybook(curveadm, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2) run playground | ||
err = pb.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 3) print targets | ||
curveadm.WriteOutln("") | ||
curveadm.WriteOutln(color.GreenString("Delete target (tid=%s) on %s success ^_^"), | ||
options.tid, options.host) | ||
return nil | ||
} |
Oops, something went wrong.