add apt gestion
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
274ea454dd
commit
c4776d81dd
22 changed files with 1465 additions and 163 deletions
|
|
@ -2,11 +2,13 @@ package core
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
aptclone "github.com/syonad/clonepack/internal/clone/apt"
|
||||
"github.com/syonad/clonepack/internal/clone/rpm"
|
||||
"github.com/syonad/clonepack/internal/store"
|
||||
)
|
||||
|
|
@ -28,8 +30,8 @@ func (s *CloneService) StartClone(ctx context.Context, repoID int64) (int64, err
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if repo.Type != "rpm" {
|
||||
return 0, fmt.Errorf("clone is only supported for rpm repositories")
|
||||
if repo.Type != "rpm" && repo.Type != "apt" {
|
||||
return 0, fmt.Errorf("clone is only supported for rpm and apt repositories")
|
||||
}
|
||||
|
||||
running, err := s.jobStore.HasRunningCloneJob(ctx, repoID)
|
||||
|
|
@ -45,7 +47,7 @@ func (s *CloneService) StartClone(ctx context.Context, repoID int64) (int64, err
|
|||
return 0, err
|
||||
}
|
||||
|
||||
go s.runClone(repoID, jobID, repo.SourceURL, repo.Type)
|
||||
go s.runClone(repoID, jobID, repo)
|
||||
return jobID, nil
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +55,7 @@ func (s *CloneService) GetLatestCloneJob(ctx context.Context, repoID int64) (*st
|
|||
return s.jobStore.GetLatestCloneJob(ctx, repoID)
|
||||
}
|
||||
|
||||
func (s *CloneService) runClone(repoID, jobID int64, sourceURL, repoType string) {
|
||||
func (s *CloneService) runClone(repoID, jobID int64, repo *store.Repo) {
|
||||
ctx := context.Background()
|
||||
|
||||
if err := s.jobStore.MarkCloneJobStarted(ctx, jobID); err != nil {
|
||||
|
|
@ -61,23 +63,41 @@ func (s *CloneService) runClone(repoID, jobID int64, sourceURL, repoType string)
|
|||
return
|
||||
}
|
||||
|
||||
destDir := filepath.Join(s.dataDir, "repos", fmt.Sprintf("%d", repoID), repoType)
|
||||
cloner := rpm.New(sourceURL, destDir)
|
||||
cloner.OnProgress = func(p rpm.Progress) {
|
||||
log.Printf("clone job %d: %s (%d bytes)", jobID, p.File, p.BytesDone)
|
||||
destDir := filepath.Join(s.dataDir, "repos", fmt.Sprintf("%d", repoID), repo.Type)
|
||||
var cloneErr error
|
||||
|
||||
switch repo.Type {
|
||||
case "rpm":
|
||||
cloner := rpm.New(repo.SourceURL, destDir)
|
||||
cloner.OnProgress = func(p rpm.Progress) {
|
||||
log.Printf("clone job %d: %s (%d bytes)", jobID, p.File, p.BytesDone)
|
||||
}
|
||||
if err := cloner.Clone(ctx); err != nil {
|
||||
cloneErr = err
|
||||
} else if err := rpm.RegenerateMetadata(destDir); err != nil {
|
||||
log.Printf("clone job %d: metadata regeneration failed: %v", jobID, err)
|
||||
}
|
||||
|
||||
case "apt":
|
||||
var cfg aptclone.Config
|
||||
if err := json.Unmarshal([]byte(repo.Config), &cfg); err != nil {
|
||||
cloneErr = fmt.Errorf("parse apt config: %w", err)
|
||||
break
|
||||
}
|
||||
cloner := aptclone.New(repo.SourceURL, destDir, cfg)
|
||||
cloner.OnProgress = func(file string, bytes int64) {
|
||||
log.Printf("clone job %d: %s (%d bytes)", jobID, file, bytes)
|
||||
}
|
||||
cloneErr = cloner.Clone(ctx)
|
||||
}
|
||||
|
||||
if err := cloner.Clone(ctx); err != nil {
|
||||
errMsg := err.Error()
|
||||
if cloneErr != nil {
|
||||
errMsg := cloneErr.Error()
|
||||
_ = s.jobStore.MarkCloneJobFinished(ctx, jobID, store.CloneJobFailed, &errMsg)
|
||||
log.Printf("clone job %d: failed: %v", jobID, err)
|
||||
log.Printf("clone job %d: failed: %v", jobID, cloneErr)
|
||||
return
|
||||
}
|
||||
|
||||
if err := rpm.RegenerateMetadata(destDir); err != nil {
|
||||
log.Printf("clone job %d: metadata regeneration failed: %v", jobID, err)
|
||||
}
|
||||
|
||||
_ = s.jobStore.MarkCloneJobFinished(ctx, jobID, store.CloneJobCompleted, nil)
|
||||
log.Printf("clone job %d: completed", jobID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue