state: moved stdlib lock to upper function to fix race condition

Signed-off-by: Sam Alba <samalba@users.noreply.github.com>
This commit is contained in:
Sam Alba 2021-12-21 14:25:18 -08:00
parent fc106e9a91
commit fdb4b94276
2 changed files with 24 additions and 20 deletions

View File

@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"github.com/gofrs/flock"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/keychain"
"go.dagger.io/dagger/plancontext"
@ -31,6 +32,7 @@ const (
planDir = "plan"
manifestFile = "values.yaml"
computedFile = "computed.json"
lockFilePath = "dagger.lock"
)
type Project struct {
@ -357,12 +359,6 @@ func cueModInit(ctx context.Context, parentDir string) error {
lg := log.Ctx(ctx)
modDir := path.Join(parentDir, "cue.mod")
if err := os.Mkdir(modDir, 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
modFile := path.Join(modDir, "module.cue")
if _, err := os.Stat(modFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
@ -395,6 +391,28 @@ func VendorUniverse(ctx context.Context, p string) error {
p = getCueModParent()
}
cueModDir := path.Join(p, "cue.mod")
if err := os.Mkdir(cueModDir, 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
if err := os.MkdirAll(cueModDir, 0755); err != nil {
return err
}
lockFilePath := path.Join(cueModDir, lockFilePath)
fileLock := flock.New(lockFilePath)
if err := fileLock.Lock(); err != nil {
return err
}
defer func() {
fileLock.Unlock()
os.Remove(lockFilePath)
}()
// ensure cue module is initialized
if err := cueModInit(ctx, p); err != nil {
return err

View File

@ -8,8 +8,6 @@ import (
"os"
"path"
"path/filepath"
"github.com/gofrs/flock"
)
var (
@ -20,21 +18,9 @@ var (
ModuleName = "alpha.dagger.io"
EnginePackage = fmt.Sprintf("%s/europa/dagger/engine", ModuleName)
Path = path.Join("cue.mod", "pkg", ModuleName)
lockFilePath = path.Join("cue.mod", "dagger.lock")
)
func Vendor(ctx context.Context, mod string) error {
lockFilePath := path.Join(mod, lockFilePath)
fileLock := flock.New(lockFilePath)
if err := fileLock.Lock(); err != nil {
return err
}
defer func() {
fileLock.Unlock()
os.Remove(lockFilePath)
}()
// Remove any existing copy of the universe
if err := os.RemoveAll(path.Join(mod, Path)); err != nil {
return err