Merge pull request #238 from abperiasamy/donut-cleanup

donut cleanup
This commit is contained in:
Anand Babu (AB) Periasamy
2015-04-05 11:22:10 -07:00
10 changed files with 67 additions and 505 deletions
+45 -15
View File
@@ -50,9 +50,9 @@ func parseDestinationArgs(urlParsed *url.URL, destination, source object) (objec
case urlParsed.Scheme == "http" || urlParsed.Scheme == "https":
if urlParsed.Host == "" {
if urlParsed.Path == "" {
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
destination.host = urlParsed.Host
destination.scheme = urlParsed.Scheme
@@ -64,7 +64,7 @@ func parseDestinationArgs(urlParsed *url.URL, destination, source object) (objec
}
case urlParsed.Scheme == "":
if urlParsed.Host != "" {
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
if urlParsed.Path == "." {
destination.key = source.key
@@ -73,7 +73,7 @@ func parseDestinationArgs(urlParsed *url.URL, destination, source object) (objec
}
destination.bucket = urlParsed.Host
case urlParsed.Scheme != "http" && urlParsed.Scheme != "https":
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
return destination, nil
}
@@ -83,9 +83,9 @@ func parseSourceArgs(urlParsed *url.URL, firstArg string, source object) (object
case urlParsed.Scheme == "http" || urlParsed.Scheme == "https":
if urlParsed.Host == "" {
if urlParsed.Path == "" {
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
source.scheme = urlParsed.Scheme
source.host = urlParsed.Host
@@ -97,17 +97,17 @@ func parseSourceArgs(urlParsed *url.URL, firstArg string, source object) (object
}
case urlParsed.Scheme == "":
if urlParsed.Host != "" {
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
if urlParsed.Path != firstArg {
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
if urlParsed.Path == "." {
return object{}, errFskey
}
source.key = strings.TrimPrefix(urlParsed.Path, "/")
case urlParsed.Scheme != "http" && urlParsed.Scheme != "https":
return object{}, errInvalidScheme
return object{}, errUnsupportedScheme
}
return source, nil
}
@@ -170,12 +170,42 @@ func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) {
auth.AccessKeyID = hostCfg.Auth.AccessKeyID
auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey
traceTransport := getTraceTransport()
if debug {
clnt = s3.GetNewClient(&auth, urlStr, traceTransport)
} else {
clnt = s3.GetNewClient(&auth, urlStr, http.DefaultTransport)
uType, err := getURLType(urlStr)
if err != nil {
return nil, err
}
switch uType {
case urlS3: // Minio and S3 compatible object storage
traceTransport := getTraceTransport()
if debug {
clnt = s3.GetNewClient(&auth, urlStr, traceTransport)
} else {
clnt = s3.GetNewClient(&auth, urlStr, http.DefaultTransport)
}
return clnt, nil
// TODO: donut.GetNewClient should only take donutName as input. Rest it should read from its own config file.
/*
case urlDonut: // Donut object storage
host, err := url2Host(urlStr)
if err != nil {
return nil, err
}
clnt, err := donut.GetNewClient(host, nodeDiskMap)
if err != nil {
return nil, err
}
return clnt, nil */
case urlFile: // POSIX compatible file systems
fallthrough
case urlUnknown: // Unknown type
fallthrough
default:
return nil, errUnsupportedScheme
}
return clnt, nil
}
-8
View File
@@ -130,13 +130,6 @@ EXAMPLES:
},
},
}
donutCmd = cli.Command{
Name: "donut",
Usage: "donut admin",
Description: "",
Subcommands: donutOptions,
}
)
type object struct {
@@ -159,7 +152,6 @@ var options = []cli.Command{
cpCmd,
lsCmd,
mbCmd,
donutCmd,
configCmd,
}
-73
View File
@@ -1,73 +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
func isStringInSlice(items []string, item string) bool {
for _, s := range items {
if s == item {
return true
}
}
return false
}
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
}
-87
View File
@@ -1,87 +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 (
"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
}
-142
View File
@@ -1,142 +0,0 @@
package main
import (
"fmt"
"io"
"net/url"
"os"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client/donut"
)
func upload(urlArg1, urlArg2 *url.URL, mcDonutConfigData *mcDonutConfig) {
st, stErr := os.Stat(urlArg1.Path)
if os.IsNotExist(stErr) {
panic(stErr)
}
if st.IsDir() {
panic("is a directory")
}
reader, err := os.OpenFile(urlArg1.Path, 2, os.ModeAppend)
defer reader.Close()
if err != nil {
panic(err)
}
nodes := make(map[string][]string)
for k, v := range mcDonutConfigData.Donuts[urlArg2.Host].Node {
nodes[k] = v.ActiveDisks
}
d, err := donut.GetNewClient(urlArg2.Host, nodes)
if err != nil {
fatal(err.Error())
}
bucketName, objectName, err := url2Object(urlArg2.String())
if err != nil {
fatal(err.Error())
}
if err := d.Put(bucketName, objectName, "", st.Size(), reader); err != nil {
fatal(err.Error())
}
}
func download(urlArg1, urlArg2 *url.URL, mcDonutConfigData *mcDonutConfig) {
nodes := make(map[string][]string)
for k, v := range mcDonutConfigData.Donuts[urlArg1.Host].Node {
nodes[k] = v.ActiveDisks
}
d, err := donut.GetNewClient(urlArg1.Host, nodes)
if err != nil {
fatal(err.Error())
}
bucketName, objectName, err := url2Object(urlArg1.String())
if err != nil {
fatal(err.Error())
}
// Send HEAD request to validate if file exists.
objectSize, _, err := d.Stat(bucketName, objectName)
if err != nil {
fatal(err.Error())
}
// Check if the object already exists
st, err := os.Stat(urlArg2.Path)
switch os.IsNotExist(err) {
case true:
writer, err := os.Create(urlArg2.Path)
defer writer.Close()
if err != nil {
fatal(err.Error())
}
reader, size, _, err := d.Get(bucketName, objectName)
if err != nil {
fatal(err.Error())
}
_, err = io.CopyN(writer, reader, size)
if err != nil {
fatal(err.Error())
}
reader.Close()
case false:
downloadedSize := st.Size()
// Verify if file is already downloaded
if downloadedSize == objectSize {
msg := fmt.Sprintf("%s object has been already downloaded", urlArg2.Path)
fatal(msg)
}
writer, err := os.OpenFile(urlArg2.Path, os.O_RDWR, 0600)
defer writer.Close()
if err != nil {
fatal(err.Error())
}
_, err = writer.Seek(downloadedSize, os.SEEK_SET)
if err != nil {
fatal(err.Error())
}
remainingSize := objectSize - downloadedSize
reader, size, _, err := d.GetPartial(bucketName, objectName, downloadedSize, remainingSize)
if err != nil {
fatal(err.Error())
}
_, err = io.CopyN(writer, reader, size)
if err != nil {
fatal(err.Error())
}
reader.Close()
}
}
func doDonutCPCmd(c *cli.Context) {
if !c.Args().Present() {
fatal("no args?")
}
mcDonutConfigData, err := loadDonutConfig()
if err != nil {
fatal(err.Error())
}
switch len(c.Args()) {
case 2:
urlArg1, errArg1 := url.Parse(c.Args().Get(0))
if errArg1 != nil {
fatal(errArg1.Error())
}
urlArg2, errArg2 := url.Parse(c.Args().Get(1))
if errArg2 != nil {
fatal(errArg2.Error())
}
switch true {
case urlArg1.Scheme != "" && urlArg2.Scheme == "":
if _, ok := mcDonutConfigData.Donuts[urlArg1.Host]; !ok {
msg := fmt.Sprintf("requested donut: <%s> does not exist", urlArg1.Host)
fatal(msg)
}
download(urlArg1, urlArg2, mcDonutConfigData)
case urlArg1.Scheme == "" && urlArg2.Scheme != "":
if _, ok := mcDonutConfigData.Donuts[urlArg2.Host]; !ok {
msg := fmt.Sprintf("requested donut: <%s> does not exist", urlArg2.Host)
fatal(msg)
}
upload(urlArg1, urlArg2, mcDonutConfigData)
}
}
}
-71
View File
@@ -1,71 +0,0 @@
/*
* 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 (
"fmt"
"net/url"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client/donut"
)
// doDonutListCmd - list buckets and objects
func doDonutListCmd(c *cli.Context) {
if !c.Args().Present() {
fatal("no args?")
}
urlArg1, err := url.Parse(c.Args().First())
if err != nil {
fatal(err.Error())
}
donutConfigData, err := loadDonutConfig()
if err != nil {
fatal(err.Error())
}
if _, ok := donutConfigData.Donuts[urlArg1.Host]; !ok {
msg := fmt.Sprintf("requested donut: <%s> does not exist", urlArg1.Host)
fatal(msg)
}
nodes := make(map[string][]string)
for k, v := range donutConfigData.Donuts[urlArg1.Host].Node {
nodes[k] = v.ActiveDisks
}
d, err := donut.GetNewClient(urlArg1.Host, nodes)
if err != nil {
fatal(err.Error())
}
bucketName, objectName, err := url2Object(urlArg1.String())
if err != nil {
fatal(err.Error())
}
if bucketName == "" { // List Buckets
buckets, err := d.ListBuckets()
if err != nil {
fatal(err.Error())
}
printBuckets(buckets)
} else { // List objects in a bucket
items, err := d.ListObjects(bucketName, objectName)
if err != nil {
fatal(err.Error())
}
printObjects(items)
}
}
-58
View File
@@ -1,58 +0,0 @@
/*
* 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 (
"fmt"
"strings"
"net/url"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client/donut"
)
// doMakeDonutBucketCmd creates a new bucket
func doMakeDonutBucketCmd(c *cli.Context) {
if !c.Args().Present() {
fatal("no args?")
}
urlArg1, err := url.Parse(c.Args().First())
if err != nil {
fatal(err.Error())
}
mcDonutConfigData, err := loadDonutConfig()
if err != nil {
fatal(err.Error())
}
if _, ok := mcDonutConfigData.Donuts[urlArg1.Host]; !ok {
msg := fmt.Sprintf("requested donut: <%s> does not exist", urlArg1.Host)
fatal(msg)
}
nodes := make(map[string][]string)
for k, v := range mcDonutConfigData.Donuts[urlArg1.Host].Node {
nodes[k] = v.ActiveDisks
}
d, err := donut.GetNewClient(urlArg1.Host, nodes)
if err != nil {
fatal(err.Error())
}
err = d.PutBucket(strings.TrimPrefix(urlArg1.Path, "/"))
if err != nil {
fatal(err.Error())
}
}
-48
View File
@@ -1,48 +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"
)
var cpDonutCmd = cli.Command{
Name: "cp",
Usage: "cp",
Description: "Copies a local file or dir or object or bucket to another location locally or to Donut or to S3.",
Action: doDonutCPCmd,
}
var mbDonutCmd = cli.Command{
Name: "mb",
Usage: "make bucket",
Description: "Make a new bucket",
Action: doMakeDonutBucketCmd,
}
var lsDonutCmd = cli.Command{
Name: "ls",
Usage: "get list of objects",
Description: `List Objects and common prefixes under a prefix or all Buckets`,
Action: doDonutListCmd,
}
var donutOptions = []cli.Command{
mbDonutCmd,
lsDonutCmd,
cpDonutCmd,
}
+1 -1
View File
@@ -26,7 +26,7 @@ var (
errEmptyURL = errors.New("URL is empty")
// url
errInvalidScheme = errors.New("Invalid URL scheme only http{s}:// supported")
errUnsupportedScheme = errors.New("Unsupported URL scheme")
// url
errInvalidURL = errors.New("Invalid URL")
+21 -2
View File
@@ -28,7 +28,7 @@ import (
type urlType int
const (
urlUnknown urlType = iota // Minio and S3 compatible object storage
urlUnknown urlType = iota // Unknown type
urlS3 // Minio and S3 compatible object storage
urlDonut // Donut object storage
urlFile // POSIX compatible file systems
@@ -107,6 +107,25 @@ func fixFileURL(urlStr string) (fixedURL string, err error) {
}
// url2Host extracts hostname from the URL.
func url2Host(urlStr string) (host string, err error) {
if urlStr == "" {
return "", errEmptyURL
}
utype, e := getURLType(urlStr)
if e != nil || utype != urlFile {
return "", e
}
u, e := url.Parse(urlStr)
if e != nil {
return "", e
}
return u.Host, nil
}
// url2Object converts URL to bucket and objectname
func url2Object(urlStr string) (bucketName, objectName string, err error) {
u, err := url.Parse(urlStr)
@@ -155,7 +174,7 @@ func parseURL(arg string) (urlStr string, err error) {
}
if !isValidURL(urlStr) {
return "", errInvalidScheme
return "", errUnsupportedScheme
}
// If it is a file URL, rewrite to file:///path/to form