2019-08-14 03:56:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-08-14 04:12:34 +00:00
|
|
|
package lock
|
2019-08-14 03:56:28 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-11 17:14:20 +00:00
|
|
|
"crypto/sha1"
|
2019-08-21 22:17:32 +00:00
|
|
|
"fmt"
|
2019-08-14 03:56:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-11-14 18:57:11 +00:00
|
|
|
"os/user"
|
2019-08-14 03:56:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2019-08-21 22:17:32 +00:00
|
|
|
"github.com/juju/clock"
|
|
|
|
"github.com/juju/mutex"
|
2019-08-14 03:56:28 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-11-14 18:57:11 +00:00
|
|
|
var (
|
|
|
|
// forceID is a user id for consistent testing
|
|
|
|
forceID = ""
|
|
|
|
)
|
|
|
|
|
2019-08-21 22:17:32 +00:00
|
|
|
// WriteFile decorates ioutil.WriteFile with a file lock and retry
|
|
|
|
func WriteFile(filename string, data []byte, perm os.FileMode) error {
|
2019-11-14 18:57:11 +00:00
|
|
|
spec := UserMutexSpec(filename)
|
2019-12-11 17:54:44 +00:00
|
|
|
glog.Infof("WriteFile acquiring %s: %+v", filename, spec)
|
2019-08-21 22:17:32 +00:00
|
|
|
releaser, err := mutex.Acquire(spec)
|
2019-08-14 03:56:28 +00:00
|
|
|
if err != nil {
|
2019-12-11 17:54:44 +00:00
|
|
|
return errors.Wrapf(err, "failed to acquire lock for %s: %+v", filename, spec)
|
2019-08-14 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:17:32 +00:00
|
|
|
defer releaser.Release()
|
|
|
|
|
2019-08-14 18:05:03 +00:00
|
|
|
if err = ioutil.WriteFile(filename, data, perm); err != nil {
|
2019-12-11 17:54:44 +00:00
|
|
|
return errors.Wrapf(err, "writefile failed for %s", filename)
|
2019-08-14 03:56:28 +00:00
|
|
|
}
|
2019-08-14 18:05:03 +00:00
|
|
|
return err
|
2019-08-14 03:56:28 +00:00
|
|
|
}
|
2019-08-21 22:42:57 +00:00
|
|
|
|
2019-11-14 18:57:11 +00:00
|
|
|
// UserMutexSpec returns a mutex spec that will not collide with other users
|
|
|
|
func UserMutexSpec(path string) mutex.Spec {
|
|
|
|
id := forceID
|
|
|
|
if forceID == "" {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
id = u.Uid
|
|
|
|
}
|
2019-08-21 22:42:57 +00:00
|
|
|
}
|
2019-12-11 17:14:20 +00:00
|
|
|
name := getMutexNameForPath(fmt.Sprintf("%s-%s", path, id))
|
2019-11-14 18:57:11 +00:00
|
|
|
s := mutex.Spec{
|
2019-12-11 17:14:20 +00:00
|
|
|
Name: name,
|
2019-11-14 18:57:11 +00:00
|
|
|
Clock: clock.WallClock,
|
|
|
|
// Poll the lock twice a second
|
|
|
|
Delay: 500 * time.Millisecond,
|
|
|
|
// panic after a minute instead of locking infinitely
|
|
|
|
Timeout: 60 * time.Second,
|
2019-08-21 22:42:57 +00:00
|
|
|
}
|
2019-11-14 18:57:11 +00:00
|
|
|
return s
|
|
|
|
}
|
2019-08-21 22:42:57 +00:00
|
|
|
|
2019-11-14 18:57:11 +00:00
|
|
|
func getMutexNameForPath(path string) string {
|
|
|
|
// juju requires that names match ^[a-zA-Z][a-zA-Z0-9-]*$", and be under 40 chars long.
|
2019-12-11 17:14:20 +00:00
|
|
|
name := fmt.Sprintf("mk%x", sha1.Sum([]byte(path)))
|
|
|
|
return name[0:40]
|
2019-08-21 22:42:57 +00:00
|
|
|
}
|