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 runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-10-13 18:32:21 +00:00
|
|
|
"fmt"
|
2022-05-05 13:07:06 +00:00
|
|
|
|
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"
|
2022-05-05 13:07:06 +00:00
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (r *Runner) ListAllPools(ctx context.Context) ([]params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return []params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
2023-01-17 17:32:28 +01:00
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
pools, err := r.store.ListAllPools(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching pools")
|
|
|
|
|
}
|
|
|
|
|
return pools, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) GetPoolByID(ctx context.Context, poolID string) (params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, err := r.store.GetPoolByID(ctx, poolID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
return pool, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) DeletePoolByID(ctx context.Context, poolID string) error {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 16:10:02 +00:00
|
|
|
pool, err := r.store.GetPoolByID(ctx, poolID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.Is(err, runnerErrors.ErrNotFound) {
|
|
|
|
|
return errors.Wrap(err, "fetching pool")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(pool.Instances) > 0 {
|
|
|
|
|
return runnerErrors.NewBadRequestError("pool has runners")
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
if err := r.store.DeletePoolByID(ctx, poolID); err != nil {
|
2023-06-15 14:11:49 +03:00
|
|
|
return errors.Wrap(err, "deleting pool")
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) UpdatePoolByID(ctx context.Context, poolID string, param params.UpdatePoolParams) (params.Pool, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return params.Pool{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, err := r.store.GetPoolByID(ctx, poolID)
|
|
|
|
|
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-07-06 17:37:18 +00:00
|
|
|
if param.RunnerBootstrapTimeout != nil && *param.RunnerBootstrapTimeout == 0 {
|
2022-06-29 23:44:03 +00:00
|
|
|
return params.Pool{}, runnerErrors.NewBadRequestError("runner_bootstrap_timeout cannot be 0")
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
if minIdleRunners > maxRunners {
|
|
|
|
|
return params.Pool{}, runnerErrors.NewBadRequestError("min_idle_runners cannot be larger than max_runners")
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 11:41:38 +00:00
|
|
|
if param.Tags != nil && len(param.Tags) > 0 {
|
2023-03-19 21:28:24 +02:00
|
|
|
newTags, err := r.processTags(string(pool.OSArch), pool.OSType, param.Tags)
|
2022-06-24 11:41:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "processing tags")
|
|
|
|
|
}
|
|
|
|
|
param.Tags = newTags
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 13:07:06 +00:00
|
|
|
var newPool params.Pool
|
|
|
|
|
|
|
|
|
|
if pool.RepoID != "" {
|
|
|
|
|
newPool, err = r.store.UpdateRepositoryPool(ctx, pool.RepoID, poolID, param)
|
|
|
|
|
} else if pool.OrgID != "" {
|
|
|
|
|
newPool, err = r.store.UpdateOrganizationPool(ctx, pool.OrgID, poolID, param)
|
2022-10-13 18:32:21 +00:00
|
|
|
} else if pool.EnterpriseID != "" {
|
|
|
|
|
newPool, err = r.store.UpdateEnterprisePool(ctx, pool.EnterpriseID, poolID, param)
|
|
|
|
|
} else {
|
2023-06-20 11:03:49 +03:00
|
|
|
return params.Pool{}, fmt.Errorf("pool not found to a repo, org or enterprise")
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return params.Pool{}, errors.Wrap(err, "updating pool")
|
|
|
|
|
}
|
|
|
|
|
return newPool, nil
|
|
|
|
|
}
|
2023-06-23 21:15:46 +00:00
|
|
|
|
|
|
|
|
func (r *Runner) ListAllJobs(ctx context.Context) ([]params.Job, error) {
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
return []params.Job{}, runnerErrors.ErrUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobs, err := r.store.ListAllJobs(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching jobs")
|
|
|
|
|
}
|
|
|
|
|
return jobs, nil
|
|
|
|
|
}
|