From a62661e5f02a72f24ca8e91361b69fb9f2ad2e96 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 26 Mar 2015 16:48:21 -0700 Subject: [PATCH] Add donut make command --- donut-cmd-make.go | 162 ++++++++++++++++++++++++++++++++++++++++++++++ donut-options.go | 8 +++ 2 files changed, 170 insertions(+) create mode 100644 donut-cmd-make.go diff --git a/donut-cmd-make.go b/donut-cmd-make.go new file mode 100644 index 00000000..a3507f45 --- /dev/null +++ b/donut-cmd-make.go @@ -0,0 +1,162 @@ +/* + * Minimalist Object Storage, (C) 2014,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" + + "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) + mcDonutConfigData.Donuts[donutName] = donutConfig{ + Node: make(map[string]nodeConfig), + } + mcDonutConfigData.Donuts[donutName].Node["localhost"] = nodeConfig{ + ActiveDisks: make([]string, 0), + InactiveDisks: make([]string, 0), + } + return mcDonutConfigData, nil +} + +// doMakeDonutCmd creates a new donut +func doMakeDonutCmd(c *cli.Context) { + if len(c.Args()) != 1 { + fatal("Invalid args") + } + donutName := c.Args().First() + if !isValidDonutName(donutName) { + fatal("Invalid donutName") + } + mcDonutConfigData, err := loadDonutConfig() + if os.IsNotExist(err) { + mcDonutConfigData, err = newDonutConfig(donutName) + if err != nil { + fatal(err.Error()) + } + if err := saveDonutConfig(mcDonutConfigData); err != nil { + fatal(err.Error()) + } + } else if err != nil { + fatal(err.Error()) + } + mcDonutConfigData.Donuts[donutName] = donutConfig{ + Node: make(map[string]nodeConfig), + } + mcDonutConfigData.Donuts[donutName].Node["localhost"] = nodeConfig{ + ActiveDisks: make([]string, 0), + InactiveDisks: make([]string, 0), + } + if err := saveDonutConfig(mcDonutConfigData); err != nil { + fatal(err.Error()) + } +} diff --git a/donut-options.go b/donut-options.go index 9cfda544..35118e64 100644 --- a/donut-options.go +++ b/donut-options.go @@ -46,6 +46,13 @@ var mbDonutCmd = cli.Command{ Action: doMakeDonutBucketCmd, } +var makeDonutCmd = cli.Command{ + Name: "make", + Usage: "make", + Description: "", + Action: doMakeDonutCmd, +} + var donutOptions = []cli.Command{ healCmd, attachCmd, @@ -53,6 +60,7 @@ var donutOptions = []cli.Command{ rebalanceCmd, cpDonutCmd, mbDonutCmd, + makeDonutCmd, } func doHealCmd(c *cli.Context) {