2024-12-14 08:35:45 +00:00
|
|
|
package reposervice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2024-12-15 06:36:33 +00:00
|
|
|
|
2024-12-15 08:39:18 +00:00
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
|
2024-12-15 06:36:33 +00:00
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
2024-12-14 08:35:45 +00:00
|
|
|
)
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
type Config interface {
|
|
|
|
GetUploadDir() string
|
|
|
|
}
|
|
|
|
|
2024-12-14 08:35:45 +00:00
|
|
|
type Service struct {
|
2024-12-15 06:16:51 +00:00
|
|
|
db *gorm.DB
|
|
|
|
config Config
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
func New(db *gorm.DB, cfg Config) *Service {
|
2024-12-14 08:35:45 +00:00
|
|
|
return &Service{
|
2024-12-15 06:16:51 +00:00
|
|
|
db: db,
|
|
|
|
config: cfg,
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
func (s *Service) futureRepoPathPrefix() string {
|
|
|
|
return path.Join(
|
|
|
|
s.config.GetUploadDir(),
|
|
|
|
".future_repo",
|
|
|
|
)
|
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
func (s *Service) currentRepoPathPrefix() string {
|
|
|
|
return path.Join(
|
|
|
|
s.config.GetUploadDir(),
|
|
|
|
"repo",
|
|
|
|
)
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
func (s *Service) oldRepoPathPrefix() string {
|
|
|
|
return path.Join(
|
|
|
|
s.config.GetUploadDir(),
|
|
|
|
".old_repo",
|
2024-12-15 06:36:33 +00:00
|
|
|
)
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
func (s *Service) ForceUpdate() error {
|
|
|
|
const REPO_NAME = "aides"
|
|
|
|
|
|
|
|
architectures := []string{
|
|
|
|
"x86_64",
|
|
|
|
"noarch",
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := []string{
|
|
|
|
"Sisyphus",
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
log := logger.GetLogger()
|
|
|
|
log.Info("Start repo update")
|
|
|
|
|
|
|
|
err := os.MkdirAll(s.futureRepoPathPrefix(), os.ModePerm)
|
2024-12-14 08:35:45 +00:00
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
return err
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
2024-12-24 19:40:37 +00:00
|
|
|
defer os.RemoveAll(s.futureRepoPathPrefix())
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
err = os.MkdirAll(s.oldRepoPathPrefix(), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
2024-12-24 19:40:37 +00:00
|
|
|
defer os.RemoveAll(s.oldRepoPathPrefix())
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
err = os.MkdirAll(s.currentRepoPathPrefix(), os.ModePerm)
|
2024-12-15 08:39:18 +00:00
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
return err
|
2024-12-15 08:39:18 +00:00
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
for _, r := range repos {
|
|
|
|
var tasks []models.Task
|
|
|
|
altRepo := models.ALTRepo{
|
|
|
|
Name: r,
|
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
s.db.
|
|
|
|
Where(&altRepo).
|
|
|
|
First(&altRepo)
|
|
|
|
|
|
|
|
s.db.
|
|
|
|
Model(&models.GitRepoAltRepoTask{}).
|
|
|
|
Select("tasks.*").
|
|
|
|
Joins("JOIN tasks ON tasks.id = git_repo_alt_repo_tasks.last_task_id").
|
|
|
|
Where(&models.GitRepoAltRepoTask{
|
|
|
|
ALTRepoID: altRepo.ID,
|
|
|
|
}).
|
|
|
|
Preload("Files").
|
|
|
|
Find(&tasks)
|
|
|
|
|
|
|
|
futureRepoPath := path.Join(
|
|
|
|
s.futureRepoPathPrefix(),
|
|
|
|
altRepo.Name,
|
|
|
|
)
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
err := os.MkdirAll(
|
|
|
|
futureRepoPath,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arch := range architectures {
|
|
|
|
err = createRepoDirs(futureRepoPath, REPO_NAME, arch)
|
2024-12-15 08:39:18 +00:00
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
return err
|
2024-12-15 08:39:18 +00:00
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
for _, el := range tasks {
|
|
|
|
for _, fileInfo := range el.Files {
|
|
|
|
localFilePath := path.Join(
|
2024-12-26 13:42:54 +00:00
|
|
|
strconv.FormatUint(uint64(el.ID), 10), fileInfo.Filename,
|
2024-12-24 19:40:37 +00:00
|
|
|
)
|
|
|
|
symLink := path.Join(
|
|
|
|
futureRepoPath,
|
|
|
|
fileInfo.Arch,
|
|
|
|
"RPMS.aides",
|
2024-12-26 13:42:54 +00:00
|
|
|
fileInfo.Filename,
|
2024-12-24 19:40:37 +00:00
|
|
|
)
|
|
|
|
targetPath := path.Join("../../../../tasks/", localFilePath)
|
|
|
|
err := createSymlink(targetPath, symLink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
for _, arch := range architectures {
|
|
|
|
err = runGenbasedir(futureRepoPath, arch, REPO_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
s.db.
|
|
|
|
Model(&models.GitRepoAltRepoTask{}).
|
|
|
|
Where(&models.GitRepoAltRepoTask{
|
|
|
|
ALTRepoID: altRepo.ID,
|
|
|
|
}).
|
|
|
|
Update(
|
|
|
|
"current_task_id", gorm.Expr("last_task_id"),
|
|
|
|
)
|
|
|
|
|
|
|
|
currentRepoPath := path.Join(
|
|
|
|
s.currentRepoPathPrefix(),
|
|
|
|
altRepo.Name,
|
|
|
|
)
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
oldRepoPath := path.Join(
|
|
|
|
s.oldRepoPathPrefix(),
|
|
|
|
altRepo.Name,
|
|
|
|
)
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
if err := renameIfExists(
|
|
|
|
currentRepoPath,
|
|
|
|
oldRepoPath,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
if err := os.Rename(
|
|
|
|
futureRepoPath,
|
|
|
|
currentRepoPath,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-14 08:35:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-12-24 19:40:37 +00:00
|
|
|
log.Info("Successful repo update")
|
|
|
|
return nil
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|