feat(store): available ref, update mode and services-by-repo for the webhook (#8)
This commit is contained in:
@@ -149,7 +149,8 @@ func (s *Store) RemoveService(ctx context.Context, agentID int64, clusterID, age
|
||||
const serviceColumns = `s.id, s.kind, s.location_key, s.agent_id, COALESCE(a.name, ''),
|
||||
s.compose_project, s.compose_service, s.working_dir, s.config_files, s.built, s.swarm_cluster_id,
|
||||
s.swarm_service, s.container_name, s.image_repo, s.desired_ref, s.actual_ref, s.actual_digest,
|
||||
s.status, s.self, s.update_mode, s.first_seen_at, s.updated_at, s.removed_at`
|
||||
s.status, s.self, s.update_mode, s.available_ref, s.available_source, s.available_at,
|
||||
s.first_seen_at, s.updated_at, s.removed_at`
|
||||
|
||||
func scanService(row pgx.Row) (model.Service, error) {
|
||||
var m model.Service
|
||||
@@ -157,7 +158,8 @@ func scanService(row pgx.Row) (model.Service, error) {
|
||||
err := row.Scan(&m.ID, &kind, &m.LocationKey, &m.AgentID, &m.AgentName,
|
||||
&m.ComposeProject, &m.ComposeService, &m.WorkingDir, &m.ConfigFiles, &m.Built, &m.SwarmClusterID,
|
||||
&m.SwarmService, &m.ContainerName, &m.ImageRepo, &m.DesiredRef, &m.ActualRef, &m.ActualDigest,
|
||||
&m.Status, &m.Self, &m.UpdateMode, &m.FirstSeenAt, &m.UpdatedAt, &m.RemovedAt)
|
||||
&m.Status, &m.Self, &m.UpdateMode, &m.AvailableRef, &m.AvailableSource, &m.AvailableAt,
|
||||
&m.FirstSeenAt, &m.UpdatedAt, &m.RemovedAt)
|
||||
m.Kind = protocol.Kind(kind)
|
||||
return m, err
|
||||
}
|
||||
@@ -165,13 +167,23 @@ func scanService(row pgx.Row) (model.Service, error) {
|
||||
// ListServices returns all services not marked removed: machine-bound
|
||||
// services grouped by agent name first, Swarm services last.
|
||||
func (s *Store) ListServices(ctx context.Context) ([]model.Service, error) {
|
||||
return s.queryServices(ctx, "list services", "", nil)
|
||||
}
|
||||
|
||||
// ServicesByRepo returns the services not marked removed whose image repo
|
||||
// equals repo (case-sensitive), in the same order as ListServices.
|
||||
func (s *Store) ServicesByRepo(ctx context.Context, repo string) ([]model.Service, error) {
|
||||
return s.queryServices(ctx, "services by repo", "AND s.image_repo = $1", []any{repo})
|
||||
}
|
||||
|
||||
func (s *Store) queryServices(ctx context.Context, what, extraWhere string, args []any) ([]model.Service, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT `+serviceColumns+`
|
||||
FROM services s LEFT JOIN agents a ON a.id = s.agent_id
|
||||
WHERE s.removed_at IS NULL
|
||||
ORDER BY s.kind = 'swarm', COALESCE(a.name, ''), s.location_key`)
|
||||
WHERE s.removed_at IS NULL `+extraWhere+`
|
||||
ORDER BY s.kind = 'swarm', COALESCE(a.name, ''), s.location_key`, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list services: %w", err)
|
||||
return nil, fmt.Errorf("%s: %w", what, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []model.Service
|
||||
@@ -185,6 +197,32 @@ func (s *Store) ListServices(ctx context.Context) ([]model.Service, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SetAvailable records a newer ref for the service, replacing any earlier
|
||||
// suggestion.
|
||||
func (s *Store) SetAvailable(ctx context.Context, id int64, a model.Available) error {
|
||||
if _, err := s.pool.Exec(ctx, `
|
||||
UPDATE services SET available_ref = $2, available_source = $3, available_at = $4
|
||||
WHERE id = $1`, id, a.Ref, a.Source, a.At); err != nil {
|
||||
return fmt.Errorf("set available: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetUpdateMode sets the service's update mode (model.ModeManual or
|
||||
// model.ModeAuto). Returns model.ErrNotFound if the service does not exist
|
||||
// or is marked removed.
|
||||
func (s *Store) SetUpdateMode(ctx context.Context, id int64, mode string) error {
|
||||
tag, err := s.pool.Exec(ctx,
|
||||
`UPDATE services SET update_mode = $2 WHERE id = $1 AND removed_at IS NULL`, id, mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set update mode: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetService returns one service, including a removed one (RemovedAt set).
|
||||
// Returns model.ErrNotFound if the ID does not exist.
|
||||
func (s *Store) GetService(ctx context.Context, id int64) (model.Service, error) {
|
||||
|
||||
Reference in New Issue
Block a user