update

package
v0.0.0-...-8acab51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OnlineURL            = "https://ota.zimaos.com"
	OnlineFallbackURL    = "https://ota2.zimaos.com"
	OnlineFallbackIP     = "http://139.224.8.35"
	OnlineFallbackHeader = "ota2.zimaos.com"
)
View Source
const (
	StateIdle        = "idle"
	StateChecking    = "checking"
	StateDownloading = "downloading"
	StateApplying    = "applying"
	StateRestarting  = "restarting"
	StateFailed      = "failed"
)

Update state constants

View Source
const (
	StatusSuccess    = "success"
	StatusFailed     = "failed"
	StatusRolledBack = "rolled_back"
)

History status constants

View Source
const (
	ChannelStable = "stable"
	ChannelBeta   = "beta"
	ChannelAlpha  = "alpha"
)

Release channel constants

Variables

This section is empty.

Functions

func GetStartTime

func GetStartTime() time.Time

GetStartTime returns the service start time (preserved across hot updates)

func GetUptime

func GetUptime() time.Duration

GetUptime returns the service uptime

func IsNewerVersionString

func IsNewerVersionString(current, target string) bool

IsNewerVersionString compares two version strings, returns true if target > current.

Types

type Applier

type Applier struct {
	// contains filtered or unexported fields
}

Applier handles applying updates

func NewApplier

func NewApplier(binaryPath, storagePath string, backupCount int) *Applier

NewApplier creates a new update applier

func (*Applier) Apply

func (a *Applier) Apply(downloadedPath string) error

Apply applies the downloaded update (backup + replace + exec). Note: on success the process is replaced and this never returns.

func (*Applier) PrepareAndReplace

func (a *Applier) PrepareAndReplace(downloadedPath string) error

PrepareAndReplace backs up the current binary and replaces it with the new one. Does NOT restart — call Restart() separately when ready. On Windows, if the binary is locked (running process), the new binary is saved as .new and the bat-script-based exec() will handle the swap after exit.

func (*Applier) Restart

func (a *Applier) Restart() error

Restart replaces the current process with the new binary. On Unix this calls syscall.Exec (never returns on success). On Windows this spawns a new process and exits.

func (*Applier) Rollback

func (a *Applier) Rollback() error

Rollback restores the previous version

func (*Applier) SetExecutor

func (a *Applier) SetExecutor(e Executor)

SetExecutor overrides the default platform executor (for testing).

type ApplyRequest

type ApplyRequest struct {
	ResumeContext      map[string]interface{} `json:"resume_context,omitempty"`
	ResumeDelaySeconds int                    `json:"resume_delay_seconds,omitempty"`
}

ApplyRequest supports optional resume context persistence before restart. Clients can send contextual payload so the new process can recover unfinished work.

type Config

type Config struct {
	Enabled        bool          `yaml:"enabled"`
	CheckInterval  time.Duration `yaml:"check_interval"`
	AutoDownload   bool          `yaml:"auto_download"`
	AutoApply      bool          `yaml:"auto_apply"`
	ReleaseChannel string        `yaml:"release_channel"`
	BackupCount    int           `yaml:"backup_count"`
	StoragePath    string        `yaml:"storage_path"`
}

Config holds update configuration

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader handles binary downloads

func NewDownloader

func NewDownloader(storagePath string) *Downloader

NewDownloader creates a new downloader

func (*Downloader) Download

func (d *Downloader) Download(ctx context.Context, url, expectedChecksum string) (string, error)

Download downloads a binary from URL and verifies checksum

func (*Downloader) Progress

func (d *Downloader) Progress() float64

Progress returns current download progress

func (*Downloader) SetProgressCallback

func (d *Downloader) SetProgressCallback(fn func(float64))

SetProgressCallback sets the progress callback

type Executor

type Executor interface {
	Exec(binaryPath string, args []string, env []string) error
}

Executor abstracts the process-replacement step so it can be mocked in tests.

type GitHubClient

type GitHubClient struct {
	// contains filtered or unexported fields
}

GitHubClient handles version discovery from GitHub

func NewGitHubClient

func NewGitHubClient() *GitHubClient

NewGitHubClient creates a new GitHub client

func (*GitHubClient) GetDownloadURL

func (c *GitHubClient) GetDownloadURL(version string) string

GetDownloadURL returns the download URL for a specific version

func (*GitHubClient) GetLatestVersion

func (c *GitHubClient) GetLatestVersion(ctx context.Context, channel string) (*Version, error)

GetLatestVersion returns the latest version for a given channel

func (*GitHubClient) GetReleaseNotes

func (c *GitHubClient) GetReleaseNotes(ctx context.Context, version string) (string, error)

GetReleaseNotes fetches release notes for a version

func (*GitHubClient) ListVersions

func (c *GitHubClient) ListVersions(ctx context.Context) ([]*Version, error)

ListVersions lists all available versions from release-note directory

type GitHubContent

type GitHubContent struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Path string `json:"path"`
}

GitHubContent represents GitHub API content response

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles update API endpoints

func NewHandler

func NewHandler(version string, cfg *Config) *Handler

NewHandler creates a new update handler

func (*Handler) Apply

func (h *Handler) Apply(c echo.Context) error

The response is sent BEFORE the process is replaced, so the client should start polling /api/v1/health to detect when the new version is up.

func (*Handler) ApplyUpdate

func (h *Handler) ApplyUpdate() error

ApplyUpdate applies the downloaded update.

func (*Handler) Check

func (h *Handler) Check(c echo.Context) error

Check checks for available updates

func (*Handler) CheckForUpdate

func (h *Handler) CheckForUpdate(ctx context.Context) (*UpdateInfo, error)

CheckForUpdate checks for available updates and returns the info.

func (*Handler) Download

func (h *Handler) Download(c echo.Context) error

Download downloads the update

func (*Handler) DownloadOTA

func (h *Handler) DownloadOTA(c echo.Context) error

DownloadOTA downloads the update from OTA package URLs (with mirror fallback). POST /api/v1/system/update/download-ota

func (*Handler) GetCurrentVersion

func (h *Handler) GetCurrentVersion() string

GetCurrentVersion returns the current version string.

func (*Handler) GetStatus

func (h *Handler) GetStatus() *UpdateStatus

GetStatus returns the current update status.

func (*Handler) History

func (h *Handler) History(c echo.Context) error

History returns update history

func (*Handler) Info

func (h *Handler) Info(c echo.Context) error

Info returns current update info

func (*Handler) LoadHistory

func (h *Handler) LoadHistory(path string) error

LoadHistory loads history from file

func (*Handler) OTAStatus

func (h *Handler) OTAStatus(c echo.Context) error

OTAStatus returns the latest OTA check result. GET /api/v1/system/update/ota?desktop=1

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(g *echo.Group)

RegisterRoutes registers update routes

func (*Handler) ReleaseNotes

func (h *Handler) ReleaseNotes(c echo.Context) error

ReleaseNotes fetches release notes content via the downloader (GitHub URL expansion + IPv4 fallback). GET /api/v1/system/update/release-notes

func (*Handler) Rollback

func (h *Handler) Rollback(c echo.Context) error

Rollback rolls back to previous version

func (*Handler) SetOTAChecker

func (h *Handler) SetOTAChecker(ota *OTAChecker)

SetOTAChecker attaches the background OTA checker to the handler.

func (*Handler) SetResumeRecoverer

func (h *Handler) SetResumeRecoverer(recoverer ResumeRecoverer)

SetResumeRecoverer sets the callback used to restore workloads after OTA restart.

func (*Handler) StartAutoUpdater

func (h *Handler) StartAutoUpdater(parent context.Context)

StartAutoUpdater starts background auto-update checks when enabled. It is a no-op unless update.enabled and one of auto_download/auto_apply is true.

func (*Handler) StartDownload

func (h *Handler) StartDownload(ctx context.Context) error

StartDownload starts downloading the update in the background.

type OTAChecker

type OTAChecker struct {
	// contains filtered or unexported fields
}

OTAChecker performs periodic update checks against the OTA server.

func NewOTAChecker

func NewOTAChecker(version, dataDir, lang string) *OTAChecker

NewOTAChecker creates a new OTA checker.

func (*OTAChecker) FetchForDesktop

func (o *OTAChecker) FetchForDesktop(ctx context.Context) (*OTAResponse, error)

FetchForDesktop performs an OTA check with the -desktop OS suffix.

func (*OTAChecker) GetLatest

func (o *OTAChecker) GetLatest() *OTAResponse

GetLatest returns the latest OTA response, if any.

func (*OTAChecker) Run

func (o *OTAChecker) Run(ctx context.Context)

Run starts the OTA check loop. Intended to be called via lifecycle manager.

func (*OTAChecker) UpdateAvailable

func (o *OTAChecker) UpdateAvailable() bool

UpdateAvailable returns true if a newer version is available.

type OTAResponse

type OTAResponse struct {
	Version        string   `json:"version"`
	Packages       []string `json:"packages"`
	ReleaseNoteURL string   `json:"release_note_url"`
	Delay          int      `json:"delay,omitempty"` // cooldown seconds before allowing update
}

OTAResponse is the JSON response from the OTA server (BlueReleaseInfo).

type ReleaseInfo

type ReleaseInfo struct {
	Version     string    `json:"version"`
	Channel     string    `json:"channel"` // stable, beta, alpha
	DownloadURL string    `json:"download_url"`
	Checksum    string    `json:"checksum"`
	Size        int64     `json:"size"`
	Notes       string    `json:"notes"`
	PublishedAt time.Time `json:"published_at"`
}

ReleaseInfo represents a release from GitHub

type ResumeRecoverInput

type ResumeRecoverInput struct {
	TaskID         string                 `json:"task_id"`
	FromVersion    string                 `json:"from_version"`
	TargetVersion  string                 `json:"target_version,omitempty"`
	CurrentVersion string                 `json:"current_version"`
	Context        map[string]interface{} `json:"context,omitempty"`
}

ResumeRecoverInput carries persisted OTA resume context into an external recoverer.

type ResumeRecoverer

type ResumeRecoverer func(ctx context.Context, input ResumeRecoverInput) error

ResumeRecoverer executes post-restart recovery using persisted resume context.

type ResumeRecovery

type ResumeRecovery struct {
	TaskID         string                 `json:"task_id"`
	Status         string                 `json:"status"` // success, failed
	FromVersion    string                 `json:"from_version"`
	TargetVersion  string                 `json:"target_version"`
	CurrentVersion string                 `json:"current_version"`
	RecoveredAt    time.Time              `json:"recovered_at"`
	Context        map[string]interface{} `json:"context,omitempty"`
	Error          string                 `json:"error,omitempty"`
}

ResumeRecovery reports the latest one-time post-restart recovery execution.

type UpdateHistory

type UpdateHistory struct {
	ID          string    `json:"id"`
	FromVersion string    `json:"from_version"`
	ToVersion   string    `json:"to_version"`
	Status      string    `json:"status"` // success, failed, rolled_back
	AppliedAt   time.Time `json:"applied_at"`
	RolledBack  bool      `json:"rolled_back"`
}

UpdateHistory represents update history entry

type UpdateInfo

type UpdateInfo struct {
	CurrentVersion  string    `json:"current_version"`
	LatestVersion   string    `json:"latest_version"`
	UpdateAvailable bool      `json:"update_available"`
	ReleaseChannel  string    `json:"release_channel"`
	ReleaseNotes    string    `json:"release_notes"`
	DownloadURL     string    `json:"download_url"`
	Checksum        string    `json:"checksum"`
	Size            int64     `json:"size"`
	PublishedAt     time.Time `json:"published_at"`
}

UpdateInfo represents available update information

type UpdateStatus

type UpdateStatus struct {
	State          string    `json:"state"` // idle, checking, downloading, applying, restarting, failed
	Progress       float64   `json:"progress"`
	Error          string    `json:"error,omitempty"`
	LastChecked    time.Time `json:"last_checked"`
	DownloadedPath string    `json:"downloaded_path,omitempty"`
}

UpdateStatus represents current update status

type Version

type Version struct {
	Major      int
	Minor      int
	Patch      int
	Prerelease string // alpha, beta, rc
	PreNum     int    // alpha1 -> 1, or bare revision like 0.4.2-1 -> 1
}

Version represents a semantic version

func ParseVersion

func ParseVersion(s string) (*Version, error)

ParseVersion parses a version string. Supports: 1.2.3, v1.2.3, 0.4.2-alpha1, 0.4.2-beta.1, 0.4.2-rc1, 0.4.2-1

func (*Version) Channel

func (v *Version) Channel() string

Channel returns the release channel based on prerelease tag

func (*Version) Compare

func (v *Version) Compare(other *Version) int

Compare compares two versions. Returns -1 if v < other, 0 if equal, 1 if v > other

func (*Version) IsNewerThan

func (v *Version) IsNewerThan(other *Version) bool

IsNewerThan returns true if v is newer than other

func (*Version) String

func (v *Version) String() string

String returns the version string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL