Skip to content

Commit

Permalink
feat: Updater cleans up temporary directory (#596)
Browse files Browse the repository at this point in the history
* remove tmpdir on rollback or update success

* remove temp directory in failure scenarios

* comment why we use a noop logger for failure

* move installer creation to where it's actually used

* fix redundant calls to removeTmpDir
  • Loading branch information
BinaryFissionGames authored and Corbin Phelps committed Aug 1, 2022
1 parent 6796ef8 commit a0ea027
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
48 changes: 33 additions & 15 deletions updater/cmd/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/observiq/observiq-otel-collector/packagestate"
Expand Down Expand Up @@ -48,43 +49,43 @@ func main() {
// to create the logger. So we pass a Nop logger here.
installDir, err := path.InstallDir(zap.NewNop())
if err != nil {
// Can't use "fail" here since we don't know the install directory
log.Fatalf("Failed to determine install directory: %s", err)
}

logger, err := logging.NewLogger(installDir)
if err != nil {
log.Fatalf("Failed to create logger: %s", err)
log.Default().Printf("Failed to create logger: %s\n", err)
// No logger, can't really log if we fail to remove tmpdir
fail(zap.NewNop(), installDir)
}

// Create a monitor and load the package status file
monitor, err := state.NewCollectorMonitor(logger, installDir)
if err != nil {
logger.Fatal("Failed to create monitor", zap.Error(err))
}

installer, err := install.NewInstaller(logger, installDir)
if err != nil {
logger.Fatal("Failed to create installer", zap.Error(err))
}

rb, err := rollback.NewRollbacker(logger, installDir)
if err != nil {
logger.Fatal("Failed to create rollbacker", zap.Error(err))
logger.Error("Failed to create monitor", zap.Error(err))
fail(logger, installDir)
}

rb := rollback.NewRollbacker(logger, installDir)
if err := rb.Backup(); err != nil {
logger.Fatal("Failed to backup", zap.Error(err))
logger.Error("Failed to backup", zap.Error(err))
fail(logger, installDir)
}

installer := install.NewInstaller(logger, installDir)
if err := installer.Install(rb); err != nil {
logger.Error("Failed to install", zap.Error(err))

// Set the state to failed before rollback so collector knows it failed
if setErr := monitor.SetState(packagestate.CollectorPackageName, protobufs.PackageStatus_InstallFailed, err); setErr != nil {
logger.Error("Failed to set state on install failure", zap.Error(setErr))
}

rb.Rollback()
logger.Fatal("Rollback complete")

logger.Error("Rollback complete")
fail(logger, installDir)
}

// Create a context with timeout to wait for a success or failed status
Expand All @@ -106,9 +107,26 @@ func main() {
}

rb.Rollback()
logger.Fatal("Rollback complete")

logger.Error("Rollback complete")
fail(logger, installDir)
}

removeTmpDir(logger, installDir)
// Successful update
logger.Info("Update Complete")
}

// fail removes the temporary directory, and calls os.Exit(1)
func fail(logger *zap.Logger, installDir string) {
removeTmpDir(logger, installDir)
os.Exit(1)
}

// removeTmpDir removes the temporary directory and any files in it.
func removeTmpDir(logger *zap.Logger, installDir string) {
err := os.RemoveAll(path.TempDir(installDir))
if err != nil {
logger.Error("failed to remove temporary directory", zap.Error(err))
}
}
5 changes: 2 additions & 3 deletions updater/internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ type Installer struct {
}

// NewInstaller returns a new instance of an Installer.
func NewInstaller(logger *zap.Logger, installDir string) (*Installer, error) {
func NewInstaller(logger *zap.Logger, installDir string) *Installer {
namedLogger := logger.Named("installer")

return &Installer{
latestDir: path.LatestDir(installDir),
svc: service.NewService(namedLogger, installDir),
installDir: installDir,
logger: namedLogger,
}, nil
}
}

// Install installs the unpacked artifacts in latestDir to installDir,
Expand Down
9 changes: 2 additions & 7 deletions updater/internal/rollback/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,15 @@ type Rollbacker struct {
}

// NewRollbacker returns a new Rollbacker
func NewRollbacker(logger *zap.Logger, installDir string) (*Rollbacker, error) {
func NewRollbacker(logger *zap.Logger, installDir string) *Rollbacker {
namedLogger := logger.Named("rollbacker")

installDir, err := path.InstallDir(namedLogger.Named("install-dir"))
if err != nil {
return nil, fmt.Errorf("failed to determine install dir: %w", err)
}

return &Rollbacker{
backupDir: path.BackupDir(installDir),
installDir: installDir,
logger: namedLogger,
originalSvc: service.NewService(namedLogger, installDir),
}, nil
}
}

// AppendAction records the action that was performed, so that it may be undone later.
Expand Down

0 comments on commit a0ea027

Please sign in to comment.