Skip to content

Modern Docker Management, Designed for Everyone

5.9 KB
Raw Blame
package upgrade

import (
	"database/sql/driver"
	"time"

	"github.com/getarcaneapp/arcane/backend/v2/internal/database"
)

// EnvironmentUpdateJobStatus is the lifecycle status of a fleet-wide "update all
// environments" job.
type EnvironmentUpdateJobStatus string

const (
	// EnvironmentUpdateJobStatusPendingRestart means the agents are done and the manager triggered its own
	// self-upgrade; the job is finalized once it restarts on the new version.
	EnvironmentUpdateJobStatusPendingRestart EnvironmentUpdateJobStatus = "pending_restart"
	// EnvironmentUpdateJobStatusRunning means the agents phase is in progress.
	EnvironmentUpdateJobStatusRunning EnvironmentUpdateJobStatus = "running"
	// EnvironmentUpdateJobStatusCompleted means every environment has been processed.
	EnvironmentUpdateJobStatusCompleted EnvironmentUpdateJobStatus = "completed"
	// EnvironmentUpdateJobStatusFailed means the job stopped before completing.
	EnvironmentUpdateJobStatusFailed EnvironmentUpdateJobStatus = "failed"

	// EnvironmentUpdateResultStatusPending is the initial state of an environment
	// seeded into the job but not yet being processed.
	EnvironmentUpdateResultStatusPending EnvironmentUpdateResultStatus = "pending"
	// EnvironmentUpdateResultStatusUpdating means this environment is being processed
	// right now; it drives the live per-environment progress indicator in the UI.
	EnvironmentUpdateResultStatusUpdating EnvironmentUpdateResultStatus = "updating"
	// EnvironmentUpdateResultStatusUpdated means the upgrade was triggered and the
	// new version was confirmed.
	EnvironmentUpdateResultStatusUpdated EnvironmentUpdateResultStatus = "updated"
	// EnvironmentUpdateResultStatusTriggered means the upgrade was triggered but not
	// confirmed within the wait window (still likely succeeding in the background).
	EnvironmentUpdateResultStatusTriggered EnvironmentUpdateResultStatus = "triggered"
	// EnvironmentUpdateResultStatusUpToDate means the environment already ran the
	// target image, so the pull found nothing new and no container was recreated.
	EnvironmentUpdateResultStatusUpToDate EnvironmentUpdateResultStatus = "up_to_date"
	// EnvironmentUpdateResultStatusSkippedOffline means the environment was unreachable.
	EnvironmentUpdateResultStatusSkippedOffline EnvironmentUpdateResultStatus = "skipped_offline"
	// EnvironmentUpdateResultStatusFailed means the upgrade trigger failed.
	EnvironmentUpdateResultStatusFailed EnvironmentUpdateResultStatus = "failed"
)

// EnvironmentUpdateResultStatus is the per-environment outcome within a job.
type EnvironmentUpdateResultStatus string

// EnvironmentUpdateStage is the live step an updating environment is in.
type EnvironmentUpdateStage string

const (
	EnvironmentUpdateStageChecking     EnvironmentUpdateStage = "checking"
	EnvironmentUpdateStageStarting     EnvironmentUpdateStage = "starting"
	EnvironmentUpdateStageReconnecting EnvironmentUpdateStage = "reconnecting"
	EnvironmentUpdateStageVerifying    EnvironmentUpdateStage = "verifying"
)

// EnvironmentUpdateResult is the outcome for a single environment within a job.
// The manager appears as the first entry with EnvironmentID "0".
type EnvironmentUpdateResult struct {
	EnvironmentID   string                        `json:"environmentId"`
	EnvironmentName string                        `json:"environmentName"`
	Status          EnvironmentUpdateResultStatus `json:"status"`
	FromVersion     string                        `json:"fromVersion,omitempty"`
	FromDigest      string                        `json:"fromDigest,omitempty"`
	ToVersion       string                        `json:"toVersion,omitempty"`
	Stage           EnvironmentUpdateStage        `json:"stage,omitempty"`
	StageStartedAt  *time.Time                    `json:"stageStartedAt,omitempty"`
	Error           string                        `json:"error,omitempty"`
}

// clearStage drops live-progress fields once the row leaves updating.
func (r EnvironmentUpdateResult) clearStage() EnvironmentUpdateResult {
	return EnvironmentUpdateResult{ //exhaustruct:enforce
		EnvironmentID:   r.EnvironmentID,
		EnvironmentName: r.EnvironmentName,
		Status:          r.Status,
		FromVersion:     r.FromVersion,
		FromDigest:      r.FromDigest,
		ToVersion:       r.ToVersion,
		Stage:           "",
		StageStartedAt:  nil,
		Error:           r.Error,
	}
}

// EnvironmentUpdateResults is a JSON-serialized slice of per-environment results,
// stored in a single TEXT column.
//
//nolint:recvcheck
type EnvironmentUpdateResults []EnvironmentUpdateResult

func (r EnvironmentUpdateResults) Value() (driver.Value, error) {
	return database.JSONValue(r == nil, r)
}

func (r *EnvironmentUpdateResults) Scan(value any) error {
	return database.JSONScan(r, value)
}

// EnvironmentUpdateJob is a persisted fleet-wide update record. It survives the manager's
// self-upgrade restart so the next boot can finalize the manager result.
type EnvironmentUpdateJob struct {
	database.BaseModel

	Status                EnvironmentUpdateJobStatus `json:"status" gorm:"column:status"`
	UserID                string                     `json:"userId" gorm:"column:user_id"`
	Username              string                     `json:"username" gorm:"column:username"`
	ManagerVersionAtStart string                     `json:"managerVersionAtStart" gorm:"column:manager_version_at_start"`
	ManagerDigestAtStart  string                     `json:"managerDigestAtStart" gorm:"column:manager_digest_at_start"`
	ManagerTargetVersion  string                     `json:"managerTargetVersion" gorm:"column:manager_target_version"`
	Results               EnvironmentUpdateResults   `json:"results,omitempty" gorm:"column:results;type:text"`
	Error                 *string                    `json:"error,omitempty" gorm:"column:error"`
	CompletedAt           *time.Time                 `json:"completedAt,omitempty" gorm:"column:completed_at"`
}

func (EnvironmentUpdateJob) TableName() string {
	return "environment_update_jobs"
}