From c5e919dc86f84c9eefa308e9fc3660df7c22a75a Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 12 Jul 2024 16:13:44 +0200 Subject: [PATCH] Remove runtime dir locking code --- go/cmd/entrypoint/admin.go | 10 ++- go/cmd/entrypoint/bootstrap_util.go | 26 ------- go/cmd/entrypoint/daemon.go | 6 -- go/cmd/entrypoint/garage.go | 10 ++- go/cmd/entrypoint/hosts.go | 5 +- go/cmd/entrypoint/proc_lock.go | 103 ---------------------------- go/cmd/entrypoint/sub_cmd.go | 14 +--- 7 files changed, 13 insertions(+), 161 deletions(-) delete mode 100644 go/cmd/entrypoint/bootstrap_util.go delete mode 100644 go/cmd/entrypoint/proc_lock.go diff --git a/go/cmd/entrypoint/admin.go b/go/cmd/entrypoint/admin.go index 4342b3a..a1112b0 100644 --- a/go/cmd/entrypoint/admin.go +++ b/go/cmd/entrypoint/admin.go @@ -42,9 +42,8 @@ func readAdmin(path string) (admin.Admin, error) { } var subCmdAdminCreateBootstrap = subCmd{ - name: "create-bootstrap", - descr: "Creates a new bootstrap.json file for a particular host and writes it to stdout", - checkLock: false, + name: "create-bootstrap", + descr: "Creates a new bootstrap.json file for a particular host and writes it to stdout", do: func(subCmdCtx subCmdCtx) error { var ( flags = subCmdCtx.flagSet(false) @@ -113,9 +112,8 @@ var subCmdAdminCreateBootstrap = subCmd{ } var subCmdAdminCreateNebulaCert = subCmd{ - name: "create-nebula-cert", - descr: "Creates a signed nebula certificate file and writes it to stdout", - checkLock: false, + name: "create-nebula-cert", + descr: "Creates a signed nebula certificate file and writes it to stdout", do: func(subCmdCtx subCmdCtx) error { var ( flags = subCmdCtx.flagSet(false) diff --git a/go/cmd/entrypoint/bootstrap_util.go b/go/cmd/entrypoint/bootstrap_util.go deleted file mode 100644 index 178504b..0000000 --- a/go/cmd/entrypoint/bootstrap_util.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "io/fs" - "isle/bootstrap" -) - -func loadHostBootstrap() (bootstrap.Bootstrap, error) { - - stateDirPath := bootstrap.StateDirPath(daemonEnvVars.StateDirPath) - - hostBootstrap, err := bootstrap.FromFile(stateDirPath) - if errors.Is(err, fs.ErrNotExist) { - return bootstrap.Bootstrap{}, fmt.Errorf( - "%q not found, has the daemon ever been run?", - stateDirPath, - ) - - } else if err != nil { - return bootstrap.Bootstrap{}, fmt.Errorf("loading %q: %w", stateDirPath, err) - } - - return hostBootstrap, nil -} diff --git a/go/cmd/entrypoint/daemon.go b/go/cmd/entrypoint/daemon.go index 409df71..f924fec 100644 --- a/go/cmd/entrypoint/daemon.go +++ b/go/cmd/entrypoint/daemon.go @@ -49,12 +49,6 @@ var subCmdDaemon = subCmd{ logger := subCmdCtx.logger.WithMaxLevel(logLevel.Int()) - runtimeDirCleanup, err := setupAndLockRuntimeDir(ctx, logger) - if err != nil { - return fmt.Errorf("setting up runtime directory: %w", err) - } - defer runtimeDirCleanup() - daemonConfig, err := daemon.LoadConfig(envAppDirPath, *daemonConfigPath) if err != nil { return fmt.Errorf("loading daemon config: %w", err) diff --git a/go/cmd/entrypoint/garage.go b/go/cmd/entrypoint/garage.go index 2ba2a29..093e21b 100644 --- a/go/cmd/entrypoint/garage.go +++ b/go/cmd/entrypoint/garage.go @@ -32,9 +32,8 @@ func initMCConfigDir() (string, error) { } var subCmdGarageMC = subCmd{ - name: "mc", - descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias", - checkLock: true, + name: "mc", + descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias", do: func(subCmdCtx subCmdCtx) error { flags := subCmdCtx.flagSet(true) @@ -116,9 +115,8 @@ var subCmdGarageMC = subCmd{ } var subCmdGarageCLI = subCmd{ - name: "cli", - descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon", - checkLock: true, + name: "cli", + descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon", do: func(subCmdCtx subCmdCtx) error { var clientParams bootstrap.GarageClientParams diff --git a/go/cmd/entrypoint/hosts.go b/go/cmd/entrypoint/hosts.go index 2f84143..1c105e2 100644 --- a/go/cmd/entrypoint/hosts.go +++ b/go/cmd/entrypoint/hosts.go @@ -46,9 +46,8 @@ var subCmdHostsList = subCmd{ } var subCmdHostsDelete = subCmd{ - name: "delete", - descr: "Deletes a host from the network", - checkLock: true, + name: "delete", + descr: "Deletes a host from the network", do: func(subCmdCtx subCmdCtx) error { flags := subCmdCtx.flagSet(false) diff --git a/go/cmd/entrypoint/proc_lock.go b/go/cmd/entrypoint/proc_lock.go deleted file mode 100644 index 32bfbfd..0000000 --- a/go/cmd/entrypoint/proc_lock.go +++ /dev/null @@ -1,103 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "io/fs" - "os" - "path/filepath" - - "dev.mediocregopher.com/mediocre-go-lib.git/mctx" - "dev.mediocregopher.com/mediocre-go-lib.git/mlog" - "github.com/shirou/gopsutil/process" -) - -var errDaemonNotRunning = errors.New("no isle daemon process running") - -func lockFilePath() string { - return filepath.Join(daemonEnvVars.RuntimeDirPath, "lock") -} - -func writeLock() error { - - lockFilePath := lockFilePath() - - lockFile, err := os.OpenFile( - lockFilePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400, - ) - - if errors.Is(err, os.ErrExist) { - return fmt.Errorf( - "lock file %q already exists, if the isle daemon is not already running you can safely delete this file", - lockFilePath, - ) - - } else if err != nil { - return fmt.Errorf("opening lockfile %q: %w", lockFilePath, err) - } - - defer lockFile.Close() - - if _, err := fmt.Fprintf(lockFile, "%d\n", os.Getpid()); err != nil { - return fmt.Errorf("writing pid to %q: %w", lockFilePath, err) - } - - return nil -} - -// returns a cleanup function which will clean up the created runtime directory. -func setupAndLockRuntimeDir(ctx context.Context, logger *mlog.Logger) (func(), error) { - - ctx = mctx.Annotate(ctx, "runtimeDirPath", daemonEnvVars.RuntimeDirPath) - logger.Info(ctx, "will use runtime directory for temporary state") - - if err := os.MkdirAll(daemonEnvVars.RuntimeDirPath, 0700); err != nil { - return nil, fmt.Errorf("creating directory %q: %w", daemonEnvVars.RuntimeDirPath, err) - - } else if err := writeLock(); err != nil { - return nil, err - } - - return func() { - logger.Info(ctx, "cleaning up runtime directory") - if err := os.RemoveAll(daemonEnvVars.RuntimeDirPath); err != nil { - logger.Error(ctx, "removing temporary directory", err) - } - }, nil -} - -// checks that the lock file exists and that the process which created it also -// still exists. -func assertLock() error { - - lockFilePath := lockFilePath() - - lockFile, err := os.Open(lockFilePath) - - if errors.Is(err, fs.ErrNotExist) { - return errDaemonNotRunning - - } else if err != nil { - return fmt.Errorf("checking lock file %q: %w", lockFilePath, err) - } - - defer lockFile.Close() - - var pid int32 - - if _, err := fmt.Fscan(lockFile, &pid); err != nil { - return fmt.Errorf("scanning pid from lock file %q: %w", lockFilePath, err) - } - - procExists, err := process.PidExists(pid) - - if err != nil { - return fmt.Errorf("checking if process %d exists: %w", pid, err) - - } else if !procExists { - return errDaemonNotRunning - } - - return nil -} diff --git a/go/cmd/entrypoint/sub_cmd.go b/go/cmd/entrypoint/sub_cmd.go index c31dfb5..0af7b76 100644 --- a/go/cmd/entrypoint/sub_cmd.go +++ b/go/cmd/entrypoint/sub_cmd.go @@ -24,10 +24,9 @@ type subCmdCtx struct { } type subCmd struct { - name string - descr string - checkLock bool - do func(subCmdCtx) error + name string + descr string + do func(subCmdCtx) error } func (ctx subCmdCtx) usagePrefix() string { @@ -103,13 +102,6 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error { printUsageExit(subCmdName) } - if subCmd.checkLock { - - if err := assertLock(); err != nil { - return fmt.Errorf("checking lock file: %w", err) - } - } - daemonRCPClient := jsonrpc2.NewUnixHTTPClient( daemon.HTTPSocketPath(), daemonHTTPRPCPath, )