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-05 13:07:06 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-01-30 14:29:55 +00:00
|
|
|
"fmt"
|
2022-05-05 13:07:06 +00:00
|
|
|
|
2023-04-10 00:03:49 +00:00
|
|
|
"github.com/google/uuid"
|
2022-05-05 13:07:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-03-28 10:08:19 +00:00
|
|
|
"gorm.io/datatypes"
|
2023-01-30 14:29:55 +00:00
|
|
|
"gorm.io/gorm"
|
2024-02-22 16:54:38 +01:00
|
|
|
|
|
|
|
|
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
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-05 13:07:06 +00:00
|
|
|
)
|
|
|
|
|
|
2024-02-22 17:20:05 +01:00
|
|
|
const (
|
|
|
|
|
entityTypeEnterpriseName = "enterprise_id"
|
|
|
|
|
entityTypeOrgName = "org_id"
|
|
|
|
|
entityTypeRepoName = "repo_id"
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-22 16:54:38 +01:00
|
|
|
func (s *sqlDatabase) ListAllPools(_ context.Context) ([]params.Pool, error) {
|
2022-05-05 13:07:06 +00:00
|
|
|
var pools []Pool
|
|
|
|
|
|
|
|
|
|
q := s.conn.Model(&Pool{}).
|
|
|
|
|
Preload("Tags").
|
|
|
|
|
Preload("Organization").
|
2025-05-22 18:43:32 +00:00
|
|
|
Preload("Organization.Endpoint").
|
2022-05-05 13:07:06 +00:00
|
|
|
Preload("Repository").
|
2025-05-22 18:43:32 +00:00
|
|
|
Preload("Repository.Endpoint").
|
2022-10-13 18:32:21 +00:00
|
|
|
Preload("Enterprise").
|
2025-05-22 18:43:32 +00:00
|
|
|
Preload("Enterprise.Endpoint").
|
2023-01-30 00:40:01 +00:00
|
|
|
Omit("extra_specs").
|
2022-05-05 13:07:06 +00:00
|
|
|
Find(&pools)
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
return nil, errors.Wrap(q.Error, "fetching all pools")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make([]params.Pool, len(pools))
|
2023-08-28 08:11:44 +00:00
|
|
|
var err error
|
2022-05-05 13:07:06 +00:00
|
|
|
for idx, val := range pools {
|
2023-08-28 08:11:44 +00:00
|
|
|
ret[idx], err = s.sqlToCommonPool(val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "converting pool")
|
|
|
|
|
}
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
func (s *sqlDatabase) GetPoolByID(_ context.Context, poolID string) (params.Pool, error) {
|
2025-05-22 18:43:32 +00:00
|
|
|
preloadList := []string{
|
|
|
|
|
"Tags",
|
|
|
|
|
"Instances",
|
|
|
|
|
"Enterprise",
|
|
|
|
|
"Enterprise.Endpoint",
|
|
|
|
|
"Organization",
|
|
|
|
|
"Organization.Endpoint",
|
|
|
|
|
"Repository",
|
|
|
|
|
"Repository.Endpoint",
|
|
|
|
|
}
|
|
|
|
|
pool, err := s.getPoolByID(s.conn, poolID, preloadList...)
|
2022-05-05 13:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "fetching pool by ID")
|
|
|
|
|
}
|
2023-08-28 08:11:44 +00:00
|
|
|
return s.sqlToCommonPool(pool)
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
func (s *sqlDatabase) DeletePoolByID(_ context.Context, poolID string) (err error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
pool, err := s.getPoolByID(s.conn, poolID)
|
2022-05-05 13:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching pool by ID")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 12:19:58 +00:00
|
|
|
defer func() {
|
2024-04-03 14:46:32 +00:00
|
|
|
if err == nil {
|
2024-06-19 12:19:58 +00:00
|
|
|
s.sendNotify(common.PoolEntityType, common.DeleteOperation, params.Pool{ID: poolID})
|
2024-04-03 14:46:32 +00:00
|
|
|
}
|
2024-06-19 12:19:58 +00:00
|
|
|
}()
|
2024-04-03 14:46:32 +00:00
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
if q := s.conn.Unscoped().Delete(&pool); q.Error != nil {
|
|
|
|
|
return errors.Wrap(q.Error, "removing pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-01-30 14:29:55 +00:00
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) getEntityPool(tx *gorm.DB, entityType params.ForgeEntityType, entityID, poolID string, preload ...string) (Pool, error) {
|
2023-01-30 14:29:55 +00:00
|
|
|
if entityID == "" {
|
|
|
|
|
return Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "missing entity id")
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 00:03:49 +00:00
|
|
|
u, err := uuid.Parse(poolID)
|
2023-01-30 14:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fieldName string
|
2024-03-29 18:18:29 +00:00
|
|
|
var entityField string
|
2023-01-30 14:29:55 +00:00
|
|
|
switch entityType {
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeRepository:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeRepoName
|
2025-04-27 20:28:06 +00:00
|
|
|
entityField = repositoryFieldName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeOrganization:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeOrgName
|
2025-04-27 20:28:06 +00:00
|
|
|
entityField = organizationFieldName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeEnterprise:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeEnterpriseName
|
2025-04-27 20:28:06 +00:00
|
|
|
entityField = enterpriseFieldName
|
2023-01-30 14:29:55 +00:00
|
|
|
default:
|
|
|
|
|
return Pool{}, fmt.Errorf("invalid entityType: %v", entityType)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
q := tx
|
|
|
|
|
q = q.Preload(entityField)
|
|
|
|
|
if len(preload) > 0 {
|
|
|
|
|
for _, item := range preload {
|
|
|
|
|
q = q.Preload(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 14:29:55 +00:00
|
|
|
var pool Pool
|
|
|
|
|
condition := fmt.Sprintf("id = ? and %s = ?", fieldName)
|
|
|
|
|
err = q.Model(&Pool{}).
|
|
|
|
|
Where(condition, u, entityID).
|
|
|
|
|
First(&pool).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return Pool{}, errors.Wrap(runnerErrors.ErrNotFound, "finding pool")
|
|
|
|
|
}
|
|
|
|
|
return Pool{}, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pool, nil
|
|
|
|
|
}
|
2023-04-10 00:03:49 +00:00
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) listEntityPools(tx *gorm.DB, entityType params.ForgeEntityType, entityID string, preload ...string) ([]Pool, error) {
|
2023-04-10 00:03:49 +00:00
|
|
|
if _, err := uuid.Parse(entityID); err != nil {
|
|
|
|
|
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
if err := s.hasGithubEntity(tx, entityType, entityID); err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "checking entity existence")
|
2023-04-10 00:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
var preloadEntity string
|
2023-04-10 00:03:49 +00:00
|
|
|
var fieldName string
|
|
|
|
|
switch entityType {
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeRepository:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeRepoName
|
2024-03-29 18:18:29 +00:00
|
|
|
preloadEntity = "Repository"
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeOrganization:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeOrgName
|
2024-03-29 18:18:29 +00:00
|
|
|
preloadEntity = "Organization"
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeEnterprise:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeEnterpriseName
|
2024-03-29 18:18:29 +00:00
|
|
|
preloadEntity = "Enterprise"
|
2023-04-10 00:03:49 +00:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("invalid entityType: %v", entityType)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
q := tx
|
|
|
|
|
q = q.Preload(preloadEntity)
|
|
|
|
|
if len(preload) > 0 {
|
|
|
|
|
for _, item := range preload {
|
|
|
|
|
q = q.Preload(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 00:03:49 +00:00
|
|
|
var pools []Pool
|
|
|
|
|
condition := fmt.Sprintf("%s = ?", fieldName)
|
|
|
|
|
err := q.Model(&Pool{}).
|
|
|
|
|
Where(condition, entityID).
|
|
|
|
|
Omit("extra_specs").
|
|
|
|
|
Find(&pools).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return []Pool{}, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pools, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) findPoolByTags(id string, poolType params.ForgeEntityType, tags []string) ([]params.Pool, error) {
|
2023-04-10 00:03:49 +00:00
|
|
|
if len(tags) == 0 {
|
|
|
|
|
return nil, runnerErrors.NewBadRequestError("missing tags")
|
|
|
|
|
}
|
|
|
|
|
u, err := uuid.Parse(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fieldName string
|
|
|
|
|
switch poolType {
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeRepository:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeRepoName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeOrganization:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeOrgName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeEnterprise:
|
2024-02-22 17:20:05 +01:00
|
|
|
fieldName = entityTypeEnterpriseName
|
2023-04-10 00:03:49 +00:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("invalid poolType: %v", poolType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pools []Pool
|
2024-09-27 07:50:24 +00:00
|
|
|
where := fmt.Sprintf("tags.name COLLATE NOCASE in ? and %s = ? and enabled = true", fieldName)
|
2023-04-10 00:03:49 +00:00
|
|
|
q := s.conn.Joins("JOIN pool_tags on pool_tags.pool_id=pools.id").
|
|
|
|
|
Joins("JOIN tags on tags.id=pool_tags.tag_id").
|
|
|
|
|
Group("pools.id").
|
|
|
|
|
Preload("Tags").
|
|
|
|
|
Having("count(1) = ?", len(tags)).
|
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
|
|
|
Where(where, tags, u).
|
|
|
|
|
Order("priority desc").
|
|
|
|
|
Find(&pools)
|
2023-04-10 00:03:49 +00:00
|
|
|
|
|
|
|
|
if q.Error != nil {
|
|
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return nil, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.Wrap(q.Error, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(pools) == 0 {
|
|
|
|
|
return nil, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make([]params.Pool, len(pools))
|
|
|
|
|
for idx, val := range pools {
|
2023-08-28 08:11:44 +00:00
|
|
|
ret[idx], err = s.sqlToCommonPool(val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "converting pool")
|
|
|
|
|
}
|
2023-04-10 00:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) FindPoolsMatchingAllTags(_ context.Context, entityType params.ForgeEntityType, entityID string, tags []string) ([]params.Pool, error) {
|
2023-04-10 00:03:49 +00:00
|
|
|
if len(tags) == 0 {
|
|
|
|
|
return nil, runnerErrors.NewBadRequestError("missing tags")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pools, err := s.findPoolByTags(entityID, entityType, tags)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, runnerErrors.ErrNotFound) {
|
|
|
|
|
return []params.Pool{}, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pools")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pools, nil
|
|
|
|
|
}
|
2024-03-28 10:08:19 +00:00
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) CreateEntityPool(_ context.Context, entity params.ForgeEntity, param params.CreatePoolParams) (pool params.Pool, err error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
if len(param.Tags) == 0 {
|
|
|
|
|
return params.Pool{}, runnerErrors.NewBadRequestError("no tags specified")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.sendNotify(common.PoolEntityType, common.CreateOperation, pool)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-28 10:08:19 +00:00
|
|
|
newPool := Pool{
|
|
|
|
|
ProviderName: param.ProviderName,
|
|
|
|
|
MaxRunners: param.MaxRunners,
|
|
|
|
|
MinIdleRunners: param.MinIdleRunners,
|
|
|
|
|
RunnerPrefix: param.GetRunnerPrefix(),
|
|
|
|
|
Image: param.Image,
|
|
|
|
|
Flavor: param.Flavor,
|
|
|
|
|
OSType: param.OSType,
|
|
|
|
|
OSArch: param.OSArch,
|
|
|
|
|
Enabled: param.Enabled,
|
|
|
|
|
RunnerBootstrapTimeout: param.RunnerBootstrapTimeout,
|
|
|
|
|
GitHubRunnerGroup: param.GitHubRunnerGroup,
|
|
|
|
|
Priority: param.Priority,
|
|
|
|
|
}
|
|
|
|
|
if len(param.ExtraSpecs) > 0 {
|
|
|
|
|
newPool.ExtraSpecs = datatypes.JSON(param.ExtraSpecs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entityID, err := uuid.Parse(entity.ID)
|
|
|
|
|
if err != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return params.Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch entity.EntityType {
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeRepository:
|
2024-03-28 10:08:19 +00:00
|
|
|
newPool.RepoID = &entityID
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeOrganization:
|
2024-03-28 10:08:19 +00:00
|
|
|
newPool.OrgID = &entityID
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeEnterprise:
|
2024-03-28 10:08:19 +00:00
|
|
|
newPool.EnterpriseID = &entityID
|
|
|
|
|
}
|
|
|
|
|
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
2024-03-29 18:18:29 +00:00
|
|
|
if err := s.hasGithubEntity(tx, entity.EntityType, entity.ID); err != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return errors.Wrap(err, "checking entity existence")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 10:08:19 +00:00
|
|
|
tags := []Tag{}
|
|
|
|
|
for _, val := range param.Tags {
|
|
|
|
|
t, err := s.getOrCreateTag(tx, val)
|
|
|
|
|
if err != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return errors.Wrap(err, "creating tag")
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
tags = append(tags, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q := tx.Create(&newPool)
|
|
|
|
|
if q.Error != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return errors.Wrap(q.Error, "creating pool")
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range tags {
|
|
|
|
|
if err := tx.Model(&newPool).Association("Tags").Append(&tags[i]); err != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return errors.Wrap(err, "associating tags")
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-03-29 18:18:29 +00:00
|
|
|
return params.Pool{}, err
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
dbPool, err := s.getPoolByID(s.conn, newPool.ID.String(), "Tags", "Instances", "Enterprise", "Organization", "Repository")
|
2024-03-28 10:08:19 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
return s.sqlToCommonPool(dbPool)
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) GetEntityPool(_ context.Context, entity params.ForgeEntity, poolID string) (params.Pool, error) {
|
2024-03-28 18:23:49 +00:00
|
|
|
pool, err := s.getEntityPool(s.conn, entity.EntityType, entity.ID, poolID, "Tags", "Instances")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, fmt.Errorf("fetching pool: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return s.sqlToCommonPool(pool)
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) DeleteEntityPool(_ context.Context, entity params.ForgeEntity, poolID string) (err error) {
|
2024-03-28 18:23:49 +00:00
|
|
|
entityID, err := uuid.Parse(entity.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 14:46:32 +00:00
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
pool := params.Pool{
|
|
|
|
|
ID: poolID,
|
|
|
|
|
}
|
|
|
|
|
s.sendNotify(common.PoolEntityType, common.DeleteOperation, pool)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
poolUUID, err := uuid.Parse(poolID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(runnerErrors.ErrBadRequest, "parsing pool id")
|
|
|
|
|
}
|
|
|
|
|
var fieldName string
|
|
|
|
|
switch entity.EntityType {
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeRepository:
|
2024-03-28 18:23:49 +00:00
|
|
|
fieldName = entityTypeRepoName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeOrganization:
|
2024-03-28 18:23:49 +00:00
|
|
|
fieldName = entityTypeOrgName
|
2025-05-12 21:47:13 +00:00
|
|
|
case params.ForgeEntityTypeEnterprise:
|
2024-03-28 18:23:49 +00:00
|
|
|
fieldName = entityTypeEnterpriseName
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("invalid entityType: %v", entity.EntityType)
|
|
|
|
|
}
|
|
|
|
|
condition := fmt.Sprintf("id = ? and %s = ?", fieldName)
|
|
|
|
|
if err := s.conn.Unscoped().Where(condition, poolUUID, entityID).Delete(&Pool{}).Error; err != nil {
|
|
|
|
|
return errors.Wrap(err, "removing pool")
|
|
|
|
|
}
|
2024-03-28 10:08:19 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) UpdateEntityPool(_ context.Context, entity params.ForgeEntity, poolID string, param params.UpdatePoolParams) (updatedPool params.Pool, err error) {
|
2024-06-14 20:24:45 +00:00
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.sendNotify(common.PoolEntityType, common.UpdateOperation, updatedPool)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
2024-03-29 18:18:29 +00:00
|
|
|
pool, err := s.getEntityPool(tx, entity.EntityType, entity.ID, poolID, "Tags", "Instances")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updatedPool, err = s.updatePool(tx, pool, param)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "updating pool")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, err
|
|
|
|
|
}
|
|
|
|
|
return updatedPool, nil
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) ListEntityPools(_ context.Context, entity params.ForgeEntity) ([]params.Pool, error) {
|
2024-03-29 18:18:29 +00:00
|
|
|
pools, err := s.listEntityPools(s.conn, entity.EntityType, entity.ID, "Tags")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pools")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make([]params.Pool, len(pools))
|
|
|
|
|
for idx, pool := range pools {
|
|
|
|
|
ret[idx], err = s.sqlToCommonPool(pool)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (s *sqlDatabase) ListEntityInstances(_ context.Context, entity params.ForgeEntity) ([]params.Instance, error) {
|
2024-03-29 18:18:29 +00:00
|
|
|
pools, err := s.listEntityPools(s.conn, entity.EntityType, entity.ID, "Instances", "Instances.Job")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching entity")
|
|
|
|
|
}
|
|
|
|
|
ret := []params.Instance{}
|
|
|
|
|
for _, pool := range pools {
|
2025-04-11 10:42:31 +00:00
|
|
|
instances := pool.Instances
|
|
|
|
|
pool.Instances = nil
|
|
|
|
|
for _, instance := range instances {
|
|
|
|
|
instance.Pool = pool
|
2024-03-29 18:18:29 +00:00
|
|
|
paramsInstance, err := s.sqlToParamsInstance(instance)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching instance")
|
|
|
|
|
}
|
|
|
|
|
ret = append(ret, paramsInstance)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
2024-03-28 10:08:19 +00:00
|
|
|
}
|