2016-08-31 14:39:08 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (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 (
|
2016-10-20 16:15:28 -04:00
|
|
|
"errors"
|
2016-08-31 14:39:08 -04:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2016-10-20 16:15:28 -04:00
|
|
|
"strings"
|
2016-08-31 14:39:08 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/cli"
|
2016-10-11 03:50:27 -04:00
|
|
|
"github.com/minio/mc/pkg/console"
|
2016-08-31 14:39:08 -04:00
|
|
|
)
|
|
|
|
|
2016-10-11 03:50:27 -04:00
|
|
|
var lockFlags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "older-than",
|
2016-10-20 16:15:28 -04:00
|
|
|
Usage: "Include locks older than given time.",
|
2016-10-11 03:50:27 -04:00
|
|
|
Value: "24h",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Usage: "Lists more information about locks.",
|
|
|
|
},
|
2016-10-20 16:15:28 -04:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "recursive",
|
|
|
|
Usage: "Recursively clear locks.",
|
|
|
|
},
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var lockCmd = cli.Command{
|
|
|
|
Name: "lock",
|
2016-10-11 03:50:27 -04:00
|
|
|
Usage: "Prints current lock information.",
|
2016-08-31 14:39:08 -04:00
|
|
|
Action: lockControl,
|
2016-10-11 03:50:27 -04:00
|
|
|
Flags: append(lockFlags, globalFlags...),
|
2016-08-31 14:39:08 -04:00
|
|
|
CustomHelpTemplate: `NAME:
|
|
|
|
minio control {{.Name}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
2016-10-11 03:50:27 -04:00
|
|
|
minio control {{.Name}} [list|clear] http://localhost:9000/
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2016-09-01 18:12:49 -04:00
|
|
|
FLAGS:
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
2016-08-31 14:39:08 -04:00
|
|
|
EAMPLES:
|
2016-10-11 03:50:27 -04:00
|
|
|
1. List all currently active locks from all nodes. Defaults to list locks held longer than 24hrs.
|
|
|
|
$ minio control {{.Name}} list http://localhost:9000/
|
|
|
|
|
2016-10-20 16:15:28 -04:00
|
|
|
2. List all currently active locks from all nodes. Request locks older than 1minute.
|
2016-10-11 03:50:27 -04:00
|
|
|
$ minio control {{.Name}} --older-than=1m list http://localhost:9000/
|
2016-10-20 16:15:28 -04:00
|
|
|
|
|
|
|
3. Clear lock named 'bucket/object' (exact match).
|
|
|
|
$ minio control {{.Name}} clear http://localhost:9000/bucket/object
|
|
|
|
|
|
|
|
4. Clear all locks with names that start with 'bucket/prefix' (wildcard match).
|
|
|
|
$ minio control {{.Name}} --recursive clear http://localhost:9000/bucket/prefix
|
|
|
|
|
|
|
|
5. Clear all locks older than 10minutes.
|
|
|
|
$ minio control {{.Name}} --older-than=10m clear http://localhost:9000/
|
|
|
|
|
|
|
|
6. Clear all locks with names that start with 'bucket/a' and that are older than 1hour.
|
|
|
|
$ minio control {{.Name}} --recursive --older-than=1h clear http://localhost:9000/bucket/a
|
2016-08-31 14:39:08 -04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2016-10-11 03:50:27 -04:00
|
|
|
// printLockStateVerbose - pretty prints systemLockState, additionally this filters out based on a given duration.
|
|
|
|
func printLockStateVerbose(lkStateRep map[string]SystemLockState, olderThan time.Duration) {
|
|
|
|
console.Println("Duration Server LockType LockAcquired Status LockOrigin Resource")
|
|
|
|
for server, lockState := range lkStateRep {
|
|
|
|
for _, lockInfo := range lockState.LocksInfoPerObject {
|
|
|
|
lockedResource := path.Join(lockInfo.Bucket, lockInfo.Object)
|
|
|
|
for _, lockDetails := range lockInfo.LockDetailsOnObject {
|
|
|
|
if lockDetails.Duration < olderThan {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
console.Println(lockDetails.Duration, server,
|
|
|
|
lockDetails.LockType, lockDetails.Since,
|
|
|
|
lockDetails.Status, lockDetails.LockOrigin,
|
|
|
|
lockedResource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// printLockState - pretty prints systemLockState, additionally this filters out based on a given duration.
|
|
|
|
func printLockState(lkStateRep map[string]SystemLockState, olderThan time.Duration) {
|
|
|
|
console.Println("Duration Server LockType Resource")
|
|
|
|
for server, lockState := range lkStateRep {
|
|
|
|
for _, lockInfo := range lockState.LocksInfoPerObject {
|
|
|
|
lockedResource := path.Join(lockInfo.Bucket, lockInfo.Object)
|
|
|
|
for _, lockDetails := range lockInfo.LockDetailsOnObject {
|
|
|
|
if lockDetails.Duration < olderThan {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
console.Println(lockDetails.Duration, server,
|
|
|
|
lockDetails.LockType, lockedResource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:15:28 -04:00
|
|
|
// clearLockState - clear locks based on a filter for a given duration and a name or prefix to match
|
|
|
|
func clearLockState(f func(bucket, object string), lkStateRep map[string]SystemLockState, olderThan time.Duration, match string, recursive bool) {
|
|
|
|
console.Println("Status Duration Server LockType Resource")
|
|
|
|
for server, lockState := range lkStateRep {
|
|
|
|
for _, lockInfo := range lockState.LocksInfoPerObject {
|
|
|
|
lockedResource := path.Join(lockInfo.Bucket, lockInfo.Object)
|
|
|
|
for _, lockDetails := range lockInfo.LockDetailsOnObject {
|
|
|
|
if lockDetails.Duration < olderThan {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if match != "" {
|
|
|
|
if recursive {
|
|
|
|
if !strings.HasPrefix(lockedResource, match) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if lockedResource != match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(lockInfo.Bucket, lockInfo.Object)
|
|
|
|
console.Println("CLEARED", lockDetails.Duration, server,
|
|
|
|
lockDetails.LockType, lockedResource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 14:39:08 -04:00
|
|
|
// "minio control lock" entry point.
|
|
|
|
func lockControl(c *cli.Context) {
|
2016-10-11 03:50:27 -04:00
|
|
|
if !c.Args().Present() && len(c.Args()) != 2 {
|
2016-08-31 14:39:08 -04:00
|
|
|
cli.ShowCommandHelpAndExit(c, "lock", 1)
|
|
|
|
}
|
|
|
|
|
2016-10-11 03:50:27 -04:00
|
|
|
parsedURL, err := url.Parse(c.Args().Get(1))
|
2016-08-31 14:39:08 -04:00
|
|
|
fatalIf(err, "Unable to parse URL.")
|
|
|
|
|
2016-10-11 03:50:27 -04:00
|
|
|
// Parse older than string.
|
|
|
|
olderThanStr := c.String("older-than")
|
|
|
|
olderThan, err := time.ParseDuration(olderThanStr)
|
|
|
|
fatalIf(err, "Unable to parse older-than time duration.")
|
|
|
|
|
|
|
|
// Verbose flag.
|
|
|
|
verbose := c.Bool("verbose")
|
|
|
|
|
2016-10-20 16:15:28 -04:00
|
|
|
// Recursive flag.
|
|
|
|
recursive := c.Bool("recursive")
|
|
|
|
|
2016-08-31 14:39:08 -04:00
|
|
|
authCfg := &authConfig{
|
|
|
|
accessKey: serverConfig.GetCredential().AccessKeyID,
|
|
|
|
secretKey: serverConfig.GetCredential().SecretAccessKey,
|
2016-09-30 02:42:37 -04:00
|
|
|
secureConn: parsedURL.Scheme == "https",
|
2016-08-31 14:39:08 -04:00
|
|
|
address: parsedURL.Host,
|
|
|
|
path: path.Join(reservedBucket, controlPath),
|
2016-10-11 03:50:27 -04:00
|
|
|
loginMethod: "Control.LoginHandler",
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
client := newAuthClient(authCfg)
|
|
|
|
|
2016-10-11 03:50:27 -04:00
|
|
|
args := &GenericArgs{
|
|
|
|
// This is necessary so that the remotes,
|
|
|
|
// don't end up sending requests back and forth.
|
|
|
|
Remote: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
subCommand := c.Args().Get(0)
|
|
|
|
switch subCommand {
|
|
|
|
case "list":
|
|
|
|
lkStateRep := make(map[string]SystemLockState)
|
|
|
|
// Request lock info, fetches from all the nodes in the cluster.
|
|
|
|
err = client.Call("Control.LockInfo", args, &lkStateRep)
|
|
|
|
fatalIf(err, "Unable to fetch system lockInfo.")
|
|
|
|
if !verbose {
|
|
|
|
printLockState(lkStateRep, olderThan)
|
|
|
|
} else {
|
|
|
|
printLockStateVerbose(lkStateRep, olderThan)
|
|
|
|
}
|
|
|
|
case "clear":
|
2016-10-20 16:15:28 -04:00
|
|
|
path := parsedURL.Path
|
|
|
|
if strings.HasPrefix(path, "/") {
|
|
|
|
path = path[1:] // Strip leading slash
|
|
|
|
}
|
|
|
|
if path == "" && c.NumFlags() == 0 {
|
|
|
|
fatalIf(errors.New("Bad arguments"), "Need to either pass a path or older-than argument")
|
|
|
|
}
|
|
|
|
if !c.IsSet("older-than") { // If not set explicitly, change default to 0 instead of 24h
|
|
|
|
olderThan = 0
|
|
|
|
}
|
|
|
|
lkStateRep := make(map[string]SystemLockState)
|
|
|
|
// Request lock info, fetches from all the nodes in the cluster.
|
|
|
|
err = client.Call("Control.LockInfo", args, &lkStateRep)
|
|
|
|
fatalIf(err, "Unable to fetch system lockInfo.")
|
|
|
|
|
|
|
|
// Helper function to call server for actual removal of lock
|
|
|
|
f := func(bucket, object string) {
|
|
|
|
args := LockClearArgs{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
reply := GenericReply{}
|
|
|
|
// Call server to clear the lock based on the name of the object.
|
|
|
|
err := client.Call("Control.LockClear", &args, &reply)
|
|
|
|
fatalIf(err, "Unable to clear lock.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all locks and determine whether to clear or not.
|
|
|
|
clearLockState(f, lkStateRep, olderThan, path, recursive)
|
2016-10-11 03:50:27 -04:00
|
|
|
default:
|
|
|
|
fatalIf(errInvalidArgument, "Unsupported lock control operation %s", c.Args().Get(0))
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|