Skip to content

Commit

Permalink
Fix lockfile write problems on new install (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Otterverse authored Feb 15, 2024
1 parent d583426 commit 8a553fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ upload-stable: bin/viam-agent-$(PATH_VERSION)-x86_64 bin/viam-agent-$(PATH_VERSI
test "$(PATH_VERSION)" != "custom"
gsutil -h "Cache-Control:no-cache" cp bin/viam-agent-$(PATH_VERSION)-x86_64 bin/viam-agent-$(PATH_VERSION)-aarch64 bin/viam-agent-stable-x86_64 bin/viam-agent-stable-aarch64 gs://packages.viam.com/apps/viam-agent/


.PHONY: upload-installer
upload-installer:
gsutil -h "Cache-Control:no-cache" cp install.sh uninstall.sh gs://packages.viam.com/apps/viam-agent/
3 changes: 3 additions & 0 deletions cmd/viam-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func main() {
}
}

// set up folder structure
exitIfError(agent.InitPaths())

// use a lockfile to prevent running two agents on the same machine
pidFile, err := lockfile.New(filepath.Join(agent.ViamDirs["tmp"], "viam-agent.pid"))
exitIfError(errors.Wrap(err, "cannot init lock file"))
Expand Down
35 changes: 1 addition & 34 deletions subsystems/viamagent/viamagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"os/exec"
"path/filepath"
"syscall"

"github.com/pkg/errors"
"github.com/viamrobotics/agent"
Expand Down Expand Up @@ -98,7 +97,7 @@ func GetRevision() string {

func Install(logger *zap.SugaredLogger) error {
// Create/check required folder structure exists.
if err := initPaths(); err != nil {
if err := agent.InitPaths(); err != nil {
return err
}

Expand Down Expand Up @@ -166,35 +165,3 @@ func Install(logger *zap.SugaredLogger) error {

return nil
}

func initPaths() error {
uid := os.Getuid()
for _, p := range agent.ViamDirs {
info, err := os.Stat(p)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
//nolint:gosec
if err := os.MkdirAll(p, 0o755); err != nil {
return err
}
continue
}
return err
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
// should be impossible on Linux
return errors.New("cannot convert to syscall.Stat_t")
}
if uid != int(stat.Uid) {
return errors.Errorf("%s is owned by UID %d but the current UID is %d", p, stat.Uid, uid)
}
if !info.IsDir() {
return errors.Errorf("%s should be a directory, but is not", p)
}
if info.Mode().Perm() != 0o755 {
return errors.Errorf("%s should be have permission set to 0755, but has permissions %d", p, info.Mode().Perm())
}
}
return nil
}
33 changes: 33 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path"
"path/filepath"
"strings"
"syscall"
"time"

errw "github.com/pkg/errors"
Expand All @@ -30,6 +31,38 @@ func init() {
ViamDirs["etc"] = filepath.Join(ViamDirs["viam"], "etc")
}

func InitPaths() error {
uid := os.Getuid()
for _, p := range ViamDirs {
info, err := os.Stat(p)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
//nolint:gosec
if err := os.MkdirAll(p, 0o755); err != nil {
return errw.Wrapf(err, "Error creating directory %s", p)
}
continue
}
return errw.Wrapf(err, "Error checking directory %s", p)
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
// should be impossible on Linux
return errw.New("cannot convert to syscall.Stat_t")
}
if uid != int(stat.Uid) {
return errw.Errorf("%s is owned by UID %d but the current UID is %d", p, stat.Uid, uid)
}
if !info.IsDir() {
return errw.Errorf("%s should be a directory, but is not", p)
}
if info.Mode().Perm() != 0o755 {
return errw.Errorf("%s should be have permission set to 0755, but has permissions %d", p, info.Mode().Perm())
}
}
return nil
}

// DownloadFile downloads a file into the cache directory and returns a path to the file.
func DownloadFile(ctx context.Context, rawURL string) (outPath string, errRet error) {
parsedURL, err := url.Parse(rawURL)
Expand Down

0 comments on commit 8a553fc

Please sign in to comment.