This change adds 2 new options to gitea forge endpoints: * Tools metadata URL * Use internal tools URLs By default, GARM looks in the releases page of the gitea arc_runner to determine where it can download the runner binary from for a particular OS/arch. The tools metadata URL option can be set on an endpoint and can point to a mirror of the upstream repo. The requirement is that the asset names exactly mirror upstream naming conventions. The second option disables GARM calling out to the tools metadata URL entirely. GARM has some hardcoded values for nightly binaries. If this option is checked, GARM will use those values, without making any kind of outgoing API call to determine availability. This is useful in air-gapped environments. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
168 lines
4.2 KiB
Go
168 lines
4.2 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 cache
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/cloudbase/garm/params"
|
|
)
|
|
|
|
var (
|
|
credentialsCache *CredentialCache
|
|
giteaCredentialsCache *CredentialCache
|
|
)
|
|
|
|
func init() {
|
|
ghCredentialsCache := &CredentialCache{
|
|
cache: make(map[uint]params.ForgeCredentials),
|
|
}
|
|
gtCredentialsCache := &CredentialCache{
|
|
cache: make(map[uint]params.ForgeCredentials),
|
|
}
|
|
|
|
credentialsCache = ghCredentialsCache
|
|
giteaCredentialsCache = gtCredentialsCache
|
|
}
|
|
|
|
type CredentialCache struct {
|
|
mux sync.Mutex
|
|
|
|
cache map[uint]params.ForgeCredentials
|
|
}
|
|
|
|
func (g *CredentialCache) SetCredentialsRateLimit(credsID uint, rateLimit params.GithubRateLimit) {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
if creds, ok := g.cache[credsID]; ok {
|
|
creds.RateLimit = &rateLimit
|
|
g.cache[credsID] = creds
|
|
}
|
|
}
|
|
|
|
func (g *CredentialCache) UpdateCredentialsUsingEndpoint(ep params.ForgeEndpoint) {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
for _, creds := range g.cache {
|
|
if creds.Endpoint.Name == ep.Name {
|
|
creds.Endpoint = ep
|
|
g.setCredentialsAndUpdateEntities(creds)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (g *CredentialCache) setCredentialsAndUpdateEntities(credentials params.ForgeCredentials) {
|
|
g.cache[credentials.ID] = credentials
|
|
UpdateCredentialsInAffectedEntities(credentials)
|
|
}
|
|
|
|
func (g *CredentialCache) SetCredentials(credentials params.ForgeCredentials) {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
g.setCredentialsAndUpdateEntities(credentials)
|
|
}
|
|
|
|
func (g *CredentialCache) GetCredentials(id uint) (params.ForgeCredentials, bool) {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
if creds, ok := g.cache[id]; ok {
|
|
return creds, true
|
|
}
|
|
return params.ForgeCredentials{}, false
|
|
}
|
|
|
|
func (g *CredentialCache) DeleteCredentials(id uint) {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
delete(g.cache, id)
|
|
}
|
|
|
|
func (g *CredentialCache) GetAllCredentials() []params.ForgeCredentials {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
creds := make([]params.ForgeCredentials, 0, len(g.cache))
|
|
for _, cred := range g.cache {
|
|
creds = append(creds, cred)
|
|
}
|
|
|
|
// Sort the credentials by ID
|
|
sortByID(creds)
|
|
return creds
|
|
}
|
|
|
|
func (g *CredentialCache) GetAllCredentialsAsMap() map[uint]params.ForgeCredentials {
|
|
g.mux.Lock()
|
|
defer g.mux.Unlock()
|
|
|
|
creds := make(map[uint]params.ForgeCredentials, len(g.cache))
|
|
for id, cred := range g.cache {
|
|
creds[id] = cred
|
|
}
|
|
|
|
return creds
|
|
}
|
|
|
|
func SetGithubCredentials(credentials params.ForgeCredentials) {
|
|
credentialsCache.SetCredentials(credentials)
|
|
}
|
|
|
|
func GetGithubCredentials(id uint) (params.ForgeCredentials, bool) {
|
|
return credentialsCache.GetCredentials(id)
|
|
}
|
|
|
|
func DeleteGithubCredentials(id uint) {
|
|
credentialsCache.DeleteCredentials(id)
|
|
}
|
|
|
|
func GetAllGithubCredentials() []params.ForgeCredentials {
|
|
return credentialsCache.GetAllCredentials()
|
|
}
|
|
|
|
func SetCredentialsRateLimit(credsID uint, rateLimit params.GithubRateLimit) {
|
|
credentialsCache.SetCredentialsRateLimit(credsID, rateLimit)
|
|
}
|
|
|
|
func GetAllGithubCredentialsAsMap() map[uint]params.ForgeCredentials {
|
|
return credentialsCache.GetAllCredentialsAsMap()
|
|
}
|
|
|
|
func SetGiteaCredentials(credentials params.ForgeCredentials) {
|
|
giteaCredentialsCache.SetCredentials(credentials)
|
|
}
|
|
|
|
func GetGiteaCredentials(id uint) (params.ForgeCredentials, bool) {
|
|
return giteaCredentialsCache.GetCredentials(id)
|
|
}
|
|
|
|
func DeleteGiteaCredentials(id uint) {
|
|
giteaCredentialsCache.DeleteCredentials(id)
|
|
}
|
|
|
|
func GetAllGiteaCredentials() []params.ForgeCredentials {
|
|
return giteaCredentialsCache.GetAllCredentials()
|
|
}
|
|
|
|
func GetAllGiteaCredentialsAsMap() map[uint]params.ForgeCredentials {
|
|
return giteaCredentialsCache.GetAllCredentialsAsMap()
|
|
}
|
|
|
|
func UpdateCredentialsUsingEndpoint(ep params.ForgeEndpoint) {
|
|
giteaCredentialsCache.UpdateCredentialsUsingEndpoint(ep)
|
|
}
|