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
- Variables
- func Short(hash string) string
- func WebhookPath(token string) string
- type Auto
- type CreateRequest
- type CredentialInput
- type CredentialView
- type Environments
- type JobRunner
- type KnownHost
- type KnownHosts
- type Service
- func (s *Service) ByWebhook(ctx context.Context, token string) (db.GitStack, error)
- func (s *Service) Check(ctx context.Context, req CreateRequest) (CreateRequest, error)
- func (s *Service) CheckRemote(ctx context.Context, name string) (before, after db.GitStack, err error)
- func (s *Service) Clone(ctx context.Context, dir, url, ref string, credentialID *int64) (gitrepo.Commit, error)
- func (s *Service) Create(ctx context.Context, req CreateRequest) (stacks.Definition, error)
- func (s *Service) CreateCredential(ctx context.Context, in CredentialInput) (CredentialView, error)
- func (s *Service) Credentials(ctx context.Context) ([]CredentialView, error)
- func (s *Service) DeleteCredential(ctx context.Context, id int64) error
- func (s *Service) DeleteWebhook(ctx context.Context, name string) error
- func (s *Service) ForgetHost(ctx context.Context, host string) error
- func (s *Service) Get(ctx context.Context, name string) (db.GitStack, error)
- func (s *Service) KnownHostList(ctx context.Context) ([]KnownHost, error)
- func (s *Service) NewWebhook(ctx context.Context, name string) (string, error)
- func (s *Service) Pull(ctx context.Context, name string) (before, after gitrepo.Commit, def stacks.Definition, err error)
- func (s *Service) PullAndDeploy(ctx context.Context, out io.Writer, name string, redeploy, force bool, ...) (gitrepo.Commit, error)
- func (s *Service) Remove(ctx context.Context, name string) error
- func (s *Service) SetAuto(ctx context.Context, name, mode string, interval int64) error
- func (s *Service) UpdateCredential(ctx context.Context, id int64, in CredentialInput) (CredentialView, error)
- type Store
Constants ¶
const ( AutoOff = "off" AutoNotify = "notify" AutoDeploy = "deploy" )
What happens on a new commit.
const AutoUser = "nerdui (Git)"
AutoUser is the user of jobs started by polling and webhooks.
Variables ¶
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 WebhookPath ¶
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) Deploy ¶
Deploy pulls a stack as a job and redeploys it where it runs when the commit changed.
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 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 ¶
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.
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) 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) Clone ¶
func (s *Service) Clone(ctx context.Context, dir, url, ref string, credentialID *int64) (gitrepo.Commit, error)
Clone clones a repository into dir (which must not exist) for another use than a stack, an image build (#45): the same URL checks, credentials and known hosts as Git stacks.
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 ¶
DeleteCredential deletes a credential no stack uses.
func (*Service) DeleteWebhook ¶
DeleteWebhook removes the webhook of a stack.
func (*Service) ForgetHost ¶
ForgetHost removes the trusted keys of a host: the next connection trusts the key it sees.
func (*Service) KnownHostList ¶
KnownHostList lists the trusted SSH host keys.
func (*Service) NewWebhook ¶
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 ¶
Remove forgets a Git stack and deletes its clone, .env and other local files included.
func (*Service) SetAuto ¶
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.