The /update command went silent between "starting" and one final message, gave no version summary, and required manual intervention on failure. CommandInstaller now streams the installer script's stdout live instead of buffering it, and parses a structured result (previous/installed versions, per-component status) from a sentinel line. Because the reference install script hands the daemon restart off to a detached systemd unit specifically so restarting archied.service doesn't kill the process that requested the update, the outcome is reported in two phases: the synchronous build/install result comes back through Install() itself, and the post-restart health check/rollback outcome is written by a new watchdog script and relayed by whichever archied process boots next, via a small JSON handoff file cleared once relayed. Also fixes three defects an adversarial review pass found in the first draft: the caller's request/handler context was passed straight into the install child process, so a concurrent /restart or a browser disconnect could SIGKILL it mid binary-copy; Telegram and the web dashboard each tracked "install in progress" with their own private flag despite sharing one Service, allowing two installs to race the same .prev backups; and the watchdog reported rolled_back:true even when no backup existed to restore, which would have told an operator to stand down while the daemon was still broken. internal/webui gains an unauthenticated /healthz the watchdog polls after restarting archied.
77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package releaseupdate
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// Report is the phase-2 outcome of an update: whether the daemon came back
|
|
// up healthy after restarting, written by whichever process determined that
|
|
// (the deployment's restart/health-check tooling) and read, relayed to
|
|
// Channel/ChatID/ThreadID, and cleared by whichever archied process boots
|
|
// next -- which may be the newly installed version (success) or the
|
|
// previous one, if a crash-loop forced a rollback before this code could
|
|
// ever run on the new version.
|
|
type Report struct {
|
|
Channel string `json:"channel"`
|
|
ChatID int64 `json:"chat_id"`
|
|
ThreadID int `json:"thread_id"`
|
|
Previous map[string]string `json:"previous"`
|
|
Installed map[string]string `json:"installed"`
|
|
HealthCheck string `json:"health_check"` // "passed" or "failed"
|
|
RolledBack bool `json:"rolled_back"`
|
|
}
|
|
|
|
func (r Report) validate() error {
|
|
if r.RolledBack && r.HealthCheck != "failed" {
|
|
return errors.New("update report: rolled_back requires health_check to be \"failed\"")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WritePendingReport persists r to path atomically, the same way
|
|
// saveDeferrals does.
|
|
func WritePendingReport(path string, r Report) error {
|
|
if err := r.validate(); err != nil {
|
|
return err
|
|
}
|
|
data, err := json.Marshal(r)
|
|
if err != nil {
|
|
return fmt.Errorf("encode update report: %w", err)
|
|
}
|
|
if err := writeFileAtomic(path, data); err != nil {
|
|
return fmt.Errorf("write update report: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ReadPendingReport reads a Report previously written by WritePendingReport.
|
|
// found is false, with a nil error, when no update is in flight -- the
|
|
// common case on every normal startup.
|
|
func ReadPendingReport(path string) (Report, bool, error) {
|
|
data, err := os.ReadFile(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return Report{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return Report{}, false, fmt.Errorf("read update report: %w", err)
|
|
}
|
|
var report Report
|
|
if err := json.Unmarshal(data, &report); err != nil {
|
|
return Report{}, false, fmt.Errorf("decode update report: %w", err)
|
|
}
|
|
return report, true, nil
|
|
}
|
|
|
|
// ClearPendingReport removes a report once it has been relayed. A report
|
|
// that is already gone (cleared by a previous boot attempt) is not an
|
|
// error.
|
|
func ClearPendingReport(path string) error {
|
|
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return fmt.Errorf("clear update report: %w", err)
|
|
}
|
|
return nil
|
|
}
|