From 37d0ccaa6cc625a29286e4aa4a942bbbe02c985d Mon Sep 17 00:00:00 2001 From: ThisIsClark Date: Mon, 26 Aug 2019 15:47:14 +0800 Subject: [PATCH 1/3] Fix the segmentation during "make test" --- install/CI/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/CI/test b/install/CI/test index b7bf71f3e..b55ffdacc 100755 --- a/install/CI/test +++ b/install/CI/test @@ -28,7 +28,7 @@ split_line(){ # Start the openapi-spec validation. split_line "Start openapi-spec validation" -wget https://github.com/go-swagger/go-swagger/releases/download/v0.19.0/swagger_linux_amd64 +wget https://github.com/go-swagger/go-swagger/releases/download/v0.19.0/swagger_linux_amd64 -O swagger_linux_amd64 chmod +x ./swagger_linux_amd64 && ./swagger_linux_amd64 validate --stop-on-error openapi-spec/swagger.yaml rm ./swagger_linux_amd64 From c901c2f83ac5c2d835399ac1cbeb7de7175c79dd Mon Sep 17 00:00:00 2001 From: ThisIsClark Date: Tue, 15 Oct 2019 17:20:16 +0800 Subject: [PATCH 2/3] Support dock import through osdsctl dock import --- osdsctl/cli/dock.go | 65 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/osdsctl/cli/dock.go b/osdsctl/cli/dock.go index 094e06819..6edcd1e7c 100644 --- a/osdsctl/cli/dock.go +++ b/osdsctl/cli/dock.go @@ -20,9 +20,14 @@ This module implements a entry into the OpenSDS service. package cli import ( + "fmt" "os" + "os/exec" + "strings" + "io/ioutil" "github.com/spf13/cobra" + "github.com/opensds/opensds/pkg/utils/constants" ) var dockCommand = &cobra.Command{ @@ -43,6 +48,12 @@ var dockListCommand = &cobra.Command{ Run: dockListAction, } +var dockImportCommand = &cobra.Command{ + Use: "import ", + Short: "import dock to opensds", + Run: dockImportAction, +} + var ( dockLimit string dockOffset string @@ -55,6 +66,7 @@ var ( dockStorageType string dockEndpoint string dockDriverName string + dockPoolConfigFilePath string ) func init() { @@ -69,10 +81,11 @@ func init() { dockListCommand.Flags().StringVarP(&dockStorageType, "storageType", "", "", "list docks by storage type") dockListCommand.Flags().StringVarP(&dockEndpoint, "endpoint", "", "", "list docks by endpoint") dockListCommand.Flags().StringVarP(&dockDriverName, "driverName", "", "", "list docks by driver name") + dockImportCommand.Flags().StringVarP(&dockPoolConfigFilePath, "poolConfig", "p", "", "indicate the pool config file") dockCommand.AddCommand(dockShowCommand) dockCommand.AddCommand(dockListCommand) - + dockCommand.AddCommand(dockImportCommand) } func dockAction(cmd *cobra.Command, args []string) { @@ -107,3 +120,53 @@ func dockListAction(cmd *cobra.Command, args []string) { keys := KeyList{"Id", "Name", "Description", "Endpoint", "DriverName"} PrintList(resp, keys, dockFormatters) } + +func dockImportAction(cmd *cobra.Command, args []string) { + ArgsNumCheck(cmd, args, 2) + storageType := args[0] + dockConfigFilePath := args[1] + addBackendType(storageType) + addBackendInfo(dockConfigFilePath) + if dockPoolConfigFilePath != "" { + importDriverConfig(storageType, dockPoolConfigFilePath) + } +} + +func addBackendType(backendType string) { + config, err := ioutil.ReadFile(constants.OpensdsConfigPath) + if err != nil { + Errorln(constants.OpensdsConfigPath, " is not exist!") + os.Exit(1) + } + lines := strings.Split(string(config), "\n") + for i, line := range lines { + if strings.Contains(line, "enabled_backends") { + lines[i] = lines[i] + "," + backendType + } + } + output := strings.Join(lines, "\n") + err = ioutil.WriteFile(constants.OpensdsConfigPath, []byte(output), 0644) +} + +func addBackendInfo(backendConfig string) { + dockConfig, err := ioutil.ReadFile(backendConfig) + if err != nil { + Errorln(backendConfig, " is not exist!") + os.Exit(1) + } + importDockCmd := fmt.Sprintf("cat>>%s< Date: Wed, 6 Nov 2019 16:56:27 +0800 Subject: [PATCH 3/3] Code improvement --- osdsctl/cli/dock.go | 83 +++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/osdsctl/cli/dock.go b/osdsctl/cli/dock.go index 6edcd1e7c..791df8a1c 100644 --- a/osdsctl/cli/dock.go +++ b/osdsctl/cli/dock.go @@ -21,13 +21,13 @@ package cli import ( "fmt" + "io/ioutil" "os" "os/exec" "strings" - "io/ioutil" - "github.com/spf13/cobra" "github.com/opensds/opensds/pkg/utils/constants" + "github.com/spf13/cobra" ) var dockCommand = &cobra.Command{ @@ -55,18 +55,18 @@ var dockImportCommand = &cobra.Command{ } var ( - dockLimit string - dockOffset string - dockSortDir string - dockSortKey string - dockId string - dockName string - dockDescription string - dockStatus string - dockStorageType string - dockEndpoint string - dockDriverName string - dockPoolConfigFilePath string + dockLimit string + dockOffset string + dockSortDir string + dockSortKey string + dockId string + dockName string + dockDescription string + dockStatus string + dockStorageType string + dockEndpoint string + dockDriverName string + poolConfigFilePath string ) func init() { @@ -81,7 +81,7 @@ func init() { dockListCommand.Flags().StringVarP(&dockStorageType, "storageType", "", "", "list docks by storage type") dockListCommand.Flags().StringVarP(&dockEndpoint, "endpoint", "", "", "list docks by endpoint") dockListCommand.Flags().StringVarP(&dockDriverName, "driverName", "", "", "list docks by driver name") - dockImportCommand.Flags().StringVarP(&dockPoolConfigFilePath, "poolConfig", "p", "", "indicate the pool config file") + dockImportCommand.Flags().StringVarP(&poolConfigFilePath, "poolConfig", "p", "", "indicate the pool config file") dockCommand.AddCommand(dockShowCommand) dockCommand.AddCommand(dockListCommand) @@ -125,19 +125,26 @@ func dockImportAction(cmd *cobra.Command, args []string) { ArgsNumCheck(cmd, args, 2) storageType := args[0] dockConfigFilePath := args[1] - addBackendType(storageType) - addBackendInfo(dockConfigFilePath) - if dockPoolConfigFilePath != "" { - importDriverConfig(storageType, dockPoolConfigFilePath) + + importDockConfig(storageType, dockConfigFilePath) + if poolConfigFilePath != "" { + importPoolConfig(storageType, poolConfigFilePath) } } -func addBackendType(backendType string) { +func importDockConfig(storageType, dockConfigFilePath string) { + addEnabledBackendType(storageType) + addEnabledBackendInfo(dockConfigFilePath) +} + +func addEnabledBackendType(backendType string) { + // Read contents from file config, err := ioutil.ReadFile(constants.OpensdsConfigPath) if err != nil { - Errorln(constants.OpensdsConfigPath, " is not exist!") + Errorln("open ", constants.OpensdsConfigPath, " failed") os.Exit(1) } + // Add storage type to line "enabled_backends" lines := strings.Split(string(config), "\n") for i, line := range lines { if strings.Contains(line, "enabled_backends") { @@ -145,28 +152,44 @@ func addBackendType(backendType string) { } } output := strings.Join(lines, "\n") - err = ioutil.WriteFile(constants.OpensdsConfigPath, []byte(output), 0644) + // Write contents back to file + fileStat, err := os.Lstat(constants.OpensdsConfigPath) + if err != nil { + Errorln("open ", constants.OpensdsConfigPath, " failed") + os.Exit(1) + } + err = ioutil.WriteFile(constants.OpensdsConfigPath, []byte(output), fileStat.Mode().Perm()) + if err != nil { + Errorln("write ", constants.OpensdsConfigPath, " failed") + os.Exit(1) + } } -func addBackendInfo(backendConfig string) { +func addEnabledBackendInfo(backendConfig string) { dockConfig, err := ioutil.ReadFile(backendConfig) if err != nil { - Errorln(backendConfig, " is not exist!") + Errorln(backendConfig, " is not exist") os.Exit(1) } importDockCmd := fmt.Sprintf("cat>>%s<>%s<