2022-05-05 13:25:50 +00:00
|
|
|
// Copyright 2022 Cloudbase Solutions SRL
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
|
// a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
2022-05-04 13:15:27 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2024-06-18 17:45:48 +00:00
|
|
|
"log/slog"
|
2023-01-23 17:43:32 +01:00
|
|
|
|
2023-04-10 00:03:49 +00:00
|
|
|
"github.com/google/uuid"
|
2022-05-04 13:15:27 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"gorm.io/gorm"
|
2024-02-22 16:54:38 +01:00
|
|
|
|
|
|
|
|
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
|
|
|
|
"github.com/cloudbase/garm-provider-common/util"
|
2024-04-03 14:46:32 +00:00
|
|
|
"github.com/cloudbase/garm/database/common"
|
2024-02-22 16:54:38 +01:00
|
|
|
"github.com/cloudbase/garm/params"
|
2022-05-04 13:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
2025-05-14 00:34:54 +00:00
|
|
|
func (s *sqlDatabase) CreateRepository(ctx context.Context, owner, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (param params.Repository, err error) {
|
2024-04-03 14:46:32 +00:00
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.sendNotify(common.RepositoryEntityType, common.CreateOperation, param)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2023-01-23 17:43:32 +01:00
|
|
|
if webhookSecret == "" {
|
|
|
|
|
return params.Repository{}, errors.New("creating repo: missing secret")
|
|
|
|
|
}
|
2023-08-19 14:39:16 +00:00
|
|
|
secret, err := util.Seal([]byte(webhookSecret), []byte(s.cfg.Passphrase))
|
2023-01-23 17:43:32 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, fmt.Errorf("failed to encrypt string")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
newRepo := Repository{
|
|
|
|
|
Name: name,
|
|
|
|
|
Owner: owner,
|
|
|
|
|
WebhookSecret: secret,
|
|
|
|
|
PoolBalancerType: poolBalancerType,
|
|
|
|
|
}
|
2024-04-15 08:32:19 +00:00
|
|
|
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
2025-05-14 00:34:54 +00:00
|
|
|
switch credentials.ForgeType {
|
|
|
|
|
case params.GithubEndpointType:
|
|
|
|
|
newRepo.CredentialsID = &credentials.ID
|
|
|
|
|
case params.GiteaEndpointType:
|
|
|
|
|
newRepo.GiteaCredentialsID = &credentials.ID
|
|
|
|
|
default:
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrBadRequest, "unsupported credentials type")
|
2024-04-17 12:10:00 +00:00
|
|
|
}
|
2024-04-15 08:32:19 +00:00
|
|
|
|
2025-05-14 00:34:54 +00:00
|
|
|
newRepo.EndpointName = &credentials.Endpoint.Name
|
2024-04-15 08:32:19 +00:00
|
|
|
q := tx.Create(&newRepo)
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
return errors.Wrap(q.Error, "creating repository")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "creating repository")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-22 18:43:32 +00:00
|
|
|
ret, err := s.GetRepositoryByID(ctx, newRepo.ID.String())
|
2025-05-14 00:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "creating repository")
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-22 18:43:32 +00:00
|
|
|
return ret, nil
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
func (s *sqlDatabase) GetRepository(ctx context.Context, owner, name, endpointName string) (params.Repository, error) {
|
|
|
|
|
repo, err := s.getRepo(ctx, owner, name, endpointName)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
param, err := s.sqlToCommonRepository(repo, true)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2023-01-23 17:43:32 +01:00
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return param, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 22:37:18 +00:00
|
|
|
func (s *sqlDatabase) ListRepositories(_ context.Context, filter params.RepositoryFilter) ([]params.Repository, error) {
|
2022-05-04 13:15:27 +00:00
|
|
|
var repos []Repository
|
2024-04-17 12:45:42 +00:00
|
|
|
q := s.conn.
|
|
|
|
|
Preload("Credentials").
|
2025-05-14 00:34:54 +00:00
|
|
|
Preload("GiteaCredentials").
|
2024-04-17 12:45:42 +00:00
|
|
|
Preload("Credentials.Endpoint").
|
2025-05-14 00:34:54 +00:00
|
|
|
Preload("GiteaCredentials.Endpoint").
|
2025-06-17 22:37:18 +00:00
|
|
|
Preload("Endpoint")
|
|
|
|
|
if filter.Owner != "" {
|
|
|
|
|
q = q.Where("owner = ?", filter.Owner)
|
|
|
|
|
}
|
|
|
|
|
if filter.Name != "" {
|
|
|
|
|
q = q.Where("name = ?", filter.Name)
|
|
|
|
|
}
|
|
|
|
|
if filter.Endpoint != "" {
|
|
|
|
|
q = q.Where("endpoint_name = ?", filter.Endpoint)
|
|
|
|
|
}
|
|
|
|
|
q = q.Find(&repos)
|
2022-05-04 13:15:27 +00:00
|
|
|
if q.Error != nil {
|
|
|
|
|
return []params.Repository{}, errors.Wrap(q.Error, "fetching user from database")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make([]params.Repository, len(repos))
|
|
|
|
|
for idx, val := range repos {
|
2023-01-23 17:43:32 +01:00
|
|
|
var err error
|
2024-04-16 17:05:18 +00:00
|
|
|
ret[idx], err = s.sqlToCommonRepository(val, true)
|
2023-01-23 17:43:32 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching repositories")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err error) {
|
2025-05-14 00:34:54 +00:00
|
|
|
repo, err := s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
defer func(repo Repository) {
|
|
|
|
|
if err == nil {
|
|
|
|
|
asParam, innerErr := s.sqlToCommonRepository(repo, true)
|
|
|
|
|
if innerErr == nil {
|
|
|
|
|
s.sendNotify(common.RepositoryEntityType, common.DeleteOperation, asParam)
|
2024-06-18 17:45:48 +00:00
|
|
|
} else {
|
|
|
|
|
slog.With(slog.Any("error", innerErr)).ErrorContext(ctx, "error sending delete notification", "repo", repoID)
|
2024-04-03 14:46:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}(repo)
|
|
|
|
|
|
2022-05-04 21:57:08 +00:00
|
|
|
q := s.conn.Unscoped().Delete(&repo)
|
2022-05-04 13:15:27 +00:00
|
|
|
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return errors.Wrap(q.Error, "deleting repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (newParams params.Repository, err error) {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.sendNotify(common.RepositoryEntityType, common.UpdateOperation, newParams)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2024-04-16 17:05:18 +00:00
|
|
|
var repo Repository
|
|
|
|
|
var creds GithubCredentials
|
2024-04-03 14:46:32 +00:00
|
|
|
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
2024-04-16 17:05:18 +00:00
|
|
|
var err error
|
2024-04-17 12:10:00 +00:00
|
|
|
repo, err = s.getRepoByID(ctx, tx, repoID)
|
2024-04-16 17:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
2024-04-17 12:10:00 +00:00
|
|
|
if repo.EndpointName == nil {
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrUnprocessable, "repository has no endpoint")
|
|
|
|
|
}
|
2022-05-04 13:15:27 +00:00
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
if param.CredentialsName != "" {
|
|
|
|
|
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching credentials")
|
|
|
|
|
}
|
2024-04-17 12:10:00 +00:00
|
|
|
if creds.EndpointName == nil {
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *creds.EndpointName != *repo.EndpointName {
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrBadRequest, "endpoint mismatch")
|
|
|
|
|
}
|
2024-04-16 17:05:18 +00:00
|
|
|
repo.CredentialsID = &creds.ID
|
|
|
|
|
}
|
2022-05-04 13:15:27 +00:00
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
if param.WebhookSecret != "" {
|
|
|
|
|
secret, err := util.Seal([]byte(param.WebhookSecret), []byte(s.cfg.Passphrase))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("saving repo: failed to encrypt string: %w", err)
|
|
|
|
|
}
|
|
|
|
|
repo.WebhookSecret = secret
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
if param.PoolBalancerType != "" {
|
|
|
|
|
repo.PoolBalancerType = param.PoolBalancerType
|
|
|
|
|
}
|
Add pool balancing strategy
This change adds the ability to specify the pool balancing strategy to
use when processing queued jobs. Before this change, GARM would round-robin
through all pools that matched the set of tags requested by queued jobs.
When round-robin (default) is used for an entity (repo, org or enterprise)
and you have 2 pools defined for that entity with a common set of tags that
match 10 jobs (for example), then those jobs would trigger the creation of
a new runner in each of the two pools in turn. Job 1 would go to pool 1,
job 2 would go to pool 2, job 3 to pool 1, job 4 to pool 2 and so on.
When "stack" is used, those same 10 jobs would trigger the creation of a
new runner in the pool with the highest priority, every time.
In both cases, if a pool is full, the next one would be tried automatically.
For the stack case, this would mean that if pool 2 had a priority of 10 and
pool 1 would have a priority of 5, pool 2 would be saturated first, then
pool 1.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-03-14 20:04:34 +00:00
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
q := tx.Save(&repo)
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
return errors.Wrap(q.Error, "saving repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "saving repo")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-14 00:34:54 +00:00
|
|
|
repo, err = s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
2024-04-17 12:10:00 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "updating enterprise")
|
|
|
|
|
}
|
2024-04-03 14:46:32 +00:00
|
|
|
|
|
|
|
|
newParams, err = s.sqlToCommonRepository(repo, true)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2023-01-23 17:43:32 +01:00
|
|
|
return params.Repository{}, errors.Wrap(err, "saving repo")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return newParams, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sqlDatabase) GetRepositoryByID(ctx context.Context, repoID string) (params.Repository, error) {
|
2025-05-22 18:43:32 +00:00
|
|
|
preloadList := []string{
|
|
|
|
|
"Pools",
|
|
|
|
|
"Credentials",
|
|
|
|
|
"Endpoint",
|
|
|
|
|
"Credentials.Endpoint",
|
|
|
|
|
"GiteaCredentials",
|
|
|
|
|
"GiteaCredentials.Endpoint",
|
|
|
|
|
"Events",
|
|
|
|
|
}
|
|
|
|
|
repo, err := s.getRepoByID(ctx, s.conn, repoID, preloadList...)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
param, err := s.sqlToCommonRepository(repo, true)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2023-01-23 17:43:32 +01:00
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return param, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
func (s *sqlDatabase) getRepo(_ context.Context, owner, name, endpointName string) (Repository, error) {
|
2022-05-04 13:15:27 +00:00
|
|
|
var repo Repository
|
|
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
q := s.conn.Where("name = ? COLLATE NOCASE and owner = ? COLLATE NOCASE and endpoint_name = ? COLLATE NOCASE", name, owner, endpointName).
|
2024-04-15 08:32:19 +00:00
|
|
|
Preload("Credentials").
|
2024-04-17 08:05:06 +00:00
|
|
|
Preload("Credentials.Endpoint").
|
2025-05-14 00:34:54 +00:00
|
|
|
Preload("GiteaCredentials").
|
|
|
|
|
Preload("GiteaCredentials.Endpoint").
|
2024-04-15 08:32:19 +00:00
|
|
|
Preload("Endpoint").
|
2022-05-04 13:15:27 +00:00
|
|
|
First(&repo)
|
|
|
|
|
|
|
|
|
|
q = q.First(&repo)
|
|
|
|
|
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return Repository{}, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
return Repository{}, errors.Wrap(q.Error, "fetching repository from database")
|
|
|
|
|
}
|
|
|
|
|
return repo, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
func (s *sqlDatabase) getRepoByID(_ context.Context, tx *gorm.DB, id string, preload ...string) (Repository, error) {
|
2023-04-10 00:03:49 +00:00
|
|
|
u, err := uuid.Parse(id)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return Repository{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
|
|
|
|
}
|
|
|
|
|
var repo Repository
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
q := tx
|
2022-05-04 13:15:27 +00:00
|
|
|
if len(preload) > 0 {
|
|
|
|
|
for _, field := range preload {
|
|
|
|
|
q = q.Preload(field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
q = q.Where("id = ?", u).First(&repo)
|
|
|
|
|
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return Repository{}, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
return Repository{}, errors.Wrap(q.Error, "fetching repository from database")
|
|
|
|
|
}
|
|
|
|
|
return repo, nil
|
|
|
|
|
}
|