aides-repo-api/internal/services/reposervice/service.go

161 lines
3.7 KiB
Go

package reposervice
import (
"fmt"
"os"
"os/exec"
"path"
"strconv"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"gorm.io/gorm"
)
type Config interface {
GetUploadDir() string
}
type Service struct {
db *gorm.DB
config Config
}
func New(db *gorm.DB, cfg Config) *Service {
return &Service{
db: db,
config: cfg,
}
}
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",
}
s.db.
Where(&altRepo).
First(&altRepo)
s.db.Debug().
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)
repoPath := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus")
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(
s.config.GetUploadDir(), "tasks", strconv.FormatUint(uint64(el.ID), 10),
)
for _, fileInfo := range el.Files {
localFilePath := path.Join(
strconv.FormatUint(uint64(el.ID), 10), fileInfo.Name,
)
symLink := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus", fileInfo.Arch, "RPMS.aides", fileInfo.Name)
targetPath := path.Join("../../../../tasks/", localFilePath)
createSymlink(targetPath, symLink)
fmt.Println(path.Join(taskPath, fileInfo.Name))
}
}
for _, arch := range architectures {
runGenbasedir(repoPath, arch, repoName)
}
s.db.Debug().
Model(&models.GitRepoAltRepoTask{}).
Where(&models.GitRepoAltRepoTask{
ALTRepoID: altRepo.ID,
}).
Update(
"current_task_id", gorm.Expr("last_task_id"),
)
os.MkdirAll(path.Join(s.config.GetUploadDir(), "repo"), os.ModePerm)
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")
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 {
}
os.RemoveAll(path.Join(s.config.GetUploadDir(), "future_repo"))
}