Maxim Slipenko
6693ceabcc
Some checks failed
Format and Lint / format-check (push) Has been cancelled
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package reposervice
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
|
|
)
|
|
|
|
func createRepoDirs(repoDir, repoName, arch string) error {
|
|
log := logger.GetLogger()
|
|
// Create the 'base' directory
|
|
baseDir := path.Join(repoDir, arch, "base")
|
|
err := os.MkdirAll(baseDir, os.ModePerm)
|
|
if err != nil {
|
|
log.Error("Failed to create directory", map[string]interface{}{
|
|
"directory": baseDir,
|
|
"err": err,
|
|
})
|
|
return err
|
|
}
|
|
|
|
// 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 {
|
|
log.Error("Failed to create directory", map[string]interface{}{
|
|
"directory": rpmsDir,
|
|
"err": err,
|
|
})
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runGenbasedir(repoDir, arch, repoName string) error {
|
|
log := logger.GetLogger()
|
|
cmd := exec.Command(
|
|
"genbasedir",
|
|
"--bloat",
|
|
"--progress",
|
|
fmt.Sprintf("--topdir=%s", repoDir),
|
|
arch,
|
|
repoName,
|
|
)
|
|
cmd.Stdout = nil
|
|
cmd.Stderr = nil
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
log.Error(
|
|
"Failed to run genbasedir",
|
|
map[string]interface{}{
|
|
"arch": arch,
|
|
"error": err,
|
|
"cmd_error": cmd.Stderr,
|
|
},
|
|
)
|
|
return err
|
|
}
|
|
log.Debug("Successfully ran genbasedir", map[string]interface{}{
|
|
"arch": arch,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
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 renameIfExists(oldPath, newPath string) error {
|
|
if _, err := os.Stat(oldPath); err == nil {
|
|
return os.Rename(oldPath, newPath)
|
|
} else if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|