From 152a1b792e337f925ca75c83dcebd5bdfae9c687 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Thu, 5 Jan 2023 16:16:19 -0500 Subject: [PATCH] orbit: Always update orbit symlink when changing channels (#9188) --- .../changes/bug-9053-update-symlink-on-change | 2 ++ orbit/pkg/update/runner.go | 34 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 orbit/changes/bug-9053-update-symlink-on-change diff --git a/orbit/changes/bug-9053-update-symlink-on-change b/orbit/changes/bug-9053-update-symlink-on-change new file mode 100644 index 0000000000..4af36519d4 --- /dev/null +++ b/orbit/changes/bug-9053-update-symlink-on-change @@ -0,0 +1,2 @@ +* Orbit now restarts and switches channels when needed, +even if the new channel is already installed \ No newline at end of file diff --git a/orbit/pkg/update/runner.go b/orbit/pkg/update/runner.go index 2855d5efad..e07464df64 100644 --- a/orbit/pkg/update/runner.go +++ b/orbit/pkg/update/runner.go @@ -138,9 +138,20 @@ func (r *Runner) UpdateAction() (bool, error) { if err != nil { return didUpdate, fmt.Errorf("select hash for cache: %w", err) } + + // Check if we need to update the orbit symlink (e.g. if channel changed) + needsSymlinkUpdate := false + if target == "orbit" { + var err error + needsSymlinkUpdate, err = r.needsOrbitSymlinkUpdate() + if err != nil { + return false, fmt.Errorf("check symlink failed: %w", err) + } + } + // Check whether the hash of the repository is different than // that of the target local file. - if !bytes.Equal(r.localHashes[target], metaHash) { + if !bytes.Equal(r.localHashes[target], metaHash) || needsSymlinkUpdate { // Update detected log.Info().Str("target", target).Msg("update detected") if err := r.updateTarget(target); err != nil { @@ -156,6 +167,27 @@ func (r *Runner) UpdateAction() (bool, error) { return didUpdate, nil } +func (r *Runner) needsOrbitSymlinkUpdate() (bool, error) { + localTarget, err := r.updater.Get("orbit") + if err != nil { + return false, fmt.Errorf("get binary: %w", err) + } + path := localTarget.ExecPath + + // Symlink Orbit binary + linkPath := filepath.Join(r.updater.opt.RootDirectory, "bin", "orbit", filepath.Base(path)) + + existingPath, err := os.Readlink(linkPath) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return true, nil + } + return false, fmt.Errorf("read existing symlink: %w", err) + } + + return existingPath != path, nil +} + func (r *Runner) updateTarget(target string) error { localTarget, err := r.updater.Get(target) if err != nil {