garm/runner/templates.go
Gabriel 23f92bc335
Add runner install template management (#525)
* Add template api endpoints

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Added template bypass

Pools and scale sets will automatically migrate to the new template
system for runner install scripts. If a pool or a scale set cannot be
migrate, it is left alone. It is expected that users set a runner install
template manually for scenarios we don't yet have a template for (windows
on gitea for example).

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Integrate templates with pool create/update

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Add webapp integration with templates

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Add unit tests

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Populate all relevant context fields

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Update dependencies

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Fix lint

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Validate uint

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Add CLI template management

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Some editor improvements and bugfixes

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Fix scale set return values post create

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

* Fix template websocket events filter

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>

---------

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2025-09-23 13:46:27 +03:00

104 lines
3.3 KiB
Go

// Copyright 2025 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.
package runner
import (
"context"
"errors"
"fmt"
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm/auth"
"github.com/cloudbase/garm/params"
)
func (r *Runner) CreateTemplate(ctx context.Context, param params.CreateTemplateParams) (params.Template, error) {
if !auth.IsAdmin(ctx) {
return params.Template{}, runnerErrors.ErrUnauthorized
}
if err := param.Validate(); err != nil {
return params.Template{}, runnerErrors.NewBadRequestError("invalid create params %q", err)
}
template, err := r.store.CreateTemplate(ctx, param)
if err != nil {
return params.Template{}, fmt.Errorf("failed to create template: %w", err)
}
return template, nil
}
func (r *Runner) GetTemplate(ctx context.Context, id uint) (params.Template, error) {
if !auth.IsAdmin(ctx) {
return params.Template{}, runnerErrors.ErrUnauthorized
}
template, err := r.store.GetTemplate(ctx, id)
if err != nil {
return params.Template{}, fmt.Errorf("failed to get template: %w", err)
}
return template, nil
}
func (r *Runner) GetTemplateByName(ctx context.Context, templateName string) (params.Template, error) {
if !auth.IsAdmin(ctx) {
return params.Template{}, runnerErrors.ErrUnauthorized
}
template, err := r.store.GetTemplateByName(ctx, templateName)
if err != nil {
return params.Template{}, fmt.Errorf("failed to get template: %w", err)
}
return template, nil
}
func (r *Runner) ListTemplates(ctx context.Context, osType *commonParams.OSType, forgeType *params.EndpointType, partialName *string) ([]params.Template, error) {
if !auth.IsAdmin(ctx) {
return nil, runnerErrors.ErrUnauthorized
}
templates, err := r.store.ListTemplates(ctx, osType, forgeType, partialName)
if err != nil {
return nil, fmt.Errorf("failed to list templates: %w", err)
}
return templates, nil
}
func (r *Runner) UpdateTemplate(ctx context.Context, id uint, param params.UpdateTemplateParams) (params.Template, error) {
if !auth.IsAdmin(ctx) {
return params.Template{}, runnerErrors.ErrUnauthorized
}
if err := param.Validate(); err != nil {
return params.Template{}, runnerErrors.NewBadRequestError("invalid update params: %q", err)
}
template, err := r.store.UpdateTemplate(ctx, id, param)
if err != nil {
return params.Template{}, fmt.Errorf("failed to update template: %w", err)
}
return template, nil
}
func (r *Runner) DeleteTemplate(ctx context.Context, id uint) error {
if !auth.IsAdmin(ctx) {
return runnerErrors.ErrUnauthorized
}
if err := r.store.DeleteTemplate(ctx, id); err != nil {
if !errors.Is(err, runnerErrors.ErrNotFound) {
return fmt.Errorf("failed to delete template: %w", err)
}
}
return nil
}