diff --git a/common.go b/cmd-common.go similarity index 100% rename from common.go rename to cmd-common.go diff --git a/donut-cmd-attach-disk.go b/donut-cmd-attach-disk.go new file mode 100644 index 00000000..d84f4058 --- /dev/null +++ b/donut-cmd-attach-disk.go @@ -0,0 +1,60 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, 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. + */ + +package main + +import ( + "fmt" + + "github.com/minio-io/cli" +) + +func doAttachDiskCmd(c *cli.Context) { + if !c.Args().Present() { + fatal("no args?") + } + disks := c.Args() + mcDonutConfigData, err := loadDonutConfig() + if err != nil { + fatal(err.Error()) + } + donutName := c.String("name") + if donutName == "" { + fatal("Invalid --donut is needed for attach") + } + if _, ok := mcDonutConfigData.Donuts[donutName]; !ok { + msg := fmt.Sprintf("Requested donut name %s does not exist, please use ``mc donut make`` first\n", donutName) + fatal(msg) + } + if _, ok := mcDonutConfigData.Donuts[donutName].Node["localhost"]; !ok { + msg := fmt.Sprintf("Corrupted donut config, please consult donut experts\n") + fatal(msg) + } + activeDisks := mcDonutConfigData.Donuts[donutName].Node["localhost"].ActiveDisks + inactiveDisks := mcDonutConfigData.Donuts[donutName].Node["localhost"].InactiveDisks + for _, disk := range disks { + activeDisks = appendUniq(activeDisks, disk) + inactiveDisks = deleteFromSlice(inactiveDisks, disk) + } + + mcDonutConfigData.Donuts[donutName].Node["localhost"] = nodeConfig{ + ActiveDisks: activeDisks, + InactiveDisks: inactiveDisks, + } + if err := saveDonutConfig(mcDonutConfigData); err != nil { + fatal(err.Error()) + } +} diff --git a/donut-cmd-attachdisk.go b/donut-cmd-attachdisk.go deleted file mode 100644 index 0c2fc293..00000000 --- a/donut-cmd-attachdisk.go +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Minimalist Object Storage, (C) 2015 Minio, 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. - */ - -package main - -import ( - "github.com/minio-io/cli" -) - -func doAttachDiskCmd(c *cli.Context) { -} - -func doDetachDiskCmd(c *cli.Context) { -} diff --git a/donut-cmd-common.go b/donut-cmd-common.go new file mode 100644 index 00000000..b0862905 --- /dev/null +++ b/donut-cmd-common.go @@ -0,0 +1,64 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, 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. + */ + +package main + +func deleteFromSlice(items []string, item string) []string { + var newitems []string + for _, s := range items { + if s == item { + continue + } + newitems = append(newitems, s) + } + return newitems +} + +func appendUniq(slice []string, i string) []string { + for _, ele := range slice { + if ele == i { + return slice + } + } + return append(slice, i) +} + +// Is alphanumeric? +func isalnum(c rune) bool { + return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' +} + +// isValidDonutName - verify donutName to be valid +func isValidDonutName(donutName string) bool { + if len(donutName) > 1024 || len(donutName) == 0 { + return false + } + for _, char := range donutName { + if isalnum(char) { + continue + } + switch char { + case '-': + case '.': + case '_': + case '~': + continue + default: + return false + } + } + return true +} diff --git a/donut-cmd-config.go b/donut-cmd-config.go new file mode 100644 index 00000000..6915c7fa --- /dev/null +++ b/donut-cmd-config.go @@ -0,0 +1,87 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, 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. + */ + +package main + +import ( + "os" + "path" + + "encoding/json" + "io/ioutil" +) + +type nodeConfig struct { + ActiveDisks []string + InactiveDisks []string +} + +type donutConfig struct { + Node map[string]nodeConfig +} + +type mcDonutConfig struct { + Donuts map[string]donutConfig +} + +func getDonutConfigFilename() string { + return path.Join(getMcConfigDir(), "donuts.json") +} + +// saveDonutConfig writes configuration data in json format to donut config file. +func saveDonutConfig(donutConfigData *mcDonutConfig) error { + jsonConfig, err := json.MarshalIndent(donutConfigData, "", "\t") + if err != nil { + return err + } + + err = os.MkdirAll(getMcConfigDir(), 0755) + if !os.IsExist(err) && err != nil { + return err + } + + configFile, err := os.OpenFile(getDonutConfigFilename(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer configFile.Close() + + _, err = configFile.Write(jsonConfig) + if err != nil { + return err + } + return nil +} + +func loadDonutConfig() (donutConfigData *mcDonutConfig, err error) { + configFile := getDonutConfigFilename() + _, err = os.Stat(configFile) + if err != nil { + return nil, err + } + + configBytes, err := ioutil.ReadFile(configFile) + if err != nil { + return nil, err + } + + err = json.Unmarshal(configBytes, &donutConfigData) + if err != nil { + return nil, err + } + + return donutConfigData, nil +} diff --git a/donut-cmd-detach-disk.go b/donut-cmd-detach-disk.go new file mode 100644 index 00000000..7d1aeed2 --- /dev/null +++ b/donut-cmd-detach-disk.go @@ -0,0 +1,60 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, 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. + */ + +package main + +import ( + "fmt" + + "github.com/minio-io/cli" +) + +func doDetachDiskCmd(c *cli.Context) { + if !c.Args().Present() { + fatal("no args?") + } + disks := c.Args() + mcDonutConfigData, err := loadDonutConfig() + if err != nil { + fatal(err.Error()) + } + donutName := c.String("name") + if donutName == "" { + fatal("Invalid --donut is needed for attach") + } + if _, ok := mcDonutConfigData.Donuts[donutName]; !ok { + msg := fmt.Sprintf("Requested donut name %s does not exist, please use ``mc donut make`` first\n", donutName) + fatal(msg) + } + if _, ok := mcDonutConfigData.Donuts[donutName].Node["localhost"]; !ok { + msg := fmt.Sprintf("Corrupted donut config, please consult donut experts\n") + fatal(msg) + } + + inactiveDisks := mcDonutConfigData.Donuts[donutName].Node["localhost"].InactiveDisks + activeDisks := mcDonutConfigData.Donuts[donutName].Node["localhost"].ActiveDisks + for _, disk := range disks { + inactiveDisks = appendUniq(inactiveDisks, disk) + activeDisks = deleteFromSlice(activeDisks, disk) + } + mcDonutConfigData.Donuts[donutName].Node["localhost"] = nodeConfig{ + ActiveDisks: activeDisks, + InactiveDisks: inactiveDisks, + } + if err := saveDonutConfig(mcDonutConfigData); err != nil { + fatal(err.Error()) + } +} diff --git a/donut-cmd-make.go b/donut-cmd-make.go index 98252405..2742f3b0 100644 --- a/donut-cmd-make.go +++ b/donut-cmd-make.go @@ -18,103 +18,10 @@ package main import ( "os" - "path" - - "encoding/json" - "io/ioutil" "github.com/minio-io/cli" ) -type nodeConfig struct { - ActiveDisks []string - InactiveDisks []string -} - -type donutConfig struct { - Node map[string]nodeConfig -} - -type mcDonutConfig struct { - Donuts map[string]donutConfig -} - -// Is alphanumeric? -func isalnum(c rune) bool { - return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' -} - -// isValidDonutName - verify donutName to be valid -func isValidDonutName(donutName string) bool { - if len(donutName) > 1024 || len(donutName) == 0 { - return false - } - for _, char := range donutName { - if isalnum(char) { - continue - } - switch char { - case '-': - case '.': - case '_': - case '~': - continue - default: - return false - } - } - return true -} - -func getDonutConfigFilename() string { - return path.Join(getMcConfigDir(), "donuts.json") -} - -// saveDonutConfig writes configuration data in json format to donut config file. -func saveDonutConfig(donutConfigData *mcDonutConfig) error { - jsonConfig, err := json.MarshalIndent(donutConfigData, "", "\t") - if err != nil { - return err - } - - err = os.MkdirAll(getMcConfigDir(), 0755) - if !os.IsExist(err) && err != nil { - return err - } - - configFile, err := os.OpenFile(getDonutConfigFilename(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - defer configFile.Close() - - _, err = configFile.Write(jsonConfig) - if err != nil { - return err - } - return nil -} - -func loadDonutConfig() (donutConfigData *mcDonutConfig, err error) { - configFile := getDonutConfigFilename() - _, err = os.Stat(configFile) - if err != nil { - return nil, err - } - - configBytes, err := ioutil.ReadFile(configFile) - if err != nil { - return nil, err - } - - err = json.Unmarshal(configBytes, &donutConfigData) - if err != nil { - return nil, err - } - - return donutConfigData, nil -} - func newDonutConfig(donutName string) (*mcDonutConfig, error) { mcDonutConfigData := new(mcDonutConfig) mcDonutConfigData.Donuts = make(map[string]donutConfig) @@ -130,8 +37,11 @@ func newDonutConfig(donutName string) (*mcDonutConfig, error) { // doMakeDonutCmd creates a new donut func doMakeDonutCmd(c *cli.Context) { + if !c.Args().Present() { + fatal("no args?") + } if len(c.Args()) != 1 { - fatal("Invalid args") + fatal("invalid number of args") } donutName := c.Args().First() if !isValidDonutName(donutName) { diff --git a/donut-options.go b/donut-cmd-options.go similarity index 91% rename from donut-options.go rename to donut-cmd-options.go index dd9901b1..efb3ce34 100644 --- a/donut-options.go +++ b/donut-cmd-options.go @@ -32,6 +32,12 @@ var attachDiskCmd = cli.Command{ Usage: "attach disk", Description: "", Action: doAttachDiskCmd, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name", + Usage: "Donut name", + }, + }, } var detachDiskCmd = cli.Command{ @@ -39,6 +45,12 @@ var detachDiskCmd = cli.Command{ Usage: "detach disk", Description: "", Action: doDetachDiskCmd, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name", + Usage: "Donut name", + }, + }, } var healDonutCmd = cli.Command{