| 
 | 1 | +/*  | 
 | 2 | + *  Copyright (c) 2021 NetEase Inc.  | 
 | 3 | + *  | 
 | 4 | + *  Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + *  you may not use this file except in compliance with the License.  | 
 | 6 | + *  You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *      http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + *  Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + *  distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + *  See the License for the specific language governing permissions and  | 
 | 14 | + *  limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +/*  | 
 | 18 | + * Project: CurveAdm  | 
 | 19 | + * Created Date: 2022-02-08  | 
 | 20 | + * Author: Jingli Chen (Wine93)  | 
 | 21 | + */  | 
 | 22 | + | 
 | 23 | +package spdk_target  | 
 | 24 | + | 
 | 25 | +import (  | 
 | 26 | +	"github.com/fatih/color"  | 
 | 27 | +	"github.com/opencurve/curveadm/cli/cli"  | 
 | 28 | +	"github.com/opencurve/curveadm/cli/command/client"  | 
 | 29 | +	comm "github.com/opencurve/curveadm/internal/common"  | 
 | 30 | +	"github.com/opencurve/curveadm/internal/configure"  | 
 | 31 | +	"github.com/opencurve/curveadm/internal/configure/topology"  | 
 | 32 | +	"github.com/opencurve/curveadm/internal/errno"  | 
 | 33 | +	"github.com/opencurve/curveadm/internal/playbook"  | 
 | 34 | +	"github.com/opencurve/curveadm/internal/task/task/bs"  | 
 | 35 | +	cliutil "github.com/opencurve/curveadm/internal/utils"  | 
 | 36 | +	utils "github.com/opencurve/curveadm/internal/utils"  | 
 | 37 | +	"github.com/spf13/cobra"  | 
 | 38 | +)  | 
 | 39 | + | 
 | 40 | +var (  | 
 | 41 | +	ADD_PLAYBOOK_STEPS = []int{  | 
 | 42 | +		//playbook.CREATE_VOLUME,  | 
 | 43 | +		playbook.ADD_TARGET,  | 
 | 44 | +	}  | 
 | 45 | +)  | 
 | 46 | + | 
 | 47 | +type addOptions struct {  | 
 | 48 | +	image     string  | 
 | 49 | +	host      string  | 
 | 50 | +	size      string  | 
 | 51 | +	create    bool  | 
 | 52 | +	filename  string  | 
 | 53 | +	blocksize string  | 
 | 54 | +}  | 
 | 55 | + | 
 | 56 | +func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {  | 
 | 57 | +	if _, _, err := client.ParseImage(options.image); err != nil {  | 
 | 58 | +		return err  | 
 | 59 | +	} else if _, err = client.ParseSize(options.size); err != nil {  | 
 | 60 | +		return err  | 
 | 61 | +	} else if _, err = client.ParseBlockSize(options.blocksize); err != nil {  | 
 | 62 | +		return err  | 
 | 63 | +	} else if !utils.PathExist(options.filename) {  | 
 | 64 | +		return errno.ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST.  | 
 | 65 | +			F("file path: %s", utils.AbsPath(options.filename))  | 
 | 66 | +	}  | 
 | 67 | +	return nil  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {  | 
 | 71 | +	var options addOptions  | 
 | 72 | + | 
 | 73 | +	cmd := &cobra.Command{  | 
 | 74 | +		Use:   "add USER:VOLUME [OPTIONS]",  | 
 | 75 | +		Short: "Add a spdk target of CurveBS",  | 
 | 76 | +		Args:  cliutil.ExactArgs(1),  | 
 | 77 | +		PreRunE: func(cmd *cobra.Command, args []string) error {  | 
 | 78 | +			options.image = args[0]  | 
 | 79 | +			return checkAddOptions(curveadm, options)  | 
 | 80 | +		},  | 
 | 81 | +		RunE: func(cmd *cobra.Command, args []string) error {  | 
 | 82 | +			options.image = args[0]  | 
 | 83 | +			return runAdd(curveadm, options)  | 
 | 84 | +		},  | 
 | 85 | +		DisableFlagsInUseLine: true,  | 
 | 86 | +	}  | 
 | 87 | + | 
 | 88 | +	flags := cmd.Flags()  | 
 | 89 | +	flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host")  | 
 | 90 | +	flags.BoolVar(&options.create, "create", false, "Create volume if not exist")  | 
 | 91 | +	flags.StringVar(&options.size, "size", "10GiB", "Specify volume size")  | 
 | 92 | +	flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")  | 
 | 93 | +	flags.StringVar(&options.blocksize, "blocksize", "4096B", "Specify volume blocksize")  | 
 | 94 | +	return cmd  | 
 | 95 | +}  | 
 | 96 | + | 
 | 97 | +func genAddPlaybook(curveadm *cli.CurveAdm,  | 
 | 98 | +	ccs []*configure.ClientConfig,  | 
 | 99 | +	options addOptions) (*playbook.Playbook, error) {  | 
 | 100 | +	user, name, _ := client.ParseImage(options.image)  | 
 | 101 | +	size, _ := client.ParseSize(options.size)  | 
 | 102 | +	blocksize, _ := client.ParseBlockSize(options.blocksize)  | 
 | 103 | +	steps := ADD_PLAYBOOK_STEPS  | 
 | 104 | +	pb := playbook.NewPlaybook(curveadm)  | 
 | 105 | +	for _, step := range steps {  | 
 | 106 | +		pb.AddStep(&playbook.PlaybookStep{  | 
 | 107 | +			Type:    step,  | 
 | 108 | +			Configs: ccs,  | 
 | 109 | +			Options: map[string]interface{}{  | 
 | 110 | +				comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{  | 
 | 111 | +					Host:      options.host,  | 
 | 112 | +					User:      user,  | 
 | 113 | +					Volume:    name,  | 
 | 114 | +					Size:      uint64(size),  | 
 | 115 | +					Blocksize: blocksize,  | 
 | 116 | +					Create:    options.create,  | 
 | 117 | +				},  | 
 | 118 | +			},  | 
 | 119 | +		})  | 
 | 120 | +	}  | 
 | 121 | +	return pb, nil  | 
 | 122 | +}  | 
 | 123 | + | 
 | 124 | +func runAdd(curveadm *cli.CurveAdm, options addOptions) error {  | 
 | 125 | +	// 1) parse client configure  | 
 | 126 | +	cc, err := configure.ParseClientConfig(options.filename)  | 
 | 127 | +	if err != nil {  | 
 | 128 | +		return err  | 
 | 129 | +	} else if cc.GetKind() != topology.KIND_CURVEBS {  | 
 | 130 | +		return errno.ERR_REQUIRE_CURVEBS_KIND_CLIENT_CONFIGURE_FILE.  | 
 | 131 | +			F("kind: %s", cc.GetKind())  | 
 | 132 | +	}  | 
 | 133 | + | 
 | 134 | +	// 2) generate map playbook  | 
 | 135 | +	pb, err := genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options)  | 
 | 136 | +	if err != nil {  | 
 | 137 | +		return err  | 
 | 138 | +	}  | 
 | 139 | + | 
 | 140 | +	// 3) run playground  | 
 | 141 | +	err = pb.Run()  | 
 | 142 | +	if err != nil {  | 
 | 143 | +		return err  | 
 | 144 | +	}  | 
 | 145 | + | 
 | 146 | +	// 4) print success prompt  | 
 | 147 | +	curveadm.WriteOutln("")  | 
 | 148 | +	curveadm.WriteOutln(color.GreenString("Add target (%s) to %s success ^_^"),  | 
 | 149 | +		options.image, options.host)  | 
 | 150 | +	return nil  | 
 | 151 | +}  | 
0 commit comments