2024-12-14 08:35:45 +00:00
|
|
|
package reposervice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSymlink(target, link string) error {
|
|
|
|
if _, err := os.Lstat(link); err == nil {
|
|
|
|
if err := os.Remove(link); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove existing file or symlink: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Symlink(target, link); err != nil {
|
|
|
|
return fmt.Errorf("failed to create symlink: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenbasedir(repoDir, arch, repoName string) {
|
|
|
|
cmd := exec.Command("genbasedir", "--bloat", "--progress", fmt.Sprintf("--topdir=%s", repoDir), arch, repoName)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to run genbasedir for %s: %v\n", arch, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Successfully ran genbasedir for %s\n", arch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRepoDirs(repoDir, repoName, arch string) {
|
|
|
|
// Create the 'base' directory
|
|
|
|
baseDir := path.Join(repoDir, arch, "base")
|
|
|
|
err := os.MkdirAll(baseDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create directory %s: %v\n", baseDir, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the 'RPMS.<REPO_NAME>' directory
|
|
|
|
rpmsDir := path.Join(repoDir, arch, fmt.Sprintf("RPMS.%s", repoName))
|
|
|
|
err = os.MkdirAll(rpmsDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create directory %s: %v\n", rpmsDir, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) ForceUpdate() {
|
|
|
|
var tasks []models.Task
|
|
|
|
altRepo := models.ALTRepo{
|
|
|
|
Name: "Sisyphus",
|
|
|
|
}
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
s.db.
|
2024-12-14 08:35:45 +00:00
|
|
|
Where(&altRepo).
|
|
|
|
First(&altRepo)
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
s.db.Debug().
|
2024-12-14 08:35:45 +00:00
|
|
|
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)
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
repoPath := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus")
|
2024-12-14 08:35:45 +00:00
|
|
|
|
|
|
|
os.MkdirAll(
|
|
|
|
repoPath,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
|
|
|
|
repoName := "aides"
|
|
|
|
architectures := []string{"x86_64", "noarch"}
|
|
|
|
|
|
|
|
for _, arch := range architectures {
|
|
|
|
createRepoDirs(repoPath, repoName, arch)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range tasks {
|
|
|
|
taskPath := path.Join(
|
2024-12-15 06:16:51 +00:00
|
|
|
s.config.GetUploadDir(), "tasks", strconv.FormatUint(uint64(el.ID), 10),
|
2024-12-14 08:35:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, fileInfo := range el.Files {
|
|
|
|
localFilePath := path.Join(
|
|
|
|
strconv.FormatUint(uint64(el.ID), 10), fileInfo.Name,
|
|
|
|
)
|
2024-12-15 06:16:51 +00:00
|
|
|
symLink := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus", fileInfo.Arch, "RPMS.aides", fileInfo.Name)
|
2024-12-14 08:35:45 +00:00
|
|
|
targetPath := path.Join("../../../../tasks/", localFilePath)
|
|
|
|
createSymlink(targetPath, symLink)
|
|
|
|
|
|
|
|
fmt.Println(path.Join(taskPath, fileInfo.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arch := range architectures {
|
|
|
|
runGenbasedir(repoPath, arch, repoName)
|
|
|
|
}
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
s.db.Debug().
|
2024-12-14 08:35:45 +00:00
|
|
|
Model(&models.GitRepoAltRepoTask{}).
|
|
|
|
Where(&models.GitRepoAltRepoTask{
|
|
|
|
ALTRepoID: altRepo.ID,
|
|
|
|
}).
|
|
|
|
Update(
|
|
|
|
"current_task_id", gorm.Expr("last_task_id"),
|
|
|
|
)
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
os.MkdirAll(path.Join(s.config.GetUploadDir(), "repo"), os.ModePerm)
|
2024-12-14 08:35:45 +00:00
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
aPath := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus")
|
|
|
|
bPath := path.Join(s.config.GetUploadDir(), "repo", "Sisyphus")
|
|
|
|
cPath := path.Join(s.config.GetUploadDir(), "repo", ".Sisyphus")
|
2024-12-14 08:35:45 +00:00
|
|
|
|
|
|
|
if _, err := os.Stat(bPath); err == nil {
|
|
|
|
fmt.Printf("Moving %s to %s\n", bPath, cPath)
|
|
|
|
if err := os.Rename(bPath, cPath); err != nil {
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(aPath, bPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(cPath); err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
os.RemoveAll(path.Join(s.config.GetUploadDir(), "future_repo"))
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|