minikube/pkg/util/lock/lock.go

84 lines
2.3 KiB
Go
Raw Normal View History

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-08-21 22:17:32 +00:00
"fmt"
2019-08-14 03:56:28 +00:00
"io/ioutil"
"os"
2019-08-21 22:17:32 +00:00
"path/filepath"
2019-08-21 22:42:57 +00:00
"strconv"
2019-08-21 22:17:32 +00:00
"strings"
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-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 {
spec := mutex.Spec{
2019-08-21 22:42:57 +00:00
Name: getMutexName(filename),
2019-08-21 22:17:32 +00:00
Clock: clock.WallClock,
Delay: 13 * time.Second,
}
glog.Infof("attempting to write to file %q with filemode %v", filename, perm)
2019-08-14 18:05:03 +00:00
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-08-14 18:05:03 +00:00
return errors.Wrapf(err, "error acquiring lock for %s", filename)
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-08-14 03:56:28 +00:00
return errors.Wrapf(err, "error writing file %s", filename)
}
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
func getMutexName(filename string) string {
// Make the mutex name the file name and its parent directory
dir, name := filepath.Split(filename)
// Replace underscores and periods with dashes, the only valid punctuation for mutex name
name = strings.ReplaceAll(name, ".", "-")
name = strings.ReplaceAll(name, "_", "-")
2019-08-22 01:55:35 +00:00
p := strings.ReplaceAll(filepath.Base(dir), ".", "-")
p = strings.ReplaceAll(p, "_", "-")
mutexName := fmt.Sprintf("%s-%s", p, strings.ReplaceAll(name, ".", "-"))
2019-08-21 22:42:57 +00:00
// Check if name starts with an int and prepend a string instead
if _, err := strconv.Atoi(mutexName[:1]); err == nil {
mutexName = "m" + mutexName
}
// There's an arbitrary hard max on mutex name at 40.
if len(mutexName) > 40 {
mutexName = mutexName[:40]
}
2019-08-22 19:45:04 +00:00
// Make sure name doesn't start or end with punctuation
mutexName = strings.TrimPrefix(mutexName, "-")
mutexName = strings.TrimSuffix(mutexName, "-")
2019-08-21 22:42:57 +00:00
return mutexName
}