Skip to content

Commit

Permalink
feat: add lockfile (#528)
Browse files Browse the repository at this point in the history
* feat: add lockfile

* fix: wrong cobra hook
  • Loading branch information
Al-Pragliola authored Jul 31, 2024
1 parent b715795 commit 9dc5171
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -20,6 +22,7 @@ import (
"github.com/sighupio/furyctl/internal/cluster"
"github.com/sighupio/furyctl/internal/config"
"github.com/sighupio/furyctl/internal/git"
"github.com/sighupio/furyctl/internal/lockfile"
cobrax "github.com/sighupio/furyctl/internal/x/cobra"
execx "github.com/sighupio/furyctl/internal/x/exec"
"github.com/sighupio/furyctl/pkg/dependencies"
Expand Down Expand Up @@ -171,6 +174,42 @@ func NewApplyCmd() *cobra.Command {
DryRun: flags.DryRun,
})

lockFileHandler := lockfile.NewLockFile(res.MinimalConf.Metadata.Name)
sigs := make(chan os.Signal, 1)

go func() {
<-sigs

if lockFileHandler != nil {
logrus.Debugf("Removing lock file %s", lockFileHandler.Path)

if err := lockFileHandler.Remove(); err != nil {
logrus.Errorf("error while removing lock file: %v", err)
}
}

os.Exit(1) //nolint:revive // ignore error
}()

signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

err = lockFileHandler.Verify()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while verifying lock file: %w", err)
}

err = lockFileHandler.Create()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while creating lock file: %w", err)
}
defer lockFileHandler.Remove() //nolint:errcheck // ignore error

basePath := filepath.Join(outDir, ".furyctl", res.MinimalConf.Metadata.Name)

// Init second half of collaborators.
Expand Down
39 changes: 39 additions & 0 deletions cmd/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -20,6 +22,7 @@ import (
"github.com/sighupio/furyctl/internal/cluster"
"github.com/sighupio/furyctl/internal/config"
"github.com/sighupio/furyctl/internal/git"
"github.com/sighupio/furyctl/internal/lockfile"
cobrax "github.com/sighupio/furyctl/internal/x/cobra"
execx "github.com/sighupio/furyctl/internal/x/exec"
iox "github.com/sighupio/furyctl/internal/x/io"
Expand Down Expand Up @@ -158,6 +161,42 @@ func NewClusterCmd() *cobra.Command {
DryRun: flags.DryRun,
})

lockFileHandler := lockfile.NewLockFile(res.MinimalConf.Metadata.Name)
sigs := make(chan os.Signal, 1)

go func() {
<-sigs

if lockFileHandler != nil {
logrus.Debugf("Removing lock file %s", lockFileHandler.Path)

if err := lockFileHandler.Remove(); err != nil {
logrus.Errorf("error while removing lock file: %v", err)
}
}

os.Exit(1) //nolint:revive // ignore exit code
}()

signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

err = lockFileHandler.Verify()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while verifying lock file: %w", err)
}

err = lockFileHandler.Create()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while creating lock file: %w", err)
}
defer lockFileHandler.Remove() //nolint:errcheck // ignore error

basePath := filepath.Join(outDir, ".furyctl", res.MinimalConf.Metadata.Name)

// Init second half of collaborators.
Expand Down
57 changes: 57 additions & 0 deletions internal/lockfile/lockfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2017-present SIGHUP s.r.l All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package lockfile

import (
"errors"
"fmt"
"os"
"path/filepath"
)

var ErrLockFileExists = errors.New("lock file exists")

type LockFile struct {
Path string
}

func NewLockFile(clusterName string) *LockFile {
fileName := "furyctl-" + clusterName

path := filepath.Join(os.TempDir(), fileName)

return &LockFile{Path: path}
}

func (l *LockFile) Verify() error {
_, err := os.Stat(l.Path)
if err == nil {
return ErrLockFileExists
}

if !os.IsNotExist(err) {
return fmt.Errorf("error while checking lock file: %w", err)
}

return nil
}

func (l *LockFile) Create() error {
_, err := os.Create(l.Path)
if err != nil {
return fmt.Errorf("error while creating lock file: %w", err)
}

return nil
}

func (l *LockFile) Remove() error {
err := os.Remove(l.Path)
if err != nil {
return fmt.Errorf("error while removing lock file: %w", err)
}

return nil
}

0 comments on commit 9dc5171

Please sign in to comment.