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-04-29 16:08:31 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-10-20 17:22:47 +03:00
|
|
|
"fmt"
|
2024-01-05 23:32:16 +00:00
|
|
|
"log/slog"
|
2022-05-05 13:25:50 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2024-02-22 16:54:38 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
2023-07-22 22:26:47 +00:00
|
|
|
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
2023-03-12 16:01:49 +02:00
|
|
|
"github.com/cloudbase/garm/auth"
|
|
|
|
|
"github.com/cloudbase/garm/params"
|
|
|
|
|
"github.com/cloudbase/garm/runner/common"
|
2023-03-14 14:15:10 +02:00
|
|
|
"github.com/cloudbase/garm/util/appdefaults"
|
2022-04-29 16:08:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (r *Runner) CreateRepository(ctx context.Context, param params.CreateRepoParams) (repo params.Repository, err error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return repo, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := param.Validate(); err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "validating params")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 17:05:18 +00:00
|
|
|
creds, err := r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true)
|
|
|
|
|
if err != nil {
|
2022-04-29 16:08:31 +00:00
|
|
|
return params.Repository{}, runnerErrors.NewBadRequestError("credentials %s not defined", param.CredentialsName)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
_, err = r.store.GetRepository(ctx, param.Owner, param.Name, creds.Endpoint.Name)
|
2022-04-29 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
if !errors.Is(err, runnerErrors.ErrNotFound) {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return params.Repository{}, runnerErrors.NewConflictError("repository %s/%s already exists", param.Owner, param.Name)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
repo, err = r.store.CreateRepository(ctx, param.Owner, param.Name, creds.Name, param.WebhookSecret, param.PoolBalancerType)
|
2022-04-29 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "creating repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
2023-01-20 22:19:54 +02:00
|
|
|
if deleteErr := r.store.DeleteRepository(ctx, repo.ID); deleteErr != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", deleteErr)).ErrorContext(
|
|
|
|
|
ctx, "failed to delete repository",
|
|
|
|
|
"repository_id", repo.ID)
|
2023-01-20 22:19:54 +02:00
|
|
|
}
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-04-24 12:23:45 +00:00
|
|
|
// Use the admin context in the pool manager. Any access control is already done above when
|
|
|
|
|
// updating the store.
|
2022-08-10 12:15:12 +03:00
|
|
|
poolMgr, err := r.poolManagerCtrl.CreateRepoPoolManager(r.ctx, repo, r.providers, r.store)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "creating repo pool manager")
|
|
|
|
|
}
|
2022-04-29 16:08:31 +00:00
|
|
|
if err := poolMgr.Start(); err != nil {
|
2022-08-10 12:15:12 +03:00
|
|
|
if deleteErr := r.poolManagerCtrl.DeleteRepoPoolManager(repo); deleteErr != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", deleteErr)).ErrorContext(
|
|
|
|
|
ctx, "failed to cleanup pool manager for repo",
|
|
|
|
|
"repository_id", repo.ID)
|
2022-08-10 12:15:12 +03:00
|
|
|
}
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "starting repo pool manager")
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
return repo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) ListRepositories(ctx context.Context) ([]params.Repository, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return nil, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repos, err := r.store.ListRepositories(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "listing repositories")
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 17:22:47 +03:00
|
|
|
var allRepos []params.Repository
|
|
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
|
poolMgr, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
repo.PoolManagerStatus.IsRunning = false
|
|
|
|
|
repo.PoolManagerStatus.FailureReason = fmt.Sprintf("failed to get pool manager: %q", err)
|
|
|
|
|
} else {
|
|
|
|
|
repo.PoolManagerStatus = poolMgr.Status()
|
|
|
|
|
}
|
|
|
|
|
allRepos = append(allRepos, repo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allRepos, nil
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) GetRepositoryByID(ctx context.Context, repoID string) (params.Repository, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Repository{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repo, err := r.store.GetRepositoryByID(ctx, repoID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "fetching repository")
|
|
|
|
|
}
|
2022-10-20 17:22:47 +03:00
|
|
|
|
|
|
|
|
poolMgr, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
repo.PoolManagerStatus.IsRunning = false
|
|
|
|
|
repo.PoolManagerStatus.FailureReason = fmt.Sprintf("failed to get pool manager: %q", err)
|
|
|
|
|
}
|
|
|
|
|
repo.PoolManagerStatus = poolMgr.Status()
|
2022-04-29 16:08:31 +00:00
|
|
|
return repo, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 09:46:04 +00:00
|
|
|
func (r *Runner) DeleteRepository(ctx context.Context, repoID string, keepWebhook bool) error {
|
2022-04-29 16:08:31 +00:00
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repo, err := r.store.GetRepositoryByID(ctx, repoID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
entity, err := repo.GetEntity()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "getting entity")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pools, err := r.store.ListEntityPools(ctx, entity)
|
2022-04-29 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo pools")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(pools) > 0 {
|
2024-02-22 17:28:39 +01:00
|
|
|
poolIDs := []string{}
|
2022-04-29 16:08:31 +00:00
|
|
|
for _, pool := range pools {
|
2024-02-22 17:28:39 +01:00
|
|
|
poolIDs = append(poolIDs, pool.ID)
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 17:28:39 +01:00
|
|
|
return runnerErrors.NewBadRequestError("repo has pools defined (%s)", strings.Join(poolIDs, ", "))
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 10:42:31 +00:00
|
|
|
scaleSets, err := r.store.ListEntityScaleSets(ctx, entity)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo scale sets")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(scaleSets) > 0 {
|
|
|
|
|
return runnerErrors.NewBadRequestError("repo has scale sets defined; delete them first")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 10:48:25 +00:00
|
|
|
if !keepWebhook && r.config.Default.EnableWebhookManagement {
|
2023-08-16 09:46:04 +00:00
|
|
|
poolMgr, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching pool manager")
|
|
|
|
|
}
|
2023-08-16 09:39:18 +00:00
|
|
|
|
2023-08-16 09:46:04 +00:00
|
|
|
if err := poolMgr.UninstallWebhook(ctx); err != nil {
|
2024-02-22 17:33:19 +01:00
|
|
|
// nolint:golangci-lint,godox
|
2023-08-16 09:46:04 +00:00
|
|
|
// TODO(gabriel-samfira): Should we error out here?
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(
|
|
|
|
|
ctx, "failed to uninstall webhook",
|
|
|
|
|
"pool_manager_id", poolMgr.ID())
|
2023-08-16 09:46:04 +00:00
|
|
|
}
|
2023-08-16 09:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-10 12:15:12 +03:00
|
|
|
if err := r.poolManagerCtrl.DeleteRepoPoolManager(repo); err != nil {
|
|
|
|
|
return errors.Wrap(err, "deleting repo pool manager")
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 21:57:08 +00:00
|
|
|
if err := r.store.DeleteRepository(ctx, repoID); err != nil {
|
2022-04-29 16:08:31 +00:00
|
|
|
return errors.Wrap(err, "removing repository")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 23:47:55 +00:00
|
|
|
func (r *Runner) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (params.Repository, error) {
|
2022-04-29 16:08:31 +00:00
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Repository{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.mux.Lock()
|
|
|
|
|
defer r.mux.Unlock()
|
|
|
|
|
|
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
|
|
|
switch param.PoolBalancerType {
|
2024-03-15 07:26:04 +00:00
|
|
|
case params.PoolBalancerTypeRoundRobin, params.PoolBalancerTypePack, params.PoolBalancerTypeNone:
|
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
|
|
|
default:
|
|
|
|
|
return params.Repository{}, runnerErrors.NewBadRequestError("invalid pool balancer type: %s", param.PoolBalancerType)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 15:28:56 +00:00
|
|
|
slog.InfoContext(ctx, "updating repository", "repo_id", repoID, "param", param)
|
2024-04-16 17:05:18 +00:00
|
|
|
repo, err := r.store.UpdateRepository(ctx, repoID, param)
|
2022-04-29 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Repository{}, errors.Wrap(err, "updating repo")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 15:28:56 +00:00
|
|
|
poolMgr, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
2022-08-10 12:15:12 +03:00
|
|
|
if err != nil {
|
2024-09-28 19:13:05 +00:00
|
|
|
return params.Repository{}, errors.Wrap(err, "getting pool manager")
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-04 22:47:09 +00:00
|
|
|
repo.PoolManagerStatus = poolMgr.Status()
|
2022-04-29 16:08:31 +00:00
|
|
|
return repo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) CreateRepoPool(ctx context.Context, repoID string, param params.CreatePoolParams) (params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 16:27:24 +00:00
|
|
|
createPoolParams, err := r.appendTagsToCreatePoolParams(param)
|
2022-04-29 23:43:37 +00:00
|
|
|
if err != nil {
|
2024-09-28 19:13:05 +00:00
|
|
|
return params.Pool{}, errors.Wrap(err, "appending tags to create pool params")
|
2022-04-29 23:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-30 06:55:13 +00:00
|
|
|
if createPoolParams.RunnerBootstrapTimeout == 0 {
|
2023-03-14 14:15:10 +02:00
|
|
|
createPoolParams.RunnerBootstrapTimeout = appdefaults.DefaultRunnerBootstrapTimeout
|
2022-06-29 23:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 10:08:19 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, err := r.store.CreateEntityPool(ctx, entity, createPoolParams)
|
2022-04-29 16:08:31 +00:00
|
|
|
if err != nil {
|
2024-09-28 19:13:05 +00:00
|
|
|
return params.Pool{}, errors.Wrap(err, "creating pool")
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
return pool, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) GetRepoPoolByID(ctx context.Context, repoID, poolID string) (params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Pool{}, runnerErrors.ErrUnauthorized
|
2022-04-29 23:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, err := r.store.GetEntityPool(ctx, entity, poolID)
|
2022-05-02 17:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
2024-03-28 18:23:49 +00:00
|
|
|
|
2022-04-29 16:08:31 +00:00
|
|
|
return pool, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
func (r *Runner) DeleteRepoPool(ctx context.Context, repoID, poolID string) error {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
2022-04-29 16:08:31 +00:00
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
2024-03-28 18:23:49 +00:00
|
|
|
pool, err := r.store.GetEntityPool(ctx, entity, poolID)
|
2022-05-02 17:55:29 +00:00
|
|
|
if err != nil {
|
2024-03-28 18:23:49 +00:00
|
|
|
return errors.Wrap(err, "fetching pool")
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 17:33:19 +01:00
|
|
|
// nolint:golangci-lint,godox
|
2022-05-02 17:55:29 +00:00
|
|
|
// TODO: implement a count function
|
2024-03-28 18:23:49 +00:00
|
|
|
if len(pool.Instances) > 0 {
|
2022-05-02 17:55:29 +00:00
|
|
|
runnerIDs := []string{}
|
2024-03-28 18:23:49 +00:00
|
|
|
for _, run := range pool.Instances {
|
2022-05-02 17:55:29 +00:00
|
|
|
runnerIDs = append(runnerIDs, run.ID)
|
|
|
|
|
}
|
|
|
|
|
return runnerErrors.NewBadRequestError("pool has runners: %s", strings.Join(runnerIDs, ", "))
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
if err := r.store.DeleteEntityPool(ctx, entity, poolID); err != nil {
|
2022-05-03 12:40:59 +00:00
|
|
|
return errors.Wrap(err, "deleting pool")
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
2022-04-29 16:08:31 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
func (r *Runner) ListRepoPools(ctx context.Context, repoID string) ([]params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return []params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
2024-03-29 18:18:29 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
|
|
|
|
}
|
|
|
|
|
pools, err := r.store.ListEntityPools(ctx, entity)
|
2022-05-02 17:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pools")
|
|
|
|
|
}
|
|
|
|
|
return pools, nil
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 19:49:14 +00:00
|
|
|
func (r *Runner) ListPoolInstances(ctx context.Context, poolID string) ([]params.Instance, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return nil, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
instances, err := r.store.ListPoolInstances(ctx, poolID)
|
2022-05-03 19:49:14 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return []params.Instance{}, errors.Wrap(err, "fetching instances")
|
|
|
|
|
}
|
|
|
|
|
return instances, nil
|
2022-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
func (r *Runner) UpdateRepoPool(ctx context.Context, repoID, poolID string, param params.UpdatePoolParams) (params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
|
|
|
|
}
|
|
|
|
|
pool, err := r.store.GetEntityPool(ctx, entity, poolID)
|
2022-05-02 17:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxRunners := pool.MaxRunners
|
|
|
|
|
minIdleRunners := pool.MinIdleRunners
|
|
|
|
|
|
|
|
|
|
if param.MaxRunners != nil {
|
|
|
|
|
maxRunners = *param.MaxRunners
|
|
|
|
|
}
|
|
|
|
|
if param.MinIdleRunners != nil {
|
|
|
|
|
minIdleRunners = *param.MinIdleRunners
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:40:59 +00:00
|
|
|
if minIdleRunners > maxRunners {
|
2022-05-02 17:55:29 +00:00
|
|
|
return params.Pool{}, runnerErrors.NewBadRequestError("min_idle_runners cannot be larger than max_runners")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:18:29 +00:00
|
|
|
newPool, err := r.store.UpdateEntityPool(ctx, entity, poolID, param)
|
2022-05-02 17:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "updating pool")
|
|
|
|
|
}
|
|
|
|
|
return newPool, nil
|
|
|
|
|
}
|
2022-05-03 19:49:14 +00:00
|
|
|
|
|
|
|
|
func (r *Runner) ListRepoInstances(ctx context.Context, repoID string) ([]params.Instance, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return nil, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
2024-03-29 18:18:29 +00:00
|
|
|
entity := params.GithubEntity{
|
|
|
|
|
ID: repoID,
|
|
|
|
|
EntityType: params.GithubEntityTypeRepository,
|
|
|
|
|
}
|
|
|
|
|
instances, err := r.store.ListEntityInstances(ctx, entity)
|
2022-05-03 19:49:14 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return []params.Instance{}, errors.Wrap(err, "fetching instances")
|
|
|
|
|
}
|
|
|
|
|
return instances, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
func (r *Runner) findRepoPoolManager(owner, name, endpointName string) (common.PoolManager, error) {
|
2022-05-04 16:27:24 +00:00
|
|
|
r.mux.Lock()
|
|
|
|
|
defer r.mux.Unlock()
|
2022-05-03 19:49:14 +00:00
|
|
|
|
2024-07-29 17:35:57 +00:00
|
|
|
repo, err := r.store.GetRepository(r.ctx, owner, name, endpointName)
|
2022-05-04 16:27:24 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching repo")
|
2022-05-03 19:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-10 12:15:12 +03:00
|
|
|
poolManager, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pool manager for repo")
|
2022-05-03 19:49:14 +00:00
|
|
|
}
|
2022-08-10 12:15:12 +03:00
|
|
|
return poolManager, nil
|
2022-05-03 19:49:14 +00:00
|
|
|
}
|
2023-08-15 17:19:06 +00:00
|
|
|
|
2023-08-16 09:11:45 +00:00
|
|
|
func (r *Runner) InstallRepoWebhook(ctx context.Context, repoID string, param params.InstallWebhookParams) (params.HookInfo, error) {
|
2023-08-15 17:19:06 +00:00
|
|
|
if !auth.IsAdmin(ctx) {
|
2023-08-16 09:11:45 +00:00
|
|
|
return params.HookInfo{}, runnerErrors.ErrUnauthorized
|
2023-08-15 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repo, err := r.store.GetRepositoryByID(ctx, repoID)
|
|
|
|
|
if err != nil {
|
2023-08-16 09:11:45 +00:00
|
|
|
return params.HookInfo{}, errors.Wrap(err, "fetching repo")
|
2023-08-15 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poolManager, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
2023-08-16 09:11:45 +00:00
|
|
|
return params.HookInfo{}, errors.Wrap(err, "fetching pool manager for repo")
|
2023-08-15 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-16 09:11:45 +00:00
|
|
|
info, err := poolManager.InstallWebhook(ctx, param)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.HookInfo{}, errors.Wrap(err, "installing webhook")
|
2023-08-15 17:19:06 +00:00
|
|
|
}
|
2023-08-16 09:11:45 +00:00
|
|
|
return info, nil
|
2023-08-15 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) UninstallRepoWebhook(ctx context.Context, repoID string) error {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repo, err := r.store.GetRepositoryByID(ctx, repoID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poolManager, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "fetching pool manager for repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := poolManager.UninstallWebhook(ctx); err != nil {
|
|
|
|
|
return errors.Wrap(err, "uninstalling webhook")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-08-16 09:11:45 +00:00
|
|
|
|
|
|
|
|
func (r *Runner) GetRepoWebhookInfo(ctx context.Context, repoID string) (params.HookInfo, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.HookInfo{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repo, err := r.store.GetRepositoryByID(ctx, repoID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.HookInfo{}, errors.Wrap(err, "fetching repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poolManager, err := r.poolManagerCtrl.GetRepoPoolManager(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.HookInfo{}, errors.Wrap(err, "fetching pool manager for repo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info, err := poolManager.GetWebhookInfo(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.HookInfo{}, errors.Wrap(err, "getting webhook info")
|
|
|
|
|
}
|
|
|
|
|
return info, nil
|
|
|
|
|
}
|