Merge pull request #188 from harshavardhana/pr_out_implement_attach_detach_disk_into_donut_config
This commit is contained in:
@@ -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 <name> 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())
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 <name> 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())
|
||||
}
|
||||
}
|
||||
+4
-94
@@ -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) {
|
||||
|
||||
@@ -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{
|
||||
Reference in New Issue
Block a user