Add admin lock command (#1942)
This commit is contained in:
committed by
Harshavardhana
parent
86777e7863
commit
7cec8f2285
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Minio Client (C) 2016 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 cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
var (
|
||||
adminLockClearFlags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "older-than, o",
|
||||
Usage: "Only show locks that are older than a specified time, i.e. 30s",
|
||||
Value: "0s",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
var adminLockClearCmd = cli.Command{
|
||||
Name: "clear",
|
||||
Usage: "Clear locks hold in a given Minio server",
|
||||
Action: mainAdminLockClear,
|
||||
Flags: append(adminLockClearFlags, globalFlags...),
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc admin lock {{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
mc admin lock {{.Name}} ALIAS/BUCKET/PREFIX
|
||||
|
||||
FLAGS:
|
||||
{{range .Flags}}{{.}}
|
||||
{{end}}
|
||||
|
||||
EXAMPLES:
|
||||
1. Clear all locks hold by 'testbucket' in a Minio server represented by its alias 'play'.
|
||||
$ mc admin lock {{.Name}} play/testbucket
|
||||
|
||||
2. Clear all 'testbucket' locks that are older than 15 minutes.
|
||||
$ mc admin lock {{.Name}} --older-than 15m play/testbucket/
|
||||
|
||||
3. Clear all locks hold by all objects under 'dir' prefix
|
||||
$ mc admin lock {{.Name}} play/testbucket/dir/
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// lockClearMessage container to hold locks information.
|
||||
type lockClearMessage struct {
|
||||
Status string `json:"status"`
|
||||
LockInfo madmin.VolumeLockInfo `json:"volumeLockInfo"`
|
||||
}
|
||||
|
||||
// String colorized service status message.
|
||||
func (u lockClearMessage) String() string {
|
||||
msg := fmt.Sprintf("%s/%s (LocksOnObject: %d, locksAcquiredOnObject: %d, totalBlockLocks:%d): ",
|
||||
u.LockInfo.Bucket,
|
||||
u.LockInfo.Object,
|
||||
u.LockInfo.LocksOnObject,
|
||||
u.LockInfo.LocksAcquiredOnObject,
|
||||
u.LockInfo.TotalBlockedLocks)
|
||||
for _, detail := range u.LockInfo.LockDetailsOnObject {
|
||||
msg += fmt.Sprintf(" %+v", detail)
|
||||
}
|
||||
msg += "\n"
|
||||
return msg
|
||||
}
|
||||
|
||||
// JSON jsonified service status Message message.
|
||||
func (u lockClearMessage) JSON() string {
|
||||
u.Status = "success"
|
||||
statusJSONBytes, e := json.Marshal(u)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(statusJSONBytes)
|
||||
}
|
||||
|
||||
// checkAdminLockClearSyntax - validate all the passed arguments
|
||||
func checkAdminLockClearSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "clear", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
func mainAdminLockClear(ctx *cli.Context) error {
|
||||
|
||||
setGlobalsFromContext(ctx)
|
||||
checkAdminLockClearSyntax(ctx)
|
||||
|
||||
// Get the alias parameter from cli
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
|
||||
// Parse older-than flag
|
||||
olderThan, e := time.ParseDuration(ctx.String("older-than"))
|
||||
fatalIf(probe.NewError(e), "Unable to parse the passed older-than flag.")
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
|
||||
splits := splitStr(aliasedURL, "/", 3)
|
||||
|
||||
// Clear locks related to a specified pair of bucket and prefix
|
||||
locksInfo, e := client.ClearLocks(splits[1], splits[2], olderThan)
|
||||
fatalIf(probe.NewError(e), "Cannot clear the specified locks.")
|
||||
|
||||
for _, l := range locksInfo {
|
||||
printMsg(lockClearMessage{LockInfo: l})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Minio Client (C) 2016 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 cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
var (
|
||||
adminLockListFlags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "older-than, o",
|
||||
Usage: "Only show locks that are older than a specified time, i.e. 30s",
|
||||
Value: "0s",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
var adminLockListCmd = cli.Command{
|
||||
Name: "list",
|
||||
Usage: "Get the list of locks hold in a given Minio server",
|
||||
Action: mainAdminLockList,
|
||||
Flags: append(adminLockListFlags, globalFlags...),
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc admin lock {{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
mc admin lock {{.Name}} ALIAS/BUCKET/PREFIX
|
||||
|
||||
FLAGS:
|
||||
{{range .Flags}}{{.}}
|
||||
{{end}}
|
||||
|
||||
EXAMPLES:
|
||||
1. List hold locks related to testbucket in a Minio server represented by its alias 'play'.
|
||||
$ mc admin lock {{.Name}} play/testbucket/
|
||||
|
||||
2. List locks that are hold for more than 15 minutes.
|
||||
$ mc admin lock {{.Name}} --older-than 15m play/testbucket/
|
||||
|
||||
3. List locks hold by all objects under dir prefix
|
||||
$ mc admin lock {{.Name}} play/testbucket/dir/
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// lockListMessage container to hold locks information.
|
||||
type lockListMessage struct {
|
||||
Status string `json:"status"`
|
||||
LockInfo madmin.VolumeLockInfo `json:"volumeLockInfo"`
|
||||
}
|
||||
|
||||
// String colorized service status message.
|
||||
func (u lockListMessage) String() string {
|
||||
msg := fmt.Sprintf("%s/%s (LocksOnObject: %d, locksAcquiredOnObject: %d, totalBlockLocks:%d): ",
|
||||
u.LockInfo.Bucket,
|
||||
u.LockInfo.Object,
|
||||
u.LockInfo.LocksOnObject,
|
||||
u.LockInfo.LocksAcquiredOnObject,
|
||||
u.LockInfo.TotalBlockedLocks)
|
||||
for _, detail := range u.LockInfo.LockDetailsOnObject {
|
||||
msg += fmt.Sprintf(" %+v", detail)
|
||||
}
|
||||
msg += "\n"
|
||||
return msg
|
||||
}
|
||||
|
||||
// JSON jsonified service status Message message.
|
||||
func (u lockListMessage) JSON() string {
|
||||
u.Status = "success"
|
||||
statusJSONBytes, e := json.Marshal(u)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(statusJSONBytes)
|
||||
}
|
||||
|
||||
// checkAdminLockListSyntax - validate all the passed arguments
|
||||
func checkAdminLockListSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "list", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
func mainAdminLockList(ctx *cli.Context) error {
|
||||
|
||||
setGlobalsFromContext(ctx)
|
||||
checkAdminLockListSyntax(ctx)
|
||||
|
||||
// Get the alias parameter from cli
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
|
||||
// Parse older-than flag
|
||||
olderThan, e := time.ParseDuration(ctx.String("older-than"))
|
||||
fatalIf(probe.NewError(e), "Unable to parse the passed older-than flag.")
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
|
||||
splits := splitStr(aliasedURL, "/", 3)
|
||||
|
||||
// Fetch the lock info related to a specified pair of bucket and prefix
|
||||
locksInfo, e := client.ListLocks(splits[1], splits[2], olderThan)
|
||||
fatalIf(probe.NewError(e), "Cannot get lock status.")
|
||||
|
||||
for _, l := range locksInfo {
|
||||
printMsg(lockListMessage{LockInfo: l})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Minio Client (C) 2016 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 cmd
|
||||
|
||||
import "github.com/minio/cli"
|
||||
|
||||
var (
|
||||
adminLockFlags = []cli.Flag{}
|
||||
)
|
||||
|
||||
var adminLockCmd = cli.Command{
|
||||
Name: "lock",
|
||||
Usage: "Control locks in servers.",
|
||||
Action: mainAdminLock,
|
||||
Flags: append(adminLockFlags, globalFlags...),
|
||||
Subcommands: []cli.Command{
|
||||
adminLockListCmd,
|
||||
adminLockClearCmd,
|
||||
},
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.Name}} [FLAGS] COMMAND
|
||||
|
||||
FLAGS:
|
||||
{{range .Flags}}{{.}}
|
||||
{{end}}
|
||||
COMMANDS:
|
||||
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||
{{end}}
|
||||
`,
|
||||
}
|
||||
|
||||
// mainAdminLock is the handle for "mc admin lock" command.
|
||||
func mainAdminLock(ctx *cli.Context) error {
|
||||
// Set global flags from context.
|
||||
setGlobalsFromContext(ctx)
|
||||
|
||||
if ctx.Args().First() != "" { // command help.
|
||||
cli.ShowCommandHelp(ctx, ctx.Args().First())
|
||||
} else {
|
||||
// command with Subcommands is an App.
|
||||
cli.ShowAppHelp(ctx)
|
||||
}
|
||||
|
||||
return nil
|
||||
// Sub-commands like "list", "unlock" have their own main.
|
||||
}
|
||||
@@ -29,6 +29,7 @@ var adminCmd = cli.Command{
|
||||
Flags: append(adminFlags, globalFlags...),
|
||||
Subcommands: []cli.Command{
|
||||
adminServiceCmd,
|
||||
adminLockCmd,
|
||||
},
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.Name}} - {{.Usage}}
|
||||
|
||||
+4
-20
@@ -920,19 +920,8 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) {
|
||||
path = string(c.targetURL.Separator) + bucket + c.targetURL.Path
|
||||
}
|
||||
}
|
||||
splits := strings.SplitN(path, string(c.targetURL.Separator), 3)
|
||||
switch len(splits) {
|
||||
case 0, 1:
|
||||
bucketName = ""
|
||||
objectName = ""
|
||||
case 2:
|
||||
bucketName = splits[1]
|
||||
objectName = ""
|
||||
case 3:
|
||||
bucketName = splits[1]
|
||||
objectName = splits[2]
|
||||
}
|
||||
return bucketName, objectName
|
||||
tokens := splitStr(path, string(c.targetURL.Separator), 3)
|
||||
return tokens[1], tokens[2]
|
||||
}
|
||||
|
||||
// splitPath split path into bucket and object.
|
||||
@@ -955,13 +944,8 @@ func (c *s3Client) splitPath(path string) (bucketName, objectName string) {
|
||||
}
|
||||
}
|
||||
|
||||
tokens := strings.SplitN(path, string(c.targetURL.Separator), 2)
|
||||
bucketName = tokens[0]
|
||||
if len(tokens) == 2 {
|
||||
objectName = tokens[1]
|
||||
}
|
||||
|
||||
return bucketName, objectName
|
||||
tokens := splitStr(path, string(c.targetURL.Separator), 2)
|
||||
return tokens[0], tokens[1]
|
||||
}
|
||||
|
||||
/// Bucket API operations.
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
@@ -75,3 +76,14 @@ func dumpTLSCertificates(t *tls.ConnectionState) {
|
||||
func isStdIO(reader io.Reader) bool {
|
||||
return reader == os.Stdin || reader == os.Stdout || reader == os.Stderr
|
||||
}
|
||||
|
||||
// splitStr splits a string into n parts, empty strings are added
|
||||
// if we are not able to reach n elements
|
||||
func splitStr(path, sep string, n int) []string {
|
||||
splits := strings.SplitN(path, sep, n)
|
||||
// Add empty strings if we found elements less than nr
|
||||
for i := n - len(splits); i > 0; i-- {
|
||||
splits = append(splits, "")
|
||||
}
|
||||
return splits
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user