gitstacks

package
v0.2.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gitstacks deploys stacks from Git repositories (#37) with saved credentials (#93, ADR-0039). A Git stack is cloned into <data-dir>/git/<name>; its compose file is read-only and changes by pulling, while .env and other untracked files stay local.

Index

Constants

View Source
const (
	AutoOff    = "off"
	AutoNotify = "notify"
	AutoDeploy = "deploy"
)

What happens on a new commit.

View Source
const AutoUser = "nerdui (Git)"

AutoUser is the user of jobs started by polling and webhooks.

Variables

View Source
var PollIntervals = []int64{0, 60, 300, 900, 3600, 6 * 3600, 24 * 3600}

PollIntervals are the allowed polling intervals in seconds; 0 turns polling off.

Functions

func Short

func Short(hash string) string

Short abbreviates a commit hash.

func WebhookPath

func WebhookPath(token string) string

WebhookPath is the path of the webhook URL of a token.

Types

type Auto

type Auto struct {
	Service      *Service
	Environments Environments
	Jobs         JobRunner
	// Notify, when set, receives new commits (notify mode) and done updates.
	Notify func(notify.Event)
	Logger *slog.Logger
	// Base is the context of checks started by webhooks, canceled on shutdown.
	Base context.Context //nolint:containedctx // background work outlives the webhook request
	// Tick is how often polling looks for due stacks; default 30 seconds.
	Tick time.Duration
}

Auto polls the remotes of Git stacks and acts on new commits.

func (*Auto) Check

func (a *Auto) Check(ctx context.Context, name string)

Check looks for a new commit of a stack and announces or deploys it.

func (*Auto) Deploy

func (a *Auto) Deploy(ctx context.Context, name string) (jobs.Job, error)

Deploy pulls a stack as a job and redeploys it where it runs when the commit changed.

func (*Auto) Poll

func (a *Auto) Poll(ctx context.Context)

Poll checks the stacks whose polling interval elapsed.

func (*Auto) Run

func (a *Auto) Run(ctx context.Context)

Run polls due stacks until ctx ends.

func (*Auto) Trigger

func (a *Auto) Trigger(name string)

Trigger checks a stack in the background (webhooks).

type CreateRequest

type CreateRequest struct {
	Name string `json:"name"`
	URL  string `json:"url"`
	// Ref is a branch or tag; empty means the default branch.
	Ref string `json:"ref"`
	// ComposePath is the compose file or its directory, relative to the repository root;
	// empty means the root directory.
	ComposePath  string `json:"composePath"`
	CredentialID *int64 `json:"credentialId,omitempty"`
}

CreateRequest creates a Git stack. Keep in sync with api/openapi.yaml.

type CredentialInput

type CredentialInput struct {
	Name       string `json:"name"`
	Kind       string `json:"kind"`
	Username   string `json:"username"`
	Secret     string `json:"secret"`
	Passphrase string `json:"passphrase"`
	Generate   bool   `json:"generate"`
}

CredentialInput creates or replaces a credential. An empty Secret keeps the stored secret and passphrase while the kind is unchanged; Generate creates a new ed25519 SSH key. Keep in sync with api/openapi.yaml.

type CredentialView

type CredentialView struct {
	ID       int64  `json:"id"`
	Name     string `json:"name"`
	Kind     string `json:"kind"`
	Username string `json:"username,omitempty"`
	// PublicKey of an SSH key credential, to add as a deploy key.
	PublicKey  string    `json:"publicKey,omitempty"`
	Passphrase bool      `json:"passphrase"`
	Stacks     int64     `json:"stacks"`
	UpdatedAt  time.Time `json:"updatedAt"`
}

CredentialView is a credential without its secrets. Keep in sync with api/openapi.yaml.

type Environments

type Environments interface {
	List() []*environment.Environment
}

Environments lists environments; *environment.Registry implements it.

type JobRunner

type JobRunner interface {
	Start(spec jobs.Spec, fn jobs.Func) (jobs.Job, error)
}

JobRunner starts jobs; *jobs.Manager implements it.

type KnownHost

type KnownHost struct {
	Host        string    `json:"host"`
	Type        string    `json:"type"`
	Fingerprint string    `json:"fingerprint"`
	AddedAt     time.Time `json:"addedAt"`
}

KnownHost is a trusted SSH host key. Keep in sync with api/openapi.yaml.

type KnownHosts

type KnownHosts struct {
	Store Store
	Now   func() time.Time
}

KnownHosts stores trusted SSH host keys in the database.

func (KnownHosts) AddHostKey

func (k KnownHosts) AddHostKey(ctx context.Context, host, key string) error

AddHostKey trusts a key for a host.

func (KnownHosts) HostKeys

func (k KnownHosts) HostKeys(ctx context.Context, host string) ([]string, error)

HostKeys returns the keys trusted for a host.

type Service

type Service struct {
	// Dir holds the clones (<data-dir>/git); it is also stacks.Service.GitDir.
	Dir    string
	Store  Store
	Stacks *stacks.Service
	// AllowFile permits file:// URLs (tests).
	AllowFile bool
	Now       func() time.Time
	// contains filtered or unexported fields
}

Service manages Git stacks.

func (*Service) ByWebhook

func (s *Service) ByWebhook(ctx context.Context, token string) (db.GitStack, error)

ByWebhook returns the stack of a webhook token.

func (*Service) Check

func (s *Service) Check(ctx context.Context, req CreateRequest) (CreateRequest, error)

Check validates a request before cloning starts: the name is free, the URL, ref and path are valid and the credential exists and suits the URL. It returns the normalized request.

func (*Service) CheckRemote

func (s *Service) CheckRemote(ctx context.Context, name string) (before, after db.GitStack, err error)

CheckRemote asks the remote for the newest commit of the stack's ref and records it. It returns the stack before and after; after.RemoteHash is set when a newer commit exists.

func (*Service) Create

func (s *Service) Create(ctx context.Context, req CreateRequest) (stacks.Definition, error)

Create clones the repository and registers the stack. The request must have passed Check. The clone happens in a temporary directory renamed at the end: nothing is left on failure.

func (*Service) CreateCredential

func (s *Service) CreateCredential(ctx context.Context, in CredentialInput) (CredentialView, error)

CreateCredential saves a new credential.

func (*Service) Credentials

func (s *Service) Credentials(ctx context.Context) ([]CredentialView, error)

Credentials lists the credentials.

func (*Service) DeleteCredential

func (s *Service) DeleteCredential(ctx context.Context, id int64) error

DeleteCredential deletes a credential no stack uses.

func (*Service) DeleteWebhook

func (s *Service) DeleteWebhook(ctx context.Context, name string) error

DeleteWebhook removes the webhook of a stack.

func (*Service) ForgetHost

func (s *Service) ForgetHost(ctx context.Context, host string) error

ForgetHost removes the trusted keys of a host: the next connection trusts the key it sees.

func (*Service) Get

func (s *Service) Get(ctx context.Context, name string) (db.GitStack, error)

Get returns a Git stack.

func (*Service) KnownHostList

func (s *Service) KnownHostList(ctx context.Context) ([]KnownHost, error)

KnownHostList lists the trusted SSH host keys.

func (*Service) NewWebhook

func (s *Service) NewWebhook(ctx context.Context, name string) (string, error)

NewWebhook creates (or replaces) the webhook token of a stack. Only its hash is stored: the token is returned once.

func (*Service) Pull

func (s *Service) Pull(ctx context.Context, name string) (before, after gitrepo.Commit, def stacks.Definition, err error)

Pull fetches the ref and checks out its newest commit, keeping untracked files. A failure is recorded on the stack; the checked-out commit stays.

func (*Service) PullAndDeploy

func (s *Service) PullAndDeploy(ctx context.Context, out io.Writer, name string, redeploy, force bool,
	deploy func(context.Context, stacks.Definition) error,
) (gitrepo.Commit, error)

PullAndDeploy pulls a stack and, when redeploy is set and the commit changed (or always with force), calls deploy with the new definition. It writes progress to out and returns the checked-out commit.

func (*Service) Remove

func (s *Service) Remove(ctx context.Context, name string) error

Remove forgets a Git stack and deletes its clone, .env and other local files included.

func (*Service) SetAuto

func (s *Service) SetAuto(ctx context.Context, name, mode string, interval int64) error

SetAuto sets what happens on new commits and how often the remote is polled.

func (*Service) UpdateCredential

func (s *Service) UpdateCredential(ctx context.Context, id int64, in CredentialInput) (CredentialView, error)

UpdateCredential replaces a credential; stacks using it pick it up on their next pull.

type Store

type Store interface {
	ListGitStacks(ctx context.Context) ([]db.GitStack, error)
	GetGitStack(ctx context.Context, name string) (db.GitStack, error)
	CreateGitStack(ctx context.Context, arg db.CreateGitStackParams) error
	SetGitStackCommit(ctx context.Context, arg db.SetGitStackCommitParams) error
	SetGitStackError(ctx context.Context, arg db.SetGitStackErrorParams) error
	DeleteGitStack(ctx context.Context, name string) (int64, error)
	SetGitStackAuto(ctx context.Context, arg db.SetGitStackAutoParams) (int64, error)
	SetGitStackWebhook(ctx context.Context, arg db.SetGitStackWebhookParams) (int64, error)
	GetGitStackByWebhook(ctx context.Context, webhookHash string) (db.GitStack, error)
	SetGitStackChecked(ctx context.Context, arg db.SetGitStackCheckedParams) error

	ListGitCredentials(ctx context.Context) ([]db.GitCredential, error)
	GetGitCredential(ctx context.Context, id int64) (db.GitCredential, error)
	CreateGitCredential(ctx context.Context, arg db.CreateGitCredentialParams) (db.GitCredential, error)
	UpdateGitCredential(ctx context.Context, arg db.UpdateGitCredentialParams) (db.GitCredential, error)
	DeleteGitCredential(ctx context.Context, id int64) (int64, error)
	CountGitStacksByCredential(ctx context.Context, credentialID sql.NullInt64) (int64, error)

	ListGitKnownHosts(ctx context.Context) ([]db.GitKnownHost, error)
	ListGitHostKeys(ctx context.Context, host string) ([]string, error)
	AddGitHostKey(ctx context.Context, arg db.AddGitHostKeyParams) error
	DeleteGitHostKeys(ctx context.Context, host string) (int64, error)
}

Store keeps Git stacks, credentials and known hosts; *db.Queries implements it.

Jump to

Keyboard shortcuts

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